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

refactor: remove unnecessary functions from the gns realm #482

Merged
merged 4 commits into from
Jan 24, 2025
Merged
Changes from 1 commit
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
Next Next commit
refactor: remove unnecessary function from gns
  • Loading branch information
r3v4s committed Jan 22, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit b14b12eef4ea2cf364035a610f65c1e8871e9cef
44 changes: 13 additions & 31 deletions contract/r/gnoswap/gns/gns.gno
Original file line number Diff line number Diff line change
@@ -49,37 +49,10 @@ func init() {
burnAmount = uint64(0)
}

func GetName() string {
return Token.GetName()
}

func GetSymbol() string {
return Token.GetSymbol()
}

func GetDecimals() uint {
return Token.GetDecimals()
}

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

func KnownAccounts() int {
return Token.KnownAccounts()
}

func BalanceOfAddress(owner std.Address) uint64 {
common.AssertValidAddr(owner)
return Token.BalanceOf(owner)
}

func AllowanceOfAddress(owner, spender std.Address) uint64 {
common.AssertValidAddr(owner)
common.AssertValidAddr(spender)
return Token.Allowance(owner, spender)
}

func BalanceOf(owner std.Address) uint64 {
return UserTeller.BalanceOf(owner)
}
@@ -88,10 +61,7 @@ func Allowance(owner, spender std.Address) uint64 {
return UserTeller.Allowance(owner, spender)
}

func SpendAllowance(owner, spender std.Address, amount uint64) {
checkErr(privateLedger.SpendAllowance(owner, spender, amount))
}

