Skip to content

Commit

Permalink
Merge branch 'main' into dry-swap
Browse files Browse the repository at this point in the history
  • Loading branch information
onlyhyde authored Dec 11, 2024
2 parents 7d3f7db + 78dfa75 commit e11c132
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 509 deletions.
7 changes: 4 additions & 3 deletions _deploy/r/gnoswap/common/errors.gno
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
)

var (
errNoPermission = errors.New("[GNOSWAP-COMMON-001] caller has no permission")
errHalted = errors.New("[GNOSWAP-COMMON-002] halted")
errOutOfRange = errors.New("[GNOSWAP-COMMON-003] value out of range")
errNoPermission = errors.New("[GNOSWAP-COMMON-001] caller has no permission")
errHalted = errors.New("[GNOSWAP-COMMON-002] halted")
errOutOfRange = errors.New("[GNOSWAP-COMMON-003] value out of range")
errNotRegistered = errors.New("[GNOSWAP-COMMON-004] token is not registered")
)

func addDetailToError(err error, detail string) string {
Expand Down
11 changes: 11 additions & 0 deletions _deploy/r/gnoswap/common/grc20reg_helper.gno
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,14 @@ func IsRegistered(path string) error {
}
return nil
}

// MustRegistered is a helper function to check if token is registered to grc20reg
// if token is not registered, it will panic
func MustRegistered(path string) {
if err := IsRegistered(path); err != nil {
panic(addDetailToError(
errNotRegistered,
ufmt.Sprintf("token(%s) is not registered", path),
))
}
}
40 changes: 30 additions & 10 deletions _deploy/r/gnoswap/common/grc20reg_helper_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ var (
)

func TestGetToken(t *testing.T) {
t.Run("registered(by default)", func(t *testing.T) {
t.Run("get regsitered token", func(t *testing.T) {
token := GetToken(tokenPath)
if token == nil {
t.Error("Expected non-nil teller for foo20")
t.Error("Expected non-nil token for foo20")
}
})

t.Run("not registered", func(t *testing.T) {
t.Run("get non registered token", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic for non-registered token")
}
}()
GetToken("not_registered")
GetToken("not_registered_token")
})
}

Expand Down Expand Up @@ -75,20 +75,20 @@ func TestTokenMethod(t *testing.T) {
}

func TestGetTokenTeller(t *testing.T) {
t.Run("registered(by default)", func(t *testing.T) {
t.Run("get registered token teller", func(t *testing.T) {
teller := GetTokenTeller(tokenPath)
if teller == nil {
t.Error("Expected non-nil teller for foo20")
}
})

t.Run("not registered", func(t *testing.T) {
t.Run("get non registered token teller", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic for non-registered token")
t.Errorf("Expected panic for non-registered token teller")
}
}()
GetTokenTeller("not_registered")
GetTokenTeller("not_registered_teller")
})
}

Expand Down Expand Up @@ -129,6 +129,26 @@ func TestTellerMethod(t *testing.T) {
}

func TestIsRegistered(t *testing.T) {
uassert.NoError(t, IsRegistered(tokenPath))
uassert.Error(t, IsRegistered("not_registered"))
t.Run("check if token is registered", func(t *testing.T) {
uassert.NoError(t, IsRegistered(tokenPath))
})

t.Run("check if token is not registered", func(t *testing.T) {
uassert.Error(t, IsRegistered("not_registered_token"))
})
}

func TestMustRegistered(t *testing.T) {
t.Run("must be registered", func(t *testing.T) {
MustRegistered(tokenPath)
})

t.Run("panic for non registered token", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic for non-registered token")
}
}()
MustRegistered("not_registered")
})
}
117 changes: 0 additions & 117 deletions pool/_helper_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -45,123 +45,6 @@ const (
addr02 = testutils.TestAddress("addr02")
)

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))

RegisterGRC20Interface(wugnotPath, WugnotToken{})
RegisterGRC20Interface(gnsPath, GNSToken{})
RegisterGRC20Interface(barPath, BarToken{})
RegisterGRC20Interface(bazPath, BazToken{})
RegisterGRC20Interface(fooPath, FooToken{})
RegisterGRC20Interface(oblPath, OBLToken{})
RegisterGRC20Interface(quxPath, QuxToken{})
}

