Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TRD-783] Implement CalcAmountIn for kyber limit order #657

Merged

Conversation

chrisngyn
Copy link
Member

Why did we need it?

  • To support solve orders that need exact amount out through limit order

Related Issue

Release Note

How Has This Been Tested?

  • Unittest

Screenshots (if appropriate):

@chrisngyn chrisngyn force-pushed the feature/TRD-783-implement-calc-amount-in-for-kyber-limit-order branch 2 times, most recently from d3452a4 to 7af5090 Compare December 17, 2024 07:22
@kyber-ci-bot
Copy link

kyber-ci-bot commented Dec 17, 2024

Test coverage changes:
Package Before After Diff
github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity 26.30% 26.30% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/algebra/integral 28.50% 28.50% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/algebra/v1 16.40% 17.90% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/ambient 11.80% 11.80% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/balancer-v1 29.60% 32.80% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/balancer-v2/composable-stable 36.40% 38.20% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/balancer-v2/math 49.00% 49.00% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/balancer-v2/stable 21.20% 24.90% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/balancer-v2/weighted 28.90% 32.30% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/bancor-v21 25.10% 25.10% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/bancor-v3 34.80% 37.30% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/bebop 21.90% 21.90% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/bedrock/unieth 6.00% 6.00% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/clipper 23.60% 23.60% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/curve/plain 21.00% 21.90% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/curve/stable-meta-ng 9.00% 10.10% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/curve/stable-ng 11.40% 12.40% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/curve/tricrypto-ng 23.60% 24.40% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/curve/twocrypto-ng 22.90% 23.80% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/dai-usds 10.50% 17.80% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/deltaswap-v1 36.20% 36.20% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/dexalot 29.80% 29.80% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/dodo/classical 25.40% 28.00% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/dodo/dpp 10.80% 14.10% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/dodo/dsp 11.20% 14.50% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/dodo/dvm 11.70% 15.20% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/dodo/libv1 73.30% 73.30% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/dodo/libv2 37.60% 37.60% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/ethena/susde 9.00% 9.00% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/ether-vista 2.00% 2.00% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/etherfi/eeth 7.70% 7.70% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/etherfi/vampire 4.50% 4.50% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/etherfi/weeth 6.20% 6.20% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/fluid/dex-t1 53.00% 53.00% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/fluid/vault-t1 9.30% 9.30% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/frax/sfrxeth 8.90% 8.90% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/frax/sfrxeth-convertor 16.90% 16.90% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/generic-simple-rate 17.40% 21.80% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/gyroscope/2clp 30.30% 30.30% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/gyroscope/3clp 32.60% 32.60% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/gyroscope/eclp 50.10% 50.10% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/gyroscope/math 14.30% 14.30% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/hashflow-v3 53.50% 53.50% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/integral 21.90% 26.60% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/kelp/rseth 6.90% 6.90% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/lo1inch 48.10% 48.10% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/mantle/meth 14.20% 14.20% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/mkr-sky 14.30% 21.10% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/mx-trading 35.00% 35.00% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/native-v1 30.90% 30.90% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/nomiswap/nomiswapstable 6.60% 7.90% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/ondo-usdy 13.50% 13.50% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/puffer/pufeth 10.60% 10.60% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/renzo/ezeth 5.70% 5.70% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/ringswap 11.00% 12.60% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/rocketpool/reth 10.30% 10.30% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/staderethx 15.50% 15.50% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/swaap-v2 19.90% 26.70% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/swell/rsweth 4.30% 4.30% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/swell/sweth 4.30% 4.30% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/syncswapv2/aqua 18.00% 20.00% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/syncswapv2/classic 15.40% 19.20% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/syncswapv2/stable 20.70% 24.10% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/uniswap-v1 8.60% 12.80% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/uniswap-v2 7.60% 9.30% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/usd0pp 13.70% 13.70% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/velocore-v2/cpmm 40.30% 42.50% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/velocore-v2/math 16.80% 16.80% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/velocore-v2/math/sd59x18 51.90% 51.90% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/velocore-v2/wombat-stable 19.10% 23.90% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/velodrome-v1 26.40% 29.70% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/velodrome-v2 28.50% 31.90% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/virtual-fun 27.90% 27.90% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/woofi-v2 37.40% 41.30% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/woofi-v21 36.30% 39.40% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/algebrav1 16.40% 17.90% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/balancer-composable-stable 39.90% 42.40% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/balancer/stable 29.50% 33.40% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/balancer/weighted 29.60% 32.70% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/camelot 27.50% 31.80% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/curve/aave 19.00% 20.30% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/curve/base 11.80% 13.10% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/curve/compound 11.30% 12.70% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/curve/meta 19.10% 20.20% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/curve/plain-oracle 10.00% 11.30% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/curve/tricrypto 25.00% 26.10% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/curve/two 11.10% 12.20% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/dmm 19.40% 24.70% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/dodo 28.50% 31.50% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/equalizer 18.00% 22.90% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/fraxswap 13.40% 18.70% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/fulcrom 22.70% 26.00% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/fxdx 17.60% 19.40% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/gmx 22.70% 24.20% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/gmx-glp 22.60% 23.70% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/iziswap 19.40% 23.50% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/kokonut-crypto 19.90% 21.00% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/kyber-pmm 29.10% 32.60% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/lido 15.60% 21.60% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/lido-steth 12.60% 17.90% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/limitorder 30.30% 38.70% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/liquiditybookv20 24.90% 27.00% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/liquiditybookv21 33.00% 34.90% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/madmex 21.70% 23.30% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/makerpsm 23.30% 28.90% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/mantisswap 32.90% 36.10% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/maverickv1 48.30% 49.60% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/metavault 24.00% 25.80% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/nuriv2 14.50% 17.50% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/pancakev3 30.30% 32.60% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/platypus 18.70% 21.60% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/pol-matic 7.70% 15.00% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/quickperps 19.90% 21.90% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/ramsesv2 14.40% 17.30% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/saddle 47.30% 50.40% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/slipstream 14.00% 14.00% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/smardex 29.90% 34.10% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/solidly-v3 16.70% 19.50% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/swapbased-perp 26.60% 28.30% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/syncswap/syncswapclassic 20.10% 24.80% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/syncswap/syncswapstable 28.60% 32.90% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/synthetix 31.10% 32.30% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/traderjoev20 2.60% 2.60% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/traderjoev21 2.60% 2.60% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/uniswap 15.50% 16.80% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/uniswapv3 29.70% 31.80% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/usdfi 28.60% 33.50% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/velocimeter 29.80% 34.80% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/velodrome 29.40% 34.20% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/velodromev2 29.40% 34.40% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/vooi 22.00% 26.70% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/wombat/wombatlsd 20.10% 23.10% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/wombat/wombatmain 20.40% 23.40% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/woofiv2 36.10% 40.20% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/util 4.50% 4.50% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/abi 75.00% 75.00% ✔️
github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/bignumber 14.80% 14.80% ✔️

