Search Apps Documentation Source Content File Folder Download Copy Actions Download

swap_single.gno

1.27 Kb ยท 43 lines
 1package v1
 2
 3import (
 4	u256 "gno.land/p/gnoswap/uint256"
 5	"gno.land/r/gnoswap/common"
 6)
 7
 8var zero = u256.Zero()
 9
10// SwapExecutor defines the interface for executing swaps.
11type SwapExecutor interface {
12	// execute performs the swap operation.
13	execute(p *SingleSwapParams) (int64, int64)
14}
15
16// executeSwap is the common logic for both real and dry swaps.
17func (r *routerV1) executeSwap(executor SwapExecutor, p *SingleSwapParams) (int64, int64) {
18	if p.tokenIn == p.tokenOut {
19		panic(errSameTokenSwap)
20	}
21
22	common.MustRegistered(p.tokenIn, p.tokenOut)
23
24	return executor.execute(p)
25}
26
27var (
28	_ SwapExecutor = (*RealSwapExecutor)(nil)
29	_ SwapExecutor = (*DrySwapExecutor)(nil)
30)
31
32// singleSwap executes a swap within a single pool using the provided parameters.
33// It processes a token swap within two assets using a specific fee tier and
34// automatically sets the recipient to the caller's address.
35func (r *routerV1) singleSwap(p *SingleSwapParams) (int64, int64) {
36	return r.executeSwap(&RealSwapExecutor{router: r}, p)
37}
38
39// singleDrySwap simulates a single-token swap operation without executing it.
40// It performs a dry run simulation and does not alter the state.
41func (r *routerV1) singleDrySwap(p *SingleSwapParams) (int64, int64) {
42	return r.executeSwap(&DrySwapExecutor{router: r}, p)
43}