var (
admin = pusers.AddressOrName(consts.ADMIN)
alice = pusers.AddressOrName(testutils.TestAddress("alice"))
Expand Down
46 changes: 29 additions & 17 deletions pool/pool.gno
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,18 @@ func Collect(
// Smallest of three: amount0Requested, position.tokensOwed0, pool.balances.token0
amount0Req := u256.MustFromDecimal(amount0Requested)
amount0, position.tokensOwed0, pool.balances.token0 = collectToken(amount0Req, position.tokensOwed0, pool.balances.token0)
transferByRegisterCall(pool.token0Path, recipient, amount0.Uint64())
token0 := common.GetTokenTeller(pool.token0Path)
checkTransferError(token0.Transfer(recipient, amount0.Uint64()))

// Smallest of three: amount0Requested, position.tokensOwed0, pool.balances.token0
amount1Req := u256.MustFromDecimal(amount1Requested)
amount1, position.tokensOwed1, pool.balances.token1 = collectToken(amount1Req, position.tokensOwed1, pool.balances.token1)
transferByRegisterCall(pool.token1Path, recipient, amount1.Uint64())

// Update state first then transfer
position.tokensOwed1 = new(u256.Uint).Sub(position.tokensOwed1, amount1)
pool.balances.token1 = new(u256.Uint).Sub(pool.balances.token1, amount1)
token1 := common.GetTokenTeller(pool.token1Path)
checkTransferError(token1.Transfer(recipient, amount1.Uint64()))

pool.positions[positionKey] = position

Expand Down Expand Up @@ -258,9 +264,12 @@ func CollectProtocolByAdmin(
token1Path string,
fee uint32,
recipient std.Address,
amount0Requested string,
amount1Requested string,
) (string, string) {
amount0Requested string, // uint128
amount1Requested string, // uint128
) (string, string) { // uint128 x2
common.MustRegistered(token0Path)
common.MustRegistered(token1Path)

caller := std.PrevRealm().Addr()
if err := common.AdminOnly(caller); err != nil {
panic(err)
Expand Down Expand Up @@ -300,9 +309,12 @@ func CollectProtocol(
token1Path string,
fee uint32,
recipient std.Address,
amount0Requested string,
amount1Requested string,
) (string, string) {
amount0Requested string, // uint128
amount1Requested string, // uint128
) (string, string) { // uint128 x2
common.MustRegistered(token0Path)
common.MustRegistered(token1Path)

caller := std.PrevRealm().Addr()
if err := common.GovernanceOnly(caller); err != nil {
panic(err)
Expand Down Expand Up @@ -355,8 +367,11 @@ func collectProtocol(
uAmount0 := amount0.Uint64()
uAmount1 := amount1.Uint64()

transferByRegisterCall(pool.token0Path, recipient, uAmount0)
transferByRegisterCall(pool.token1Path, recipient, uAmount1)
token0Teller := common.GetTokenTeller(pool.token0Path)
checkTransferError(token0Teller.Transfer(recipient, uAmount0))

token1Teller := common.GetTokenTeller(pool.token1Path)
checkTransferError(token1Teller.Transfer(recipient, uAmount1))

return amount0.ToString(), amount1.ToString()
}
Expand Down Expand Up @@ -407,7 +422,8 @@ func (pool *Pool) transferAndVerify(
panic(err)
}

transferByRegisterCall(tokenPath, to, amountUint64)
token := common.GetTokenTeller(tokenPath)
checkTransferError(token.Transfer(to, amountUint64))

newBalance, err := updatePoolBalance(token0, token1, absAmount, isToken0)
if err != nil {
Expand Down Expand Up @@ -484,12 +500,8 @@ func (pool *Pool) transferFromAndVerify(
panic(err)
}

// try sending
// will panic if following conditions are met:
// - POOL does not have enough approved amount
// - from does not have enough balance
// - token is not registered
transferFromByRegisterCall(tokenPath, from, to, amountUint64)
token := common.GetTokenTeller(tokenPath)
checkTransferError(token.TransferFrom(from, to, amountUint64))

// update pool balances
if isToken0 {
Expand Down
7 changes: 5 additions & 2 deletions pool/pool_manager.gno
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ func CreatePool(
// wrap first
token0Path, token1Path = poolInfo.wrap()

poolPath := GetPoolPath(token0Path, token1Path, fee)

// reinitialize poolInfo with wrapped tokens
poolInfo = newPoolParams(token0Path, token1Path, fee, _sqrtPriceX96)

Expand All @@ -171,8 +173,6 @@ func CreatePool(
))
}

poolPath := GetPoolPath(token0Path, token1Path, fee)

// TODO: make this as a parameter
prevAddr, prevRealm := getPrev()

Expand Down Expand Up @@ -252,6 +252,9 @@ func GetPoolFromPoolPath(poolPath string) *Pool {
// GetPoolPath generates a poolPath from the given token paths and fee.
// The poolPath is constructed by joining the token paths and fee with colons.
func GetPoolPath(token0Path, token1Path string, fee uint32) string {
common.MustRegistered(token0Path)
common.MustRegistered(token1Path)

// TODO: this check is not unnecessary, if we are sure that
// all the token paths in the pool are sorted in alphabetical order.
if strings.Compare(token1Path, token0Path) < 0 {
Expand Down
Loading

0 comments on commit e11c132

Please sign in to comment.