@chrisngyn chrisngyn force-pushed the feature/TRD-783-implement-calc-amount-in-for-kyber-limit-order branch from fcc721d to a45bf52 Compare December 17, 2024 09:05
@chrisngyn chrisngyn force-pushed the feature/TRD-783-implement-calc-amount-in-for-kyber-limit-order branch from 68572b4 to adf23e3 Compare December 17, 2024 10:28
filledTakingAmountWei := totalAmountInAfterFee
filledMakingAmountWei, _ := amountOutWei.Int(nil)
filledMakingAmountWei := new(big.Int).Div(
new(big.Int).Mul(filledTakingAmountWei, remainingMakingAmountWei),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be order's original makingAmount/takingAmount instead of the remaining amounts, there might be no different at all but it's better to follow SC logic https://github.com/KyberNetwork/ks-limit-order-sc/blob/develop/src/offchain/helpers/AmountCalculator.sol#L34

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad. I've updated

assert.Equal(t, nil, err)
got, err := testutil.MustConcurrentSafe[*pool.CalcAmountInResult](t, func() (any, error) {
limit := swaplimit.NewInventory("", p.CalculateLimit())
return p.CalcAmountIn(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe you can call CalcAmountOut on the result here, and check if the result is equal to tt.args.tokenAmountOut (or different within 1-2 wei)

or even better, write a random test to asset that CalcAmountOut(CalcAmountIn(random_input)) is equal or close to random_input

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added more tests for calling both CalcAmountOut and CalcAmountIn with same input.

@chrisngyn chrisngyn force-pushed the feature/TRD-783-implement-calc-amount-in-for-kyber-limit-order branch from 7884316 to 2b1b81d Compare December 18, 2024 07:19
Copy link
Member

@it4rb it4rb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry I don't have time to review full CalcAmountIn logic right now, but given that the reversed test cases (out->in->out) have passed I think we're good to go

@chrisngyn chrisngyn merged commit c555b4e into main Dec 18, 2024
5 checks passed
@chrisngyn chrisngyn deleted the feature/TRD-783-implement-calc-amount-in-for-kyber-limit-order branch December 18, 2024 07:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants