Skip to content

Commit

Permalink
handle amount scaling, save token original to swapinfo for calling fi…
Browse files Browse the repository at this point in the history
…rm qupte
  • Loading branch information
tien7668 committed Oct 27, 2024
1 parent 49c473c commit 05b3bef
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
15 changes: 13 additions & 2 deletions pkg/liquidity-source/dexalot/client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"context"
"encoding/json"
"errors"
"math/big"

"github.com/KyberNetwork/aevm/common"

Check failure on line 9 in pkg/liquidity-source/dexalot/client/http.go

View workflow job for this annotation

GitHub Actions / ci / test

no required module provides package github.com/KyberNetwork/aevm/common; to add it:
dexalot "github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/dexalot"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/bignumber"
"github.com/KyberNetwork/logger"
"github.com/ethereum/go-ethereum/common"
"github.com/go-resty/resty/v2"
)

Expand Down Expand Up @@ -40,14 +42,23 @@ func NewHTTPClient(config *dexalot.HTTPClientConfig) *HTTPClient {

func (c *HTTPClient) Quote(ctx context.Context, params dexalot.FirmQuoteParams) (dexalot.FirmQuoteResult, error) {
// token address case-sensitive
upscaledTakerAmount := bignumber.NewBig(params.TakerAmount)
upscaledTakerAmount.Mul(
upscaledTakerAmount,
big.NewInt(12),
).Div(
upscaledTakerAmount,
big.NewInt(10),
)
upscaledTakerAmount.String()
req := c.client.R().
SetContext(ctx).
// the SellTokens address must follow the HEX format
SetBody(map[string]interface{}{
dexalot.ParamsChainID: params.ChainID,
dexalot.ParamsTakerAsset: common.HexToAddress(params.TakerAsset).Hex(),
dexalot.ParamsMakerAsset: common.HexToAddress(params.MakerAsset).Hex(),
dexalot.ParamsTakerAmount: params.TakerAmount,
dexalot.ParamsTakerAmount: upscaledTakerAmount.String(),
dexalot.ParamsUserAddress: params.UserAddress,
dexalot.ParamsExecutor: params.Executor,
})
Expand Down
38 changes: 24 additions & 14 deletions pkg/liquidity-source/dexalot/pool_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ type (
ZeroToOnePriceLevels []PriceLevel
OneToZeroPriceLevels []PriceLevel
gas Gas
Token0Original string
Token1Original string
}
SwapInfo struct {
BaseToken string `json:"b" mapstructure:"b"`
BaseTokenAmount string `json:"bAmt" mapstructure:"bAmt"`
QuoteToken string `json:"q" mapstructure:"q"`
QuoteTokenAmount string `json:"qAmt" mapstructure:"qAmt"`
MarketMaker string `json:"mm,omitempty" mapstructure:"mm"`
ExpirySecs uint `json:"exp,omitempty" mapstructure:"exp"`
BaseToken string `json:"b" mapstructure:"b"`
BaseTokenAmount string `json:"bAmt" mapstructure:"bAmt"`
QuoteToken string `json:"q" mapstructure:"q"`
QuoteTokenAmount string `json:"qAmt" mapstructure:"qAmt"`
MarketMaker string `json:"mm,omitempty" mapstructure:"mm"`
ExpirySecs uint `json:"exp,omitempty" mapstructure:"exp"`
BaseTokenOriginal string `json:"bo,omitempty" mapstructure:"bo"`
QuoteTokenOriginal string `json:"qo,omitempty" mapstructure:"qo"`
}

Gas struct {
Expand All @@ -54,6 +58,8 @@ type (
Extra struct {
ZeroToOnePriceLevels []PriceLevelRaw `json:"0to1"`
OneToZeroPriceLevels []PriceLevelRaw `json:"1to0"`
Token0Address string `json:"token0"`
Token1Address string `json:"token1"`
}

MetaInfo struct {
Expand Down Expand Up @@ -95,18 +101,20 @@ func NewPoolSimulator(entityPool entity.Pool) (*PoolSimulator, error) {
},
Token0: *entityPool.Tokens[0],
Token1: *entityPool.Tokens[1],
Token0Original: extra.Token0Address,
Token1Original: extra.Token1Address,
ZeroToOnePriceLevels: zeroToOnePriceLevels,
OneToZeroPriceLevels: oneToZeroPriceLevels,
gas: defaultGas,
}, nil
}

func (p *PoolSimulator) CalcAmountOut(params pool.CalcAmountOutParams) (*pool.CalcAmountOutResult, error) {
tokenIn, tokenOut, levels := p.Token0, p.Token1, p.ZeroToOnePriceLevels
tokenIn, tokenOut, tokenInOriginal, tokenOutOriginal, levels := p.Token0, p.Token1, p.Token0Original, p.Token1Original, p.ZeroToOnePriceLevels
if params.TokenAmountIn.Token == p.Info.Tokens[1] {
tokenIn, tokenOut, levels = p.Token1, p.Token0, p.OneToZeroPriceLevels
tokenIn, tokenOut, tokenInOriginal, tokenOutOriginal, levels = p.Token1, p.Token0, p.Token1Original, p.Token0Original, p.OneToZeroPriceLevels
}
amountOut, _, err := p.swap(params.TokenAmountIn.Amount, tokenIn, tokenOut, levels)
amountOut, _, err := p.swap(params.TokenAmountIn.Amount, tokenIn, tokenOut, tokenInOriginal, tokenOutOriginal, levels)
return amountOut, err
}
func (p *PoolSimulator) UpdateBalance(params pool.UpdateBalanceParams) {
Expand All @@ -118,7 +126,7 @@ func (p *PoolSimulator) GetMetaInfo(_ string, _ string) interface{} {
}

func (p *PoolSimulator) swap(amountIn *big.Int, baseToken, quoteToken entity.PoolToken,
priceLevel []PriceLevel) (*pool.CalcAmountOutResult, string, error) {
baseOriginal, quoteOriginal string, priceLevel []PriceLevel) (*pool.CalcAmountOutResult, string, error) {

var amountInAfterDecimals, decimalsPow, amountInBF, amountOutBF big.Float

Expand All @@ -139,10 +147,12 @@ func (p *PoolSimulator) swap(amountIn *big.Int, baseToken, quoteToken entity.Poo
Fee: &pool.TokenAmount{Token: baseToken.Address, Amount: bignumber.ZeroBI},
Gas: p.gas.Quote,
SwapInfo: SwapInfo{
BaseToken: baseToken.Address,
BaseTokenAmount: amountIn.String(),
QuoteToken: quoteToken.Address,
QuoteTokenAmount: amountOut.String(),
BaseToken: baseToken.Address,
BaseTokenAmount: amountIn.String(),
QuoteToken: quoteToken.Address,
QuoteTokenAmount: amountOut.String(),
BaseTokenOriginal: baseOriginal,
QuoteTokenOriginal: quoteOriginal,
},
}, amountOutAfterDecimals.String(), nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/liquidity-source/dexalot/rfq.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func (h *RFQHandler) RFQ(ctx context.Context, params pool.RFQParams) (*pool.RFQR

p := FirmQuoteParams{
ChainID: int(params.NetworkID),
TakerAsset: swapInfo.BaseToken,
MakerAsset: swapInfo.QuoteToken,
TakerAsset: swapInfo.BaseTokenOriginal,
MakerAsset: swapInfo.QuoteTokenOriginal,
TakerAmount: swapInfo.BaseTokenAmount,
UserAddress: params.RFQSender,
Executor: params.RFQRecipient,
Expand Down
4 changes: 2 additions & 2 deletions pkg/liquidity-source/dexalot/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ type FirmQuoteFail struct {
}

func (r FirmQuoteFail) Failed() bool {
return !r.Success || r.ReasonCode != "" || r.Reason != ""
return r.ReasonCode != "" || r.Reason != ""
}

type Order struct {
NonceAndMeta string `json:"nonceAndMeta"`
Expiry int64 `json:"expiry"`
Expiry int `json:"expiry"`
MakerAsset string `json:"makerAsset"`
TakerAsset string `json:"takerAsset"`
Maker string `json:"maker"`
Expand Down

0 comments on commit 05b3bef

Please sign in to comment.