Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

assert.gno

2.96 Kb · 128 lines
  1package router
  2
  3import (
  4	"errors"
  5	"strings"
  6	"time"
  7
  8	ufmt "gno.land/p/nt/ufmt/v0"
  9
 10	u256 "gno.land/p/gnoswap/uint256/v1"
 11
 12	"gno.land/r/gnoswap/pool"
 13
 14	"gno.land/r/gnoswap/common"
 15)
 16
 17// assertIsNotExpired ensures the transaction deadline has not passed.
 18func assertIsNotExpired(deadline int64) {
 19	now := time.Now().Unix()
 20
 21	if now > deadline {
 22		panic(makeErrorWithDetails(
 23			errExpired,
 24			ufmt.Sprintf("transaction too old, now(%d) > deadline(%d)", now, deadline),
 25		))
 26	}
 27}
 28
 29func assertSingleSwap(p *SingleSwapParams) {
 30	if p.tokenIn == p.tokenOut {
 31		panic(errors.New(errSameTokenSwap))
 32	}
 33
 34	common.MustRegistered(p.tokenIn, p.tokenOut)
 35}
 36
 37func assertIsValidSqrtPriceLimitX96(sqrtPriceLimitX96 string) {
 38	if sqrtPriceLimitX96 == "" {
 39		panic(makeErrorWithDetails(
 40			errInvalidInput,
 41			ufmt.Sprintf("invalid sqrtPriceLimitX96: %s", sqrtPriceLimitX96),
 42		))
 43	}
 44
 45	_, err := u256.FromDecimal(sqrtPriceLimitX96)
 46	if err != nil {
 47		panic(makeErrorWithDetails(
 48			errInvalidInput,
 49			ufmt.Sprintf("invalid sqrtPriceLimitX96: %s", sqrtPriceLimitX96),
 50		))
 51	}
 52}
 53
 54func assertIsValidSingleSwapRouteArrPath(routePaths, inputToken, outputToken string) {
 55	if routePaths == "" {
 56		panic(makeErrorWithDetails(
 57			errInvalidInput,
 58			ufmt.Sprintf("invalid route: %s", routePaths),
 59		))
 60	}
 61
 62	if strings.Count(routePaths, ",") > 0 {
 63		panic(makeErrorWithDetails(
 64			errInvalidInput,
 65			ufmt.Sprintf("invalid routePaths: %s", routePaths),
 66		))
 67	}
 68
 69	if strings.Count(routePaths, POOL_SEPARATOR) > 0 {
 70		panic(makeErrorWithDetails(
 71			errInvalidInput,
 72			ufmt.Sprintf("invalid routePaths: %s", routePaths),
 73		))
 74	}
 75
 76	assertIsValidRoutePaths(routePaths, inputToken, outputToken)
 77}
 78
 79func assertIsValidRoutePaths(routePaths, inputToken, outputToken string) {
 80	err := validateRoutePaths(routePaths, inputToken, outputToken)
 81	if err != nil {
 82		panic(err)
 83	}
 84}
 85
 86func assertIsExistsPools(routePathArr string) {
 87	poolPaths, err := parsePoolPathsByRoutePathArr(routePathArr)
 88	if err != nil {
 89		panic(err)
 90	}
 91
 92	for _, poolPath := range poolPaths {
 93		if !pool.ExistsPoolPath(poolPath) {
 94			panic(makeErrorWithDetails(
 95				errInvalidInput,
 96				ufmt.Sprintf("pool does not exist: %s", poolPath),
 97			))
 98		}
 99	}
100}
101
102func assertIsRouterImplementation(caller address) {
103	if caller != routerImplAddr {
104		panic(makeErrorWithDetails(
105			errUnAuthorizedCaller,
106			ufmt.Sprintf("caller %s is not router implementation(%s)", caller, routerImplAddr),
107		))
108	}
109}
110
111// assertHopFullyConsumed reverts when an exact-in hop consumed less input than specified.
112func assertHopFullyConsumed(hopIndex int, specifiedAmount, consumedAmount int64) {
113	err := validateHopFullyConsumed(hopIndex, specifiedAmount, consumedAmount)
114	if err != nil {
115		panic(err)
116	}
117}
118
119func validateHopFullyConsumed(hopIndex int, specifiedAmount, consumedAmount int64) error {
120	if consumedAmount != specifiedAmount {
121		return makeErrorWithDetails(
122			errInsufficientLiquidity,
123			ufmt.Sprintf("partial fill on hop(%d): requested=%d, consumed=%d", hopIndex, specifiedAmount, consumedAmount),
124		)
125	}
126
127	return nil
128}