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