This is a repo containing toy examples of filling 0x-API quotes either directly with web3 or through a smart contract.
Clone this repo then, from inside the project, run:
yarn -D
# or
npm install --dev
This will also compile the contracts in contracts/
and produce artifacts in build/
.
The following example scripts are included:
Script | Guide | Description |
---|---|---|
src/direct-swap.js |
Swap tokens with 0x API | Perform a token swap with web3. |
src/swap-contract.js |
Use 0x API liquidity in your smart contracts | Perform a token swap in a smart contract. |
The examples can be run locally (without actually mining transactions) through the magic of ganache forking. You will first need to start a forked ganache instance with the following command, replacing ETHEREUM_RPC_URL
with the HTTP or websocket RPC URL of your mainnet ethereum node (e.g., Infura mainnet):
RPC_URL=ETHEREUM_RPC_URL npm run start-fork
To run the direct swap example, in a separate terminal run:
npm run swap-fork
To run the contract swap example, in a seperate terminal run:
npm run deploy-fork # Only need to do this once per ganache instance.
npm run swap-contract-fork
You can also run the examples and perform actual swaps that will get mined with a little effort:
- Modify the
mnemonic
inpackage.json
to one only you know. Do not use the default mnemonic on mainnet! - Fund the first HD wallet account associated with that mnemonic with some ETH. You can run
npm run print-hd-wallet-accounts
to list the addresses associated with the configured mnemonic.
To run the direct swap example live, run the following:
# Replace ETHEREUM_RPC_URL with your mainnet node RPC endpoint.
RPC_URL=ETHEREUM_RPC_URL npm run swap-live
# You can also configure how much WETH is sold with the -a option, e.g.
RPC_URL=ETHEREUM_RPC_URL npm run swap-live -a 0.1
To run the contract swap example live, first deploy the contract to mainnet.
# Replace ETHEREUM_RPC_URL with your mainnet node RPC endpoint.
RPC_URL=ETHEREUM_RPC_URL npm run deploy-live --network main
Note the address to which the contract has been deployed. You can now run the script to perform the swap.
# Replace ETHEREUM_RPC_URL with your mainnet node RPC endpoint
# and CONTRACT_ADDRESS with the deployed address of the contract.
RPC_URL=ETHEREUM_RPC_URL npm run swap-contract-live CONTRACT_ADDRESS
# You can also configure how much WETH is sold with the -a option, e.g.
RPC_URL=ETHEREUM_RPC_URL npm run swap-contract-live -a 0.1 CONTRACT_ADDRESS
Keep in mind that tokens will remain in the contract after the swap and can only be retrieved by your first HD wallet account through withdrawToken()
or withdrawETH()
.
- Refer to our 0x API specification for detailed documentation.
- 0x API is open source! Look through the codebase and deploy your own 0x API instance.
- Don’t hesitate to reach out on Discord for help! The 0x Core team is active on Discord to help teams building with all things 0x.