CustomSwaps (Governance Safe Smart Swaps)

name is not final, all suggestions are welcome

docsarrow-up-right

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.networkarrow-up-right 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.solarrow-up-right)

uniswap verified on etherscanarrow-up-right

sushiswap verified on etherscanarrow-up-right

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 etherscanarrow-up-right

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.solarrow-up-right)

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.solarrow-up-right

Extends SafeSmartSwapAbstract.solarrow-up-right, constructor needs address _governanceSwap.

3 key/test functions are:

Last updated