Skip to content

Commit

Permalink
Merge pull request #82 from gnoswap-labs/GSW-401-spend-gns-when-creat…
Browse files Browse the repository at this point in the history
…ing-new-pool

GSW-401 feat: spend 500 gns when creating new pool
  • Loading branch information
r3v4s authored Nov 8, 2023
2 parents 030936c + fadd1bf commit 7a87614
Show file tree
Hide file tree
Showing 17 changed files with 436 additions and 320 deletions.
7 changes: 7 additions & 0 deletions gov/const.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package gov

import "std"

const (
GNS_TOKEN_ADDRESS = std.DerivePkgAddr("gno.land/r/gns")
)
10 changes: 5 additions & 5 deletions gov/helper.gno → gov/gns_helper.gno
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ package gov
import (
"std"

gnos "gno.land/r/gnos"
gns "gno.land/r/gns"

"gno.land/r/demo/users"
)

func balanceOf(address std.Address) uint64 {
return gnos.BalanceOf(users.AddressOrName(address))
return gns.BalanceOf(users.AddressOrName(address))
}

func transfer(to std.Address, amount uint64) {
gnos.Transfer(users.AddressOrName(to), amount)
gns.Transfer(users.AddressOrName(to), amount)
}

func mint(recipient std.Address, amount uint64) {
gnos.Mint(users.AddressOrName(recipient), amount)
}
gns.Mint(users.AddressOrName(recipient), amount)
}
20 changes: 7 additions & 13 deletions gov/proposal_community_pool_spend.gno
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package gov
package gov

import (
"errors"
"std"

gnos "gno.land/r/gnos" // GNOS, Gnoswap Share
)

//////////////////////////
Expand All @@ -24,25 +23,20 @@ func (p ProposalCommunityPoolSpend) ProposalType() ProposalType {
func (p ProposalCommunityPoolSpend) isProposalData() {}

func (p ProposalCommunityPoolSpend) isValid() bool {
/*
if p.Token != GNOS_TOKEN_ADDRESS {
if p.Token != GNS_TOKEN_ADDRESS {
return false
}
*/

}

func (p ProposalCommunityPoolSpend) execute() error {
// XXXXXXXX
// commented out, replace `GNOS_TOKEN_ADDRESS` with the address of the GNOS token contract
/*
if p.Token != GNOS_TOKEN_ADDRESS {
return errors.New("unimplemented: only GNOS token is supported")
if p.Token != GNS_TOKEN_ADDRESS {
return errors.New("unimplemented: only GNS token is supported")
}
*/

// TODO: if transfer failes, return error
// TODO: we might need to whitelist set of tokens that can be transferred from the community pool, as some might panic on transfer
transfer(p.Recipient, p.Amount)

return nil
}
}
17 changes: 7 additions & 10 deletions gov/proposal_mint.gno
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package gov
package gov

import (
"std"

"errors"

gnos "gno.land/r/gnos" // GNOS, Gnoswap Share
)

//////////////////////////
Expand All @@ -15,8 +13,8 @@ var _ ProposalData = (*ProposalMint)(nil)

type ProposalMint struct {
Recipient std.Address
Amount uint64
Token std.Address
Amount uint64
Token std.Address
}

