github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/r/demo/wugnot/wugnot.gno (about)

     1  package wugnot
     2  
     3  import (
     4  	"std"
     5  	"strings"
     6  
     7  	"gno.land/p/demo/grc/grc20"
     8  	"gno.land/p/demo/ufmt"
     9  
    10  	"gno.land/r/demo/users"
    11  
    12  	pusers "gno.land/p/demo/users"
    13  )
    14  
    15  var (
    16  	// wugnot is the admin token, able to mint and burn.
    17  	wugnot *grc20.AdminToken = grc20.NewAdminToken("wrapped GNOT", "wugnot", 0)
    18  	// WUGNOT is the banker usable by users directly.
    19  	WUGNOT = wugnot.GRC20()
    20  )
    21  
    22  const (
    23  	ugnotMinDeposit  uint64 = 1000
    24  	wugnotMinDeposit uint64 = 1
    25  )
    26  
    27  // wrapper.
    28  //
    29  
    30  func Deposit() {
    31  	caller := std.PrevRealm().Addr()
    32  	sent := std.GetOrigSend()
    33  	amount := sent.AmountOf("ugnot")
    34  
    35  	if uint64(amount) < ugnotMinDeposit {
    36  		panic(ufmt.Sprintf("Deposit below minimum: %d/%d ugnot.", amount, ugnotMinDeposit))
    37  	}
    38  	wugnot.Mint(caller, uint64(amount))
    39  }
    40  
    41  func Withdraw(amount uint64) {
    42  	if amount < wugnotMinDeposit {
    43  		panic(ufmt.Sprintf("Deposit below minimum: %d/%d wugnot.", amount, wugnotMinDeposit))
    44  	}
    45  
    46  	caller := std.PrevRealm().Addr()
    47  	pkgaddr := std.CurrentRealm().Addr()
    48  
    49  	callerBal, _ := wugnot.BalanceOf(caller)
    50  	if callerBal < amount {
    51  		panic(ufmt.Sprintf("Insufficient balance: %d available, %d needed.", callerBal, amount))
    52  	}
    53  
    54  	// send swapped ugnots to caller
    55  	banker := std.GetBanker(std.BankerTypeRealmSend)
    56  	send := std.Coins{{"ugnot", int64(amount)}}
    57  	banker.SendCoins(pkgaddr, caller, send)
    58  	wugnot.Burn(caller, amount)
    59  }
    60  
    61  // render.
    62  //
    63  
    64  func Render(path string) string {
    65  	parts := strings.Split(path, "/")
    66  	c := len(parts)
    67  
    68  	switch {
    69  	case path == "":
    70  		return wugnot.RenderHome()
    71  	case c == 2 && parts[0] == "balance":
    72  		owner := std.Address(parts[1])
    73  		balance, _ := wugnot.BalanceOf(owner)
    74  		return ufmt.Sprintf("%d\n", balance)
    75  	default:
    76  		return "404\n"
    77  	}
    78  }
    79  
    80  // XXX: if we could call WUGNOT.XXX instead of XXX from gnokey, then, all the following lines would not be needed.
    81  
    82  // direct getters.
    83  // XXX: remove them in favor of q_call wugnot.XXX
    84  
    85  func TotalSupply() uint64 {
    86  	return wugnot.TotalSupply()
    87  }
    88  
    89  func BalanceOf(owner pusers.AddressOrName) uint64 {
    90  	balance, err := wugnot.BalanceOf(users.Resolve(owner))
    91  	if err != nil {
    92  		panic(err)
    93  	}
    94  	return balance
    95  }
    96  
    97  func Allowance(owner, spender pusers.AddressOrName) uint64 {
    98  	allowance, err := wugnot.Allowance(users.Resolve(owner), users.Resolve(spender))
    99  	if err != nil {
   100  		panic(err)
   101  	}
   102  	return allowance
   103  }
   104  
   105  // setters.
   106  //
   107  
   108  func Transfer(to pusers.AddressOrName, amount uint64) {
   109  	caller := std.PrevRealm().Addr()
   110  	err := wugnot.Transfer(caller, users.Resolve(to), amount)
   111  	if err != nil {
   112  		panic(err)
   113  	}
   114  }
   115  
   116  func Approve(spender pusers.AddressOrName, amount uint64) {
   117  	caller := std.PrevRealm().Addr()
   118  	err := wugnot.Approve(caller, users.Resolve(spender), amount)
   119  	if err != nil {
   120  		panic(err)
   121  	}
   122  }
   123  
   124  func TransferFrom(from, to pusers.AddressOrName, amount uint64) {
   125  	caller := std.PrevRealm().Addr()
   126  	err := wugnot.TransferFrom(caller, users.Resolve(from), users.Resolve(to), amount)
   127  	if err != nil {
   128  		panic(err)
   129  	}
   130  }