Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Playground

gnomod.toml

module = "gno.land/r/gnoland/wugnot"
gno = "0.9"

[addpkg]
  creator = "g1edq4dugw0sgat4zxcw9xardvuydqf6cgleuc8p"

wugnot.gno

package wugnot

import (
	"chain"
	"chain/banker"
	"chain/runtime"
	"chain/runtime/unsafe"
	"strings"

	"gno.land/p/nt/grc20/v0"
	"gno.land/p/nt/ufmt/v0"
	"gno.land/r/nt/grc20reg/v0"
)

var (
	Token *grc20.Token
	adm   *grc20.PrivateLedger
)

const (
	ugnotMinDeposit  int64 = 1000
	wugnotMinDeposit int64 = 1
)

func init(cur realm) {
	// wugnot only ever creates this one token, so id 0 can't collide.
	Token, adm = grc20.NewToken("wrapped GNOT", "wugnot", 0, 0, cur)
	grc20reg.Register(cross(cur), Token, "")
}

func Deposit(cur realm) {
	// Prevent cross-realm MITM: without this, an intermediary could
	// deposit on behalf of the caller and mint wugnot to itself
	// instead of the actual sender.
	runtime.AssertOriginCall()
	caller := cur.Previous().Address()
	sent := unsafe.OriginSend()
	amount := sent.AmountOf("ugnot")

	require(int64(amount) >= ugnotMinDeposit, ufmt.Sprintf("Deposit below minimum: %d/%d ugnot.", amount, ugnotMinDeposit))

	checkErr(adm.Mint(caller, int64(amount)))
}

func Withdraw(cur realm, amount int64) {
	runtime.AssertOriginCall()
	require(amount >= wugnotMinDeposit, ufmt.Sprintf("Deposit below minimum: %d/%d wugnot.", amount, wugnotMinDeposit))

	caller := cur.Previous().Address()
	pkgaddr := cur.Address()
	callerBal := Token.BalanceOf(caller)
	require(amount <= callerBal, ufmt.Sprintf("Insufficient balance: %d available, %d needed.", callerBal, amount))

	// send swapped ugnots to qcaller
	stdBanker := banker.NewBanker(banker.BankerTypeRealmSend, cur)
	send := chain.Coins{{"ugnot", int64(amount)}}
	stdBanker.SendCoins(pkgaddr, caller, send)
	checkErr(adm.Burn(caller, amount))
}

func Render(path string) string {
	parts := strings.Split(path, "/")
	c := len(parts)

	switch {
	case path == "":
		return Token.RenderHome()
	case c == 2 && parts[0] == "balance":
		owner := address(parts[1])
		balance := Token.BalanceOf(owner)
		return ufmt.Sprintf("%d", balance)
	default:
		return "404"
	}
}

func TotalSupply() int64 {
	return Token.TotalSupply()
}

func BalanceOf(owner address) int64 {
	return Token.BalanceOf(owner)
}

func Allowance(owner, spender address) int64 {
	return Token.Allowance(owner, spender)
}

func Transfer(cur realm, to address, amount int64) {
	userTeller := adm.CallerTeller()
	checkErr(userTeller.Transfer(0, cur, to, amount))
}

func Approve(cur realm, spender address, amount int64) {
	userTeller := adm.CallerTeller()
	checkErr(userTeller.Approve(0, cur, spender, amount))
}

func TransferFrom(cur realm, from, to address, amount int64) {
	userTeller := adm.CallerTeller()
	checkErr(userTeller.TransferFrom(0, cur, from, to, amount))
}

func require(condition bool, msg string) {
	if !condition {
		panic(msg)
	}
}

func checkErr(err error) {
	if err != nil {
		panic(err)
	}
}