Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.

Commit ef8d7a0

Browse files
committed
switch to JS
use truffle and web3 instead of @0x tooling remove margin trading example
1 parent 8041207 commit ef8d7a0

29 files changed

+8287
-16986
lines changed

.env.example

-3
This file was deleted.

.gitignore

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
generated-artifacts/*.json
2-
generated-wrappers/*.ts
3-
ganache.log
1+
build/*
2+
**.log
43
node_modules
5-
.env

LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 ZeroEx Labs
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+65-24
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,82 @@
22

33
# Get started with 0x API
44

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.
86

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. |
1224

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
1536
```
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
1743
```
1844

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
1958
```
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
2166
```
2267

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.
2469

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()`.
2779

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-
3480
## Need help?
3581
* Refer to our [0x API specification](https://0x.org/docs/api) for detailed documentation.
3682
* 0x API is open source! Look through the [codebase](https://github.com/0xProject/0x-api) and deploy your own 0x API instance.
3783
* 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

compiler.json

-25
This file was deleted.

contracts/Migrations.sol

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity >=0.4.22 <0.8.0;
3+
4+
// This contract has to exist or else truffle will complain.
5+
contract Migrations {
6+
address public owner = msg.sender;
7+
uint public last_completed_migration;
8+
9+
modifier restricted() {
10+
require(
11+
msg.sender == owner,
12+
"This function is restricted to the contract's owner"
13+
);
14+
_;
15+
}
16+
17+
function setCompleted(uint completed) public restricted {
18+
last_completed_migration = completed;
19+
}
20+
}

contracts/SimpleMarginTrading.sol

-202
This file was deleted.

0 commit comments

Comments
 (0)