Skip to content

Commit

Permalink
refactor: use grc20reg realm to support multiple grc20 tokens
Browse files Browse the repository at this point in the history
- replaces previous token_register pattern
  • Loading branch information
r3v4s authored and onlyhyde committed Dec 27, 2024
1 parent 4137b8b commit d1a5e11
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 400 deletions.
141 changes: 0 additions & 141 deletions router/_helper_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,6 @@ import (
"gno.land/r/onbloc/qux"
)

func init() {
std.TestSetRealm(std.NewUserRealm("g1er355fkjksqpdtwmhf5penwa82p0rhqxkkyhk5"))
RegisterGRC20Interface(wugnotPath, WugnotToken{})
RegisterGRC20Interface(gnsPath, GNSToken{})
RegisterGRC20Interface(barPath, BarToken{})
RegisterGRC20Interface(bazPath, BazToken{})
RegisterGRC20Interface(fooPath, FooToken{})
RegisterGRC20Interface(oblPath, OBLToken{})
RegisterGRC20Interface(quxPath, QuxToken{})
}

const (
ugnotDenom string = "ugnot"
ugnotPath string = "ugnot"
Expand Down Expand Up @@ -78,136 +67,6 @@ var (
maxTick int32 = 887220
)

type WugnotToken struct{}

func (WugnotToken) Transfer() func(to pusers.AddressOrName, amount uint64) {
return wugnot.Transfer
}

func (WugnotToken) TransferFrom() func(from, to pusers.AddressOrName, amount uint64) {
return wugnot.TransferFrom
}

func (WugnotToken) BalanceOf() func(owner pusers.AddressOrName) uint64 {
return wugnot.BalanceOf
}

func (WugnotToken) Approve() func(spender pusers.AddressOrName, amount uint64) {
return wugnot.Approve
}

type GNSToken struct{}

func (GNSToken) Transfer() func(to pusers.AddressOrName, amount uint64) {
return gns.Transfer
}

func (GNSToken) TransferFrom() func(from, to pusers.AddressOrName, amount uint64) {
return gns.TransferFrom
}

func (GNSToken) BalanceOf() func(owner pusers.AddressOrName) uint64 {
return gns.BalanceOf
}

func (GNSToken) Approve() func(spender pusers.AddressOrName, amount uint64) {
return gns.Approve
}

type BarToken struct{}

func (BarToken) Transfer() func(to pusers.AddressOrName, amount uint64) {
return bar.Transfer
}

func (BarToken) TransferFrom() func(from, to pusers.AddressOrName, amount uint64) {
return bar.TransferFrom
}

func (BarToken) BalanceOf() func(owner pusers.AddressOrName) uint64 {
return bar.BalanceOf
}

func (BarToken) Approve() func(spender pusers.AddressOrName, amount uint64) {
return bar.Approve
}

type BazToken struct{}

func (BazToken) Transfer() func(to pusers.AddressOrName, amount uint64) {
return baz.Transfer
}

func (BazToken) TransferFrom() func(from, to pusers.AddressOrName, amount uint64) {
return baz.TransferFrom
}

func (BazToken) BalanceOf() func(owner pusers.AddressOrName) uint64 {
return baz.BalanceOf
}

func (BazToken) Approve() func(spender pusers.AddressOrName, amount uint64) {
return baz.Approve
}

type FooToken struct{}

func (FooToken) Transfer() func(to pusers.AddressOrName, amount uint64) {
return foo.Transfer
}

func (FooToken) TransferFrom() func(from, to pusers.AddressOrName, amount uint64) {
return foo.TransferFrom
}

func (FooToken) BalanceOf() func(owner pusers.AddressOrName) uint64 {
return foo.BalanceOf
}

func (FooToken) Approve() func(spender pusers.AddressOrName, amount uint64) {
return foo.Approve
}

type OBLToken struct{}

func (OBLToken) Transfer() func(to pusers.AddressOrName, amount uint64) {
return obl.Transfer
}

func (OBLToken) TransferFrom() func(from, to pusers.AddressOrName, amount uint64) {
return obl.TransferFrom
}

func (OBLToken) BalanceOf() func(owner pusers.AddressOrName) uint64 {
return obl.BalanceOf
}

func (OBLToken) Approve() func(spender pusers.AddressOrName, amount uint64) {
return obl.Approve
}

type QuxToken struct{}

func (QuxToken) Transfer() func(to pusers.AddressOrName, amount uint64) {
return qux.Transfer
}

func (QuxToken) TransferFrom() func(from, to pusers.AddressOrName, amount uint64) {
return qux.TransferFrom
}

func (QuxToken) BalanceOf() func(owner pusers.AddressOrName) uint64 {
return qux.BalanceOf
}

func (QuxToken) Approve() func(spender pusers.AddressOrName, amount uint64) {
return qux.Approve
}

func init() {
std.TestSetRealm(std.NewUserRealm(consts.TOKEN_REGISTER))
}

var (
admin = pusers.AddressOrName(consts.ADMIN)
adminAddr = users.Resolve(admin)
Expand Down
11 changes: 5 additions & 6 deletions router/exact_in_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package router
import (
"std"
"testing"
"time"

"gno.land/r/gnoswap/v1/consts"

"gno.land/r/onbloc/bar"
"gno.land/r/onbloc/baz"
)

func TestExactInSwapRouteOperation_Validate(t *testing.T) {
Expand Down Expand Up @@ -96,9 +98,6 @@ func TestExactInSwapRoute(t *testing.T) {
user1Realm := std.NewUserRealm(user1Addr)
std.TestSetRealm(user1Realm)

bar := BarToken{}
baz := BazToken{}

tests := []struct {
name string
setup func()
Expand All @@ -113,8 +112,8 @@ func TestExactInSwapRoute(t *testing.T) {
{
name: "BAR -> BAZ",
setup: func() {
bar.Approve()(a2u(consts.ROUTER_ADDR), maxApprove)
baz.Approve()(a2u(consts.ROUTER_ADDR), maxApprove)
bar.Approve(a2u(consts.ROUTER_ADDR), maxApprove)
baz.Approve(a2u(consts.ROUTER_ADDR), maxApprove)
TokenFaucet(t, barPath, a2u(user1Addr))
},
inputToken: barPath,
Expand Down
3 changes: 2 additions & 1 deletion router/protocol_fee_swap.gno
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func handleSwapFee(
feeAmount.Div(feeAmount, u256.NewUint(10000))
feeAmountUint64 := feeAmount.Uint64()

transferFromByRegisterCall(outputToken, std.PrevRealm().Addr(), consts.PROTOCOL_FEE_ADDR, feeAmountUint64)
outputTeller := common.GetTokenTeller(outputToken)
outputTeller.TransferFrom(std.PrevRealm().Addr(), consts.PROTOCOL_FEE_ADDR, feeAmountUint64)

prevAddr, prevRealm := getPrev()

Expand Down
20 changes: 1 addition & 19 deletions router/protocol_fee_swap_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,10 @@ package router
import (
"testing"

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

u256 "gno.land/p/gnoswap/uint256"
)

func TestHandleSwapFee(t *testing.T) {
token0 := "token0"

mockToken := &struct {
GRC20Interface
}{
GRC20Interface: MockGRC20{
TransferFn: func(to pusers.AddressOrName, amount uint64) {},
TransferFromFn: func(from, to pusers.AddressOrName, amount uint64) {},
BalanceOfFn: func(owner pusers.AddressOrName) uint64 { return 1000000 },
ApproveFn: func(spender pusers.AddressOrName, amount uint64) {},
},
}

registerGRC20ForTest(t, token0, mockToken)
defer unregisterGRC20ForTest(t, token0)

tests := []struct {
name string
amount *u256.Uint
Expand Down Expand Up @@ -65,7 +47,7 @@ func TestHandleSwapFee(t *testing.T) {
swapFee = originalSwapFee
}()

result := handleSwapFee(token0, tt.amount)
result := handleSwapFee(barPath, tt.amount)

if !result.Eq(tt.expectedAmount) {
t.Errorf("handleSwapFee() = %v, want %v", result, tt.expectedAmount)
Expand Down
60 changes: 1 addition & 59 deletions router/router_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,13 @@ package router

import (
"std"
"strconv"
"strings"
"testing"

"gno.land/p/demo/uassert"
"gno.land/p/demo/testutils"
"testing"

"gno.land/r/gnoswap/v1/consts"
i256 "gno.land/p/gnoswap/int256"
u256 "gno.land/p/gnoswap/uint256"

pl "gno.land/r/gnoswap/v1/pool"
pn "gno.land/r/gnoswap/v1/position"
"gno.land/r/demo/wugnot"
"gno.land/r/onbloc/bar"
"gno.land/r/onbloc/baz"
"gno.land/r/onbloc/qux"
"gno.land/r/gnoswap/v1/gns"

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

func registerGRC20ForTest(t *testing.T, pkgPath string, igrc20 GRC20Interface) {
t.Helper()
registered[pkgPath] = igrc20
}

func unregisterGRC20ForTest(t *testing.T, pkgPath string) {
t.Helper()
delete(registered, pkgPath)
}

type MockGRC20 struct {
TransferFn func(to pusers.AddressOrName, amount uint64)
TransferFromFn func(from, to pusers.AddressOrName, amount uint64)
BalanceOfFn func(owner pusers.AddressOrName) uint64
ApproveFn func(spender pusers.AddressOrName, amount uint64)
AllowanceFn func(owner, spender pusers.AddressOrName) uint64
}

func (m MockGRC20) Transfer() func(to pusers.AddressOrName, amount uint64) {
return m.TransferFn
}

func (m MockGRC20) TransferFrom() func(from, to pusers.AddressOrName, amount uint64) {
return m.TransferFromFn
}

func (m MockGRC20) BalanceOf() func(owner pusers.AddressOrName) uint64 {
return m.BalanceOfFn
}

func (m MockGRC20) Approve() func(spender pusers.AddressOrName, amount uint64) {
return m.ApproveFn
}

func (m MockGRC20) Allowance() func(owner, spender pusers.AddressOrName) uint64 {
if m.AllowanceFn != nil {
return m.AllowanceFn
}
return func(owner, spender pusers.AddressOrName) uint64 {
return 1000000000000
}
}

func setupTestPool(
t *testing.T,
token0Path, token1Path string,
Expand Down
6 changes: 4 additions & 2 deletions router/swap_inner.gno
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ func swapInner(
sqrtPriceLimitX96 = calculateSqrtPriceLimitForSwap(zeroForOne, data.fee, sqrtPriceLimitX96)

// ROUTER approves POOL as spender
approveByRegisterCall(data.tokenIn, consts.POOL_ADDR, consts.UINT64_MAX)
approveByRegisterCall(data.tokenOut, consts.POOL_ADDR, consts.UINT64_MAX)
tokenIn := common.GetTokenTeller(data.tokenIn)
tokenOut := common.GetTokenTeller(data.tokenOut)
tokenIn.Approve(consts.POOL_ADDR, consts.UINT64_MAX)
tokenOut.Approve(consts.POOL_ADDR, consts.UINT64_MAX)

amount0Str, amount1Str := pl.Swap( // int256, int256
data.tokenIn,
Expand Down
Loading

0 comments on commit d1a5e11

Please sign in to comment.