swap.gno
2.88 Kb · 88 lines
1package impl
2
3import (
4 "strconv"
5
6 pdca "gno.land/p/gnoswap/order/dca/v0"
7 ufmt "gno.land/p/nt/ufmt/v0"
8
9 "gno.land/r/gnoswap/common"
10 dca "gno.land/r/gnoswap/order/dca"
11 "gno.land/r/gnoswap/pool"
12)
13
14// swapDirection returns the pool direction of selling inputToken for
15// outputToken and the pool's token order.
16func swapDirection(inputToken string, outputToken string) (zeroForOne bool, token0 string, token1 string) {
17 if inputToken < outputToken {
18 return true, inputToken, outputToken
19 }
20
21 return false, outputToken, inputToken
22}
23
24// swapExactIn sells amountIn on the pair's pool of fee tier fee, down to
25// sqrtPriceLimitX96 (see calculateSqrtPriceLimitX96), and returns the output
26// received by the escrow. A pool that reaches the limit before selling the
27// whole input fails the round.
28//
29// The pool sends the output first, then calls the escrow's swap callback,
30// which pays the input from the escrow without any allowance (see
31// dca.NewSwapCallback). The pool result is checked against the escrow's
32// balance changes: the whole input must be sold, and the output must reach
33// amountOutMin (0 disables it).
34func swapExactIn(_ int, rlm realm, group *pdca.Group, fee uint32, sqrtPriceLimitX96 string, amountIn int64, amountOutMin int64) (int64, error) {
35 escrowAddress := rlm.Address()
36 inputToken := group.InputToken()
37 outputToken := group.OutputToken()
38 zeroForOne, token0, token1 := swapDirection(inputToken, outputToken)
39
40 inputBalanceBefore := common.BalanceOf(inputToken, escrowAddress)
41 outputBalanceBefore := common.BalanceOf(outputToken, escrowAddress)
42
43 amount0, amount1 := pool.Swap(
44 cross(rlm),
45 token0,
46 token1,
47 fee,
48 escrowAddress,
49 zeroForOne,
50 strconv.FormatInt(amountIn, 10),
51 sqrtPriceLimitX96,
52 dca.NewSwapCallback(0, rlm, inputToken, zeroForOne, amountIn),
53 )
54
55 // Pool deltas: positive is owed to the pool, negative is sent out.
56 poolAmountIn, poolAmountOut := amount0, amount1
57 if !zeroForOne {
58 poolAmountIn, poolAmountOut = amount1, amount0
59 }
60
61 spentAmount := inputBalanceBefore - common.BalanceOf(inputToken, escrowAddress)
62 receivedAmount := common.BalanceOf(outputToken, escrowAddress) - outputBalanceBefore
63
64 poolInDelta, err := strconv.ParseInt(poolAmountIn, 10, 64)
65 if err != nil || spentAmount != amountIn || poolInDelta != amountIn {
66 return 0, makeErrorWithDetails(
67 errSwapMismatch,
68 ufmt.Sprintf("input spent(%d) pool(%s) expected(%d)", spentAmount, poolAmountIn, amountIn),
69 )
70 }
71
72 poolOutDelta, err := strconv.ParseInt(poolAmountOut, 10, 64)
73 if err != nil || receivedAmount < 0 || poolOutDelta != -receivedAmount {
74 return 0, makeErrorWithDetails(
75 errSwapMismatch,
76 ufmt.Sprintf("output received(%d) pool(%s)", receivedAmount, poolAmountOut),
77 )
78 }
79
80 if receivedAmount < amountOutMin {
81 return 0, makeErrorWithDetails(
82 errInsufficientSwapOutput,
83 ufmt.Sprintf("output(%d) amountOutMin(%d)", receivedAmount, amountOutMin),
84 )
85 }
86
87 return receivedAmount, nil
88}