Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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);
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(WETH_ADDRESS, 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),
const convertEthTxHash = await zeroEx.etherToken.depositAsync(WETH_ADDRESS, 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 shouldAddPersonalMessagePrefix = false;
const ecSignature = await zeroEx.signOrderHashAsync(orderHash, makerAddress, shouldAddPersonalMessagePrefix);
// Appending signature to order
const signedOrder = {
...order,
ecSignature,
};
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 shouldAddPersonalMessagePrefix = false;
const ecSignature = await zeroEx.signOrderHashAsync(orderHash, makerAddress, shouldAddPersonalMessagePrefix);
// Appending signature to order
const signedOrder = {
...order,
ecSignature,
};
// Signing orderHash -> ecSignature
const shouldAddPersonalMessagePrefix = false;
const ecSignature = await zeroEx.signOrderHashAsync(orderHash, makerAddress, shouldAddPersonalMessagePrefix);
// 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);
};
protected async _processNextRequestFireAndForgetAsync(recipientAddress: string) {
utils.consoleLog(`Processing ZRX ${recipientAddress}`);
const baseUnitAmount = ZeroEx.toBaseUnitAmount(DISPENSE_AMOUNT_ZRX, 18);
try {
const zrxTokenAddress = this._zeroEx.exchange.getZRXTokenAddress();
const txHash = await this._zeroEx.token.transferAsync(
zrxTokenAddress,
configs.DISPENSER_ADDRESS,
recipientAddress,
baseUnitAmount,
);
utils.consoleLog(`Sent ${DISPENSE_AMOUNT_ZRX} ZRX to ${recipientAddress} tx: ${txHash}`);
} catch (err) {
utils.consoleLog(`Unexpected err: ${err} - ${JSON.stringify(err)}`);
await errorReporter.reportAsync(err);
}
}
}