CustomSwaps (Governance Safe Smart Swaps)

name is not final, all suggestions are welcome

docs

These contracts were developed to allow devs to provide a safe, flexible and governable way to handle swaps on their smart contracts. This has synergy with keep3r.network since keep3rs can submit custom dex+data to reduce slippage (and possibly get a bonus for it)

Wishlist

DexHandlers

Integrations

Run tests

  • npm i

  • cp example.config.json .config.json

    • add Infura and Alchemy APIs

    • add "accounts.mainnet.publicKey" (should have ETH balance > 3 for tests to run ok)

  • npm test

    • use npm run test:gas to get gas report

Contracts

dex-handlers

Abstract contract that should be used to extend from when creating DexHandlers. (see UniswapV2DexHandler.sol)

uniswap verified on etherscan

sushiswap verified on etherscan

UniswapV2 custom DexHandler.

Overrides following DexHandlerAbstract functions:

For decodeData it always returns a message pointing to correct custom implementation:

For swapData it always returns a message pointing to correct custom implementation:

These 2 functions above will return a different message for other DexHandlers (more examples in the future)

customSwap handles decoding and overwriting the data + the UniswapV2 logic for swapExactTokensForTokens

swap

verified on etherscan

Governance managed mappings for dex and pairs.

This contract and it's governance need to be REALLY secure and trustworthy, since they have the ability to steal funds and/or block custom swaps by introducing an evil handler and/or data into a pair's default.

I need to find a way to incentivize good behaviour and minimize oversight required.

Abstract contract that has all the internal functions to safely handle and verify swaps done via GovernanceSwap + DexHandlers.

Contracts that extend SafeSmartSwap should call SafeSmartSwap(_governanceSwap) on construction. (see MockStrategy.sol)

There are only 2 functions that respectively handle default and custom swaps:

Governance (default) swaps spend less gas since they don't require extra checks (see gas spendage with npm run test:gas)

Custom swaps reverts if the output is less than the estimated output by the current Governance (default) swap for the pair.

Also checks final token balances in case the dex+data used was maliciously returning a higher _amountOut:

mock

Mocked Strategy based on yearn's StrategyCurveYVoterProxy.sol

Extends SafeSmartSwapAbstract.sol, constructor needs address _governanceSwap.

3 key/test functions are:

Last updated

Was this helpful?