Skip to content

Commit

Permalink
GSW-609 feat: change ugnot handling
Browse files Browse the repository at this point in the history
  • Loading branch information
r3v4s committed Dec 4, 2023
1 parent 2633134 commit 7fd98ad
Show file tree
Hide file tree
Showing 23 changed files with 661 additions and 49 deletions.
2 changes: 1 addition & 1 deletion _setup/grc20_wrapper/grc20_wrapper.gno
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func init() {
rRegistry.RegisterGRC20Interface("gno.land/r/gns", GnsTokenInterface{})

// staker register
// sRegistry.RegisterGRC20Interface("gno.land/r/wugnot", WugnotTokenInterface{})
sRegistry.RegisterGRC20Interface("gno.land/r/wugnot", WugnotTokenInterface{})
sRegistry.RegisterGRC20Interface("gno.land/r/obl", OblTokenInterface{})
sRegistry.RegisterGRC20Interface("gno.land/r/gns", GnsTokenInterface{})
}
19 changes: 11 additions & 8 deletions _setup/wugnot/wugnot.gno
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var (
poolAddr = std.DerivePkgAddr("gno.land/r/pool")
posAddr = std.DerivePkgAddr("gno.land/r/position")
routerAddr = std.DerivePkgAddr("gno.land/r/router")
stakerAddr = std.DerivePkgAddr("gno.land/r/staker")
)

func init() {
Expand All @@ -38,13 +39,15 @@ func init() {
lp02 = testutils.TestAddress("lp02") // Liquidity Provider 02
lp03 = testutils.TestAddress("lp03") // Liquidity Provider 03
tr01 = testutils.TestAddress("tr01") // Trader 01
ci01 = testutils.TestAddress("ci01") // Create Incentive Caller
)

wugnot.Approve(lp01, poolAddr, 50000000000)
wugnot.Approve(lp02, poolAddr, 50000000000)
wugnot.Approve(lp03, poolAddr, 50000000000)
wugnot.Approve(tr01, poolAddr, 50000000000)

wugnot.Approve(ci01, stakerAddr, 50000000000)
}

