From 3a7bb97ad1182eb9c718797bda8dca5eb5d7f9cd Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Wed, 3 Oct 2018 16:21:17 -0700 Subject: Remove artifacts from migrations package and update contract-wrappers accordingly --- packages/migrations/src/migration.ts | 192 +++++++++++++++++++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 packages/migrations/src/migration.ts (limited to 'packages/migrations/src/migration.ts') diff --git a/packages/migrations/src/migration.ts b/packages/migrations/src/migration.ts new file mode 100644 index 000000000..a22c34a23 --- /dev/null +++ b/packages/migrations/src/migration.ts @@ -0,0 +1,192 @@ +import { artifacts, wrappers } from '@0xproject/contracts'; +import { assetDataUtils } from '@0xproject/order-utils'; +import { ContractAddresses } from '@0xproject/types'; +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'; + +interface MigrationsResult { + erc20Proxy: wrappers.ERC20ProxyContract; + erc721Proxy: wrappers.ERC721ProxyContract; + zrxToken: wrappers.ZRXTokenContract; + etherToken: wrappers.WETH9Contract; + exchange: wrappers.ExchangeContract; + assetProxyOwner: wrappers.AssetProxyOwnerContract; + forwarder: wrappers.ForwarderContract; + orderValidator: wrappers.OrderValidatorContract; +} + +let _cachedMigrationsResult: MigrationsResult | undefined; +let _cachedContractAddresses: ContractAddresses | undefined; + +/** + * Custom migrations should be defined in this function. This will be called with the CLI 'migrate:v2' command. + * Migrations could be written to run in parallel, but if you want contract addresses to be created deterministically, + * the migration should be written to run synchronously. + * @param provider Web3 provider instance. + * @param txDefaults Default transaction values to use when deploying contracts. + */ +export async function runMigrationsAsync(provider: Provider, txDefaults: Partial): Promise { + 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, + zrxAssetData, + ); + + // 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, + ); + + const migrationsResult = { + erc20Proxy, + erc721Proxy, + zrxToken, + etherToken, + exchange, + assetProxyOwner, + forwarder, + orderValidator, + }; + _cachedMigrationsResult = migrationsResult; + return migrationsResult; +} + +export function getContractAddresses(): ContractAddresses { + if (!_.isUndefined(_cachedContractAddresses)) { + return _cachedContractAddresses; + } + if (_.isUndefined(_cachedMigrationsResult)) { + throw new Error( + 'Migrations have not been run! You need to call runMigrationsAsync before getContractAddresses', + ); + } + const contractAddresses = { + erc20Proxy: _cachedMigrationsResult.erc20Proxy.address, + erc721Proxy: _cachedMigrationsResult.erc721Proxy.address, + zrxToken: _cachedMigrationsResult.zrxToken.address, + etherToken: _cachedMigrationsResult.etherToken.address, + exchange: _cachedMigrationsResult.exchange.address, + assetProxyOwner: _cachedMigrationsResult.assetProxyOwner.address, + forwarder: _cachedMigrationsResult.forwarder.address, + orderValidator: _cachedMigrationsResult.orderValidator.address, + }; + _cachedContractAddresses = contractAddresses; + return contractAddresses; +} -- cgit v1.2.3 From 17c6661e2d3adf115cdc39371ec33bee1e8cd108 Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Thu, 4 Oct 2018 17:35:57 -0700 Subject: Cleanup migrations and contracts package. Make contracts private again. --- packages/migrations/src/migration.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'packages/migrations/src/migration.ts') diff --git a/packages/migrations/src/migration.ts b/packages/migrations/src/migration.ts index a22c34a23..7c76a7b7b 100644 --- a/packages/migrations/src/migration.ts +++ b/packages/migrations/src/migration.ts @@ -1,4 +1,5 @@ -import { artifacts, wrappers } from '@0xproject/contracts'; +import * as wrappers from '@0xproject/abi-gen-wrappers'; +import * as artifacts from '@0xproject/contract-artifacts'; import { assetDataUtils } from '@0xproject/order-utils'; import { ContractAddresses } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; @@ -29,7 +30,7 @@ let _cachedContractAddresses: ContractAddresses | undefined; * @param provider Web3 provider instance. * @param txDefaults Default transaction values to use when deploying contracts. */ -export async function runMigrationsAsync(provider: Provider, txDefaults: Partial): Promise { +export async function runMigrationsAsync(provider: Provider, txDefaults: Partial): Promise { const web3Wrapper = new Web3Wrapper(provider); // Proxies @@ -60,7 +61,6 @@ export async function runMigrationsAsync(provider: Provider, txDefaults: Partial artifacts.Exchange, provider, txDefaults, - zrxAssetData, ); // Multisigs @@ -165,9 +165,13 @@ export async function runMigrationsAsync(provider: Provider, txDefaults: Partial orderValidator, }; _cachedMigrationsResult = migrationsResult; - return migrationsResult; } +/** + * Returns the addresses of all contracts that were deployed during migrations. + * Throws if migrations have not been run yet. + * @returns Addresses of all contracts that were deployed. + */ export function getContractAddresses(): ContractAddresses { if (!_.isUndefined(_cachedContractAddresses)) { return _cachedContractAddresses; -- cgit v1.2.3 From a48e0a08bf4ea8a8fb0f08cfe5187a6291378afb Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Tue, 9 Oct 2018 13:52:40 -0700 Subject: Add new runMigrationsOnce method to migrations. Update contract-wrappers to use it. --- packages/migrations/src/migration.ts | 77 +++++++++++++----------------------- 1 file changed, 28 insertions(+), 49 deletions(-) (limited to 'packages/migrations/src/migration.ts') diff --git a/packages/migrations/src/migration.ts b/packages/migrations/src/migration.ts index 7c76a7b7b..fc5f390ad 100644 --- a/packages/migrations/src/migration.ts +++ b/packages/migrations/src/migration.ts @@ -9,28 +9,15 @@ import * as _ from 'lodash'; import { erc20TokenInfo, erc721TokenInfo } from './utils/token_info'; -interface MigrationsResult { - erc20Proxy: wrappers.ERC20ProxyContract; - erc721Proxy: wrappers.ERC721ProxyContract; - zrxToken: wrappers.ZRXTokenContract; - etherToken: wrappers.WETH9Contract; - exchange: wrappers.ExchangeContract; - assetProxyOwner: wrappers.AssetProxyOwnerContract; - forwarder: wrappers.ForwarderContract; - orderValidator: wrappers.OrderValidatorContract; -} - -let _cachedMigrationsResult: MigrationsResult | undefined; -let _cachedContractAddresses: ContractAddresses | undefined; - /** - * Custom migrations should be defined in this function. This will be called with the CLI 'migrate:v2' command. - * Migrations could be written to run in parallel, but if you want contract addresses to be created deterministically, - * the migration should be written to run synchronously. + * 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): Promise { +export async function runMigrationsAsync(provider: Provider, txDefaults: Partial): Promise { const web3Wrapper = new Web3Wrapper(provider); // Proxies @@ -154,43 +141,35 @@ export async function runMigrationsAsync(provider: Provider, txDefaults: Partial zrxAssetData, ); - const migrationsResult = { - erc20Proxy, - erc721Proxy, - zrxToken, - etherToken, - exchange, - assetProxyOwner, - forwarder, - orderValidator, + 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, }; - _cachedMigrationsResult = migrationsResult; } +let _cachedContractAddresses: ContractAddresses; + /** - * Returns the addresses of all contracts that were deployed during migrations. - * Throws if migrations have not been run yet. - * @returns Addresses of all contracts that were deployed. + * 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 function getContractAddresses(): ContractAddresses { +export async function runMigrationsOnceAsync( + provider: Provider, + txDefaults: Partial, +): Promise { if (!_.isUndefined(_cachedContractAddresses)) { return _cachedContractAddresses; } - if (_.isUndefined(_cachedMigrationsResult)) { - throw new Error( - 'Migrations have not been run! You need to call runMigrationsAsync before getContractAddresses', - ); - } - const contractAddresses = { - erc20Proxy: _cachedMigrationsResult.erc20Proxy.address, - erc721Proxy: _cachedMigrationsResult.erc721Proxy.address, - zrxToken: _cachedMigrationsResult.zrxToken.address, - etherToken: _cachedMigrationsResult.etherToken.address, - exchange: _cachedMigrationsResult.exchange.address, - assetProxyOwner: _cachedMigrationsResult.assetProxyOwner.address, - forwarder: _cachedMigrationsResult.forwarder.address, - orderValidator: _cachedMigrationsResult.orderValidator.address, - }; - _cachedContractAddresses = contractAddresses; - return contractAddresses; + _cachedContractAddresses = await runMigrationsAsync(provider, txDefaults); + return _cachedContractAddresses; } -- cgit v1.2.3 From 1e9ea09f087c7b3120e758d931a88812b655da08 Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Tue, 9 Oct 2018 23:10:33 -0700 Subject: Introduce new contract-addresses package and use it everywhere --- packages/migrations/src/migration.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/migrations/src/migration.ts') diff --git a/packages/migrations/src/migration.ts b/packages/migrations/src/migration.ts index fc5f390ad..0ee5b6975 100644 --- a/packages/migrations/src/migration.ts +++ b/packages/migrations/src/migration.ts @@ -1,7 +1,7 @@ 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 { ContractAddresses } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { Provider, TxData } from 'ethereum-types'; -- cgit v1.2.3