Created
October 14, 2017 14:20
-
-
Save seanwbren/7ba693ec55615d202f321a4733182062 to your computer and use it in GitHub Desktop.
0x tutorial
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Web3 = require('web3'); | |
const ZeroEx = require('0x.js').ZeroEx; | |
const BigNumber = require('bignumber.js'); | |
// Default provider for TestRPC | |
const provider = new Web3.providers.HttpProvider('http://localhost:8545') | |
// Instantiate 0x.js instance | |
const zeroEx = new ZeroEx(provider); | |
console.log("zeroEx instantiated as" + zeroEx) | |
(async () => { | |
// Number of decimals to use (for ETH and ZRX) | |
const DECIMALS = 18; | |
// Addresses | |
const NULL_ADDRESS = ZeroEx.NULL_ADDRESS; // Ethereum Null address | |
const WETH_ADDRESS = await zeroEx.etherToken.getContractAddressAsync(); // The wrapped ETH token contract | |
console.log("WETH address is " + WETH_ADDRESS) | |
const ZRX_ADDRESS = await zeroEx.exchange.getZRXTokenAddressAsync(); // The ZRX token contract | |
const EXCHANGE_ADDRESS = await zeroEx.exchange.getContractAddressAsync(); // The Exchange.sol address (0x exchange smart contract) | |
// Getting list of accounts | |
const accounts = await zeroEx.getAvailableAddressesAsync(); | |
console.log("ACCOUNTS ARE " + accounts); | |
// Set our address | |
const [makerAddress, takerAddress] = accounts; | |
// Unlimited allowances to 0x contract for maker and taker | |
const txHashAllowMaker = await zeroEx.token.setUnlimitedProxyAllowanceAsync(ZRX_ADDRESS, makerAddress); | |
await zeroEx.awaitTransactionMinedAsync(txHashAllowMaker); | |
console.log('Maker Allowance Mined...'); | |
const txHashAllowTaker = await zeroEx.token.setUnlimitedProxyAllowanceAsync(WETH_ADDRESS, takerAddress); | |
await zeroEx.awaitTransactionMinedAsync(txHashAllowTaker); | |
console.log('takerFee Allowance Mined...'); | |
// Deposit WETH | |
const ethToConvert = ZeroEx.toBaseUnitAmount(new BigNumber(1), DECIMALS); // Number of ETH to convert to WETH | |
const txHashWETH = await zeroEx.etherToken.depositAsync(ethToConvert, takerAddress); | |
await zeroEx.awaitTransactionMinedAsync(txHashWETH); | |
console.log('ETH -> WETH Mined...'); | |
// Generate order | |
console.log('Generating order') | |
const order = { | |
maker: makerAddress, | |
taker: NULL_ADDRESS, | |
feeRecipient: 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 up to an hour | |
}; | |
// Create orderHash | |
const orderHash = ZeroEx.getOrderHashHex(order); | |
console.log("orderHash is " + orderHash) | |
// Signing orderHash -> ecSignature | |
const ecSignature = await zeroEx.signOrderHashAsync(orderHash, makerAddress); | |
console.log("ecSignature is " + ecSignature) | |
// Appending signature to order | |
const signedOrder = { | |
...order, | |
ecSignature, | |
}; | |
// Verify if order is fillable | |
await zeroEx.exchange.validateOrderFillableOrThrowAsync(signedOrder); | |
// Try to fill order | |
const shouldThrowOnInsufficientBalanceOrAllowance = true; | |
const fillTakerTokenAmount = ZeroEx.toBaseUnitAmount(new BigNumber(0.1), DECIMALS); | |
// Try filling order | |
console.log("about to fill order") | |
const txHash = await zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerTokenAmount, | |
shouldThrowOnInsufficientBalanceOrAllowance, takerAddress,); | |
// Transaction Receipt | |
const txReceipt = await zeroEx.awaitTransactionMinedAsync(txHash); | |
console.log(txReceipt.logs); | |
})().catch(console.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment