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