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