Skip to content

Add Kyber V3 Index Exchange adapter #101

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

Merged
merged 13 commits into from
Jun 25, 2021

Conversation

0xSachinK
Copy link
Contributor

@0xSachinK 0xSachinK commented Jun 21, 2021

Why?
Because KNC kills us during rebalancing. The liquidity for KNC/ETH is very thin on Uniswap V2 and thus we have to do 30 trades on average to rebalance KNC token in DPI.

Solution
Move to using Kyber's latest DMM Exchange as the source of our liquidity for KNC/ETH pair.

Liquidity for KNC/ETH pair

  • Uniswap V3 = No liquidity available
  • Sushiswap = No liquidity available
  • Uniswap V2 = 1,749,990
  • Kyber V3 DMM exchange = 3,848,968
    • Effective liqudity on DMM = Total liquidity in pool * Amp factor = 3,848,968 * 1.9 = 7,313,039

Ratio of Kyber V3 liquidity to Uniswap V2 Liquidity = 7,313,039 / 1,749,990 = 4.1789

Implementation

  • The KyberV3 DMM contract interfaces are similar to Uniswap V2 contracts. It has a router contract with our familiar swap functions, swapExactTokensForTokens and swapTokensForExactTokens.

Important Differences:

  • Each pool in Kyber V3 has an amplification factor which makes the pool trade only in a specified price range and within a given region of the constant product function. Thus making the pools more capital efficient.
  • For each token pair, there can be possibly many pools with different configurations for the pricing curve (i.e different amplification factors). Hence the address of the pool to be used for trading must be passed by the off-chain infrastructure.
    • Managers can refer to DMM Analytics page to select the pool with the highest liquidity.
    • Off-chain scripts can use instructions given here to select the pool with the highest liquidity.

Relevant parts of Kyber docs:

Progress

  • Add KyberV3IndexExchangeAdapter contract
  • Write KyberV3 DMM fixtures
  • Write unit tests for adapter contract
  • Write unit tests for Kyber V3 DMM fixture
  • Write integration tests with GIM

Deadline: July Rebalance

Blockages

  • As DMM exchange has liquidity only in the new KNC tokens, we would first need to migrate DPI to new KNC tokens, using the existing KyberMigrationAdapter.

@0xSachinK 0xSachinK requested review from ncitron and bweick June 23, 2021 16:37
Copy link
Contributor

@ncitron ncitron left a comment

Choose a reason for hiding this comment

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

Overall looks really good. Left a couple of small questions and comments

const lastBlockTimestamp = await getLastBlockTimestamp();

const expectedWethPositionUnits = preciseDiv(currentWethAmount.add(expectedOut), totalSupply);
const expectedDaiPositionUnits = preciseDiv(currentDaiAmount.sub(ether(1000)), totalSupply);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make it more clear how 1000 is calculated as the sell amount. Maybe do something like
const daiToSell = // calculation for the sell amount
I recently had to write some tests for GIM, and had to figure out the math from scratch, and would have appreciated having the tests at least document the math (but better if it just does that math in a variable declaration as I showed above)

