diff options
author | Fabio Berger <me@fabioberger.com> | 2018-10-16 23:59:02 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2018-10-16 23:59:02 +0800 |
commit | c84c92663d1ce0227b755dc861f825c35a3c7999 (patch) | |
tree | 3800297ba0072233fe88839b675e33e6b44f9649 /packages/migrations/src/migration.ts | |
parent | 55a3bc8cb6772802672f60f22c5ed5c7e1b2dfdd (diff) | |
parent | c333d093b585fa0250a6973f2d396eb3cf227334 (diff) | |
download | dexon-sol-tools-c84c92663d1ce0227b755dc861f825c35a3c7999.tar dexon-sol-tools-c84c92663d1ce0227b755dc861f825c35a3c7999.tar.gz dexon-sol-tools-c84c92663d1ce0227b755dc861f825c35a3c7999.tar.bz2 dexon-sol-tools-c84c92663d1ce0227b755dc861f825c35a3c7999.tar.lz dexon-sol-tools-c84c92663d1ce0227b755dc861f825c35a3c7999.tar.xz dexon-sol-tools-c84c92663d1ce0227b755dc861f825c35a3c7999.tar.zst dexon-sol-tools-c84c92663d1ce0227b755dc861f825c35a3c7999.zip |
Merge branch 'dev-section-redesign' into reSkinReferenceDocs
* dev-section-redesign: (87 commits)
Added note about restriction on `testDirectory`
fix(dev-utils): Make chai a dev dependency since exported interface depends on it
Add changelog entries
fix(subproviders): make web3-provider-engine types a 'dependency' so it's available to users of the library
fix(sra-spec): make @loopback/openapi-v3-types a 'dependency' so it's available to users of the library
fix(sol-cov): make @types/solidity-parser-antlr a 'dependency' so it's available to users of the library
fix(dev-utils): make web3-provider-engine types a 'dependency' so it's available to users of the library
fix(0x.js): make web3-provider-engine types a 'dependency' so it's available to users of the library
fix(monorepo-scripts): Move the creation of the `.installation-test` directory OUTSIDE of the monorepo root, so that the installed packages can't reference the hoisted node_modules folder
Remove ContractNotFound errors in contract-wrappers
Update prettierignore
Update website to use the new unsubscribeAll method in contract-wrappers
In abi-gen-wrappers, ./wrappers -> ./src/generated-wrappers
In contract-wrappers, remove setProvider and add unsubscribeAll method.
take out explicit children definition in props
Update json-schemas for contract-wrappers
Add OrThrow suffix to getContractAddressesForNetwork
remove unused import
Update CHANGELOG.json for all changed packages
Remove ContractAddresses from packages/types (mistake after rebase)
...
Diffstat (limited to 'packages/migrations/src/migration.ts')
-rw-r--r-- | packages/migrations/src/migration.ts | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/packages/migrations/src/migration.ts b/packages/migrations/src/migration.ts new file mode 100644 index 000000000..0ee5b6975 --- /dev/null +++ b/packages/migrations/src/migration.ts @@ -0,0 +1,175 @@ +import * as wrappers from '@0xproject/abi-gen-wrappers'; +import { ContractAddresses } from '@0xproject/contract-addresses'; +import * as artifacts from '@0xproject/contract-artifacts'; +import { assetDataUtils } from '@0xproject/order-utils'; +import { BigNumber } from '@0xproject/utils'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; +import { Provider, TxData } from 'ethereum-types'; +import * as _ from 'lodash'; + +import { erc20TokenInfo, erc721TokenInfo } from './utils/token_info'; + +/** + * Creates and deploys all the contracts that are required for the latest + * version of the 0x protocol. Custom migrations can be defined here. This will + * be called with the CLI 'migrate:v2' command. + * @param provider Web3 provider instance. + * @param txDefaults Default transaction values to use when deploying contracts. + * @returns The addresses of the contracts that were deployed. + */ +export async function runMigrationsAsync(provider: Provider, txDefaults: Partial<TxData>): Promise<ContractAddresses> { + const web3Wrapper = new Web3Wrapper(provider); + + // Proxies + const erc20Proxy = await wrappers.ERC20ProxyContract.deployFrom0xArtifactAsync( + artifacts.ERC20Proxy, + provider, + txDefaults, + ); + const erc721Proxy = await wrappers.ERC721ProxyContract.deployFrom0xArtifactAsync( + artifacts.ERC721Proxy, + provider, + txDefaults, + ); + + // ZRX + const zrxToken = await wrappers.ZRXTokenContract.deployFrom0xArtifactAsync( + artifacts.ZRXToken, + provider, + txDefaults, + ); + + // Ether token + const etherToken = await wrappers.WETH9Contract.deployFrom0xArtifactAsync(artifacts.WETH9, provider, txDefaults); + + // Exchange + const zrxAssetData = assetDataUtils.encodeERC20AssetData(zrxToken.address); + const exchange = await wrappers.ExchangeContract.deployFrom0xArtifactAsync( + artifacts.Exchange, + provider, + txDefaults, + ); + + // Multisigs + const accounts: string[] = await web3Wrapper.getAvailableAddressesAsync(); + const owners = [accounts[0], accounts[1]]; + const confirmationsRequired = new BigNumber(2); + const secondsRequired = new BigNumber(0); + const owner = accounts[0]; + + // AssetProxyOwner + const assetProxyOwner = await wrappers.AssetProxyOwnerContract.deployFrom0xArtifactAsync( + artifacts.AssetProxyOwner, + provider, + txDefaults, + owners, + [erc20Proxy.address, erc721Proxy.address], + confirmationsRequired, + secondsRequired, + ); + + await web3Wrapper.awaitTransactionSuccessAsync( + await erc20Proxy.addAuthorizedAddress.sendTransactionAsync(exchange.address, { + from: owner, + }), + ); + await web3Wrapper.awaitTransactionSuccessAsync( + await erc20Proxy.transferOwnership.sendTransactionAsync(assetProxyOwner.address, { + from: owner, + }), + ); + await web3Wrapper.awaitTransactionSuccessAsync( + await erc721Proxy.addAuthorizedAddress.sendTransactionAsync(exchange.address, { + from: owner, + }), + ); + await web3Wrapper.awaitTransactionSuccessAsync( + await erc721Proxy.transferOwnership.sendTransactionAsync(assetProxyOwner.address, { + from: owner, + }), + ); + + // Register the Asset Proxies to the Exchange + await web3Wrapper.awaitTransactionSuccessAsync( + await exchange.registerAssetProxy.sendTransactionAsync(erc20Proxy.address), + ); + await web3Wrapper.awaitTransactionSuccessAsync( + await exchange.registerAssetProxy.sendTransactionAsync(erc721Proxy.address), + ); + + // Dummy ERC20 tokens + for (const token of erc20TokenInfo) { + const totalSupply = new BigNumber(1000000000000000000000000000); + // tslint:disable-next-line:no-unused-variable + const dummyErc20Token = await wrappers.DummyERC20TokenContract.deployFrom0xArtifactAsync( + artifacts.DummyERC20Token, + provider, + txDefaults, + token.name, + token.symbol, + token.decimals, + totalSupply, + ); + } + + // ERC721 + // tslint:disable-next-line:no-unused-variable + const cryptoKittieToken = await wrappers.DummyERC721TokenContract.deployFrom0xArtifactAsync( + artifacts.DummyERC721Token, + provider, + txDefaults, + erc721TokenInfo[0].name, + erc721TokenInfo[0].symbol, + ); + + // Forwarder + const forwarder = await wrappers.ForwarderContract.deployFrom0xArtifactAsync( + artifacts.Forwarder, + provider, + txDefaults, + exchange.address, + assetDataUtils.encodeERC20AssetData(zrxToken.address), + assetDataUtils.encodeERC20AssetData(etherToken.address), + ); + + // OrderValidator + const orderValidator = await wrappers.OrderValidatorContract.deployFrom0xArtifactAsync( + artifacts.OrderValidator, + provider, + txDefaults, + exchange.address, + zrxAssetData, + ); + + return { + erc20Proxy: erc20Proxy.address, + erc721Proxy: erc721Proxy.address, + zrxToken: zrxToken.address, + etherToken: etherToken.address, + exchange: exchange.address, + assetProxyOwner: assetProxyOwner.address, + forwarder: forwarder.address, + orderValidator: orderValidator.address, + }; +} + +let _cachedContractAddresses: ContractAddresses; + +/** + * Exactly like runMigrationsAsync but will only run the migrations the first + * time it is called. Any subsequent calls will return the cached contract + * addresses. + * @param provider Web3 provider instance. + * @param txDefaults Default transaction values to use when deploying contracts. + * @returns The addresses of the contracts that were deployed. + */ +export async function runMigrationsOnceAsync( + provider: Provider, + txDefaults: Partial<TxData>, +): Promise<ContractAddresses> { + if (!_.isUndefined(_cachedContractAddresses)) { + return _cachedContractAddresses; + } + _cachedContractAddresses = await runMigrationsAsync(provider, txDefaults); + return _cachedContractAddresses; +} |