wugnot.gno

2.26 Kb ยท 99 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  uint64 = 1000
16	wugnotMinDeposit uint64 = 1
17)
18
19func init() {
20	grc20reg.Register(Token.Getter(), "")
21}
22
23func Deposit() {
24	caller := std.PreviousRealm().Address()
25	sent := std.OriginSend()
26	amount := sent.AmountOf("ugnot")
27
28	require(uint64(amount) >= ugnotMinDeposit, ufmt.Sprintf("Deposit below minimum: %d/%d ugnot.", amount, ugnotMinDeposit))
29
30	checkErr(adm.Mint(caller, uint64(amount)))
31}
32
33func Withdraw(amount uint64) {
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() uint64 { return Token.TotalSupply() }
65
66func BalanceOf(owner std.Address) uint64 {
67	return Token.BalanceOf(owner)
68}
69
70func Allowance(owner, spender std.Address) uint64 {
71	return Token.Allowance(owner, spender)
72}
73
74func Transfer(to std.Address, amount uint64) {
75	userTeller := Token.CallerTeller()
76	checkErr(userTeller.Transfer(to, amount))
77}
78
79func Approve(spender std.Address, amount uint64) {
80	userTeller := Token.CallerTeller()
81	checkErr(userTeller.Approve(spender, amount))
82}
83
84func TransferFrom(from, to std.Address, amount uint64) {
85	userTeller := Token.CallerTeller()
86	checkErr(userTeller.TransferFrom(from, to, amount))
87}
88
89func require(condition bool, msg string) {
90	if !condition {
91		panic(msg)
92	}
93}
94
95func checkErr(err error) {
96	if err != nil {
97		panic(err)
98	}
99}