1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
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';
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<TxData>): Promise<void> {
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,
);
const migrationsResult = {
erc20Proxy,
erc721Proxy,
zrxToken,
etherToken,
exchange,
assetProxyOwner,
forwarder,
orderValidator,
};
_cachedMigrationsResult = 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;
}
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;
}
|