Created
November 28, 2018 09:58
-
-
Save derekalia/806e26123fb28ff4c5044cb7d0556f05 to your computer and use it in GitHub Desktop.
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, ZERO } from './constants'; | |
import { getContractAddressesForNetwork, getContractWrappersConfig } from './contracts'; | |
import { PrintUtils } from './print_utils'; | |
import { providerEngine } from './provider'; | |
import { getRandomFutureDateInSeconds } from './utils'; | |
import wallet from 'ethereumjs-wallet'; | |
import EthereumTx from 'ethereumjs-tx'; | |
import Web3 from 'web3'; | |
import abi from './TribeToken'; | |
/** | |
* In this scenario, the maker creates and signs an order for selling ZRX for WETH. This | |
* order is then submitted to a Relayer via the Standard Relayer API. A Taker queries | |
* this Standard Relayer API to discover orders. | |
* The taker fills this order via the 0x Exchange contract. | |
*/ | |
export async function scenarioAsync() { | |
PrintUtils.printScenario('Create Order for ERC721'); | |
// Initialize the ContractWrappers, this provides helper functions around calling | |
// 0x contracts as well as ERC20/ERC721 token contracts on the blockchain | |
const maker = '0x440639802dc08081888fe71243ebf574a2031174'; | |
const taker = '0x6265651aac0ecfca6564ee6f0ee2cc8127333c82'; | |
var web3 = new Web3('https://kovan.infura.io/'); | |
const web3Wrapper = new Web3Wrapper(providerEngine); | |
const contractWrappers = new ContractWrappers(providerEngine, getContractWrappersConfig(NETWORK_CONFIGS.networkId)); | |
const contractAddresses = getContractAddressesForNetwork(NETWORK_CONFIGS.networkId); | |
const etherTokenAddress = contractAddresses.etherToken; //wethAddress | |
//set contract | |
//let dummyERC721TokenContract = new web3.eth.Contract(abi, '0x860b40f6f1c49b76a0955d8ea3e2d038e5c83389'); | |
let dummyERC721TokenContract = new web3.eth.Contract(abi, '0xac1b890718a7a9968a9008a252fb42fe655946f8'); | |
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 wants of taker asset | |
const takerAssetAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(0.1), DECIMALS); | |
console.log(dummyERC721TokenContract._address); | |
// // 0x v2 uses hex encoded asset data strings to encode all the information needed to identify an asset | |
const takerAssetData = assetDataUtils.encodeERC20AssetData(etherTokenAddress); | |
//create new token | |
// const privateKey = Buffer.from('3e636698ff146f06701bdb8c6237f3067119a89ac0353f1972c3a9d223c00f71', 'hex'); | |
// const moneyWallet = wallet.fromPrivateKey(privateKey); | |
// let txCount = await web3.eth.getTransactionCount(moneyWallet.getAddressString()); | |
// const createContractData = dummyERC721TokenContract.methods.create('derek', 'hi/metaTribe').encodeABI(); | |
// let cost = 1 / 200; //lets say eth is $200 | |
// // console.log({ cost }); | |
// const txData = { | |
// nonce: web3.utils.toHex(txCount), | |
// from: moneyWallet.getAddressString(), | |
// to: dummyERC721TokenContract._address, //Token contract factory | |
// gasPrice: web3.utils.toHex(10e9), // 10 Gwei | |
// data: createContractData, | |
// chainId: 42, | |
// gasLimit: 500000 | |
// }; | |
// const tx = new EthereumTx(txData); | |
// tx.sign(moneyWallet.getPrivateKey()); | |
// let mintTxHash = await web3.eth.sendSignedTransaction('0x' + tx.serialize().toString('hex')); | |
// let num = parseInt(mintTxHash.logs[0].topics[3], 16); | |
const tokenId = new BigNumber(4); //hardcoding tokenId for now | |
// 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 makerERC721ApprovalTxHash = await contractWrappers.erc721Token.setProxyApprovalForAllAsync( | |
dummyERC721TokenContract._address, | |
maker, | |
true | |
); | |
await printUtils.awaitTransactionMinedSpinnerAsync('Maker ERC721 Approval', makerERC721ApprovalTxHash); | |
// Set up the Order and fill it | |
const randomExpiration = new BigNumber(1543445741); | |
const exchangeAddress = contractAddresses.exchange; | |
// Create the order | |
const orderConfigRequest = { | |
exchangeAddress, | |
makerAddress: maker, | |
takerAddress: NULL_ADDRESS, | |
senderAddress: NULL_ADDRESS, | |
feeRecipientAddress: NULL_ADDRESS, | |
expirationTimeSeconds: randomExpiration, | |
salt: generatePseudoRandomSalt(), | |
makerAssetAmount, | |
takerAssetAmount, | |
makerAssetData, | |
takerAssetData, | |
makerFee: ZERO, | |
takerFee: ZERO | |
}; | |
printUtils.printOrder(orderConfigRequest); | |
await printUtils.fetchAndPrintContractAllowancesAsync(); | |
await printUtils.fetchAndPrintContractBalancesAsync(); | |
await printUtils.fetchAndPrintERC721OwnerAsync(dummyERC721TokenContract._address, tokenId); | |
// Initialize the Standard Relayer API client | |
const httpClient = new HttpClient('http://localhost:3001/v2/'); | |
const orderConfig = await httpClient.getOrderConfigAsync(orderConfigRequest, { | |
networkId: NETWORK_CONFIGS.networkId | |
}); | |
// Create the order | |
const order = { | |
...orderConfigRequest, | |
...orderConfig | |
}; | |
// Generate the order hash and sign it | |
const orderHashHex = orderHashUtils.getOrderHashHex(order); | |
const signature = await signatureUtils.ecSignHashAsync(providerEngine, orderHashHex, maker); | |
const signedOrder = { ...order, signature }; | |
// Validate this order | |
await contractWrappers.exchange.validateOrderFillableOrThrowAsync(signedOrder); | |
// Submit the order to the SRA Endpoint | |
await httpClient.submitOrderAsync(signedOrder, { networkId: NETWORK_CONFIGS.networkId }); | |
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