-
Notifications
You must be signed in to change notification settings - Fork 0
/
token_register.gno
137 lines (106 loc) · 3.48 KB
/
token_register.gno
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package router
import (
"std"
"gno.land/p/demo/ufmt"
pusers "gno.land/p/demo/users"
"gno.land/r/gnoswap/consts"
)
type GRC20Interface interface {
Transfer() func(to pusers.AddressOrName, amount uint64)
TransferFrom() func(from, to pusers.AddressOrName, amount uint64)
BalanceOf() func(owner pusers.AddressOrName) uint64
Approve() func(spender pusers.AddressOrName, amount uint64)
}
var (
registered = make(map[string]GRC20Interface)
locked = false // mutex
)
func GetRegisteredTokens() []string {
tokens := make([]string, 0, len(registered))
for k := range registered {
tokens = append(tokens, k)
}
return tokens
}
func RegisterGRC20Interface(pkgPath string, igrc20 GRC20Interface) {
caller := std.GetOrigCaller()
if caller != consts.GNOSWAP_ADMIN {
panic(ufmt.Sprintf("[ROUTER] router_register.gno__RegisterGRC20Interface() || unauthorized address(%s) to register", caller.String()))
}
pkgPath = handleNative(pkgPath)
_, found := registered[pkgPath]
if found {
panic(ufmt.Sprintf("[ROUTER] router_register.gno__RegisterGRC20Interface() || pkgPath(%s) already registered", pkgPath))
}
registered[pkgPath] = igrc20
}
func UnregisterGRC20Interface(pkgPath string) {
// only admin can unregister
caller := std.GetOrigCaller()
if caller != consts.GNOSWAP_ADMIN {
panic(ufmt.Sprintf("[ROUTER] router_register.gno__UnregisterGRC20Interface() || unauthorized address(%s) to unregister", caller.String()))
}
pkgPath = handleNative(pkgPath)
_, found := registered[pkgPath]
if found {
delete(registered, pkgPath)
}
}
func transferByRegisterCall(pkgPath string, to std.Address, amount uint64) bool {
pkgPath = handleNative(pkgPath)
_, found := registered[pkgPath]
if !found {
panic(ufmt.Sprintf("[ROUTER] router_register.gno__transferByRegisterCall() || pkgPath(%s) not found", pkgPath))
}
if !locked {
locked = true
registered[pkgPath].Transfer()(pusers.AddressOrName(to), amount)
defer func() {
locked = false
}()
} else {
panic("[POOl] router_register.gno__transferByRegisterCall() || expected locked to be false")
}
return true
}
func transferFromByRegisterCall(pkgPath string, from, to std.Address, amount uint64) bool {
pkgPath = handleNative(pkgPath)
_, found := registered[pkgPath]
if !found {
panic(ufmt.Sprintf("[ROUTER] router_register.gno__transferFromByRegisterCall() || pkgPath(%s) not found", pkgPath))
}
if !locked {
locked = true
registered[pkgPath].TransferFrom()(pusers.AddressOrName(from), pusers.AddressOrName(to), amount)
defer func() {
locked = false
}()
} else {
panic("[POOl] router_register.gno__transferFromByRegisterCall() || expected locked to be false")
}
return true
}
func balanceOfByRegisterCall(pkgPath string, owner std.Address) uint64 {
pkgPath = handleNative(pkgPath)
_, found := registered[pkgPath]
if !found {
panic(ufmt.Sprintf("[ROUTER] router_register.gno__balanceOfByRegisterCall() || pkgPath(%s) not found", pkgPath))
}
balance := registered[pkgPath].BalanceOf()(pusers.AddressOrName(owner))
return balance
}
func approveByRegisterCall(pkgPath string, spender std.Address, amount uint64) bool {
pkgPath = handleNative(pkgPath)
_, found := registered[pkgPath]
if !found {
panic(ufmt.Sprintf("[ROUTER] router_register.gno__approveByRegisterCall() || pkgPath(%s) not found", pkgPath))
}
registered[pkgPath].Approve()(pusers.AddressOrName(spender), amount)
return true
}
func handleNative(pkgPath string) string {
if pkgPath == consts.GNOT {
return consts.WRAPPED_WUGNOT
}
return pkgPath
}