Skip to content

Commit

Permalink
GSW-401 feat: spend 500 gns when creating new pool
Browse files Browse the repository at this point in the history
  • Loading branch information
r3v4s committed Nov 7, 2023
1 parent 3828e93 commit fadd1bf
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
var (
gsa = testutils.TestAddress("gsa")
lp01 = testutils.TestAddress("lp01")
pc01 = testutils.TestAddress("pc01")

poolAddr = std.DerivePkgAddr("gno.land/r/pool")
posAddr = std.DerivePkgAddr("gno.land/r/position")
Expand All @@ -33,6 +34,8 @@ var (
func init() {
std.TestSetOrigCaller(gsa)
InitManual()

std.TestSetOrigCaller(pc01)
CreatePool(fooPath, barPath, pFee, sqrtPrice)
}

Expand Down
82 changes: 82 additions & 0 deletions pool/_TEST_pool_creation_fee_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package pool

import (
"std"
"testing"

"gno.land/p/demo/testutils"

_ "gno.land/r/grc20_wrapper"

gns "gno.land/r/gns"
)

var (
pc01 = testutils.TestAddress("pc01") // pool creator
gsa = testutils.TestAddress("gsa")

poolAddr = std.DerivePkgAddr("gno.land/r/pool")
posAddr = std.DerivePkgAddr("gno.land/r/position")
)

var (
fooPath = "gno.land/r/foo" // token1
barPath = "gno.land/r/bar" // token2
bazPath = "gno.land/r/baz"
sqrtPrice bigint = 130621891405341611593710811006

tickLower = int32(9000)
tickUpper = int32(11000)
liquidityExpect = bigint(100_000_000)

currentTick = int32(10000)
)

func init() {
std.TestSetOrigCaller(gsa)
InitManual()
}

func TestCreatePool(t *testing.T) {
std.TestSetOrigCaller(pc01)

pc01Before := gns.BalanceOf(a2u(pc01))
gsaBefore := gns.BalanceOf(a2u(gsa))
shouldEQ(t, gsaBefore, 0)

CreatePool(fooPath, barPath, uint16(100), sqrtPrice)
CreatePool(fooPath, barPath, uint16(500), sqrtPrice)
CreatePool(fooPath, barPath, uint16(3000), sqrtPrice)
CreatePool(fooPath, barPath, uint16(10000), sqrtPrice)

pc01After := gns.BalanceOf(a2u(pc01))
gsaAfter := gns.BalanceOf(a2u(gsa))

shouldEQ(t, gsaAfter-gsaBefore, 2000)
shouldEQ(t, pc01Before-pc01After, 2000)
shouldEQ(t, len(pools), 4)
}

func TestCreatePoolNotEnoughGNS(t *testing.T) {
shouldPanicWithMsg(t, func() { CreatePool(fooPath, bazPath, uint16(100), sqrtPrice) }, "insufficient balance")
shouldEQ(t, len(pools), 4)
}

func shouldEQ(t *testing.T, got, expected interface{}) {
if got != expected {
t.Errorf("got %v, expected %v", got, expected)
}
}

func shouldPanicWithMsg(t *testing.T, f func(), msg string) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
} else {
if r != msg {
t.Errorf("excepted panic(%v), got(%v)", msg, r)
}
}
}()
f()
}
2 changes: 2 additions & 0 deletions pool/_TEST_pool_multi_lp_fee_api_test.gnoa
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
var (
gsa = testutils.TestAddress("gsa") // Gnoswap Admin
lp01 = testutils.TestAddress("lp01") // Liquidity Provider 01
pc01 = testutils.TestAddress("pc01") // Pool Creator 01
tr01 = testutils.TestAddress("tr01") // Trader 01

poolAddr = std.DerivePkgAddr("gno.land/r/pool")
Expand All @@ -41,6 +42,7 @@ func TestCreatePool(t *testing.T) {
jsonStr := gjson.Parse(ApiGetPools())
shouldEQ(t, len(jsonStr.Get("response.data").Array()), 0)

std.TestSetOrigCaller(pc01)
CreatePool(fooPath, barPath, pFee, 130622891405341611593710811006)
std.TestSkipHeights(1)
// 130621891405341611593710811006 // 9999
Expand Down
4 changes: 4 additions & 0 deletions pool/_TEST_pool_multi_lp_fee_test.gnoa
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ var (

tr01 = testutils.TestAddress("tr01") // Trader 01

pc01 = testutils.TestAddress("pc01") // Pool Creator 01

poolAddr = std.DerivePkgAddr("gno.land/r/pool")
posAddr = std.DerivePkgAddr("gno.land/r/position")
)
Expand All @@ -37,6 +39,8 @@ var (
func TestFactoryCreatePool(t *testing.T) {
std.TestSetOrigCaller(gsa)
InitManual()

std.TestSetOrigCaller(pc01)
CreatePool(fooPath, barPath, pFee, 130622891405341611593710811006)
// 130621891405341611593710811006 // 9999
// 130622891405341611593710811006 // 10000
Expand Down
4 changes: 4 additions & 0 deletions pool/_TEST_pool_multi_lp_test.gnoa
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ var (

tr01 = testutils.TestAddress("tr01") // Trader 01

pc01 = testutils.TestAddress("pc01") // Pool Creator 01

poolAddr = std.DerivePkgAddr("gno.land/r/pool")
posAddr = std.DerivePkgAddr("gno.land/r/position")
)
Expand All @@ -41,6 +43,8 @@ var (
func TestFactoryCreatePool(t *testing.T) {
std.TestSetOrigCaller(gsa)
InitManual()

std.TestSetOrigCaller(pc01)
CreatePool(fooPath, barPath, pFee, 130621891405341611593710811006)

// sqrtPrice
Expand Down
3 changes: 3 additions & 0 deletions pool/_TEST_pool_multi_token_test.gnoa
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var (
gsa = testutils.TestAddress("gsa") // Gnoswap Admin
lp01 = testutils.TestAddress("lp01") // Liquidity Provider 01
tr01 = testutils.TestAddress("tr01") // Trader 01
pc01 = testutils.TestAddress("pc01") // Pool Creator 01

poolAddr = std.DerivePkgAddr("gno.land/r/pool")
posAddr = std.DerivePkgAddr("gno.land/r/position")
Expand Down Expand Up @@ -42,12 +43,14 @@ func TestInit(t *testing.T) {

// 2. Create Foo:Bar Pool
func TestCreateFooBarPool(t *testing.T) {
std.TestSetOrigCaller(pc01)
CreatePool(fooPath, barPath, pFee, 130621891405341611593710811006)
shouldEQ(t, len(pools), 1)
}

// 3. Create Bar:Baz Pool
func TestCreateBarBazPool(t *testing.T) {
std.TestSetOrigCaller(pc01)
CreatePool(barPath, bazPath, pFee, 130621891405341611593710811006)
shouldEQ(t, len(pools), 2)
}
Expand Down
8 changes: 6 additions & 2 deletions pool/_TEST_pool_same_token_pair_diff_fee_test.gnoa
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var (
gsa = testutils.TestAddress("gsa") // Gnoswap Admin
lp01 = testutils.TestAddress("lp01") // Liquidity Provider 01
tr01 = testutils.TestAddress("tr01") // Trader 01
pc01 = testutils.TestAddress("pc01") // Pool Creator 01

poolAddr = std.DerivePkgAddr("gno.land/r/pool")
posAddr = std.DerivePkgAddr("gno.land/r/position")
Expand All @@ -38,6 +39,8 @@ var (
func TestFirstPool(t *testing.T) {
std.TestSetOrigCaller(gsa)
InitManual()

std.TestSetOrigCaller(pc01)
CreatePool(fooPath, barPath, pFee1, 130621891405341611593710811006)

shouldPanic(t, func() { CreatePool(fooPath, barPath, pFee1, 130621891405341611593710811006) })
Expand Down Expand Up @@ -203,8 +206,9 @@ func TestFirstPool(t *testing.T) {

// foo & bar & 3000
func TestSecondPool(t *testing.T) {
std.TestSetOrigCaller(gsa)
// Init()
// std.TestSetOrigCaller(gsa)
// InitManual()
std.TestSetOrigCaller(pc01)
CreatePool(fooPath, barPath, pFee2, 130621891405341611593710811006)

shouldPanic(t, func() { CreatePool(fooPath, barPath, pFee2, 130621891405341611593710811006) })
Expand Down
3 changes: 3 additions & 0 deletions pool/_TEST_pool_single_lp_test.gnoa
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var (
gsa = testutils.TestAddress("gsa") // Gnoswap Admin
lp01 = testutils.TestAddress("lp01") // Liquidity Provider 01
tr01 = testutils.TestAddress("tr01") // Trader 01
pc01 = testutils.TestAddress("pc01") // Pool Creator 01

poolAddr = std.DerivePkgAddr("gno.land/r/pool")
posAddr = std.DerivePkgAddr("gno.land/r/position")
Expand All @@ -37,6 +38,8 @@ var (
func TestInitCreatePool(t *testing.T) {
std.TestSetOrigCaller(gsa)
InitManual()

std.TestSetOrigCaller(pc01)
CreatePool(fooPath, barPath, pFee, 130621891405341611593710811006)

// sqrtPrice
Expand Down
7 changes: 7 additions & 0 deletions pool/pool_manager.gno
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strconv"

"gno.land/p/demo/ufmt"

gns "gno.land/r/gns"
)

var (
Expand Down Expand Up @@ -56,6 +58,11 @@ func CreatePool(
require(!exist, ufmt.Sprintf("[POOl] pool_manager.gno__CreatePool() || pool(%s) already exist", poolKey))

if !exist {
// 500 GNS as creation fee
// recipent is same address that receives protocol fee
// r3v4_xxx: change address when publish
gns.TransferFrom(a2u(std.GetOrigCaller()), a2u(std.Address("g1vaekzh6lta047h6lta047h6lta047h6lutmjdk")), 500)

pool = newPool(token0Path, token1Path, fee, tickSpacing, sqrtPriceX96)
pools[poolKey] = pool
}
Expand Down
4 changes: 2 additions & 2 deletions pool/pool_register.gno
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func removeGRC20Interface(pkgPath string) {

func RegisterGRC20Interface(pkgPath string, igrc20 GRC20Interface) {
// only admin can register
// r3v4_xx: below logic can't be used in test case
// r3v4_xx: however must be used in production
// r3v4_xxx: below logic can't be used in test case
// r3v4_xxx: however must be used in production

// caller := std.GetOrigCaller()
// if caller != APPROVED_CALLER {
Expand Down

0 comments on commit fadd1bf

Please sign in to comment.