Created
November 27, 2018 22:40
-
-
Save derekalia/3064857d870de002fc47b2eb6ec49539 to your computer and use it in GitHub Desktop.
0x-fill-test
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
import { | |
assetDataUtils, | |
BigNumber, | |
ContractWrappers, | |
generatePseudoRandomSalt, | |
orderHashUtils, | |
signatureUtils | |
} from '0x.js'; | |
import { HttpClient } from '@0x/connect'; | |
import { Web3Wrapper } from '@0x/web3-wrapper'; | |
import { NETWORK_CONFIGS, TX_DEFAULTS } from './configs'; | |
import { DECIMALS, NULL_ADDRESS } from './constants'; | |
import { getContractAddressesForNetwork, getContractWrappersConfig } from './contracts'; | |
import { PrintUtils } from './print_utils'; | |
import { providerEngine } from './provider'; | |
export async function scenarioAsync() { | |
PrintUtils.printScenario('Fill Order Standard Relayer API'); | |
const contractWrappers = new ContractWrappers(providerEngine, getContractWrappersConfig(NETWORK_CONFIGS.networkId)); | |
const maker = '0x440639802dc08081888fe71243ebf574a2031174'; | |
const taker = '0x6265651aac0ecfca6564ee6f0ee2cc8127333c82'; | |
// console.log(contractWrappers.forwarder._web3Wrapper._provider); | |
const contractAddresses = getContractAddressesForNetwork(NETWORK_CONFIGS.networkId); //<----would be nice to have | |
const zrxTokenAddress = contractAddresses.zrxToken; // zrxAddress | |
const etherTokenAddress = contractAddresses.etherToken; //contractAddresses.etherToken; //wethAddress | |
const makerAssetData = assetDataUtils.encodeERC20AssetData(zrxTokenAddress); | |
const takerAssetData = assetDataUtils.encodeERC20AssetData(etherTokenAddress); | |
// the amount the maker wants of taker asset | |
const takerAssetAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(0.1), DECIMALS); | |
const web3Wrapper = new Web3Wrapper(providerEngine); | |
let txHash; | |
let txReceipt; | |
const printUtils = new PrintUtils( | |
web3Wrapper, | |
contractWrappers, | |
{ maker, taker }, | |
{ WETH: etherTokenAddress, ZRX: zrxTokenAddress } | |
); | |
printUtils.printAccounts(); | |
// Allow the 0x ERC20 Proxy to move WETH on behalf of takerAccount | |
const takerWETHApprovalTxHash = await contractWrappers.erc20Token.setUnlimitedProxyAllowanceAsync( | |
etherTokenAddress, | |
taker | |
); | |
// Convert ETH into WETH for taker by depositing ETH into the WETH contract | |
const takerWETHDepositTxHash = await contractWrappers.etherToken.depositAsync( | |
etherTokenAddress, | |
takerAssetAmount, | |
taker | |
); | |
await printUtils.fetchAndPrintContractBalancesAsync(); | |
// Initialize the Standard Relayer API client | |
const httpClient = new HttpClient('http://localhost:3001/v2/'); | |
// Taker queries the Orderbook from the Relayer | |
const orderbookRequest = { baseAssetData: makerAssetData, quoteAssetData: takerAssetData }; | |
const response = await httpClient.getOrderbookAsync(orderbookRequest, { networkId: NETWORK_CONFIGS.networkId }); | |
if (response.asks.total === 0) { | |
throw new Error('No orders found on the SRA Endpoint'); | |
} | |
const sraOrder = response.asks.records[1].order; | |
printUtils.printOrder(sraOrder); | |
console.log('sraOrder:', JSON.stringify(sraOrder)); | |
console.log({ takerAssetAmount }); | |
console.log({ taker }); | |
// Validate the order is Fillable given the maker and taker balances | |
let validateOrder = await contractWrappers.exchange.validateFillOrderThrowIfInvalidAsync( | |
sraOrder, | |
takerAssetAmount, | |
taker | |
); | |
// Fill the Order via 0x Exchange contract | |
txHash = await contractWrappers.exchange.fillOrderAsync(sraOrder, takerAssetAmount, taker, { | |
gasLimit: TX_DEFAULTS.gas | |
}); | |
txReceipt = await printUtils.awaitTransactionMinedSpinnerAsync('fillOrder', txHash); | |
printUtils.printTransaction('fillOrder', txReceipt, [['orderHash', orderHashHex]]); | |
// console.log({ txReceipt }); | |
// Print the Balances | |
await printUtils.fetchAndPrintContractBalancesAsync(); | |
// Stop the Provider Engine | |
providerEngine.stop(); | |
} | |
void (async () => { | |
try { | |
if (!module.parent) { | |
await scenarioAsync(); | |
} | |
} catch (e) { | |
console.log(e); | |
providerEngine.stop(); | |
process.exit(1); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment