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