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

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

musd.gno

3.06 Kb · 108 lines
  1// Package musd is Mimir USD (mUSD), the GRC20 test dollar Mimir markets stake
  2// on gno.land. It has 6 decimals, like USDC: 1 mUSD = 1_000_000 units.
  3//
  4// Anyone can call Faucet once per FaucetCooldown to receive FaucetAmount.
  5// It is a testnet token: it has no value and no backing.
  6package musd
  7
  8import (
  9	"chain"
 10	"strings"
 11	"time"
 12
 13	"gno.land/p/nt/avl/v0"
 14	"gno.land/p/nt/grc20/v0"
 15	"gno.land/p/nt/ownable/v0"
 16	"gno.land/p/nt/ufmt/v0"
 17)
 18
 19// Deployer owns Mint, used to seed agent and council wallets.
 20const Deployer = address("g12dcsmwccgdxasqde2eumua88tlrkw83kqf6tcz")
 21
 22const (
 23	Decimals       = 6
 24	Unit           = int64(1_000_000)
 25	FaucetAmount   = 1_000 * Unit
 26	FaucetCooldown = 24 * time.Hour
 27)
 28
 29var (
 30	Token      *grc20.Token
 31	ledger     *grc20.PrivateLedger
 32	userTeller grc20.Teller
 33	Ownable    *ownable.Ownable
 34	// lastDrip: address -> unix seconds of the last faucet payout.
 35	lastDrip avl.Tree
 36)
 37
 38func init(cur realm) {
 39	Token, ledger = grc20.NewToken("Mimir USD", "mUSD", Decimals, 0, cur)
 40	userTeller = ledger.CallerTeller()
 41	Ownable = ownable.NewWithAddress(Deployer)
 42}
 43
 44func TotalSupply() int64                     { return userTeller.TotalSupply() }
 45func BalanceOf(owner address) int64          { return userTeller.BalanceOf(owner) }
 46func Allowance(owner, spender address) int64 { return userTeller.Allowance(owner, spender) }
 47
 48func Transfer(cur realm, to address, amount int64) {
 49	check(userTeller.Transfer(0, cur, to, amount))
 50}
 51
 52func Approve(cur realm, spender address, amount int64) {
 53	check(userTeller.Approve(0, cur, spender, amount))
 54}
 55
 56func TransferFrom(cur realm, from, to address, amount int64) {
 57	check(userTeller.TransferFrom(0, cur, from, to, amount))
 58}
 59
 60// Faucet pays FaucetAmount to the caller, at most once per FaucetCooldown.
 61func Faucet(cur realm) {
 62	caller := cur.Previous().Address()
 63	now := time.Now().Unix()
 64	if v := lastDrip.Get(caller.String()); v != nil {
 65		next := v.(int64) + int64(FaucetCooldown/time.Second)
 66		if now < next {
 67			panic(ufmt.Sprintf("faucet: next drip at unix %d", next))
 68		}
 69	}
 70	lastDrip.Set(caller.String(), now)
 71	check(ledger.Mint(caller, FaucetAmount))
 72	chain.Emit("Faucet", "to", caller.String(), "amount", ufmt.Sprintf("%d", FaucetAmount))
 73}
 74
 75// NextDrip is the unix time from which addr may use the faucet again (0: now).
 76func NextDrip(addr address) int64 {
 77	v := lastDrip.Get(addr.String())
 78	if v == nil {
 79		return 0
 80	}
 81	return v.(int64) + int64(FaucetCooldown/time.Second)
 82}
 83
 84// Mint is for the owner, to seed agent and council wallets.
 85func Mint(cur realm, to address, amount int64) {
 86	Ownable.AssertOwnedBy(cur.Previous().Address())
 87	check(ledger.Mint(to, amount))
 88}
 89
 90func Render(path string) string {
 91	parts := strings.Split(path, "/")
 92	switch {
 93	case path == "":
 94		return Token.RenderHome() + "\n" + ufmt.Sprintf(
 95			"Faucet: %d mUSD per address every %s. Call `Faucet` from your wallet.\n",
 96			FaucetAmount/Unit, FaucetCooldown.String())
 97	case len(parts) == 2 && parts[0] == "balance":
 98		return ufmt.Sprintf("%d\n", userTeller.BalanceOf(address(parts[1])))
 99	default:
100		return "404\n"
101	}
102}
103
104func check(err error) {
105	if err != nil {
106		panic(err.Error())
107	}
108}