Skip to content

Commit

Permalink
refactor: ♻️ bebop: fix header & add entity test (#538)
Browse files Browse the repository at this point in the history
* refactor: ♻️ bebop: fix header & add entity test

Signed-off-by: thanhpp <[email protected]>

* refactor: ♻️ bebop: fix source

Signed-off-by: thanhpp <[email protected]>

---------

Signed-off-by: thanhpp <[email protected]>
  • Loading branch information
thanhpp authored Oct 11, 2024
1 parent 61f8aa1 commit 057a493
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 23 deletions.
10 changes: 5 additions & 5 deletions pkg/liquidity-source/bebop/client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
)

const (
headerNameKey = "name"
headerAuthKey = "Authorization"
querySourceKey = "source"
headerSourceAuthKey = "source-auth"

pathQuote = "v3/quote"

Expand Down Expand Up @@ -48,8 +48,7 @@ func NewHTTPClient(config *bebop.HTTPClientConfig) *HTTPClient {
SetBaseURL(config.BaseURL).
SetTimeout(config.Timeout.Duration).
SetRetryCount(config.RetryCount).
SetHeader(headerNameKey, config.Name).
SetAuthToken(config.Authorization)
SetHeader(headerSourceAuthKey, config.Authorization)

return &HTTPClient{
config: config,
Expand All @@ -70,7 +69,8 @@ func (c *HTTPClient) QuoteSingleOrderResult(ctx context.Context, params bebop.Qu
SetQueryParam(bebop.ParamsReceiverAddress, params.ReceiverAddress).
SetQueryParam(bebop.ParamsApproveType, "Standard").
SetQueryParam(bebop.ParamsSkipValidation, "true"). // not checking balance
SetQueryParam(bebop.ParamsGasLess, "false") // self-execution
SetQueryParam(bebop.ParamsGasLess, "false"). // self-execution
SetQueryParam(querySourceKey, c.config.Name)

var result bebop.QuoteSingleOrderResult
var fail bebop.QuoteFail
Expand Down
86 changes: 68 additions & 18 deletions pkg/liquidity-source/bebop/pool_simulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,38 @@ import (
"github.com/stretchr/testify/assert"
)

var entityPool = entity.Pool{
Address: "bebop_0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270_0xc2132d05d31c914a87c6611c10748aeb04b58e8f",
Exchange: "bebop",
Type: "bebop",
Reserves: []string{"9320038994403940352", "166143156993"},
Tokens: []*entity.PoolToken{
{Address: "0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270", Decimals: 18, Swappable: true},
{Address: "0xc2132d05d31c914a87c6611c10748aeb04b58e8f", Decimals: 6, Swappable: true},
},
Extra: "{\"0to1\":[{\"q\":0.0001,\"p\":0.91245042136692},{\"q\":4.659919497201971,\"p\":0.91245042136692}," +
"{\"q\":4.66001949720197,\"p\":0.91245042136692}]," +
"\"1to0\":[{\"q\":0.0001,\"p\":1.0942398729806944},{\"q\":18277.528075741084,\"p\":1.0942398729806944}," +
"{\"q\":25244.263002363805,\"p\":1.0939119116852096},{\"q\":32092.9359692824,\"p\":1.0937921053280593}," +
"{\"q\":33219.273417201824,\"p\":1.0936723252106664},{\"q\":29917.17166407224,\"p\":1.0935525713244107}," +
"{\"q\":27391.98476499627,\"p\":1.093432843660677}],\"tlrnce\":0}",
}
var (
entityPool1 = entity.Pool{
Address: "bebop_0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270_0xc2132d05d31c914a87c6611c10748aeb04b58e8f",
Exchange: "bebop",
Type: "bebop",
Reserves: []string{"9320038994403940352", "166143156993"},
Tokens: []*entity.PoolToken{
{Address: "0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270", Decimals: 18, Swappable: true},
{Address: "0xc2132d05d31c914a87c6611c10748aeb04b58e8f", Decimals: 6, Swappable: true},
},
Extra: "{\"0to1\":[{\"q\":0.0001,\"p\":0.91245042136692},{\"q\":4.659919497201971,\"p\":0.91245042136692}," +
"{\"q\":4.66001949720197,\"p\":0.91245042136692}]," +
"\"1to0\":[{\"q\":0.0001,\"p\":1.0942398729806944},{\"q\":18277.528075741084,\"p\":1.0942398729806944}," +
"{\"q\":25244.263002363805,\"p\":1.0939119116852096},{\"q\":32092.9359692824,\"p\":1.0937921053280593}," +
"{\"q\":33219.273417201824,\"p\":1.0936723252106664},{\"q\":29917.17166407224,\"p\":1.0935525713244107}," +
"{\"q\":27391.98476499627,\"p\":1.093432843660677}],\"tlrnce\":0}",
}
entityPool2 = entity.Pool{
Address: "bebop_0x7a58c0be72be218b41c608b7fe7c5bb630736c71_0xdac17f958d2ee523a2206206994597c13d831ec7",
Exchange: "bebop",
Type: "bebop",
Reserves: []string{"1740906659949395367165952", "0"},
Tokens: []*entity.PoolToken{
{Address: "0x7a58c0be72be218b41c608b7fe7c5bb630736c71", Decimals: 18, Swappable: true},
{Address: "0xdac17f958d2ee523a2206206994597c13d831ec7", Decimals: 6, Swappable: true},
},
Extra: "{\"0to1\":[{\"p\":0.07595710380734046,\"q\":48163.55734890566},{\"p\":0.07593455722483192,\"q\":52993.32482126679},{\"p\":0.07591500784305961,\"q\":58302.68056018904},{\"p\":0.0758988654988538,\"q\":64137.646452324116},{\"p\":0.07587521441482821,\"q\":70558.7131658853},{\"p\":0.07585281405928816,\"q\":77614.58448247385},{\"p\":0.07582033645336315,\"q\":16049.424434351078},{\"p\":0.07580864440234752,\"q\":85391.1771552143},{\"p\":0.07575182579202616,\"q\":93949.7020470411},{\"p\":0.0756894119059153,\"q\":103355.92190388206},{\"p\":0.07560737490511166,\"q\":113709.06592087087},{\"p\":0.0755078631348937,\"q\":125095.00342213444},{\"p\":0.07539040528554969,\"q\":137608.56064368493},{\"p\":0.07523516462137005,\"q\":151388.32489547844},{\"p\":0.07504191048482589,\"q\":166548.79293229152},{\"p\":0.07480759629995394,\"q\":183214.78020295245},{\"p\":0.07450720327739284,\"q\":192825.3995604493}],\"1to0\":[]}",
}
)

func TestPoolSimulator_NewPool(t *testing.T) {
poolSimulator, err := NewPoolSimulator(entityPool)
poolSimulator, err := NewPoolSimulator(entityPool1)
assert.NoError(t, err)
assert.Equal(t, "0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270", poolSimulator.Token0.Address)
assert.Equal(t, "0xc2132d05d31c914a87c6611c10748aeb04b58e8f", poolSimulator.Token1.Address)
Expand All @@ -44,7 +57,7 @@ func TestPoolSimulator_NewPool(t *testing.T) {
}

func TestPoolSimulator_GetAmountOut(t *testing.T) {
poolSimulator, err := NewPoolSimulator(entityPool)
poolSimulator, err := NewPoolSimulator(entityPool1)
assert.NoError(t, err)

tests := []struct {
Expand Down Expand Up @@ -90,6 +103,43 @@ func TestPoolSimulator_GetAmountOut(t *testing.T) {
}
}

func TestPoolSimulator_GetAmountOut2(t *testing.T) {
poolSimulator, err := NewPoolSimulator(entityPool2)
assert.NoError(t, err)

tests := []struct {
name string
amountIn *big.Int
expectedAmountOut *big.Int
expectedErr error
}{
{
name: "it should return correct amountOut when swap in levels",
amountIn: bigIntFromString("100000000000000000000"),
expectedAmountOut: bigIntFromString("7595710"),
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Swap one to zero
params := pool.CalcAmountOutParams{
TokenAmountIn: pool.TokenAmount{
Token: "0x7a58c0be72be218b41c608b7fe7c5bb630736c71",
Amount: tc.amountIn,
},
TokenOut: "0xdac17f958d2ee523a2206206994597c13d831ec7",
}

result, err := poolSimulator.CalcAmountOut(params)
assert.Equal(t, tc.expectedErr, err)
if tc.expectedErr == nil {
assert.Equal(t, tc.expectedAmountOut, result.TokenAmountOut.Amount)
}
})
}
}

func bigIntFromString(s string) *big.Int {
value, _ := new(big.Int).SetString(s, 10)
return value
Expand Down

0 comments on commit 057a493

Please sign in to comment.