wugnot.gno
2.36 Kb ยท 115 lines
1package wugnot
2
3import (
4 "std"
5 "strings"
6
7 "gno.land/p/demo/grc/grc20"
8 "gno.land/p/demo/ufmt"
9 "gno.land/r/demo/grc20reg"
10)
11
12var Token, adm = grc20.NewToken("wrapped GNOT", "wugnot", 0)
13
14const (
15 ugnotMinDeposit int64 = 1000
16 wugnotMinDeposit int64 = 1
17)
18
19func init() {
20 cross(grc20reg.Register)(Token, "")
21}
22
23func Deposit() {
24 crossing()
25
26 caller := std.PreviousRealm().Address()
27 sent := std.OriginSend()
28 amount := sent.AmountOf("ugnot")
29
30 require(int64(amount) >= ugnotMinDeposit, ufmt.Sprintf("Deposit below minimum: %d/%d ugnot.", amount, ugnotMinDeposit))
31
32 checkErr(adm.Mint(caller, int64(amount)))
33}
34
35func Withdraw(amount int64) {
36 crossing()
37
38 require(amount >= wugnotMinDeposit, ufmt.Sprintf("Deposit below minimum: %d/%d wugnot.", amount, wugnotMinDeposit))
39
40 caller := std.PreviousRealm().Address()
41 pkgaddr := std.CurrentRealm().Address()
42 callerBal := Token.BalanceOf(caller)
43 require(amount <= callerBal, ufmt.Sprintf("Insufficient balance: %d available, %d needed.", callerBal, amount))
44
45 // send swapped ugnots to qcaller
46 stdBanker := std.NewBanker(std.BankerTypeRealmSend)
47 send := std.Coins{{"ugnot", int64(amount)}}
48 stdBanker.SendCoins(pkgaddr, caller, send)
49 checkErr(adm.Burn(caller, amount))
50}
51
52func Render(path string) string {
53 crossing()
54 parts := strings.Split(path, "/")
55 c := len(parts)
56
57 switch {
58 case path == "":
59 return Token.RenderHome()
60 case c == 2 && parts[0] == "balance":
61 owner := std.Address(parts[1])
62 balance := Token.BalanceOf(owner)
63 return ufmt.Sprintf("%d", balance)
64 default:
65 return "404"
66 }
67}
68
69func TotalSupply() int64 {
70 crossing()
71 return Token.TotalSupply()
72}
73
74func BalanceOf(owner std.Address) int64 {
75 crossing()
76 return Token.BalanceOf(owner)
77}
78
79func Allowance(owner, spender std.Address) int64 {
80 crossing()
81 return Token.Allowance(owner, spender)
82}
83
84func Transfer(to std.Address, amount int64) {
85 crossing()
86
87 userTeller := Token.CallerTeller()
88 checkErr(userTeller.Transfer(to, amount))
89}
90
91func Approve(spender std.Address, amount int64) {
92 crossing()
93
94 userTeller := Token.CallerTeller()
95 checkErr(userTeller.Approve(spender, amount))
96}
97
98func TransferFrom(from, to std.Address, amount int64) {
99 crossing()
100
101 userTeller := Token.CallerTeller()
102 checkErr(userTeller.TransferFrom(from, to, amount))
103}
104
105func require(condition bool, msg string) {
106 if !condition {
107 panic(msg)
108 }
109}
110
111func checkErr(err error) {
112 if err != nil {
113 panic(err)
114 }
115}