Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(examples): GnoFundMe Token v0 #2843

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions examples/gno.land/r/malek_/gfmt/gfmt.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package gfmt

import (
"errors"
"std"
"strings"

"gno.land/p/demo/grc/grc20"
"gno.land/p/demo/ownable"
"gno.land/p/demo/ufmt"
pusers "gno.land/p/demo/users"
"gno.land/r/demo/users"
)

var (
banker *grc20.Banker
admin *ownable.Ownable
Token grc20.Token
ErrInvalidUint64 = errors.New("invalid uint64, must be between 0 and 18446744073709551615")
)

func init() {
admin = ownable.NewWithAddress("g10ahumypepd2qcrau7kahv8q78f7jcdns5tn54a") // owner's address (malek_)
banker = grc20.NewBanker("GnoFundMe Token", "gfmt", 0)
banker.Mint(admin.Owner(), 100_000_000_000)
Token = banker.Token()
}

const (
ugnotMinDeposit uint64 = 1000000 // 1 GNOT
gfmtMinDeposit uint64 = 1
)

func Deposit() {
caller := std.PrevRealm().Addr()
sent := std.GetOrigSend()
amount := sent.AmountOf("ugnot")

require(uint64(amount) >= ugnotMinDeposit, ufmt.Sprintf("Deposit below minimum: %d/%d ugnot.", amount, ugnotMinDeposit))
checkErr(banker.Mint(caller, uint64(amount)))
}

func Withdraw(amount uint64) {
// check for overflow
// if amount < 0 || amount > 18446744073709551615 {
// panic("Invalid amount: Overflow uint64")
// }

require(amount >= gfmtMinDeposit, ufmt.Sprintf("Deposit below minimum: %d/%d gfmt.", amount, gfmtMinDeposit))

caller := std.PrevRealm().Addr()
pkgaddr := std.CurrentRealm().Addr()
callerBal := Token.BalanceOf(caller)
require(amount <= callerBal, ufmt.Sprintf("Insufficient balance: %d available, %d needed.", callerBal, amount))

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

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

switch {
case path == "":
return banker.RenderHome()
case c == 2 && parts[0] == "balance":
owner := std.Address(parts[1])
balance := Token.BalanceOf(owner)
return ufmt.Sprintf("%s's balance is %dugnot", owner, balance)
default:
return "404"
}
}

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

func BalanceOf(owner pusers.AddressOrName) uint64 {
ownerAddr := users.Resolve(owner)
return Token.BalanceOf(ownerAddr)
}

func Allowance(owner, spender pusers.AddressOrName) uint64 {
ownerAddr := users.Resolve(owner)
spenderAddr := users.Resolve(spender)
return Token.Allowance(ownerAddr, spenderAddr)
}

func Transfer(to pusers.AddressOrName, amount uint64) {
toAddr := users.Resolve(to)
checkErr(Token.Transfer(toAddr, amount))
}

func Approve(spender pusers.AddressOrName, amount uint64) {
spenderAddr := users.Resolve(spender)
checkErr(Token.Approve(spenderAddr, amount))
}

func TransferFrom(from, to pusers.AddressOrName, amount uint64) {
fromAddr := users.Resolve(from)
toAddr := users.Resolve(to)
checkErr(Token.TransferFrom(fromAddr, toAddr, amount))
}

func Burn(from pusers.AddressOrName, amount uint64) {
admin.AssertCallerIsOwner()
fromAddr := users.Resolve(from)
checkErr(banker.Burn(fromAddr, amount))
}

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

func checkErr(err error) {
if err != nil {
panic(err)
}
}
9 changes: 9 additions & 0 deletions examples/gno.land/r/malek_/gfmt/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module gno.land/r/malek_/gfmt

require (
gno.land/p/demo/grc/grc20 v0.0.0-latest
gno.land/p/demo/ownable v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
gno.land/p/demo/users v0.0.0-latest
gno.land/r/demo/users v0.0.0-latest
)
71 changes: 71 additions & 0 deletions examples/gno.land/r/malek_/gfmt/z0_filetest.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// PKGPATH: gno.land/r/malek_/gfmt_test
package gfmt_test

import (
"fmt"
"std"

"gno.land/p/demo/testutils"
"gno.land/r/malek_/gfmt"

pusers "gno.land/p/demo/users"
)

var (
addr1 = testutils.TestAddress("test1")
addrc = std.DerivePkgAddr("gno.land/r/malek_/gfmt")
addrt = std.DerivePkgAddr("gno.land/r/malek_/gfmt_test")
)

func main() {
std.TestSetOrigPkgAddr(addrc)
std.TestIssueCoins(addrc, std.Coins{{"ugnot", 100000001}}) // TODO: remove this

// issue ugnots
std.TestIssueCoins(addr1, std.Coins{{"ugnot", 100000001}})

// print initial state
printBalances()
// println(gfmt.Render("queues"))
// println("A -", gfmt.Render(""))

std.TestSetOrigCaller(addr1)
std.TestSetOrigSend(std.Coins{{"ugnot", 1_000_000}}, nil)
gfmt.Deposit()
printBalances()
gfmt.Withdraw(4242)
printBalances()
}

func printBalances() {
printSingleBalance := func(name string, addr std.Address) {
gfmtBal := gfmt.BalanceOf(pusers.AddressOrName(addr))
std.TestSetOrigCaller(addr)
robanker := std.GetBanker(std.BankerTypeReadonly)
coins := robanker.GetCoins(addr).AmountOf("ugnot")
fmt.Printf("| %-13s | addr=%s | gfmt=%-7d | ugnot=%-9d |\n",
name, addr, gfmtBal, coins)
}
println("-----------")
printSingleBalance("gfmt_test", addrt)
printSingleBalance("gfmt", addrc)
printSingleBalance("addr1", addr1)
println("-----------")
}

// Output:
// -----------
// | gfmt_test | addr=g15pyg9fhp8nlt8vhvh4pwhvamdxzuk052mlj5s7 | gfmt=0 | ugnot=200000000 |
// | gfmt | addr=g1hralrm7msf9uc3nzjmpvmsxapfdxx2j5k4yhjm | gfmt=0 | ugnot=100000001 |
// | addr1 | addr=g1w3jhxap3ta047h6lta047h6lta047h6l4mfnm7 | gfmt=0 | ugnot=100000001 |
// -----------
// -----------
// | gfmt_test | addr=g15pyg9fhp8nlt8vhvh4pwhvamdxzuk052mlj5s7 | gfmt=1000000 | ugnot=200000000 |
// | gfmt | addr=g1hralrm7msf9uc3nzjmpvmsxapfdxx2j5k4yhjm | gfmt=0 | ugnot=100000001 |
// | addr1 | addr=g1w3jhxap3ta047h6lta047h6lta047h6l4mfnm7 | gfmt=0 | ugnot=100000001 |
// -----------
// -----------
// | gfmt_test | addr=g15pyg9fhp8nlt8vhvh4pwhvamdxzuk052mlj5s7 | gfmt=995758 | ugnot=200004242 |
// | gfmt | addr=g1hralrm7msf9uc3nzjmpvmsxapfdxx2j5k4yhjm | gfmt=0 | ugnot=99995759 |
// | addr1 | addr=g1w3jhxap3ta047h6lta047h6lta047h6l4mfnm7 | gfmt=0 | ugnot=100000001 |
// -----------
Loading