// method proxies as public functions.
Expand Down Expand Up @@ -135,26 +138,26 @@ func assertIsAdmin(address std.Address) {
// WRAP & UNWRAP
func Wrap(address users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
if !(caller == poolAddr || caller == posAddr || caller == routerAddr) {
panic("only pool, position, router contract can wrap")
if !(caller == poolAddr || caller == posAddr || caller == routerAddr || caller == stakerAddr) {
panic("only pool, position, router, staker contract can wrap")
}

wugnot.Mint(address.Resolve(), amount) // mint to user
}

func Unwrap(address users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
if !(caller == poolAddr || caller == posAddr || caller == routerAddr) {
panic("only pool, position, router contract can unwrap")
if !(caller == poolAddr || caller == posAddr || caller == routerAddr || caller == stakerAddr) {
panic("only pool, position, router, staker contract can unwrap")
}

wugnot.Burn(address.Resolve(), amount) // burn from user
}

func Transfer(to users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
if !(caller == poolAddr || caller == posAddr || caller == routerAddr) {
panic("only pool, position, router contract can transfer")
if !(caller == poolAddr || caller == posAddr || caller == routerAddr || caller == stakerAddr) {
panic("only pool, position, router, staker contract can transfer")
}
err := wugnot.Transfer(caller, to.Resolve(), amount)
if err != nil {
Expand All @@ -164,8 +167,8 @@ func Transfer(to users.AddressOrName, amount uint64) {

func TransferFrom(from, to users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
if !(caller == poolAddr || caller == posAddr || caller == routerAddr) {
panic("only pool, position, router contract can transferFrom")
if !(caller == poolAddr || caller == posAddr || caller == routerAddr || caller == stakerAddr) {
panic("only pool, position, router, staker contract can transferFrom")
}
err := wugnot.TransferFrom(caller, from.Resolve(), to.Resolve(), amount)
if err != nil {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ var (

var (
// Common
fooPath = "gno.land/r/foo"
wugnotPath = "gno.land/r/wugnot"
pFee = uint16(500)
fooPath = "gno.land/r/foo"
gnotPath = "gnot"
pFee = uint16(500)

test_tickLower = int32(9000)
test_tickUpper = int32(11000)
Expand All @@ -52,9 +52,9 @@ func TestPoolInitCreatePool(t *testing.T) {
InitManual()

std.TestSetOrigCaller(pc01)
CreatePool(fooPath, wugnotPath, pFee, 130621891405341611593710811006) // x2.7
CreatePool(fooPath, gnotPath, pFee, 130621891405341611593710811006) // x2.7

shouldPanic(t, func() { CreatePool(fooPath, wugnotPath, pFee, 130621891405341611593710811006) })
shouldPanic(t, func() { CreatePool(fooPath, gnotPath, pFee, 130621891405341611593710811006) })
}

// 2. Mint LP and Get GNFT
Expand All @@ -65,7 +65,7 @@ func TestMint(t *testing.T) {
testBanker := std.GetBanker(std.BankerTypeRealmIssue)
testBanker.IssueCoin(std.GetOrigCaller(), "ugnot", 1_000_000)

token0, token1 := fooPath, wugnotPath
token0, token1 := fooPath, gnotPath
if token1 < token0 {
token0, token1 = token1, token0
}
Expand All @@ -91,7 +91,7 @@ func TestMint(t *testing.T) {
std.TestSetPrevRealm("gno.land/r/position")
tTokenId, tLiquidity, tAmount0, tAmount1 := pos.Mint(
fooPath,
wugnotPath,
gnotPath,
pFee,
test_tickLower,
test_tickUpper,
Expand All @@ -118,7 +118,7 @@ func TestMint(t *testing.T) {
}

func TestSwapBuyNative(t *testing.T) {
pool := GetPool(fooPath, wugnotPath, pFee)
pool := GetPool(fooPath, gnotPath, pFee)

tr01OldT0Bal := balanceOfByRegisterCall(pool.token0Path, tr01)
tr01OldT1Bal := balanceOfByRegisterCall(pool.token1Path, tr01)
Expand All @@ -133,8 +133,8 @@ func TestSwapBuyNative(t *testing.T) {
std.TestSetPrevRealm("gno.land/r/router")
std.TestSetOrigCaller(tr01)
amount0, amount1 := Swap( // foo 10_000 > wugnot ??
fooPath, // token0
wugnotPath, // token1
fooPath, // token0
gnotPath, // token1
pFee,
tr01,
true,
Expand Down Expand Up @@ -162,7 +162,7 @@ func TestSwapBuyNative(t *testing.T) {
}

func TestSwapSellNative(t *testing.T) {
pool := GetPool(fooPath, wugnotPath, pFee)
pool := GetPool(fooPath, gnotPath, pFee)

poolOldT0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr)
poolOldT1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr)
Expand All @@ -183,8 +183,8 @@ func TestSwapSellNative(t *testing.T) {

std.TestSetPrevRealm("gno.land/r/router")
amount0, amount1 := Swap( // ugnot 10_000 > foo ??
fooPath, // token0
wugnotPath, // token1
fooPath, // token0
gnotPath, // token1
pFee,
tr01,
false,
Expand Down
6 changes: 6 additions & 0 deletions pool/consts.gno
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,9 @@ const (
ADDR_ROUTER std.Address = std.DerivePkgAddr("gno.land/r/router")
ADDR_POOL std.Address = std.DerivePkgAddr("gno.land/r/pool")
)

// WRAP & UNWRAP
const (
GNOT = "gnot"
WRAPPED_WUGNOT = "gno.land/r/wugnot"
)
26 changes: 26 additions & 0 deletions pool/pool_register.gno
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type GRC20Pair struct {
}

func findGRC20(pkgPath string) (int, bool) {
pkgPath = handleNative(pkgPath)

for i, pair := range registered {
if pair.pkgPath == pkgPath {
return i, true
Expand All @@ -33,10 +35,14 @@ func findGRC20(pkgPath string) (int, bool) {
}

func appendGRC20Interface(pkgPath string, igrc20 GRC20Interface) {
pkgPath = handleNative(pkgPath)

registered = append(registered, GRC20Pair{pkgPath: pkgPath, igrc20: igrc20})
}

func removeGRC20Interface(pkgPath string) {
pkgPath = handleNative(pkgPath)

i, found := findGRC20(pkgPath)
if !found {
return
Expand All @@ -55,13 +61,17 @@ func RegisterGRC20Interface(pkgPath string, igrc20 GRC20Interface) {
// panic("unauthorized address to register")
// }

pkgPath = handleNative(pkgPath)

_, found := findGRC20(pkgPath)
if !found {
appendGRC20Interface(pkgPath, igrc20)
}
}

func UnregisterGRC20Interface(pkgPath string) {
pkgPath = handleNative(pkgPath)

// do not allow realm to unregister
std.AssertOriginCall()

Expand All @@ -78,6 +88,8 @@ func UnregisterGRC20Interface(pkgPath string) {
}

func transferByRegisterCall(pkgPath string, to std.Address, amount uint64) bool {
pkgPath = handleNative(pkgPath)

i, found := findGRC20(pkgPath)
if !found {
return false
Expand All @@ -89,6 +101,8 @@ func transferByRegisterCall(pkgPath string, to std.Address, amount uint64) bool
}

func transferFromByRegisterCall(pkgPath string, from, to std.Address, amount uint64) bool {
pkgPath = handleNative(pkgPath)

i, found := findGRC20(pkgPath)
if !found {
return false
Expand All @@ -100,6 +114,8 @@ func transferFromByRegisterCall(pkgPath string, from, to std.Address, amount uin
}

func balanceOfByRegisterCall(pkgPath string, owner std.Address) uint64 {
pkgPath = handleNative(pkgPath)

i, found := findGRC20(pkgPath)
if !found {
return 0
Expand All @@ -110,6 +126,8 @@ func balanceOfByRegisterCall(pkgPath string, owner std.Address) uint64 {
}

func approveByRegisterCall(pkgPath string, spender std.Address, amount uint64) bool {
pkgPath = handleNative(pkgPath)

i, found := findGRC20(pkgPath)
if !found {
return false
Expand All @@ -119,3 +137,11 @@ func approveByRegisterCall(pkgPath string, spender std.Address, amount uint64) b

return true
}

func handleNative(pkgPath string) string {
if pkgPath == GNOT {
return WRAPPED_WUGNOT
}

return pkgPath
}
4 changes: 2 additions & 2 deletions pool/wrap_unwrap.gno
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func wrap(tokenPath string) {
if tokenPath != "gno.land/r/wugnot" {
if tokenPath != GNOT {
return
}

Expand All @@ -29,7 +29,7 @@ func wrap(tokenPath string) {
}

func unWrap(tokenPath string, amount uint64) {
if tokenPath != "gno.land/r/wugnot" {
if tokenPath != GNOT {
return
}

Expand Down
4 changes: 0 additions & 4 deletions position/_TEST_position_api_test.gnoa
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ func TestApiGetPosition(t *testing.T) {
shouldEQ(t, jsonStr.Get("response.data.token1_balance").Int(), 323523)
shouldEQ(t, jsonStr.Get("response.data.tokens_owed_0").Int(), 61)
shouldEQ(t, jsonStr.Get("response.data.tokens_owed_1").Int(), 0)
shouldEQ(t, jsonStr.Get("response.data.fee_growth_inside_0_last_x128").String(), "1694567149905485370521073160520307")
shouldEQ(t, jsonStr.Get("response.data.fee_growth_inside_1_last_x128").String(), "0")
}

func TestApiGetPositionByUser(t *testing.T) {
Expand All @@ -138,8 +136,6 @@ func TestApiGetPositionByUser(t *testing.T) {
shouldEQ(t, jsonStr.Get("response.data.1.token1_balance").Int(), 0)
shouldEQ(t, jsonStr.Get("response.data.1.tokens_owed_0").Int(), 0)
shouldEQ(t, jsonStr.Get("response.data.1.tokens_owed_1").Int(), 0)
shouldEQ(t, jsonStr.Get("response.data.1.fee_growth_inside_0_last_x128").String(), "0")
shouldEQ(t, jsonStr.Get("response.data.1.fee_growth_inside_1_last_x128").String(), "0")
}

/* HELPER */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ var (

var (
// Common
fooPath = "gno.land/r/foo"
wugnotPath = "gno.land/r/wugnot"
pFee = uint16(500)
fooPath = "gno.land/r/foo"
gnotPath = "gnot"
pFee = uint16(500)

test_tickLower = int32(9000)
test_tickUpper = int32(11000)
Expand All @@ -53,9 +53,9 @@ func TestPoolInitCreatePool(t *testing.T) {
p.InitManual()

std.TestSetOrigCaller(pc01)
p.CreatePool(fooPath, wugnotPath, pFee, 130621891405341611593710811006)
p.CreatePool(fooPath, gnotPath, pFee, 130621891405341611593710811006) // tick 10_000 ≈ ratio x2.718145

shouldPanic(t, func() { p.CreatePool(fooPath, wugnotPath, 500, 130621891405341611593710811006) })
shouldPanic(t, func() { p.CreatePool(fooPath, gnotPath, 500, 130621891405341611593710811006) })
}

// 2. Mint LP and Get GNFT
Expand All @@ -67,7 +67,7 @@ func TestMint(t *testing.T) {
testBanker := std.GetBanker(std.BankerTypeRealmIssue)
testBanker.IssueCoin(std.GetOrigCaller(), "ugnot", 1000)

token0, token1 := fooPath, wugnotPath
token0, token1 := fooPath, gnotPath
if token1 < token0 {
token0, token1 = token1, token0
}
Expand All @@ -92,7 +92,7 @@ func TestMint(t *testing.T) {
// Mint
tTokenId, tLiquidity, tAmount0, tAmount1 := Mint(
fooPath,
wugnotPath,
gnotPath,
pFee,
test_tickLower,
test_tickUpper,
Expand All @@ -109,7 +109,7 @@ func TestMint(t *testing.T) {
// 1000 => 1 wugnot // after wrap, sent 999 wugnot to pool ( little error range due to decimals )
coins := testBanker.GetCoins(lp01)
shouldEQ(t, len(coins), 0)
shouldEQ(t, Token1Bal(lp01), bigint(1))
shouldEQ(t, Token1Bal(lp01), bigint(1)) // send 999, 1 left
}

shouldEQ(t, tAmount0, bigint(367))
Expand All @@ -126,7 +126,7 @@ func TestMint(t *testing.T) {
func TestDecreaseLiquidity(t *testing.T) {
// lp01 decreases liquidity at tid 1 position ( in range )
std.TestSetOrigCaller(lp01)
pool := p.GetPool(fooPath, wugnotPath, pFee)
pool := p.GetPool(fooPath, gnotPath, pFee)

tTargetLiquidity := bigint(1234)

Expand Down Expand Up @@ -157,9 +157,9 @@ func TestCollect(t *testing.T) {
// lp01 did decrease some liquidity => there are some to collect
{
std.TestSetOrigCaller(lp01)
pool := p.GetPool(fooPath, wugnotPath, pFee)
pool := p.GetPool(fooPath, gnotPath, pFee)

poolOldLiquidity := pool.PoolGetLiquidity()
poolOldLiquidity := pool.PoolGetLiquidity() // == position[1] liquidity
poolOldToken0Bal := Token0Bal(poolAddr)
poolOldToken1Bal := Token1Bal(poolAddr)

Expand Down Expand Up @@ -189,7 +189,7 @@ func TestCollect(t *testing.T) {
// lp01 collect all
{
std.TestSetOrigCaller(lp01)
pool := p.GetPool(fooPath, wugnotPath, pFee)
pool := p.GetPool(fooPath, gnotPath, pFee)

poolOldLiquidity := pool.PoolGetLiquidity()
poolOldToken0Bal := Token0Bal(poolAddr)
Expand Down Expand Up @@ -219,11 +219,11 @@ func TestCollect(t *testing.T) {
{
testBanker := std.GetBanker(std.BankerTypeRealmIssue)
// after collects all
// 0 ugnot => 1099
// 0 ugnot => 99 // didn't burn all liquidity
// 1 wugnot // stay same
coins := testBanker.GetCoins(lp01)
coin := coins[0]
shouldEQ(t, coin, std.Coin{Amount: 198, Denom: "ugnot"})
shouldEQ(t, coin, std.Coin{Amount: 99, Denom: "ugnot"})
shouldEQ(t, Token1Bal(lp01), bigint(1))
}
}
Expand Down
File renamed without changes.
Loading

0 comments on commit 7fd98ad

Please sign in to comment.