Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
chainId: number;
}
interface ItemByChainId {
[chainId: string]: T;
}
enum RequestedAssetType {
ETH = 'ETH', // tslint:disable-line:enum-naming
WETH = 'WETH', // tslint:disable-line:enum-naming
ZRX = 'ZRX', // tslint:disable-line:enum-naming
}
const FIVE_DAYS_IN_MS = 4.32e8; // TODO: make this configurable
const NULL_ADDRESS = '0x0000000000000000000000000000000000000000';
const ZERO = new BigNumber(0);
const ASSET_AMOUNT = new BigNumber(0.1);
export class Handler {
private readonly _chainConfigByChainId: ItemByChainId = {};
private static _createProviderEngine(rpcUrl: string): Web3ProviderEngine {
if (configs.DISPENSER_PRIVATE_KEY === undefined) {
throw new Error('Dispenser Private key not found');
}
const engine = new Web3ProviderEngine();
engine.addProvider(new NonceTrackerSubprovider());
engine.addProvider(new PrivateKeyWalletSubprovider(configs.DISPENSER_PRIVATE_KEY));
engine.addProvider(new RPCSubprovider(rpcUrl));
engine.start();
return engine;
}
constructor() {
const contractAddresses = getContractAddressesForChainOrThrow(chainConfig.chainId);
const order: Order = {
makerAddress: configs.DISPENSER_ADDRESS,
takerAddress: req.params.recipient as string,
makerFee: ZERO,
takerFee: ZERO,
makerAssetAmount,
takerAssetAmount,
makerAssetData,
takerAssetData,
salt: generatePseudoRandomSalt(),
makerFeeAssetData: makerAssetData,
takerFeeAssetData: takerAssetData,
feeRecipientAddress: NULL_ADDRESS,
senderAddress: NULL_ADDRESS,
expirationTimeSeconds: new BigNumber(Date.now() + FIVE_DAYS_IN_MS)
// tslint:disable-next-line:custom-no-magic-numbers
.div(1000)
.integerValue(BigNumber.ROUND_FLOOR),
exchangeAddress: contractAddresses.exchange,
chainId: chainConfig.chainId,
};
const orderHash = orderHashUtils.getOrderHashHex(order);
const signature = await signatureUtils.ecSignHashAsync(
chainConfig.web3Wrapper.getProvider(),
orderHash,
configs.DISPENSER_ADDRESS,
);
const signedOrder: SignedOrder = {
...order,
signature,
};
const etherTokenAddress = contractAddresses.etherToken;
const dummyERC721TokenContract = dummyERC721TokenContracts[0];
if (!dummyERC721TokenContract) {
console.log('No Dummy ERC721 Tokens deployed on this network');
return;
}
// Initialize the Web3Wrapper, this provides helper functions around fetching
// account information, balances, general contract logs
const web3Wrapper = new Web3Wrapper(providerEngine);
const [maker, taker] = await web3Wrapper.getAvailableAddressesAsync();
const printUtils = new PrintUtils(web3Wrapper, contractWrappers, { maker, taker }, { WETH: etherTokenAddress });
printUtils.printAccounts();
// the amount the maker is selling of maker asset: 1 ERC721 Token
const makerAssetAmount = new BigNumber(1);
// the amount the maker is selling of maker asset
const takerAssetAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(0.1), DECIMALS);
const tokenId = generatePseudoRandomSalt();
// 0x v2 uses hex encoded asset data strings to encode all the information needed to identify an asset
const makerAssetData = assetDataUtils.encodeERC721AssetData(dummyERC721TokenContract.address, tokenId);
const takerAssetData = assetDataUtils.encodeERC20AssetData(etherTokenAddress);
let txHash;
// Mint a new ERC721 token for the maker
const mintTxHash = await dummyERC721TokenContract.mint.sendTransactionAsync(maker, tokenId, { from: maker });
await printUtils.awaitTransactionMinedSpinnerAsync('Mint ERC721 Token', mintTxHash);
// Allow the 0x ERC721 Proxy to move ERC721 tokens on behalf of maker
const isApproved = true;
const makerERC721ApprovalTxHash = await dummyERC721TokenContract.setApprovalForAll.validateAndSendTransactionAsync(
contractAddresses.erc721Proxy,
isApproved,
const [maker, taker, sender] = await web3Wrapper.getAvailableAddressesAsync();
const feeRecipientAddress = sender;
const zrxTokenAddress = contractAddresses.zrxToken;
const etherTokenAddress = contractAddresses.etherToken;
const printUtils = new PrintUtils(
web3Wrapper,
contractWrappers,
{ maker, taker, sender },
{ WETH: etherTokenAddress, ZRX: zrxTokenAddress },
);
printUtils.printAccounts();
// the amount the maker is selling of maker asset
const makerAssetAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(5), DECIMALS);
// the amount the maker wants of taker asset
const takerAssetAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(0.1), DECIMALS);
// the amount of fees the maker pays in ZRX
const makerFee = Web3Wrapper.toBaseUnitAmount(new BigNumber(0.01), DECIMALS);
// the amount of fees the taker pays in ZRX
const takerFee = Web3Wrapper.toBaseUnitAmount(new BigNumber(0.01), DECIMALS);
// 0x v2 uses hex encoded asset data strings to encode all the information needed to identify an asset
const makerAssetData = assetDataUtils.encodeERC20AssetData(zrxTokenAddress);
const takerAssetData = assetDataUtils.encodeERC20AssetData(etherTokenAddress);
let txHash;
const zrxToken = new ERC20TokenContract(zrxTokenAddress, providerEngine);
// Approve the ERC20 Proxy to move ZRX for maker
const makerZRXApprovalTxHash = await zrxToken.approve.validateAndSendTransactionAsync(
contractAddresses.erc20Proxy,
UNLIMITED_ALLOWANCE_IN_BASE_UNITS,
{ from: maker },
);
// Initialize the Web3Wrapper, this provides helper functions around fetching
// account information, balances, general contract logs
const web3Wrapper = new Web3Wrapper(providerEngine);
const [maker, taker] = await web3Wrapper.getAvailableAddressesAsync();
const zrxTokenAddress = contractAddresses.zrxToken;
const etherTokenAddress = contractAddresses.etherToken;
const printUtils = new PrintUtils(
web3Wrapper,
contractWrappers,
{ maker, taker },
{ WETH: etherTokenAddress, ZRX: zrxTokenAddress },
);
printUtils.printAccounts();
// the amount the maker is selling of maker asset
const makerAssetAmount = new BigNumber(100);
// the amount the maker wants of taker asset
const takerAssetAmount = new BigNumber(10);
// 0x v2 uses hex encoded asset data strings to encode all the information needed to identify an asset
const makerAssetData = assetDataUtils.encodeERC20AssetData(zrxTokenAddress);
const takerAssetData = assetDataUtils.encodeERC20AssetData(etherTokenAddress);
// Set up the Order and fill it
const randomExpiration = getRandomFutureDateInSeconds();
const exchangeAddress = contractAddresses.exchange;
// Rather than using a random salt, we use an incrementing salt value.
// When combined with cancelOrdersUpTo, all lesser values of salt can be cancelled
// This allows the maker to cancel many orders with one on-chain transaction
// Create the order
const order1: Order = {
const zrxTokenAddress = contractAddresses.zrxToken;
const etherTokenAddress = contractAddresses.etherToken;
const printUtils = new PrintUtils(
web3Wrapper,
contractWrappers,
{ maker, taker },
{ WETH: etherTokenAddress, ZRX: zrxTokenAddress },
);
printUtils.printAccounts();
const makerAssetData = assetDataUtils.encodeERC20AssetData(zrxTokenAddress);
const takerAssetData = assetDataUtils.encodeERC20AssetData(etherTokenAddress);
// the amount the maker is selling of maker asset
const makerAssetAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(5), DECIMALS);
// the amount the maker wants of taker asset
const takerAssetAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(0.1), DECIMALS);
let txHash;
let txReceipt;
const zrxToken = new ERC20TokenContract(zrxTokenAddress, providerEngine);
// Allow the 0x ERC20 Proxy to move ZRX on behalf of makerAccount
const makerZRXApprovalTxHash = await zrxToken.approve.validateAndSendTransactionAsync(
contractAddresses.erc20Proxy,
UNLIMITED_ALLOWANCE_IN_BASE_UNITS,
{ from: maker },
);
await printUtils.awaitTransactionMinedSpinnerAsync('Maker ZRX Approval', makerZRXApprovalTxHash);
// Allow the 0x ERC20 Proxy to move ZRX on behalf of takerAccount
const takerZRXApprovalTxHash = await zrxToken.approve.validateAndSendTransactionAsync(
contractAddresses.erc20Proxy,
{ maker, taker },
{ WETH: etherTokenAddress, ZRX: zrxTokenAddress },
);
printUtils.printAccounts();
// the amount the maker is selling of maker asset (1 set of ERC721 Token and ERC20 tokens)
// when set to 1, this order cannot be partially filled.
const makerAssetAmount = new BigNumber(1);
// the amount the maker wants of taker asset
const takerAssetAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(0.1), DECIMALS);
// Generate a random token id
const tokenId = generatePseudoRandomSalt();
// 0x v2 uses hex encoded asset data strings to encode all the information needed to identify an asset
const erc721AssetData = assetDataUtils.encodeERC721AssetData(dummyERC721TokenContract.address, tokenId);
const erc20AssetData = assetDataUtils.encodeERC20AssetData(zrxTokenAddress);
const erc20AssetAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(0.2), DECIMALS);
// combine both the ERC721 asset data and the ERC20 asset data into multi asset data
// the order is exchanging 1 ERC721 and 0.2 ZRX in exchange for 0.1 WETH
const makerAssetData = assetDataUtils.encodeMultiAssetData(
[makerAssetAmount, erc20AssetAmount],
[erc721AssetData, erc20AssetData],
);
const takerAssetData = assetDataUtils.encodeERC20AssetData(etherTokenAddress);
let txHash;
const zrxToken = new ERC20TokenContract(zrxTokenAddress, providerEngine);
// Mint a new ERC721 token for the maker
const mintTxHash = await dummyERC721TokenContract.mint.validateAndSendTransactionAsync(maker, tokenId, {
from: maker,
});
await printUtils.awaitTransactionMinedSpinnerAsync('Mint ERC721 Token', mintTxHash);
const web3Wrapper = new Web3Wrapper(providerEngine);
const [maker, taker] = await web3Wrapper.getAvailableAddressesAsync();
const zrxTokenAddress = contractAddresses.zrxToken;
const etherTokenAddress = contractAddresses.etherToken;
const printUtils = new PrintUtils(
web3Wrapper,
contractWrappers,
{ maker, taker },
{ WETH: etherTokenAddress, ZRX: zrxTokenAddress },
);
printUtils.printAccounts();
// the amount the maker is selling of maker asset
const makerAssetAmount = new BigNumber(100);
// the amount the maker wants of taker asset
const takerAssetAmount = new BigNumber(10);
// 0x v2 uses hex encoded asset data strings to encode all the information needed to identify an asset
const makerAssetData = assetDataUtils.encodeERC20AssetData(zrxTokenAddress);
const takerAssetData = assetDataUtils.encodeERC20AssetData(etherTokenAddress);
// Set up the Order and fill it
const randomExpiration = getRandomFutureDateInSeconds();
const exchangeAddress = contractAddresses.exchange;
// Rather than using a random salt, we use an incrementing salt value.
// When combined with cancelOrdersUpTo, all lesser values of salt can be cancelled
// This allows the maker to cancel many orders with one on-chain transaction
// Create the order
const order1: Order = {
exchangeAddress,
makerAddress: maker,
const printUtils = new PrintUtils(
web3Wrapper,
contractWrappers,
{ maker, taker, feeRecipient },
{ WETH: etherTokenAddress, ZRX: zrxTokenAddress },
);
printUtils.printAccounts();
// the amount the maker is selling in maker asset
const makerAssetAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(5), DECIMALS);
// the amount the maker wants of taker asset
const takerAssetAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(0.1), DECIMALS);
// the amount of fees the maker pays in ZRX
const makerFee = Web3Wrapper.toBaseUnitAmount(new BigNumber(0.01), DECIMALS);
// the amount of fees the taker pays in ZRX
const takerFee = Web3Wrapper.toBaseUnitAmount(new BigNumber(0.01), DECIMALS);
// 0x v2 uses hex encoded asset data strings to encode all the information needed to identify an asset
const makerAssetData = assetDataUtils.encodeERC20AssetData(zrxTokenAddress);
const takerAssetData = assetDataUtils.encodeERC20AssetData(etherTokenAddress);
let txHash;
let txReceipt;
const zrxToken = new ERC20TokenContract(zrxTokenAddress, providerEngine);
// Approve the ERC20 Proxy to move ZRX for maker and taker
const makerZRXApprovalTxHash = await zrxToken.approve.validateAndSendTransactionAsync(
contractAddresses.erc20Proxy,
UNLIMITED_ALLOWANCE_IN_BASE_UNITS,
{ from: maker },
);
await printUtils.awaitTransactionMinedSpinnerAsync('Maker ZRX Approval', makerZRXApprovalTxHash);
const takerZRXApprovalTxHash = await zrxToken.approve.validateAndSendTransactionAsync(
const web3Wrapper = new Web3Wrapper(providerEngine);
const [maker, taker] = await web3Wrapper.getAvailableAddressesAsync();
const zrxTokenAddress = contractAddresses.zrxToken;
const etherTokenAddress = contractAddresses.etherToken;
const printUtils = new PrintUtils(
web3Wrapper,
contractWrappers,
{ maker, taker },
{ WETH: etherTokenAddress, ZRX: zrxTokenAddress },
);
printUtils.printAccounts();
// the amount the maker is selling of maker asset
const makerAssetAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(5), DECIMALS);
// the amount the maker wants of taker asset
const takerAssetAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(0.1), DECIMALS);
// 0x v2 uses hex encoded asset data strings to encode all the information needed to identify an asset
const makerAssetData = assetDataUtils.encodeERC20AssetData(zrxTokenAddress);
const takerAssetData = assetDataUtils.encodeERC20AssetData(etherTokenAddress);
let txHash;
let txReceipt;
const zrxToken = new ERC20TokenContract(zrxTokenAddress, providerEngine);
// Allow the 0x ERC20 Proxy to move ZRX on behalf of makerAccount
const makerZRXApprovalTxHash = await zrxToken.approve.validateAndSendTransactionAsync(
contractAddresses.erc20Proxy,
UNLIMITED_ALLOWANCE_IN_BASE_UNITS,
{ from: maker },
);
await printUtils.awaitTransactionMinedSpinnerAsync('Maker ZRX Approval', makerZRXApprovalTxHash);
// With the Forwarding contract, the taker requires no set up
PrintUtils.printData('Setup', [['Maker ZRX Approval', makerZRXApprovalTxHash]]);