Refactor fill-scenarios for V2

This commit is contained in:
Leonid Logvinov
2018-07-03 15:49:51 +03:00
parent 7fbee77875
commit c65b2573c4
3 changed files with 16 additions and 49 deletions

View File

@@ -9,14 +9,14 @@
"build": "yarn pre_build && tsc", "build": "yarn pre_build && tsc",
"pre_build": "run-s update_artifacts generate_contract_wrappers", "pre_build": "run-s update_artifacts generate_contract_wrappers",
"update_artifacts": "for i in ${npm_package_config_contracts}; do copyfiles -u 4 ../migrations/artifacts/2.0.0/$i.json lib/artifacts; done;", "update_artifacts": "for i in ${npm_package_config_contracts}; do copyfiles -u 4 ../migrations/artifacts/2.0.0/$i.json lib/artifacts; done;",
"generate_contract_wrappers": "abi-gen --abis 'lib/artifacts/@(Exchange|ERC20Token|DummyERC20Token).json' --template ../contract_templates/contract.handlebars --partials '../contract_templates/partials/**/*.handlebars' --output src/generated_contract_wrappers --backend ethers", "generate_contract_wrappers": "abi-gen --abis 'lib/artifacts/@(Exchange|ERC20Token).json' --template ../contract_templates/contract.handlebars --partials '../contract_templates/partials/**/*.handlebars' --output src/generated_contract_wrappers --backend ethers",
"copy_monorepo_scripts": "copyfiles -u 3 './lib/src/monorepo_scripts/**/*' ./scripts", "copy_monorepo_scripts": "copyfiles -u 3 './lib/src/monorepo_scripts/**/*' ./scripts",
"clean": "shx rm -rf lib scripts src/generated_contract_wrappers", "clean": "shx rm -rf lib scripts src/generated_contract_wrappers",
"lint": "tslint --project . --exclude **/src/generated_contract_wrappers/**/*", "lint": "tslint --project . --exclude **/src/generated_contract_wrappers/**/*",
"manual:postpublish": "yarn build; node ./scripts/postpublish.js" "manual:postpublish": "yarn build; node ./scripts/postpublish.js"
}, },
"config": { "config": {
"contracts": "Exchange ERC20Token DummyERC20Token" "contracts": "Exchange ERC20Token"
}, },
"license": "Apache-2.0", "license": "Apache-2.0",
"repository": { "repository": {

View File

@@ -1,13 +1,9 @@
import { Artifact } from '@0xproject/types'; import { ContractArtifact } from '@0xproject/sol-compiler';
import * as DummyToken from './compact_artifacts/DummyToken.json'; import * as ERC20Token from './artifacts/ERC20Token.json';
import * as Exchange from './compact_artifacts/Exchange.json'; import * as Exchange from './artifacts/Exchange.json';
import * as Token from './compact_artifacts/Token.json';
import * as TokenTransferProxy from './compact_artifacts/TokenTransferProxy.json';
export const artifacts = { export const artifacts = {
DummyToken: (DummyToken as any) as Artifact, ERC20Token: (ERC20Token as any) as ContractArtifact,
Token: (Token as any) as Artifact, Exchange: (Exchange as any) as ContractArtifact,
TokenTransferProxy: (TokenTransferProxy as any) as Artifact,
Exchange: (Exchange as any) as Artifact,
}; };

View File

@@ -1,5 +1,5 @@
import { assetProxyUtils, orderFactory } from '@0xproject/order-utils'; import { assetProxyUtils, orderFactory } from '@0xproject/order-utils';
import { OrderWithoutExchangeAddress, SignedOrder, Token } from '@0xproject/types'; import { OrderWithoutExchangeAddress, SignedOrder } from '@0xproject/types';
import { BigNumber } from '@0xproject/utils'; import { BigNumber } from '@0xproject/utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { Web3Wrapper } from '@0xproject/web3-wrapper';
import { Provider } from 'ethereum-types'; import { Provider } from 'ethereum-types';
@@ -7,52 +7,29 @@ import * as _ from 'lodash';
import { artifacts } from './artifacts'; import { artifacts } from './artifacts';
import { constants } from './constants'; import { constants } from './constants';
import { DummyERC20TokenContract } from './generated_contract_wrappers/dummy_erc20_token';
import { ERC20TokenContract } from './generated_contract_wrappers/erc20_token'; import { ERC20TokenContract } from './generated_contract_wrappers/erc20_token';
import { ExchangeContract } from './generated_contract_wrappers/exchange'; import { ExchangeContract } from './generated_contract_wrappers/exchange';
const INITIAL_COINBASE_TOKEN_SUPPLY_IN_UNITS = new BigNumber(100);
export class FillScenarios { export class FillScenarios {
private _web3Wrapper: Web3Wrapper; private _web3Wrapper: Web3Wrapper;
private _userAddresses: string[]; private _userAddresses: string[];
private _tokens: Token[];
private _coinbase: string; private _coinbase: string;
private _zrxTokenAddress: string; private _zrxTokenAddress: string;
private _exchangeAddress: string; private _exchangeAddress: string;
private _erc20ProxyAddress: string;
constructor( constructor(
provider: Provider, provider: Provider,
userAddresses: string[], userAddresses: string[],
tokens: Token[],
zrxTokenAddress: string, zrxTokenAddress: string,
exchangeAddress: string, exchangeAddress: string,
erc20ProxyAddress: string,
) { ) {
this._web3Wrapper = new Web3Wrapper(provider); this._web3Wrapper = new Web3Wrapper(provider);
this._userAddresses = userAddresses; this._userAddresses = userAddresses;
this._tokens = tokens;
this._coinbase = userAddresses[0]; this._coinbase = userAddresses[0];
this._zrxTokenAddress = zrxTokenAddress; this._zrxTokenAddress = zrxTokenAddress;
this._exchangeAddress = exchangeAddress; this._exchangeAddress = exchangeAddress;
} this._erc20ProxyAddress = erc20ProxyAddress;
public async initTokenBalancesAsync(): Promise<void> {
for (const token of this._tokens) {
if (token.symbol !== 'ZRX' && token.symbol !== 'WETH') {
const dummyERC20Token = new DummyERC20TokenContract(
artifacts.DummyToken.abi,
token.address,
this._web3Wrapper.getProvider(),
this._web3Wrapper.getContractDefaults(),
);
const tokenSupply = Web3Wrapper.toBaseUnitAmount(
INITIAL_COINBASE_TOKEN_SUPPLY_IN_UNITS,
token.decimals,
);
const txHash = await dummyERC20Token.setBalance.sendTransactionAsync(this._coinbase, tokenSupply, {
from: this._coinbase,
});
await this._web3Wrapper.awaitTransactionSuccessAsync(txHash);
}
}
} }
public async createFillableSignedOrderAsync( public async createFillableSignedOrderAsync(
makerAssetData: string, makerAssetData: string,
@@ -138,7 +115,7 @@ export class FillScenarios {
fillableAmount, fillableAmount,
); );
const exchangeInstance = new ExchangeContract( const exchangeInstance = new ExchangeContract(
artifacts.Exchange.abi, artifacts.Exchange.compilerOutput.abi,
signedOrder.exchangeAddress, signedOrder.exchangeAddress,
this._web3Wrapper.getProvider(), this._web3Wrapper.getProvider(),
this._web3Wrapper.getContractDefaults(), this._web3Wrapper.getContractDefaults(),
@@ -215,7 +192,7 @@ export class FillScenarios {
} }
private async _increaseERC20BalanceAsync(tokenAddress: string, address: string, amount: BigNumber): Promise<void> { private async _increaseERC20BalanceAsync(tokenAddress: string, address: string, amount: BigNumber): Promise<void> {
const token = new ERC20TokenContract( const token = new ERC20TokenContract(
artifacts.Token.abi, artifacts.ERC20Token.compilerOutput.abi,
tokenAddress, tokenAddress,
this._web3Wrapper.getProvider(), this._web3Wrapper.getProvider(),
this._web3Wrapper.getContractDefaults(), this._web3Wrapper.getContractDefaults(),
@@ -230,21 +207,15 @@ export class FillScenarios {
amount: BigNumber, amount: BigNumber,
): Promise<void> { ): Promise<void> {
const tokenInstance = new ERC20TokenContract( const tokenInstance = new ERC20TokenContract(
artifacts.Token.abi, artifacts.ERC20Token.compilerOutput.abi,
tokenAddress, tokenAddress,
this._web3Wrapper.getProvider(), this._web3Wrapper.getProvider(),
this._web3Wrapper.getContractDefaults(), this._web3Wrapper.getContractDefaults(),
); );
const networkId = await this._web3Wrapper.getNetworkIdAsync(); const oldMakerAllowance = await tokenInstance.allowance.callAsync(address, this._erc20ProxyAddress);
const networkArtifactsIfExists = artifacts.TokenTransferProxy.networks[networkId];
if (_.isUndefined(networkArtifactsIfExists)) {
throw new Error(`Did not find network artifacts for networkId: ${networkId}`);
}
const proxyAddress = networkArtifactsIfExists.address;
const oldMakerAllowance = await tokenInstance.allowance.callAsync(address, proxyAddress);
const newMakerAllowance = oldMakerAllowance.plus(amount); const newMakerAllowance = oldMakerAllowance.plus(amount);
await tokenInstance.approve.sendTransactionAsync(proxyAddress, newMakerAllowance, { await tokenInstance.approve.sendTransactionAsync(this._erc20ProxyAddress, newMakerAllowance, {
from: address, from: address,
}); });
} }