func (p ProposalMint) ProposalType() ProposalType {
Expand All @@ -26,12 +24,11 @@ func (p ProposalMint) ProposalType() ProposalType {
func (p ProposalMint) isProposalData() {}

func (p ProposalMint) isValid() bool {
// XXXXXX
/*
if p.Token != GNOS_TOKEN_ADDRESS {

if p.Token != GNS_TOKEN_ADDRESS {
return false
}
*/

return true
}

Expand All @@ -43,4 +40,4 @@ func (p ProposalMint) execute() error {
mint(p.Recipient, p.Amount)

return nil
}
}
21 changes: 13 additions & 8 deletions pool/_TEST_math_logic_test.gnoa
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 All @@ -51,17 +54,19 @@ func TestDrySwap_ZeroForOneTrue_AmountSpecified_Positive_16000(t *testing.T) {
std.TestSetOrigCaller(lp01)

// no mint == no liquidity => swap will fail
shouldPanic(t, func() { DrySwap(fooPath, barPath, pFee, "_", true, 16000, MIN_PRICE) })
_, _, ok := DrySwap(fooPath, barPath, pFee, "_", true, 16000, MIN_PRICE)
shouldEQ(t, ok, false)

// not enough mint == swap will fail
Mint(fooPath, barPath, pFee, posAddr, tickLower, tickUpper, 1000)
shouldPanic(t, func() { DrySwap(fooPath, barPath, pFee, "_", true, 16000, MIN_PRICE) })
_, _, ok = DrySwap(fooPath, barPath, pFee, "_", true, 16000, MIN_PRICE)
shouldEQ(t, ok, false)

Mint(fooPath, barPath, pFee, posAddr, tickLower, tickUpper, liquidityExpect)

// zeroForOne true
// amountSpecified 16000
input, output := DrySwap(
input, output, _ := DrySwap(
fooPath, // fooPath
barPath, // barPath
pFee, // pFee
Expand All @@ -78,7 +83,7 @@ func TestDrySwap_ZeroForOneTrue_AmountSpecified_Negative_16000(t *testing.T) {
// zeroForOne true
// amountSpecified -16000

input, output := DrySwap(
input, output, _ := DrySwap(
fooPath, // fooPath
barPath, // barPath
pFee, // pFee
Expand All @@ -89,14 +94,14 @@ func TestDrySwap_ZeroForOneTrue_AmountSpecified_Negative_16000(t *testing.T) {
)

shouldEQ(t, input, bigint(5888))
shouldEQ(t, output, bigint(-15999))
shouldEQ(t, output, bigint(-16000))
}

func TestDrySwap_ZeroForOneFalse_AmountSpecified_Positive_16000(t *testing.T) {
// zeroForOne false
// amountSpecified 16000

input, output := DrySwap(
input, output, _ := DrySwap(
fooPath, // fooPath
barPath, // barPath
pFee, // pFee
Expand All @@ -113,7 +118,7 @@ func TestDrySwap_ZeroForOneFalse_AmountSpecified_Negative_16000(t *testing.T) {
// zeroForOne false
// amountSpecified -16000

input, output := DrySwap(
input, output, _ := DrySwap(
fooPath, // fooPath
barPath, // barPath
pFee, // pFee
Expand All @@ -122,7 +127,7 @@ func TestDrySwap_ZeroForOneFalse_AmountSpecified_Negative_16000(t *testing.T) {
-16000, // amountSpecified
MAX_SQRT_RATIO-1, // sqrtPriceLimitX96
)
shouldEQ(t, input, bigint(-15999))
shouldEQ(t, input, bigint(-16000))
shouldEQ(t, output, bigint(5888))
}

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()
}
14 changes: 8 additions & 6 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 Expand Up @@ -83,22 +85,22 @@ func TestSwap(t *testing.T) {
shouldEQ(t, jsonStr.Get("response.data.liquidity").Int(), 100000000)
shouldEQ(t, len(jsonStr.Get("response.data.positions").Array()), 1)

test_price_01 := bigint(MIN_SQRT_RATIO + 1) // maximum price // swap 0 -> 1
test_price_10 := bigint(MAX_SQRT_RATIO - 1) // minimum price // swap 1 -> 0

// SWAP
std.TestSetOrigCaller(tr01)

Swap(fooPath, barPath, pFee, tr01, true, bigint(150000), test_price_01)
Swap(fooPath, barPath, pFee, tr01, true, bigint(150000), MIN_PRICE, std.GetOrigCaller())
std.TestSkipHeights(1)

// Swap(fooPath, barPath, pFee, tr01, true, bigint(1500000), MIN_PRICE, std.GetOrigCaller()) // two iteration // s0: 1_500_000 // s1: -3_626_984 // currentTick: 7668
jsonStr = gjson.Parse(ApiGetPool("gno.land/r/bar:gno.land/r/foo:500"))
shouldEQ(t, jsonStr.Get("response.data.token0_balance").Int(), 3107550)
shouldEQ(t, jsonStr.Get("response.data.token1_balance").Int(), 7635058)
shouldEQ(t, jsonStr.Get("response.data.liquidity").Int(), 100000000)
shouldEQ(t, len(jsonStr.Get("response.data.positions").Array()), 1)

Swap(fooPath, barPath, pFee, tr01, false, bigint(601851), test_price_10)
// Swap(fooPath, barPath, pFee, tr01, true, bigint(1500000), test_price_01) // two iteration // s0: 1_500_000 // s1: -3_626_984 // currentTick: 7668
Swap(fooPath, barPath, pFee, tr01, false, bigint(601851), MAX_PRICE, std.GetOrigCaller())
std.TestSkipHeights(1)

jsonStr = gjson.Parse(ApiGetPool("gno.land/r/bar:gno.land/r/foo:500"))
shouldEQ(t, jsonStr.Get("response.data.token0_balance").Int(), 1496418)
shouldEQ(t, jsonStr.Get("response.data.token1_balance").Int(), 8236909)
Expand Down
Loading

0 comments on commit 7a87614

Please sign in to comment.