@@ -11,7 +11,9 @@ import {
GeneralIndexModule,
Copy link
Contributor

Choose a reason for hiding this comment

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

Also, we've been trying to separate out integration tests for new exchange and index-exchange adapters into new files now to prevent the main test from becoming too big over time. Take a look out how I did it here:
https://github.com/SetProtocol/set-protocol-v2/pull/99/files#diff-d567ba1527ee362b53fe24be298c60b9fc43228d3ecfa1d4496196859d3ed4d3R1

Copy link
Contributor

Choose a reason for hiding this comment

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

+1 this would be really nice

@0xSachinK 0xSachinK merged commit 32b50f1 into master Jun 25, 2021
richardliang pushed a commit that referenced this pull request Jul 24, 2021
* Add KyberV3 Index Exchange adapter contract

* fix typo

* Add Kyber V3 DMM fixture

* Remove getPoolWithBestLiquidity() from adapter contract

* Add tests for adapter contract

* Add integration tests with GIM

* Add tests for Kyber V3 DMM fixture

* Fix bug

* Fix bug in uniswap V3 fixture tests

* Validate pool address in exchange adapter

* Bump package to 0.0.43

* Move down pool address validation
richardliang pushed a commit that referenced this pull request Jul 24, 2021
Inject gas setting into ABIs and move tasks from hardhat.config to tasks/ (#50)

* Inject gas limit into abi on compilation
* Move solc & typechain tasks from hardhat.config to tasks/

Add AaveGovernanceV2Adapter (#14)

* add basic structure for SnapshotDelegationModule

* add delegate function

* add SnapshotGovernanceAdapter

* code review changes

* add aave v2 governance contracts to aave fixture

* add AaveGovernanceV2Adapter

* add tests for getting proper calldata

* add integration test for aave governance v2 adapter

* style fixes

* code review changes

Add CompoundBravoGovernanceAdapter (#17)

* add GovernorBravo to compoundFixture

* add CompoundBravoGovernanceAdapter

* add CompoundBravoGovernanceAdapter tests and fix new gov deploys/fixture

* add integration tests for CompoundBravoGovernanceAdapter

* code review changes

Fix AaveTokenV2Mintable type imports in utils/fixtures

Add batch fetch balances and allowances to protocol viewer

Remove aliases from import paths in utils/fixtures

Bump package version to 0.0.29

Update build and coverage badges for repo rename

Move libraryUtils to test folder

Fix image tags in build badges for repo renaming

Require fully qualified contract name params for library link util

Move libraryUtils back to utils/common

Bump new version

Fix bug in SingleIndexModule's tradeRemainingWETH test (#65)

Add CompoundWrapAdapter (#55)

Add CompoundWrapAdapter

Co-authored-by: mario <[email protected]>

port ctoken oracle (#57)

Port CToken Oracle

Fix failing CompoundWrap tests (#66)

Move getRandomAddress (w/out hardhat) to utils/common (#63)

Add GeneralIndexModule contract (#59)

Add GeneralIndexModule contract, UniswapV2ExchangeAdapterV2 updates, and BalancerV1ExchangeAdapter

Co-authored-by: bweick <[email protected]>

Remove UniswapYieldStrategy and tests. (#68)

Remove UniswapYieldStrategy and tests

Add yearn vault wrapper module and oracle (#61)

Add Yearn vaults wrap adapter and oracle for valuing yTokens.

Co-authored-by: bweick <[email protected]>

Organize integrations folder into integration type. (#70)

Updated javadocs for GeneralIndexModule. (#71)

Updated javadocs for GeneralIndexModule.

Bumped package. (#73)

Remove trailing whitespace in GeneralIndexModule.sol

Specify gas and blockGasLimit for localhost network

Add new Uniswap transfer fee adapter

Bump to 0.0.33

Add BalancerV1ExchangeAdapter tests (#72)

* Add balancerV1ExchangeAdapter tests

* Refactor uniswapV2ExchangeAdapter and add getDataParam test

* Re-add deleted functions in deployAdapter.ts

GeneralIndexModule internal review and external audit revisions (#75)

* Make all revert reason strings < 32bytes

* Remove _normalizedTargetMethod getter

* Encapsulate _noTokensToSell#canSell logic as internal method

* Encapsulate _allTargetsMet#targetUnmet logic in internal method

* Validate exchange adapter when setting & store name as bytes32 hash

* Consolidate position state and timestamp updates

* Validate params for external view methods

* Make all external setter method names use prefix `set`

* Add RebalanceStarted event

* Remove _caller param from onlyAllowedTrader modifier

* Add getDefaultPositionRealUnit getter

* Encapsulate data aggregation logic in #startRebalance in own method

* Prohibit external positions for components in #initialize and #startRebalance

* Refactor #createTradeInfo methods

* Make weth state variable immutable

* Delegate modifier logic to internal helpers in ModuleBase and GeneralIndexModule

* Delete trader permissions on module removal / add removeModule tests

* Add #validatePairsWithArray methods to AddressArrayUtils library and use in setter methods

* Add approximatelyEquals helper in PreciseUnitMath and use in #_targetUnmet

* Add AssetTargetsRaised event in RaiseAssetTargets with positionMultiplier arg

* Remove trader from permissionInfo.tradersHistory when they are de-authorized

* Add getAllowedTraders method and make tradersHistory contain unique elements

Added an exchangeData param on the GIM  (#76)

Added an exchangeData param on the GIM. Allows passing in arbitrary parameter or execution info to an exchange. Made all GIM adapters specific to GIM and not compatible with TradeModule.

restructure test/protocol/integration into folders (#79)

Final review fixes for GeneralIndexModule (#83)

* Add math boundary cases for protocol fee and raise target percentage

* Add external position modules check to #_validateTradeParameters

* Group internal methods by function and importance

* Remove TargetUnitsUpdated event, update RebalanceStarted event

* Add explanatory note about fixed / floating limit

* 0.0.35

Upgrade hardhat to 2.2.1 (Berlin) / add test:fast yarn commands (#78)

* Upgrade hardhat to 2.2.1 (Berlin) / add test:fast yarn commands

* Increase Yearn external abi gas estimates by 1 order of magnitude

KNC Migration Adapter (#86)

* Add Kyber migration adapter contract

* Add tests

* Add @openzeppelin/[email protected] as dependency

* Add external contract KyberNetworkTokenV2

* Updated KyberNetworkTokenV2 abi

* Add missing test and remove proxy from contract

* Remove external dependency openzeppelin/contracts-upgradeable

* Fix variable names and bump package

Add AXS migration wrap adapter (#87)

* Add Axie Infinity migration wrap adapter contract

* Add external TokenSwap contract

* Add tests for axie infinity migration wrap adapter

* Modify adapter to serve as middleware contract for migration

* Fix bugs and add tests

* Fixed external TokenSwap contract bytecode

* Explain migration process in javadocs

* Bump package to 0.0.37

Adding StandardTokenMock#mint

Adding CompClaimAdapter

feat: initial stab at Uniswap V3 adaptor

feat: verify uniswap path for single hops WIP

feat: verify path for multiple hops

fix: add proper tests, require statements for sellTokenForToken

refactor: move decoding tokens from encoded path into reusable function

feat: implement sellTokenForEthToUniswapV3 and sellEthForTokenToUniswapV3

fix: address review feedback

fix: sellEthForTokenToUniswapV3 impl and address review comments

refactor: clean up Uniswap V3 path decoding logic

fix: remove incorrect require check for sellEthForTokenToUniswapV3

Update package for public npm (make ethers a non-dev dep) (#92)

ZeroExAdapter null recipient fix (#93)

0.0.38 (#94)

Add Uniswap V3 Fixture (#95)

* add basic Uniswap V3 fixture

* add createPool to fixture

* add quoter

* add getTokenOrder helper

* add addLiquidityWide helper function

* add external contracts

* sort imports

* fix imports

* improve comments

* use exact values in tests

* split uniswap contracts into v2/v3 folders

* review changes

* add weth and wbtc price in initialize function

Uniswap V3 Exchange Adapter (#96)

* add basic Uniswap V3 fixture

* add createPool to fixture

* add quoter

* add getTokenOrder helper

* add addLiquidityWide helper function

* add external contracts

* sort imports

* fix imports

* improve comments

* use exact values in tests

* split uniswap contracts into v2/v3 folders

* add UniswapV3ExchangeAdapter

* remove stray console log

* fix comments

* cleanup

* add revert on path mismatch

* add uniswap v3 tests to trade module integration tests

* improve revert messages

Add forked provider infrastructure and simple TradeModule / ExchangeAdapter tests (#64)

Fix broken imports (#98)

* v0.0.39

* fix imports

Add uniswap v3 index exchange adapter (#89)

* Add external Uniswap V3 contracts

* Add UniswapV3 Index Exchange Adapter contract

* Modify compiler-version in .solhint.json

* Add tests

* Fix coverage

* Improve javadocs and add getEncodedTradePath() test

* Use soldityPack in getEncodedTradePath test

* Change compiler version to 0.6.10

* Add suggested changes
* Remove getTradeEncodedPath function
* Remove @uniswap/v3-periphery as dependency
* Add @uniswap/v3-core as dependency

* Use exactInputSingle and exactOutputSingle functions instead of exactInput and exactOutput

* Move BytesLib to external/contracts/uniswap/v3

* Add Note to set exchange data in GIM

* Remove @uniswap/v3-core dependency

*Add suggested changes

* Add getEncodedFeeData function and add suggested changes

* Refactor tests to use UniswapV3 Fixture

* Fix uniswap/v3/SwapRotuer ABI

* Add integration tests with GIM

* Trade DAI for WETH on UniswapV3 during rebalance
* Trade WETH for WBTC on UniswapV3 during rebalance

* Fix comment

fix broken test (#102)

Add AMMSplitter (#99)

* add _getUniSplit function

* add swapping

* only rever when total output < min output

* use better math for split

* add tests

* rename contracts and functions to be inline with spec

* add multi hop support

* add tradeExactOutput

* clean up imports

* shorten name

* add javadocs

* add getQuote function

* clean up ratio calculation

* remove hardhat console

* add TradeSplitterExchangeAdapter

* fix typo causing tests to break

* add TradeSplitterIndexExchangeAdapter

* improve coverage

* increase coverage

* refactor

* remove redundant if in getQuote

* improve reverts

* improve coverage

* move TradeSplitter into products

* add TradeSplitterExchangeAdapter integration test

* split getQuote into two functions for exact input/output

* add TradeSplitter GIM integration tests

* fix tests

* make TradeSplitter adhere to the UniswapV2Router interface

* use uniswap v2 adapters for trade splitter

* cleanup

* adhere to uniswap interface for getting quotes

* change name of contracts

* improve AMMSplitter tests

* refactoring

* fix bug with intermediary tokens with less than 18 decimals

* refactor

* add extra tests

* update revert messages

* improve comments

* add events

* use input and output tokens instead of path for events

v0.0.41 (#103)

change constructor parameters (#104)

fix Felix comments (#105)

Add Kyber V3 Index Exchange adapter (#101)

* Add KyberV3 Index Exchange adapter contract

* fix typo

* Add Kyber V3 DMM fixture

* Remove getPoolWithBestLiquidity() from adapter contract

* Add tests for adapter contract

* Add integration tests with GIM

* Add tests for Kyber V3 DMM fixture

* Fix bug

* Fix bug in uniswap V3 fixture tests

* Validate pool address in exchange adapter

* Bump package to 0.0.43

* Move down pool address validation

audit fixes (#108)

Patch for real -> virtual -> real unit conversion. (#100)

Patch for real -> virtual -> real unit conversion.

Bump package. (#109)

Update UniswapV2IndexExchangeAdapter to support two-hop trades (#110)

* update adapter and tests

* add integration test

* improve comments

* review changes

bump package.json version number (#111)

ClaimModule Audit fixes (#97)

* low hanging audit changes

* fix tests

* audit changes

* fix tests

* improve tests

* clean up contracts

* use removeStorage

* review changes

SingularityNet migrator that burns unmigrated tokens, thus zeroing ou… (#113)

SingularityNet migrator that burns unmigrated tokens, thus zeroing out position.

Bump package. (#114)

Address comments and add integration tests for Sushi wrap adapter
richardliang pushed a commit that referenced this pull request Aug 25, 2021
* Add KyberV3 Index Exchange adapter contract

* fix typo

* Add Kyber V3 DMM fixture

* Remove getPoolWithBestLiquidity() from adapter contract

* Add tests for adapter contract

* Add integration tests with GIM

* Add tests for Kyber V3 DMM fixture

* Fix bug

* Fix bug in uniswap V3 fixture tests

* Validate pool address in exchange adapter

* Bump package to 0.0.43

* Move down pool address validation
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.

3 participants