aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contracts/util
diff options
context:
space:
mode:
Diffstat (limited to 'packages/contracts/util')
-rw-r--r--packages/contracts/util/artifacts.ts53
-rw-r--r--packages/contracts/util/balances.ts7
-rw-r--r--packages/contracts/util/exchange_wrapper.ts73
-rw-r--r--packages/contracts/util/formatters.ts12
-rw-r--r--packages/contracts/util/multi_sig_wrapper.ts6
-rw-r--r--packages/contracts/util/order.ts14
-rw-r--r--packages/contracts/util/order_factory.ts9
-rw-r--r--packages/contracts/util/token_registry_wrapper.ts8
-rw-r--r--packages/contracts/util/types.ts35
9 files changed, 137 insertions, 80 deletions
diff --git a/packages/contracts/util/artifacts.ts b/packages/contracts/util/artifacts.ts
index ecb18cbce..fd9380d1d 100644
--- a/packages/contracts/util/artifacts.ts
+++ b/packages/contracts/util/artifacts.ts
@@ -1,28 +1,25 @@
-export class Artifacts {
- public Migrations: any;
- public TokenTransferProxy: any;
- public TokenRegistry: any;
- public MultiSigWalletWithTimeLock: any;
- public Exchange: any;
- public ZRXToken: any;
- public DummyToken: any;
- public DummyTokenV2: any;
- public EtherToken: any;
- public MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress: any;
- public MaliciousToken: any;
- constructor(artifacts: any) {
- this.Migrations = artifacts.require('Migrations');
- this.TokenTransferProxy = artifacts.require('TokenTransferProxy');
- this.TokenRegistry = artifacts.require('TokenRegistry');
- this.MultiSigWalletWithTimeLock = artifacts.require('MultiSigWalletWithTimeLock');
- this.Exchange = artifacts.require('Exchange');
- this.ZRXToken = artifacts.require('ZRXToken');
- this.DummyToken = artifacts.require('DummyToken');
- this.DummyTokenV2 = artifacts.require('DummyToken_v2');
- this.EtherToken = artifacts.require('WETH9');
- this.MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress = artifacts.require(
- 'MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress',
- );
- this.MaliciousToken = artifacts.require('MaliciousToken');
- }
-}
+import * as DummyTokenArtifact from '../artifacts/DummyToken.json';
+import * as ExchangeArtifact from '../artifacts/Exchange.json';
+import * as MaliciousTokenArtifact from '../artifacts/MaliciousToken.json';
+import * as MultiSigWalletWithTimeLockArtifact from '../artifacts/MultiSigWalletWithTimeLock.json';
+import * as MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddressArtifact from '../artifacts/MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress.json';
+import * as TokenArtifact from '../artifacts/Token.json';
+import * as TokenRegistryArtifact from '../artifacts/TokenRegistry.json';
+import * as TokenTransferProxyArtifact from '../artifacts/TokenTransferProxy.json';
+import * as EtherTokenArtifact from '../artifacts/WETH9.json';
+import * as ZRXArtifact from '../artifacts/ZRXToken.json';
+
+import { Artifact } from './types';
+
+export const artifacts = {
+ ZRXArtifact: (ZRXArtifact as any) as Artifact,
+ DummyTokenArtifact: (DummyTokenArtifact as any) as Artifact,
+ TokenArtifact: (TokenArtifact as any) as Artifact,
+ ExchangeArtifact: (ExchangeArtifact as any) as Artifact,
+ EtherTokenArtifact: (EtherTokenArtifact as any) as Artifact,
+ TokenRegistryArtifact: (TokenRegistryArtifact as any) as Artifact,
+ MaliciousTokenArtifact: (MaliciousTokenArtifact as any) as Artifact,
+ TokenTransferProxyArtifact: (TokenTransferProxyArtifact as any) as Artifact,
+ MultiSigWalletWithTimeLockArtifact: (MultiSigWalletWithTimeLockArtifact as any) as Artifact,
+ MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddressArtifact: (MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddressArtifact as any) as Artifact,
+};
diff --git a/packages/contracts/util/balances.ts b/packages/contracts/util/balances.ts
index 6a1659ab1..5bcb5145f 100644
--- a/packages/contracts/util/balances.ts
+++ b/packages/contracts/util/balances.ts
@@ -1,12 +1,13 @@
import { BigNumber } from '@0xproject/utils';
import * as _ from 'lodash';
+import * as Web3 from 'web3';
-import { BalancesByOwner, ContractInstance } from './types';
+import { BalancesByOwner } from './types';
export class Balances {
- private _tokenContractInstances: ContractInstance[];
+ private _tokenContractInstances: Web3.ContractInstance[];
private _ownerAddresses: string[];
- constructor(tokenContractInstances: ContractInstance[], ownerAddresses: string[]) {
+ constructor(tokenContractInstances: Web3.ContractInstance[], ownerAddresses: string[]) {
this._tokenContractInstances = tokenContractInstances;
this._ownerAddresses = ownerAddresses;
}
diff --git a/packages/contracts/util/exchange_wrapper.ts b/packages/contracts/util/exchange_wrapper.ts
index ca79f92c4..7a07239f5 100644
--- a/packages/contracts/util/exchange_wrapper.ts
+++ b/packages/contracts/util/exchange_wrapper.ts
@@ -1,14 +1,17 @@
+import { TransactionReceiptWithDecodedLogs, ZeroEx } from '0x.js';
import { BigNumber } from '@0xproject/utils';
import * as _ from 'lodash';
+import * as Web3 from 'web3';
import { formatters } from './formatters';
import { Order } from './order';
-import { ContractInstance } from './types';
export class ExchangeWrapper {
- private _exchange: ContractInstance;
- constructor(exchangeContractInstance: ContractInstance) {
+ private _exchange: Web3.ContractInstance;
+ private _zeroEx: ZeroEx;
+ constructor(exchangeContractInstance: Web3.ContractInstance, zeroEx: ZeroEx) {
this._exchange = exchangeContractInstance;
+ this._zeroEx = zeroEx;
}
public async fillOrderAsync(
order: Order,
@@ -17,10 +20,10 @@ export class ExchangeWrapper {
fillTakerTokenAmount?: BigNumber;
shouldThrowOnInsufficientBalanceOrAllowance?: boolean;
} = {},
- ) {
+ ): Promise<TransactionReceiptWithDecodedLogs> {
const shouldThrowOnInsufficientBalanceOrAllowance = !!opts.shouldThrowOnInsufficientBalanceOrAllowance;
const params = order.createFill(shouldThrowOnInsufficientBalanceOrAllowance, opts.fillTakerTokenAmount);
- const tx = await this._exchange.fillOrder(
+ const txHash = await this._exchange.fillOrder(
params.orderAddresses,
params.orderValues,
params.fillTakerTokenAmount,
@@ -30,24 +33,36 @@ export class ExchangeWrapper {
params.s,
{ from },
);
+ const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash);
+ tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address);
_.each(tx.logs, log => wrapLogBigNumbers(log));
return tx;
}
- public async cancelOrderAsync(order: Order, from: string, opts: { cancelTakerTokenAmount?: BigNumber } = {}) {
+ public async cancelOrderAsync(
+ order: Order,
+ from: string,
+ opts: { cancelTakerTokenAmount?: BigNumber } = {},
+ ): Promise<TransactionReceiptWithDecodedLogs> {
const params = order.createCancel(opts.cancelTakerTokenAmount);
- const tx = await this._exchange.cancelOrder(
+ const txHash = await this._exchange.cancelOrder(
params.orderAddresses,
params.orderValues,
params.cancelTakerTokenAmount,
{ from },
);
+ const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash);
+ tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address);
_.each(tx.logs, log => wrapLogBigNumbers(log));
return tx;
}
- public async fillOrKillOrderAsync(order: Order, from: string, opts: { fillTakerTokenAmount?: BigNumber } = {}) {
+ public async fillOrKillOrderAsync(
+ order: Order,
+ from: string,
+ opts: { fillTakerTokenAmount?: BigNumber } = {},
+ ): Promise<TransactionReceiptWithDecodedLogs> {
const shouldThrowOnInsufficientBalanceOrAllowance = true;
const params = order.createFill(shouldThrowOnInsufficientBalanceOrAllowance, opts.fillTakerTokenAmount);
- const tx = await this._exchange.fillOrKillOrder(
+ const txHash = await this._exchange.fillOrKillOrder(
params.orderAddresses,
params.orderValues,
params.fillTakerTokenAmount,
@@ -56,6 +71,8 @@ export class ExchangeWrapper {
params.s,
{ from },
);
+ const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash);
+ tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address);
_.each(tx.logs, log => wrapLogBigNumbers(log));
return tx;
}
@@ -66,14 +83,14 @@ export class ExchangeWrapper {
fillTakerTokenAmounts?: BigNumber[];
shouldThrowOnInsufficientBalanceOrAllowance?: boolean;
} = {},
- ) {
+ ): Promise<TransactionReceiptWithDecodedLogs> {
const shouldThrowOnInsufficientBalanceOrAllowance = !!opts.shouldThrowOnInsufficientBalanceOrAllowance;
const params = formatters.createBatchFill(
orders,
shouldThrowOnInsufficientBalanceOrAllowance,
opts.fillTakerTokenAmounts,
);
- const tx = await this._exchange.batchFillOrders(
+ const txHash = await this._exchange.batchFillOrders(
params.orderAddresses,
params.orderValues,
params.fillTakerTokenAmounts,
@@ -83,16 +100,23 @@ export class ExchangeWrapper {
params.s,
{ from },
);
+ const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash);
+ tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address);
_.each(tx.logs, log => wrapLogBigNumbers(log));
return tx;
}
public async batchFillOrKillOrdersAsync(
orders: Order[],
from: string,
- opts: { fillTakerTokenAmounts?: BigNumber[] } = {},
- ) {
- const params = formatters.createBatchFill(orders, undefined, opts.fillTakerTokenAmounts);
- const tx = await this._exchange.batchFillOrKillOrders(
+ opts: { fillTakerTokenAmounts?: BigNumber[]; shouldThrowOnInsufficientBalanceOrAllowance?: boolean } = {},
+ ): Promise<TransactionReceiptWithDecodedLogs> {
+ const shouldThrowOnInsufficientBalanceOrAllowance = !!opts.shouldThrowOnInsufficientBalanceOrAllowance;
+ const params = formatters.createBatchFill(
+ orders,
+ shouldThrowOnInsufficientBalanceOrAllowance,
+ opts.fillTakerTokenAmounts,
+ );
+ const txHash = await this._exchange.batchFillOrKillOrders(
params.orderAddresses,
params.orderValues,
params.fillTakerTokenAmounts,
@@ -101,24 +125,23 @@ export class ExchangeWrapper {
params.s,
{ from },
);
+ const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash);
+ tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address);
_.each(tx.logs, log => wrapLogBigNumbers(log));
return tx;
}
public async fillOrdersUpToAsync(
orders: Order[],
from: string,
- opts: {
- fillTakerTokenAmount?: BigNumber;
- shouldThrowOnInsufficientBalanceOrAllowance?: boolean;
- } = {},
- ) {
+ opts: { fillTakerTokenAmount: BigNumber; shouldThrowOnInsufficientBalanceOrAllowance?: boolean },
+ ): Promise<TransactionReceiptWithDecodedLogs> {
const shouldThrowOnInsufficientBalanceOrAllowance = !!opts.shouldThrowOnInsufficientBalanceOrAllowance;
const params = formatters.createFillUpTo(
orders,
shouldThrowOnInsufficientBalanceOrAllowance,
opts.fillTakerTokenAmount,
);
- const tx = await this._exchange.fillOrdersUpTo(
+ const txHash = await this._exchange.fillOrdersUpTo(
params.orderAddresses,
params.orderValues,
params.fillTakerTokenAmount,
@@ -128,6 +151,8 @@ export class ExchangeWrapper {
params.s,
{ from },
);
+ const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash);
+ tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address);
_.each(tx.logs, log => wrapLogBigNumbers(log));
return tx;
}
@@ -135,14 +160,16 @@ export class ExchangeWrapper {
orders: Order[],
from: string,
opts: { cancelTakerTokenAmounts?: BigNumber[] } = {},
- ) {
+ ): Promise<TransactionReceiptWithDecodedLogs> {
const params = formatters.createBatchCancel(orders, opts.cancelTakerTokenAmounts);
- const tx = await this._exchange.batchCancelOrders(
+ const txHash = await this._exchange.batchCancelOrders(
params.orderAddresses,
params.orderValues,
params.cancelTakerTokenAmounts,
{ from },
);
+ const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash);
+ tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address);
_.each(tx.logs, log => wrapLogBigNumbers(log));
return tx;
}
diff --git a/packages/contracts/util/formatters.ts b/packages/contracts/util/formatters.ts
index 0d0ef6df4..e16fe8d45 100644
--- a/packages/contracts/util/formatters.ts
+++ b/packages/contracts/util/formatters.ts
@@ -35,9 +35,9 @@ export const formatters = {
order.params.expirationTimestampInSec,
order.params.salt,
]);
- batchFill.v.push(order.params.v);
- batchFill.r.push(order.params.r);
- batchFill.s.push(order.params.s);
+ batchFill.v.push(order.params.v as number);
+ batchFill.r.push(order.params.r as string);
+ batchFill.s.push(order.params.s as string);
if (fillTakerTokenAmounts.length < orders.length) {
batchFill.fillTakerTokenAmounts.push(order.params.takerTokenAmount);
}
@@ -74,9 +74,9 @@ export const formatters = {
order.params.expirationTimestampInSec,
order.params.salt,
]);
- fillUpTo.v.push(order.params.v);
- fillUpTo.r.push(order.params.r);
- fillUpTo.s.push(order.params.s);
+ fillUpTo.v.push(order.params.v as number);
+ fillUpTo.r.push(order.params.r as string);
+ fillUpTo.s.push(order.params.s as string);
});
return fillUpTo;
},
diff --git a/packages/contracts/util/multi_sig_wrapper.ts b/packages/contracts/util/multi_sig_wrapper.ts
index 0e2e671ec..0a066df53 100644
--- a/packages/contracts/util/multi_sig_wrapper.ts
+++ b/packages/contracts/util/multi_sig_wrapper.ts
@@ -3,10 +3,10 @@ import ethUtil = require('ethereumjs-util');
import * as _ from 'lodash';
import * as Web3 from 'web3';
-import { ContractInstance, TransactionDataParams } from './types';
+import { TransactionDataParams } from './types';
export class MultiSigWrapper {
- private _multiSig: ContractInstance;
+ private _multiSig: Web3.ContractInstance;
public static encodeFnArgs(name: string, abi: Web3.AbiDefinition[], args: any[]) {
const abiEntity = _.find(abi, { name }) as Web3.MethodAbi;
if (_.isUndefined(abiEntity)) {
@@ -21,7 +21,7 @@ export class MultiSigWrapper {
});
return funcSig + argsData.join('');
}
- constructor(multiSigContractInstance: ContractInstance) {
+ constructor(multiSigContractInstance: Web3.ContractInstance) {
this._multiSig = multiSigContractInstance;
}
public async submitTransactionAsync(
diff --git a/packages/contracts/util/order.ts b/packages/contracts/util/order.ts
index e202d485b..57bb2bcbf 100644
--- a/packages/contracts/util/order.ts
+++ b/packages/contracts/util/order.ts
@@ -1,19 +1,17 @@
-import { BigNumber, promisify } from '@0xproject/utils';
+import { BigNumber } from '@0xproject/utils';
+import { Web3Wrapper } from '@0xproject/web3-wrapper';
import ethUtil = require('ethereumjs-util');
import * as _ from 'lodash';
-import Web3 = require('web3');
import { crypto } from './crypto';
import { OrderParams } from './types';
-// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle
-// with type `any` to a variable of type `Web3`.
-const web3: Web3 = (global as any).web3;
-
export class Order {
public params: OrderParams;
- constructor(params: OrderParams) {
+ private _web3Wrapper: Web3Wrapper;
+ constructor(web3Wrapper: Web3Wrapper, params: OrderParams) {
this.params = params;
+ this._web3Wrapper = web3Wrapper;
}
public isValidSignature() {
const { v, r, s } = this.params;
@@ -32,7 +30,7 @@ export class Order {
}
public async signAsync() {
const orderHash = this._getOrderHash();
- const signature = await promisify<string>(web3.eth.sign)(this.params.maker, orderHash);
+ const signature = await this._web3Wrapper.signTransactionAsync(this.params.maker, orderHash);
const { v, r, s } = ethUtil.fromRpcSig(signature);
this.params = _.assign(this.params, {
orderHashHex: orderHash,
diff --git a/packages/contracts/util/order_factory.ts b/packages/contracts/util/order_factory.ts
index a45877de0..2b50f13e8 100644
--- a/packages/contracts/util/order_factory.ts
+++ b/packages/contracts/util/order_factory.ts
@@ -1,5 +1,6 @@
import { ZeroEx } from '0x.js';
import { BigNumber } from '@0xproject/utils';
+import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as _ from 'lodash';
import { Order } from './order';
@@ -7,10 +8,12 @@ import { DefaultOrderParams, OptionalOrderParams, OrderParams } from './types';
export class OrderFactory {
private _defaultOrderParams: DefaultOrderParams;
- constructor(defaultOrderParams: DefaultOrderParams) {
+ private _web3Wrapper: Web3Wrapper;
+ constructor(web3Wrapper: Web3Wrapper, defaultOrderParams: DefaultOrderParams) {
this._defaultOrderParams = defaultOrderParams;
+ this._web3Wrapper = web3Wrapper;
}
- public async newSignedOrderAsync(customOrderParams: OptionalOrderParams = {}) {
+ public async newSignedOrderAsync(customOrderParams: OptionalOrderParams = {}): Promise<Order> {
const randomExpiration = new BigNumber(Math.floor((Date.now() + Math.random() * 100000000000) / 1000));
const orderParams: OrderParams = _.assign(
{},
@@ -22,7 +25,7 @@ export class OrderFactory {
this._defaultOrderParams,
customOrderParams,
);
- const order = new Order(orderParams);
+ const order = new Order(this._web3Wrapper, orderParams);
await order.signAsync();
return order;
}
diff --git a/packages/contracts/util/token_registry_wrapper.ts b/packages/contracts/util/token_registry_wrapper.ts
index 07a577dea..033b72d10 100644
--- a/packages/contracts/util/token_registry_wrapper.ts
+++ b/packages/contracts/util/token_registry_wrapper.ts
@@ -1,8 +1,10 @@
-import { ContractInstance, Token } from './types';
+import * as Web3 from 'web3';
+
+import { Token } from './types';
export class TokenRegWrapper {
- private _tokenReg: ContractInstance;
- constructor(tokenRegContractInstance: ContractInstance) {
+ private _tokenReg: Web3.ContractInstance;
+ constructor(tokenRegContractInstance: Web3.ContractInstance) {
this._tokenReg = tokenRegContractInstance;
}
public addTokenAsync(token: Token, from: string) {
diff --git a/packages/contracts/util/types.ts b/packages/contracts/util/types.ts
index e511ca9f4..38fd33161 100644
--- a/packages/contracts/util/types.ts
+++ b/packages/contracts/util/types.ts
@@ -7,6 +7,10 @@ export interface BalancesByOwner {
};
}
+export interface SubmissionContractEventArgs {
+ transactionId: BigNumber;
+}
+
export interface BatchFillOrders {
orderAddresses: string[][];
orderValues: BigNumber[][];
@@ -108,12 +112,37 @@ export interface TokenInfoByNetwork {
live: Token[];
}
-// Named type aliases to improve readability
-export type ContractInstance = any;
-
export enum ExchangeContractErrs {
ERROR_ORDER_EXPIRED,
ERROR_ORDER_FULLY_FILLED_OR_CANCELLED,
ERROR_ROUNDING_ERROR_TOO_LARGE,
ERROR_INSUFFICIENT_BALANCE_OR_ALLOWANCE,
}
+
+export enum ContractName {
+ TokenTransferProxy = 'TokenTransferProxy',
+ TokenRegistry = 'TokenRegistry',
+ MultiSigWalletWithTimeLock = 'MultiSigWalletWithTimeLock',
+ Exchange = 'Exchange',
+ ZRXToken = 'ZRXToken',
+ DummyToken = 'DummyToken',
+ EtherToken = 'WETH9',
+ MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress = 'MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress',
+ MaliciousToken = 'MaliciousToken',
+}
+
+export interface Artifact {
+ contract_name: ContractName;
+ networks: {
+ [networkId: number]: {
+ abi: Web3.ContractAbi;
+ solc_version: string;
+ keccak256: string;
+ optimizer_enabled: number;
+ unlinked_binary: string;
+ updated_at: number;
+ address: string;
+ constructor_args: string;
+ };
+ };
+}