coins.gno
2.81 Kb ยท 106 lines
1package common
2
3import "chain/banker"
4
5// IsGNOTPath checks if the given path is either native gnot or wrapped ugnot path.
6//
7// Parameters:
8// - path: token path to check
9//
10// Returns true if the path matches either GNOT_DENOM ("ugnot") or WUGNOT_PATH.
11func IsGNOTPath(path string) bool {
12 return path == GNOT_DENOM || path == WUGNOT_PATH
13}
14
15// IsGNOTNativePath checks if the given path is the native gnot path.
16//
17// Parameters:
18// - path: token path to check
19//
20// Returns true if the path matches GNOT_DENOM ("ugnot").
21func IsGNOTNativePath(path string) bool {
22 return path == GNOT_DENOM
23}
24
25// IsGNOTWrappedPath checks if the given path is the wrapped ugnot path.
26//
27// Parameters:
28// - path: token path to check
29//
30// Returns true if the path matches WUGNOT_PATH.
31func IsGNOTWrappedPath(path string) bool {
32 return path == WUGNOT_PATH
33}
34
35// ExistsUserSendCoins checks if the user has sent any coins with the transaction.
36//
37// Returns true if any coins (with positive amount) were sent with the transaction.
38func ExistsUserSendCoins() bool {
39 return len(getUserSendCoins()) > 0
40}
41
42// isUserSendGNOTAmount validates if the user sent the expected gnot amount
43// Returns true in the following cases:
44// - GNOT was sent and amount matches expected
45//
46// Returns false in the following cases:
47// - GNOT was sent and amount does not match expected
48func isUserSendGNOTAmount(amount int64) bool {
49 sendCoins := getUserSendCoins()
50
51 gnotAmount := int64(0)
52
53 if amount, exists := sendCoins[GNOT_DENOM]; exists {
54 gnotAmount = amount
55 }
56
57 return gnotAmount == amount
58}
59
60// hasUnsupportedNativeCoins checks if the user sent unsupported native coins
61// Gnoswap only supports GNOT native coin, so this function detects:
62// Returns true (has unsupported coins) in the following cases:
63// - Multiple coin types were sent (more than 1 coin type)
64// - Single non-GNOT coin was sent
65// - Single GNOT coin was sent but amount is zero or negative
66//
67// Returns false (no unsupported coins) in the following cases:
68// - No coins were sent at all
69// - Single GNOT coin was sent with positive amount
70func hasUnsupportedNativeCoins() bool {
71 sendCoins := getUserSendCoins()
72 if len(sendCoins) == 0 {
73 return false
74 }
75
76 if len(sendCoins) != 1 {
77 return true
78 }
79
80 amount, ok := sendCoins[GNOT_DENOM]
81 if !ok || amount <= 0 {
82 return true
83 }
84
85 return false
86}
87
88// getUserSendCoins retrieves and aggregates all coins sent by the user in the current transaction
89// Returns a map of denomination to total amount, filtering out zero or negative amounts
90func getUserSendCoins() map[string]int64 {
91 coinsMap := make(map[string]int64)
92
93 for _, coin := range banker.OriginSend() {
94 if coin.Amount <= 0 {
95 continue
96 }
97
98 if _, exists := coinsMap[coin.Denom]; exists {
99 coinsMap[coin.Denom] += coin.Amount
100 } else {
101 coinsMap[coin.Denom] = coin.Amount
102 }
103 }
104
105 return coinsMap
106}