diff --git a/examples/gno.land/r/malek_/gfmt/gfmt.gno b/examples/gno.land/r/malek_/gfmt/gfmt.gno new file mode 100644 index 00000000000..23d064a51ee --- /dev/null +++ b/examples/gno.land/r/malek_/gfmt/gfmt.gno @@ -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) + } +} diff --git a/examples/gno.land/r/malek_/gfmt/gno.mod b/examples/gno.land/r/malek_/gfmt/gno.mod new file mode 100644 index 00000000000..d55e2d7f8c9 --- /dev/null +++ b/examples/gno.land/r/malek_/gfmt/gno.mod @@ -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 +) diff --git a/examples/gno.land/r/malek_/gfmt/z0_filetest.gno b/examples/gno.land/r/malek_/gfmt/z0_filetest.gno new file mode 100644 index 00000000000..cb7dafe3392 --- /dev/null +++ b/examples/gno.land/r/malek_/gfmt/z0_filetest.gno @@ -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 | +// -----------