A minimal example showing how to integrate 1inch with your smart contract
git clone https://github.com/smye/1inch-swap.git
cd 1inch-swap
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
-
Add the
OpenZeppelin
dependencies viaBrownies
package managerbrownie pm install OpenZeppelin/openzeppelin-contracts@4.1.0
-
To add a
live
network toBrownie
brownie networks add Ethereum polygon host=https://matic-mainnet.chainstacklabs.com chainid=137 explorer=https://api.polygonscan.com/api
-
For a
development
networkbrownie networks add Development polygon-fork host=http://127.0.0.1 cmd=ganache-cli fork=https://matic-mainnet.chainstacklabs.com port=8545
-
Modify
brownie-config.yaml
- router: Address of the 1inch router
- tokenIn: Input token address
- tokenOut: Address of token to receive
- amount: Amount of tokenIn being swapped
- slippage: Percentage that amount of tokenOut can deviate from the quote by, before reverting
- chain_id: Chain ID of the current network, used for querying 1inch API
-
Set the
PRIVATE_KEY
environment variable (for live networks)export PRIVATE_KEY=0x....
-
To deploy the
SwapProxy
contract on amainnet-fork
brownie run deploy_swap.py --network polygon-fork --interactive
-
To execute a
swap
on an already deployedSwapProxy
contract on amainnet-fork
brownie run swap.py --network polygon-fork --interactive
-
Deploying on
mainnet
is exactly the same, but replacepolygon-fork
withpolygon
-
By default the account used in a forked environment is defined in the
get_acc()
function inswap.py
-
The slippage protection (
minOut
) isn't strictly required in the contract as it is provided in theswap_req["tx"]["data"]
value returned by the 1inch API, however, it is an additional failsafe