-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9897b66
commit 3df2cf4
Showing
3 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package gfmt | ||
|
||
import ( | ||
"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 | ||
) | ||
|
||
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) { | ||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module gno.land/r/malek_/gfmt | ||
|
||
require ( | ||
gno.land/p/demo/grc/grc20 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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | | ||
// ----------- |