|
2 | 2 |
|
3 | 3 | # Get started with 0x API
|
4 | 4 |
|
5 |
| -This is a repo containing code snippets to interact with 0x API through smart contracts |
6 |
| -or with web3. A number of the code snippets have corresponding guides that provides |
7 |
| -color to what is happening in the snippets. |
| 5 | +This is a repo containing toy examples of filling 0x-API quotes either directly with web3 or through a smart contract. |
8 | 6 |
|
9 |
| -## Finding finished code snippets |
10 |
| -On `master` branch all of the snippets are empty and will require following the guides |
11 |
| -to complete them; to find the finished snippets, check out the `finished` branch. |
| 7 | +## Installation |
| 8 | +Clone this repo then, from inside the project, run: |
| 9 | +```bash |
| 10 | +yarn -D |
| 11 | +# or |
| 12 | +npm install --dev |
| 13 | +``` |
| 14 | + |
| 15 | +This will also compile the contracts in `contracts/` and produce artifacts in `build/`. |
| 16 | + |
| 17 | +## Examples |
| 18 | +The following example scripts are included: |
| 19 | + |
| 20 | +| Script | Guide | Description | |
| 21 | +|--------|-------|-------------| |
| 22 | +| `src/direct-swap.js` | [Swap tokens with 0x API](https://0x.org/docs/guides/swap-tokens-with-0x-api) | Perform a token swap with web3. | |
| 23 | +| `src/swap-contract.js` | [Use 0x API liquidity in your smart contracts](https://0x.org/docs/guides/use-0x-api-liquidity-in-your-smart-contracts) | Perform a token swap in a smart contract. | |
12 | 24 |
|
13 |
| -## Running the snippets |
14 |
| -Before running the scripts run: |
| 25 | +### Running the examples locally (forked mainnet) |
| 26 | +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): |
| 27 | + |
| 28 | +```bash |
| 29 | +RPC_URL=ETHEREUM_RPC_URL npm run start-fork |
| 30 | +``` |
| 31 | + |
| 32 | +#### Direct swap |
| 33 | +To run the direct swap example, in a separate terminal run: |
| 34 | +```bash |
| 35 | +npm run swap-fork |
15 | 36 | ```
|
16 |
| -$ yarn |
| 37 | + |
| 38 | +#### Contract swap |
| 39 | +To run the contract swap example, in a seperate terminal run: |
| 40 | +```bash |
| 41 | +npm run deploy-fork # Only need to do this once per ganache instance. |
| 42 | +npm run swap-contract-fork |
17 | 43 | ```
|
18 | 44 |
|
| 45 | +### Running the examples on mainnet |
| 46 | +You can also run the examples and perform actual swaps that will get mined with a little effort: |
| 47 | + |
| 48 | +1. Modify the `mnemonic` in `package.json` to one only you know. |
| 49 | +2. 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. |
| 50 | + |
| 51 | +#### Direct swap |
| 52 | +To run the direct swap example *live*, run the following: |
| 53 | +```bash |
| 54 | +# Replace ETHEREUM_RPC_URL with your mainnet node RPC endpoint. |
| 55 | +RPC_URL=ETHEREUM_RPC_URL npm run swap-live |
| 56 | +# You can also configure how much WETH is sold with the -a option, e.g. |
| 57 | +RPC_URL=ETHEREUM_RPC_URL npm run swap-live -a 0.1 |
19 | 58 | ```
|
20 |
| -$ yarn build:contracts |
| 59 | + |
| 60 | +#### Contract swap |
| 61 | +To run the contract swap example *live*, first deploy the contract to mainnet. |
| 62 | + |
| 63 | +```bash |
| 64 | +# Replace ETHEREUM_RPC_URL with your mainnet node RPC endpoint. |
| 65 | +RPC_URL=ETHEREUM_RPC_URL npm run deploy-live --network main |
21 | 66 | ```
|
22 | 67 |
|
23 |
| -This codebase uses `ts-node` to run the typescript code-snippets. |
| 68 | +Note the address to which the contract has been deployed. You can now run the script to perform the swap. |
24 | 69 |
|
25 |
| -## Snippets |
26 |
| -All code snippets can be found in either `examples/` or `contracts/` (dependent to if there is any smart contract code needed for the snippet) |
| 70 | +```bash |
| 71 | +# Replace ETHEREUM_RPC_URL with your mainnet node RPC endpoint |
| 72 | +# and CONTRACT_ADDRESS with the deployed address of the contract. |
| 73 | +RPC_URL=ETHEREUM_RPC_URL npm run swap-contract-live CONTRACT_ADDRESS |
| 74 | +# You can also configure how much WETH is sold with the -a option, e.g. |
| 75 | +RPC_URL=ETHEREUM_RPC_URL npm run swap-contract-live -a 0.1 CONTRACT_ADDRESS |
| 76 | +``` |
| 77 | + |
| 78 | +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()`. |
27 | 79 |
|
28 |
| -| Main code script in `examples/` | Corresponding Guide | Description | |
29 |
| -| ------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------ | |
30 |
| -| `web3_simple_token_swap.ts` | [Swap tokens with 0x API](https://0x.org/docs/guides/swap-tokens-with-0x-api) | Perform a token swap with web3. | |
31 |
| -| `smart_contract_token_swap.ts` | [Use 0x API liquidity in your smart contracts](https://0x.org/docs/guides/use-0x-api-liquidity-in-your-smart-contracts) | Perform a token swap in a smart contract. | |
32 |
| -| `smart_contract_margin_trading.ts` | [Develop a margin trading smart contract with 0x API](https://0x.org/docs/guides/develop-a-margin-trading-smart-contract-with-0x-api) | Develop a margin trading contract with Compound Finance + 0x. | |
33 |
| - |
34 | 80 | ## Need help?
|
35 | 81 | * Refer to our [0x API specification](https://0x.org/docs/api) for detailed documentation.
|
36 | 82 | * 0x API is open source! Look through the [codebase](https://github.com/0xProject/0x-api) and deploy your own 0x API instance.
|
37 | 83 | * Don’t hesitate to reach out on [Discord](https://discordapp.com/invite/d3FTX3M) for help! The 0x Core team is active on Discord to help teams building with all things 0x.
|
38 |
| - |
39 |
| -## Legal Disclaimer |
40 |
| - |
41 |
| -The laws and regulations applicable to the use and exchange of digital assets and blockchain-native tokens, including through any software developed using the licensed work created by ZeroEx Intl. as described here (the “Work”), vary by jurisdiction. As set forth in the Apache License, Version 2.0 applicable to the Work, developers are “solely responsible for determining the appropriateness of using or redistributing the Work,” which includes responsibility for ensuring compliance with any such applicable laws and regulations. |
42 |
| -See the Apache License, Version 2.0 for the specific language governing all applicable permissions and limitations: http://www.apache.org/licenses/LICENSE-2.0 |
|
0 commit comments