Search Apps Documentation Source Content File Folder Download Copy Actions Download

assert.gno

2.16 Kb · 98 lines
 1package v1
 2
 3import (
 4	"strings"
 5	"time"
 6
 7	ufmt "gno.land/p/nt/ufmt/v0"
 8
 9	u256 "gno.land/p/gnoswap/uint256"
10
11	"gno.land/r/gnoswap/pool"
12)
13
14// assertIsNotExpired ensures the transaction deadline has not passed.
15func assertIsNotExpired(deadline int64) {
16	now := time.Now().Unix()
17
18	if now > deadline {
19		panic(makeErrorWithDetails(
20			errExpired,
21			ufmt.Sprintf("transaction too old, now(%d) > deadline(%d)", now, deadline),
22		))
23	}
24}
25
26func assertIsValidSqrtPriceLimitX96(sqrtPriceLimitX96 string) {
27	if sqrtPriceLimitX96 == "" {
28		panic(makeErrorWithDetails(
29			errInvalidInput,
30			ufmt.Sprintf("invalid sqrtPriceLimitX96: %s", sqrtPriceLimitX96),
31		))
32	}
33
34	_, err := u256.FromDecimal(sqrtPriceLimitX96)
35	if err != nil {
36		panic(makeErrorWithDetails(
37			errInvalidInput,
38			ufmt.Sprintf("invalid sqrtPriceLimitX96: %s", sqrtPriceLimitX96),
39		))
40	}
41}
42
43func assertIsValidSingleSwapRouteArrPath(routePaths, inputToken, outputToken string) {
44	if routePaths == "" {
45		panic(makeErrorWithDetails(
46			errInvalidInput,
47			ufmt.Sprintf("invalid route: %s", routePaths),
48		))
49	}
50
51	if strings.Count(routePaths, ",") > 0 {
52		panic(makeErrorWithDetails(
53			errInvalidInput,
54			ufmt.Sprintf("invalid routePaths: %s", routePaths),
55		))
56	}
57
58	if strings.Count(routePaths, POOL_SEPARATOR) > 0 {
59		panic(makeErrorWithDetails(
60			errInvalidInput,
61			ufmt.Sprintf("invalid routePaths: %s", routePaths),
62		))
63	}
64
65	assertIsValidRoutePaths(routePaths, inputToken, outputToken)
66}
67
68func assertIsValidRoutePaths(routePaths, inputToken, outputToken string) {
69	err := validateRoutePaths(routePaths, inputToken, outputToken)
70	if err != nil {
71		panic(err)
72	}
73}
74
75func assertIsExistsPools(routePathArr string) {
76	poolPaths, err := parsePoolPathsByRoutePathArr(routePathArr)
77	if err != nil {
78		panic(err)
79	}
80
81	for _, poolPath := range poolPaths {
82		if !pool.ExistsPoolPath(poolPath) {
83			panic(makeErrorWithDetails(
84				errInvalidInput,
85				ufmt.Sprintf("pool does not exist: %s", poolPath),
86			))
87		}
88	}
89}
90
91func assertIsRouterImplementation(caller address) {
92	if caller != routerImplAddr {
93		panic(makeErrorWithDetails(
94			errUnAuthorizedCaller,
95			ufmt.Sprintf("caller %s is not router implementation(%s)", caller, routerImplAddr),
96		))
97	}
98}