Search Apps Documentation Source Content File Folder Download Copy Actions Download

wugnot.gno

2.34 Kb ยท 103 lines
  1package wugnot
  2
  3import (
  4	"chain"
  5	"chain/banker"
  6	"chain/runtime"
  7	"strings"
  8
  9	"gno.land/p/demo/tokens/grc20"
 10	"gno.land/p/nt/ufmt"
 11	"gno.land/r/demo/defi/grc20reg"
 12)
 13
 14var Token, adm = grc20.NewToken("wrapped GNOT", "wugnot", 0)
 15
 16const (
 17	ugnotMinDeposit  int64 = 1000
 18	wugnotMinDeposit int64 = 1
 19)
 20
 21func init() {
 22	grc20reg.Register(cross, Token, "")
 23}
 24
 25func Deposit(cur realm) {
 26	caller := runtime.PreviousRealm().Address()
 27	sent := banker.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(cur realm, amount int64) {
 36	require(amount >= wugnotMinDeposit, ufmt.Sprintf("Deposit below minimum: %d/%d wugnot.", amount, wugnotMinDeposit))
 37
 38	caller := runtime.PreviousRealm().Address()
 39	pkgaddr := runtime.CurrentRealm().Address()
 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 := banker.NewBanker(banker.BankerTypeRealmSend)
 45	send := chain.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 := address(parts[1])
 59		balance := Token.BalanceOf(owner)
 60		return ufmt.Sprintf("%d", balance)
 61	default:
 62		return "404"
 63	}
 64}
 65
 66func TotalSupply() int64 {
 67	return Token.TotalSupply()
 68}
 69
 70func BalanceOf(owner address) int64 {
 71	return Token.BalanceOf(owner)
 72}
 73
 74func Allowance(owner, spender address) int64 {
 75	return Token.Allowance(owner, spender)
 76}
 77
 78func Transfer(cur realm, to address, amount int64) {
 79	userTeller := Token.CallerTeller()
 80	checkErr(userTeller.Transfer(to, amount))
 81}
 82
 83func Approve(cur realm, spender address, amount int64) {
 84	userTeller := Token.CallerTeller()
 85	checkErr(userTeller.Approve(spender, amount))
 86}
 87
 88func TransferFrom(cur realm, from, to address, amount int64) {
 89	userTeller := Token.CallerTeller()
 90	checkErr(userTeller.TransferFrom(from, to, amount))
 91}
 92
 93func require(condition bool, msg string) {
 94	if !condition {
 95		panic(msg)
 96	}
 97}
 98
 99func checkErr(err error) {
100	if err != nil {
101		panic(err)
102	}
103}