swap.gno
1.20 Kb · 55 lines
1package impl
2
3import (
4 "math"
5 "strconv"
6
7 plimit "gno.land/p/gnoswap/order/limit/v0"
8 u256 "gno.land/p/gnoswap/uint256/v1"
9)
10
11// The default bounds exclude the two TickMath endpoints.
12const (
13 minSwapSqrtPrice = "4295128740"
14 maxSwapSqrtPrice = "1461446703485210103287273052203988822378723970341"
15)
16
17func orderPriceLimit(price string, zeroForOne bool) (string, bool, error) {
18 value, err := u256.FromDecimal(price)
19 if err != nil {
20 return "", false, makeErrorWithDetails(errInvalidPriceLimit, price)
21 }
22
23 if !value.IsZero() {
24 return price, false, nil
25 }
26
27 if zeroForOne {
28 return minSwapSqrtPrice, true, nil
29 }
30
31 return maxSwapSqrtPrice, true, nil
32}
33
34func poolSwapAmounts(amount0, amount1 string, zeroForOne bool) (int64, int64, error) {
35 input, output := amount0, amount1
36 if !zeroForOne {
37 input, output = amount1, amount0
38 }
39
40 actualInput, err := strconv.ParseInt(input, 10, 64)
41 if err != nil || actualInput <= 0 {
42 return 0, 0, plimit.ErrInvalidFillAmount
43 }
44
45 outputDelta, err := strconv.ParseInt(output, 10, 64)
46 if err != nil || outputDelta == math.MinInt64 {
47 return 0, 0, plimit.ErrAmountOverflow
48 }
49
50 if outputDelta >= 0 {
51 return 0, 0, plimit.ErrInsufficientOutput
52 }
53
54 return actualInput, -outputDelta, nil
55}