CustomSwaps (Governance Safe Smart Swaps)
Last updated
Was this helpful?
Last updated
Was this helpful?
name is not final, all suggestions are welcome
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 since keep3rs can submit custom dex+data to reduce slippage (and possibly get a bonus for it)
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
dex-handlers
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
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.
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
3 key/test functions are:
Abstract contract that should be used to extend from when creating DexHandlers. (see )
Contracts that extend SafeSmartSwap
should call SafeSmartSwap(_governanceSwap)
on construction. (see )
Mocked Strategy based on yearn's
Extends , constructor needs address _governanceSwap
.