assert.gno
2.55 Kb ยท 114 lines
1package v1
2
3import (
4 "strconv"
5 "strings"
6 "time"
7
8 "gno.land/p/nt/ufmt"
9
10 u256 "gno.land/p/gnoswap/uint256"
11
12 "gno.land/r/gnoswap/common"
13 "gno.land/r/gnoswap/pool"
14)
15
16// assertIsNotExpired ensures the transaction deadline has not passed.
17func assertIsNotExpired(deadline int64) {
18 now := time.Now().Unix()
19
20 if now > deadline {
21 panic(makeErrorWithDetails(
22 errExpired,
23 ufmt.Sprintf("transaction too old, now(%d) > deadline(%d)", now, deadline),
24 ))
25 }
26}
27
28// assertIsValidUserCoinSend asserts that the user has sent the correct amount of native coin.
29func assertIsValidUserCoinSend(tokenPath string, amount string) {
30 if common.IsGNOTNativePath(tokenPath) {
31 amountInt64, err := strconv.ParseInt(amount, 10, 64)
32 if err != nil {
33 panic(err)
34 }
35
36 common.AssertIsUserSendGNOTAmount(amountInt64)
37 } else {
38 common.AssertIsNotHandleNativeCoin()
39 }
40}
41
42func assertIsValidSqrtPriceLimitX96(sqrtPriceLimitX96 string) {
43 if sqrtPriceLimitX96 == "" {
44 panic(makeErrorWithDetails(
45 errInvalidInput,
46 ufmt.Sprintf("invalid sqrtPriceLimitX96: %s", sqrtPriceLimitX96),
47 ))
48 }
49
50 _, err := u256.FromDecimal(sqrtPriceLimitX96)
51 if err != nil {
52 panic(makeErrorWithDetails(
53 errInvalidInput,
54 ufmt.Sprintf("invalid sqrtPriceLimitX96: %s", sqrtPriceLimitX96),
55 ))
56 }
57}
58
59func assertIsValidSingleSwapRouteArrPath(routePaths, inputToken, outputToken string) {
60 if routePaths == "" {
61 panic(makeErrorWithDetails(
62 errInvalidInput,
63 ufmt.Sprintf("invalid route: %s", routePaths),
64 ))
65 }
66
67 if strings.Count(routePaths, ",") > 0 {
68 panic(makeErrorWithDetails(
69 errInvalidInput,
70 ufmt.Sprintf("invalid routePaths: %s", routePaths),
71 ))
72 }
73
74 if strings.Count(routePaths, POOL_SEPARATOR) > 0 {
75 panic(makeErrorWithDetails(
76 errInvalidInput,
77 ufmt.Sprintf("invalid routePaths: %s", routePaths),
78 ))
79 }
80
81 assertIsValidRoutePaths(routePaths, inputToken, outputToken)
82}
83
84func assertIsValidRoutePaths(routePaths, inputToken, outputToken string) {
85 err := validateRoutePaths(routePaths, inputToken, outputToken)
86 if err != nil {
87 panic(err)
88 }
89}
90
91func assertIsExistsPools(routePathArr string) {
92 poolPaths, err := parsePoolPathsByRoutePathArr(routePathArr)
93 if err != nil {
94 panic(err)
95 }
96
97 for _, poolPath := range poolPaths {
98 if !pool.ExistsPoolPath(poolPath) {
99 panic(makeErrorWithDetails(
100 errInvalidInput,
101 ufmt.Sprintf("pool does not exist: %s", poolPath),
102 ))
103 }
104 }
105}
106
107func assertIsRouterV1(caller address) {
108 if caller != routerV1Addr {
109 panic(makeErrorWithDetails(
110 errUnAuthorizedCaller,
111 ufmt.Sprintf("caller %s is not router v1(%s)", caller, routerV1Addr),
112 ))
113 }
114}