-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
91 lines (71 loc) · 3.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const Web3 = require('web3');
const {ZeroEx} = require('0x.js');
const BigNumber = require('bignumber.js');
// Provider pointing to local TestRPC on default port 8545
const provider = new Web3.providers.HttpProvider('http://localhost:8545');
// Instantiate 0x.js instance
const zeroEx = new ZeroEx(provider);
// Number of decimals to use (for ETH and ZRX)
const DECIMALS = 18;
const mainAsync = async () => {
// Addresses
const WETH_ADDRESS = await zeroEx.etherToken.getContractAddressAsync(); // The wrapped ETH token contract
const ZRX_ADDRESS = await zeroEx.exchange.getZRXTokenAddressAsync(); // The ZRX token contract
// The Exchange.sol address (0x exchange smart contract)
const EXCHANGE_ADDRESS = await zeroEx.exchange.getContractAddressAsync();
// Getting list of accounts
const accounts = await zeroEx.getAvailableAddressesAsync();
console.log('accounts: ', accounts);
// Set our addresses
const [makerAddress, takerAddress] = accounts;
// Unlimited allowances to 0x proxy contract for maker and taker
const setMakerAllowTxHash = await zeroEx.token.setUnlimitedProxyAllowanceAsync(ZRX_ADDRESS, makerAddress);
await zeroEx.awaitTransactionMinedAsync(setMakerAllowTxHash);
console.log('Maker allowance mined...');
const setTakerAllowTxHash = await zeroEx.token.setUnlimitedProxyAllowanceAsync(WETH_ADDRESS, takerAddress);
await zeroEx.awaitTransactionMinedAsync(setTakerAllowTxHash);
console.log('Taker allowance mined...');
// Deposit WETH
const ethAmount = new BigNumber(1);
const ethToConvert = ZeroEx.toBaseUnitAmount(ethAmount, DECIMALS); // Number of ETH to convert to WETH
const convertEthTxHash = await zeroEx.etherToken.depositAsync(ethToConvert, takerAddress);
await zeroEx.awaitTransactionMinedAsync(convertEthTxHash);
console.log(`${ethAmount} ETH -> WETH conversion mined...`);
// Generate order
const order = {
maker: makerAddress,
taker: ZeroEx.NULL_ADDRESS,
feeRecipient: ZeroEx.NULL_ADDRESS,
makerTokenAddress: ZRX_ADDRESS,
takerTokenAddress: WETH_ADDRESS,
exchangeContractAddress: EXCHANGE_ADDRESS,
salt: ZeroEx.generatePseudoRandomSalt(),
makerFee: new BigNumber(0),
takerFee: new BigNumber(0),
makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(0.2), DECIMALS), // Base 18 decimals
takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(0.3), DECIMALS), // Base 18 decimals
expirationUnixTimestampSec: new BigNumber(Date.now() + 3600000), // Valid for up to an hour
};
// Create orderHash
const orderHash = ZeroEx.getOrderHashHex(order);
// Signing orderHash -> ecSignature
const ecSignature = await zeroEx.signOrderHashAsync(orderHash, makerAddress);
// Appending signature to order
const signedOrder = {
...order,
ecSignature,
};
// Verify that order is fillable
await zeroEx.exchange.validateOrderFillableOrThrowAsync(signedOrder);
// Try to fill order
const shouldThrowOnInsufficientBalanceOrAllowance = true;
const fillTakerTokenAmount = ZeroEx.toBaseUnitAmount(new BigNumber(0.1), DECIMALS);
// Filling order
const txHash = await zeroEx.exchange.fillOrderAsync(
signedOrder, fillTakerTokenAmount, shouldThrowOnInsufficientBalanceOrAllowance, takerAddress,
);
// Transaction receipt
const txReceipt = await zeroEx.awaitTransactionMinedAsync(txHash);
console.log('FillOrder transaction receipt: ', txReceipt);
};
mainAsync().catch(err => console.log);