func MintGns(address std.Address) uint64 {
func MintGns(address std.Address) uint64 {
lastGNSMintedHeight := GetLastMintedHeight()
currentHeight := std.GetHeight()
@@ -116,6 +86,7 @@ func MintGns(address std.Address) uint64 {

// mint calculated amount to address
err := privateLedger.Mint(address, amountToMint)
err := privateLedger.Mint(address, amountToMint)
if err != nil {
panic(err.Error())
}
@@ -134,9 +105,11 @@ func MintGns(address std.Address) uint64 {
return amountToMint
}

func Burn(from std.Address, amount uint64) {
func Burn(from std.Address, amount uint64) {
owner.AssertCallerIsOwner()
checkErr(privateLedger.Burn(from, amount))
checkErr(privateLedger.Burn(from, amount))

burnAmount += amount

@@ -147,19 +120,26 @@ func Burn(from std.Address, amount uint64) {
"prevRealm", prevPkgPath,
"burnedBlockHeight", formatInt(std.GetHeight()),
"burnFrom", from.String(),
"burnFrom", from.String(),
"burnedGNSAmount", formatUint(amount),
"accumBurnedGNSAmount", formatUint(GetBurnAmount()),
)
}

func Transfer(to std.Address, amount uint64) {
checkErr(UserTeller.Transfer(to, amount))
func Transfer(to std.Address, amount uint64) {
checkErr(UserTeller.Transfer(to, amount))
}

func Approve(spender std.Address, amount uint64) {
checkErr(UserTeller.Approve(spender, amount))
func Approve(spender std.Address, amount uint64) {
checkErr(UserTeller.Approve(spender, amount))
}

func TransferFrom(from, to std.Address, amount uint64) {
checkErr(UserTeller.TransferFrom(from, to, amount))
func TransferFrom(from, to std.Address, amount uint64) {
checkErr(UserTeller.TransferFrom(from, to, amount))
}
@@ -172,6 +152,8 @@ func Render(path string) string {
case path == "":
return Token.RenderHome()
case c == 2 && parts[0] == "balance":
owner := std.Address(parts[1])
balance := UserTeller.BalanceOf(owner)
owner := std.Address(parts[1])
balance := UserTeller.BalanceOf(owner)
return ufmt.Sprintf("%d\n", balance)
113 changes: 14 additions & 99 deletions contract/r/gnoswap/gns/gns_test.gno
Original file line number Diff line number Diff line change
@@ -27,114 +27,15 @@ var (
bob = testutils.TestAddress("bob")
)

func TestGetName(t *testing.T) {
uassert.Equal(t, "Gnoswap", GetName())
}

func TestGetSymbol(t *testing.T) {
uassert.Equal(t, "GNS", GetSymbol())
}

func TestGetDecimals(t *testing.T) {
uassert.Equal(t, uint(6), GetDecimals())
}

func TestTotalSupply(t *testing.T) {

uassert.Equal(t, INITIAL_MINT_AMOUNT, TotalSupply())
}

func TestKnownAccounts(t *testing.T) {
uassert.Equal(t, int(1), KnownAccounts())
}

func TestBalanceOfAddress(t *testing.T) {
t.Run(
"should return balance of address",
func(t *testing.T) {
uassert.Equal(t, INITIAL_MINT_AMOUNT, BalanceOfAddress(consts.ADMIN))
},
)
t.Run(
"should panic if address is not valid",
func(t *testing.T) {
uassert.PanicsWithMessage(t, "[GNOSWAP-COMMON-005] invalid address || 0xabcdefg", func() {
BalanceOfAddress("0xabcdefg")
})
},
)
}

func TestAllowanceOfAddress(t *testing.T) {
t.Run(
"should return allowance of address",
func(t *testing.T) {
uassert.Equal(t, uint64(0), AllowanceOfAddress(consts.ADMIN, alice))

std.TestSetOrigCaller(consts.ADMIN)
Approve(alice, uint64(123))
uassert.Equal(t, uint64(123), AllowanceOfAddress(consts.ADMIN, alice))
},
)
t.Run(
"should panic if address is not valid",
func(t *testing.T) {
uassert.PanicsWithMessage(t, "[GNOSWAP-COMMON-005] invalid address || 0xabcdefg", func() {
AllowanceOfAddress("0xabcdefg", alice)
})
},
)
t.Run(
"should panic if spender is not valid",
func(t *testing.T) {
uassert.PanicsWithMessage(t, "[GNOSWAP-COMMON-005] invalid address || 0xabcdefg", func() {
AllowanceOfAddress(consts.ADMIN, "0xabcdefg")
})
},
)
}

func TestBalanceOf(t *testing.T) {
uassert.Equal(t, INITIAL_MINT_AMOUNT, BalanceOf(consts.ADMIN))
}

func TestSpendAllowance(t *testing.T) {
t.Run(
"should spend allowance",
func(t *testing.T) {
std.TestSetOrigCaller(consts.ADMIN)
Approve(alice, uint64(123))

SpendAllowance(consts.ADMIN, alice, uint64(23))
uassert.Equal(t, uint64(100), Allowance(consts.ADMIN, alice))
},
)
t.Run(
"should panic if address is not valid",
func(t *testing.T) {
uassert.PanicsWithMessage(t, "invalid address", func() {
SpendAllowance("0xabcdefg", alice, uint64(123))
})
},
)
t.Run(
"should panic if spender is not valid",
func(t *testing.T) {
uassert.PanicsWithMessage(t, "invalid address", func() {
SpendAllowance(consts.ADMIN, "0xabcdefg", uint64(123))
})
},
)
t.Run(
"should panic if allowance is not enough",
func(t *testing.T) {
uassert.PanicsWithMessage(t, "insufficient allowance", func() {
SpendAllowance(consts.ADMIN, alice, uint64(123))
})
},
)
}

func TestAssertTooManyEmission(t *testing.T) {
tests := []struct {
name string
@@ -289,32 +190,37 @@ func TestGrc20Methods(t *testing.T) {
name: "BalanceOf(admin)",
fn: func() {
uassert.Equal(t, INITIAL_MINT_AMOUNT, BalanceOf(consts.ADMIN))
uassert.Equal(t, INITIAL_MINT_AMOUNT, BalanceOf(consts.ADMIN))
},
},
{
name: "BalanceOf(alice)",
fn: func() {
uassert.Equal(t, uint64(0), BalanceOf(alice))
uassert.Equal(t, uint64(0), BalanceOf(alice))
},
},
{
name: "Allowance(admin, alice)",
fn: func() {
uassert.Equal(t, uint64(0), Allowance(consts.ADMIN, alice))
uassert.Equal(t, uint64(0), Allowance(consts.ADMIN, alice))
},
},
{
name: "MintGns success",
fn: func() {
std.TestSetRealm(emissionRealm)
MintGns(consts.ADMIN)
MintGns(consts.ADMIN)
},
},
{
name: "MintGns without permission should panic",
fn: func() {
std.TestSkipHeights(1)
MintGns(consts.ADMIN)
MintGns(consts.ADMIN)
},
shouldPanic: true,
panicMsg: `caller(g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm) has no permission`,
@@ -324,12 +230,14 @@ func TestGrc20Methods(t *testing.T) {
fn: func() {
std.TestSetRealm(adminRealm)
Burn(consts.ADMIN, uint64(1))
Burn(consts.ADMIN, uint64(1))
},
},
{
name: "Burn without permission should panic",
fn: func() {
Burn(consts.ADMIN, uint64(1))
Burn(consts.ADMIN, uint64(1))
},
shouldPanic: true,
panicMsg: `ownable: caller is not owner`,
@@ -339,13 +247,15 @@ func TestGrc20Methods(t *testing.T) {
fn: func() {
std.TestSetRealm(adminRealm)
Transfer(alice, uint64(1))
Transfer(alice, uint64(1))
},
},
{
name: "Transfer without enough balance should panic",
fn: func() {
std.TestSetRealm(std.NewUserRealm(alice))
Transfer(bob, uint64(1))
Transfer(bob, uint64(1))
},
shouldPanic: true,
panicMsg: `insufficient balance`,
@@ -355,6 +265,7 @@ func TestGrc20Methods(t *testing.T) {
fn: func() {
std.TestSetRealm(adminRealm)
Transfer(consts.ADMIN, uint64(1))
Transfer(consts.ADMIN, uint64(1))
},
shouldPanic: true,
panicMsg: `cannot send transfer to self`,
@@ -365,20 +276,24 @@ func TestGrc20Methods(t *testing.T) {
// approve first
std.TestSetRealm(adminRealm)
Approve(alice, uint64(1))
Approve(alice, uint64(1))

// alice transfer admin's balance to bob
std.TestSetRealm(std.NewUserRealm(alice))
TransferFrom(consts.ADMIN, bob, uint64(1))
TransferFrom(consts.ADMIN, bob, uint64(1))
},
},
{
name: "TransferFrom without enough allowance should panic",
fn: func() {
std.TestSetRealm(adminRealm)
Approve(alice, uint64(1))
Approve(alice, uint64(1))

std.TestSetRealm(std.NewUserRealm(alice))
TransferFrom(consts.ADMIN, bob, uint64(2))
TransferFrom(consts.ADMIN, bob, uint64(2))
},
shouldPanic: true,
panicMsg: `insufficient allowance`,
Loading
Oops, something went wrong.