Skip to content

Commit

Permalink
cap max reserves (#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
tien7668 authored Nov 6, 2024
1 parent 789da21 commit ce15044
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
12 changes: 1 addition & 11 deletions pkg/liquidity-source/dexalot/client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package client
import (
"context"
"errors"
"math/big"

"github.com/KyberNetwork/logger"
"github.com/ethereum/go-ethereum/common"
"github.com/go-resty/resty/v2"
"github.com/goccy/go-json"

"github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/dexalot"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/bignumber"
)

const (
Expand Down Expand Up @@ -43,22 +41,14 @@ func NewHTTPClient(config *dexalot.HTTPClientConfig) *HTTPClient {

func (c *HTTPClient) Quote(ctx context.Context, params dexalot.FirmQuoteParams, upscalePercent int) (dexalot.FirmQuoteResult, error) {
// token address case-sensitive
upscaledTakerAmount := bignumber.NewBig(params.TakerAmount)
upscaledTakerAmount.Mul(
upscaledTakerAmount,
big.NewInt(int64(100+upscalePercent)),
).Div(
upscaledTakerAmount,
big.NewInt(100),
)
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: upscaledTakerAmount.String(),
dexalot.ParamsTakerAmount: params.TakerAmount,
dexalot.ParamsUserAddress: params.UserAddress,
dexalot.ParamsExecutor: params.Executor,
})
Expand Down
12 changes: 12 additions & 0 deletions pkg/liquidity-source/dexalot/pool_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type (
ExpirySecs uint `json:"exp,omitempty" mapstructure:"exp"`
BaseTokenOriginal string `json:"bo,omitempty" mapstructure:"bo"`
QuoteTokenOriginal string `json:"qo,omitempty" mapstructure:"qo"`
BaseTokenReserve string `json:"br,omitempty" mapstructure:"br"`
QuoteTokenReserve string `json:"qr,omitempty" mapstructure:"qr"`
}

Gas struct {
Expand Down Expand Up @@ -163,6 +165,14 @@ func (p *PoolSimulator) swap(amountIn *big.Int, baseToken, quoteToken entity.Poo
amountOutBF.Mul(&amountOutAfterDecimals, &decimalsPow)

amountOut, _ := amountOutBF.Int(nil)
var baseTokenReserve, quoteTokenReserve *big.Int
if strings.EqualFold(baseToken.Address, p.Info.Tokens[0]) {
baseTokenReserve = p.Info.Reserves[0]
quoteTokenReserve = p.Info.Reserves[1]
} else {
baseTokenReserve = p.Info.Reserves[1]
quoteTokenReserve = p.Info.Reserves[0]
}
return &pool.CalcAmountOutResult{
TokenAmountOut: &pool.TokenAmount{Token: quoteToken.Address, Amount: amountOut},
Fee: &pool.TokenAmount{Token: baseToken.Address, Amount: bignumber.ZeroBI},
Expand All @@ -174,6 +184,8 @@ func (p *PoolSimulator) swap(amountIn *big.Int, baseToken, quoteToken entity.Poo
QuoteTokenAmount: amountOut.String(),
BaseTokenOriginal: baseOriginal,
QuoteTokenOriginal: quoteOriginal,
BaseTokenReserve: baseTokenReserve.String(),
QuoteTokenReserve: quoteTokenReserve.String(),
},
}, amountOutAfterDecimals.String(), nil
}
Expand Down
17 changes: 16 additions & 1 deletion pkg/liquidity-source/dexalot/rfq.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math/big"

"github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/pool"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/bignumber"
"github.com/KyberNetwork/logger"
"github.com/mitchellh/mapstructure"
)
Expand Down Expand Up @@ -39,11 +40,25 @@ func (h *RFQHandler) RFQ(ctx context.Context, params pool.RFQParams) (*pool.RFQR
}
logger.Infof("params.SwapInfo: %v -> swapInfo: %v", params.SwapInfo, swapInfo)

upscaledTakerAmount := bignumber.NewBig(swapInfo.BaseTokenAmount)
upscaledTakerAmount.Mul(
upscaledTakerAmount,
big.NewInt(int64(100+h.config.UpscalePercent)),
).Div(
upscaledTakerAmount,
big.NewInt(100),
)

maxAmount := bignumber.NewBig(swapInfo.BaseTokenReserve)
if upscaledTakerAmount.Cmp(bignumber.NewBig(swapInfo.BaseTokenReserve)) > 0 {
upscaledTakerAmount = bignumber.NewBig(swapInfo.BaseTokenAmount)
upscaledTakerAmount = upscaledTakerAmount.Add(upscaledTakerAmount, maxAmount).Div(upscaledTakerAmount, bignumber.Two)
}
p := FirmQuoteParams{
ChainID: int(params.NetworkID),
TakerAsset: swapInfo.BaseTokenOriginal,
MakerAsset: swapInfo.QuoteTokenOriginal,
TakerAmount: swapInfo.BaseTokenAmount,
TakerAmount: upscaledTakerAmount.String(),
UserAddress: params.RFQSender,
Executor: params.RFQRecipient,
}
Expand Down

0 comments on commit ce15044

Please sign in to comment.