Search Apps Documentation Source Content File Folder Download Copy Actions Download

assert.gno

2.37 Kb ยท 85 lines
 1package v1
 2
 3import (
 4	"chain/runtime"
 5
 6	"gno.land/p/nt/ufmt"
 7	pl "gno.land/r/gnoswap/pool"
 8)
 9
10func assertIsNotAllowedEOA(previousRealm runtime.Realm) {
11	if previousRealm.PkgPath() == "" {
12		panic(newErrorWithDetail(
13			errNotAccessEOA,
14			ufmt.Sprintf("previousRealm(%s) is EOA", previousRealm.Address()),
15		))
16	}
17}
18
19// assertIsNotEqualsTokens asserts that the token0Path and token1Path are not equal.
20func assertIsNotEqualsTokens(token0Path, token1Path string) {
21	if token0Path == token1Path {
22		panic(newErrorWithDetail(
23			errDuplicateTokenInPool,
24			ufmt.Sprintf("expected token0Path(%s) != token1Path(%s)", token0Path, token1Path),
25		))
26	}
27}
28
29// assertIsSupportedFeeTier asserts that the fee is a supported fee tier.
30func assertIsSupportedFeeTier(fee uint32) {
31	if !isValidFeeTier(fee) {
32		panic(newErrorWithDetail(
33			errUnsupportedFeeTier,
34			ufmt.Sprintf("expected fee(%d) to be one of %d, %d, %d, %d", fee, FeeTier100, FeeTier500, FeeTier3000, FeeTier10000),
35		))
36	}
37}
38
39// assertIsNotExistsPoolPath asserts that the pool path does not exist.
40func assertIsNotExistsPoolPath(instance *poolV1, token0Path, token1Path string, fee uint32) {
41	poolPath := pl.GetPoolPath(token0Path, token1Path, fee)
42
43	pools := instance.store.GetPools()
44	if pools.Has(poolPath) {
45		panic(newErrorWithDetail(
46			errPoolAlreadyExists,
47			ufmt.Sprintf("expected poolPath(%s:%s:%d) not to exist", token0Path, token1Path, fee),
48		))
49	}
50}
51
52// assertIsValidTicks validates the tick range for a liquidity position.
53func assertIsValidTicks(tickLower, tickUpper int32) {
54	if err := validateTicks(tickLower, tickUpper); err != nil {
55		panic(err)
56	}
57}
58
59// assertAmountSpecifiedIsNotZero asserts that the amountSpecified is not zero.
60func assertAmountSpecifiedIsNotZero(amountSpecified string) {
61	if amountSpecified == "0" {
62		panic(newErrorWithDetail(
63			errInvalidSwapAmount,
64			ufmt.Sprintf("amountSpecified == 0"),
65		))
66	}
67}
68
69func assertPayerIsPreviousRealmOrOriginCaller(payer address) {
70	if payer != runtime.PreviousRealm().Address() && payer != runtime.OriginCaller() {
71		panic(newErrorWithDetail(
72			errInvalidPayer,
73			ufmt.Sprintf("expected payer(%s) to be the previous realm or the caller", payer),
74		))
75	}
76}
77
78func assertIsValidTokenOrder(token0Path, token1Path string) {
79	if token0Path >= token1Path {
80		panic(newErrorWithDetail(
81			errInvalidInput,
82			ufmt.Sprintf("expected token0Path(%s) < token1Path(%s)", token0Path, token1Path),
83		))
84	}
85}