Update contract-wrappers everywhere (#2068)

* add validateAndSendTransactionAsync to all wrappers

* remove contract-wrappers from @0x/contracts-extensions

* replace error types in contract-wrappers

* upgrade asset-buyer and asset-swapper to contract-wrappers v11

* update website and 0x.js

* remove calldataOptimizationUtils
This commit is contained in:
Xianny
2019-08-15 10:53:33 -07:00
committed by GitHub
parent 29d5db16c2
commit 38d2b70ba6
55 changed files with 1498 additions and 258 deletions

View File

@@ -23,7 +23,7 @@
"dependencies": {
"@0x/asset-buyer": "^6.1.11",
"@0x/contract-addresses": "^3.0.3",
"@0x/contract-wrappers": "^9.1.7",
"@0x/contract-wrappers": "^11.0.0",
"@0x/json-schemas": "^3.1.13",
"@0x/order-utils": "^8.2.5",
"@0x/subproviders": "^5.0.1",

View File

@@ -2,11 +2,13 @@ import {
BlockRange,
ContractWrappers,
DecodedLogEvent,
ERC20TokenContract,
ExchangeCancelEventArgs,
ExchangeEventArgs,
ExchangeEvents,
ExchangeFillEventArgs,
IndexedFilterValues,
WETH9Contract,
} from '@0x/contract-wrappers';
import { assetDataUtils, orderHashUtils, signatureUtils } from '@0x/order-utils';
import {
@@ -237,11 +239,12 @@ export class Blockchain {
utils.assert(this._contractWrappers !== undefined, 'Contract Wrappers must be instantiated.');
this._showFlashMessageIfLedger();
const txHash = await this._contractWrappers.erc20Token.setProxyAllowanceAsync(
token.address,
this._userAddressIfExists,
const erc20Token = new ERC20TokenContract(token.address, this._contractWrappers.getProvider());
const txHash = await erc20Token.approve.sendTransactionAsync(
this._contractWrappers.contractAddresses.erc20Proxy,
amountInBaseUnits,
{
from: this._userAddressIfExists,
gasPrice: this._defaultGasPrice,
},
);
@@ -274,15 +277,11 @@ export class Blockchain {
utils.assert(this._doesUserAddressExist(), BlockchainCallErrs.UserHasNoAssociatedAddresses);
this._showFlashMessageIfLedger();
const txHash = await this._contractWrappers.erc20Token.transferAsync(
token.address,
this._userAddressIfExists,
toAddress,
amountInBaseUnits,
{
gasPrice: this._defaultGasPrice,
},
);
const erc20Token = new ERC20TokenContract(token.address, this._contractWrappers.getProvider());
const txHash = await erc20Token.transfer.validateAndSendTransactionAsync(toAddress, amountInBaseUnits, {
from: this._userAddressIfExists,
gasPrice: this._defaultGasPrice,
});
await this._showEtherScanLinkAndAwaitTransactionMinedAsync(txHash);
const etherScanLinkIfExists = utils.getEtherScanLinkIfExists(txHash, this.networkId, EtherscanLinkSuffixes.Tx);
this._dispatcher.showFlashMessage(
@@ -299,11 +298,12 @@ export class Blockchain {
utils.assert(this._contractWrappers !== undefined, 'ContractWrappers must be instantiated.');
utils.assert(this._doesUserAddressExist(), BlockchainCallErrs.UserHasNoAssociatedAddresses);
this._showFlashMessageIfLedger();
const txHash = await this._contractWrappers.exchange.fillOrderAsync(
const txHash = await this._contractWrappers.exchange.fillOrder.validateAndSendTransactionAsync(
signedOrder,
fillTakerTokenAmount,
this._userAddressIfExists,
signedOrder.signature,
{
from: this._userAddressIfExists,
gasPrice: this._defaultGasPrice,
},
);
@@ -316,7 +316,8 @@ export class Blockchain {
}
public async cancelOrderAsync(signedOrder: SignedOrder): Promise<string> {
this._showFlashMessageIfLedger();
const txHash = await this._contractWrappers.exchange.cancelOrderAsync(signedOrder, {
const txHash = await this._contractWrappers.exchange.cancelOrder.validateAndSendTransactionAsync(signedOrder, {
from: signedOrder.makerAddress,
gasPrice: this._defaultGasPrice,
});
const receipt = await this._showEtherScanLinkAndAwaitTransactionMinedAsync(txHash);
@@ -329,7 +330,7 @@ export class Blockchain {
public async getUnavailableTakerAmountAsync(orderHash: string): Promise<BigNumber> {
utils.assert(orderHashUtils.isValidOrderHash(orderHash), 'Must be valid orderHash');
utils.assert(this._contractWrappers !== undefined, 'ContractWrappers must be instantiated.');
const unavailableTakerAmount = await this._contractWrappers.exchange.getFilledTakerAssetAmountAsync(orderHash);
const unavailableTakerAmount = await this._contractWrappers.exchange.filled.callAsync(orderHash);
return unavailableTakerAmount;
}
public getExchangeContractAddressIfExists(): string | undefined {
@@ -340,10 +341,13 @@ export class Blockchain {
fillTakerTokenAmount: BigNumber,
takerAddress: string,
): Promise<void> {
await this._contractWrappers.exchange.validateFillOrderThrowIfInvalidAsync(
await this._contractWrappers.exchange.fillOrder.callAsync(
signedOrder,
fillTakerTokenAmount,
takerAddress,
signedOrder.signature,
{
from: takerAddress,
},
);
}
public isValidAddress(address: string): boolean {
@@ -420,14 +424,12 @@ export class Blockchain {
utils.assert(this._doesUserAddressExist(), BlockchainCallErrs.UserHasNoAssociatedAddresses);
this._showFlashMessageIfLedger();
const txHash = await this._contractWrappers.etherToken.depositAsync(
etherTokenAddress,
amount,
this._userAddressIfExists,
{
gasPrice: this._defaultGasPrice,
},
);
const etherToken = new WETH9Contract(etherTokenAddress, this._contractWrappers.getProvider());
const txHash = await etherToken.deposit.validateAndSendTransactionAsync({
value: amount,
from: this._userAddressIfExists,
gasPrice: this._defaultGasPrice,
});
await this._showEtherScanLinkAndAwaitTransactionMinedAsync(txHash);
}
public async convertWrappedEthTokensToEthAsync(etherTokenAddress: string, amount: BigNumber): Promise<void> {
@@ -435,14 +437,11 @@ export class Blockchain {
utils.assert(this._doesUserAddressExist(), BlockchainCallErrs.UserHasNoAssociatedAddresses);
this._showFlashMessageIfLedger();
const txHash = await this._contractWrappers.etherToken.withdrawAsync(
etherTokenAddress,
amount,
this._userAddressIfExists,
{
gasPrice: this._defaultGasPrice,
},
);
const etherToken = new WETH9Contract(etherTokenAddress, this._contractWrappers.getProvider());
const txHash = await etherToken.withdraw.validateAndSendTransactionAsync(amount, {
from: this._userAddressIfExists,
gasPrice: this._defaultGasPrice,
});
await this._showEtherScanLinkAndAwaitTransactionMinedAsync(txHash);
}
public async doesContractExistAtAddressAsync(address: string): Promise<boolean> {
@@ -471,9 +470,13 @@ export class Blockchain {
let balance = new BigNumber(0);
let allowance = new BigNumber(0);
if (this._doesUserAddressExist()) {
const erc20Token = new ERC20TokenContract(tokenAddress, this._contractWrappers.getProvider());
[balance, allowance] = await Promise.all([
this._contractWrappers.erc20Token.getBalanceAsync(tokenAddress, ownerAddressIfExists),
this._contractWrappers.erc20Token.getProxyAllowanceAsync(tokenAddress, ownerAddressIfExists),
erc20Token.balanceOf.callAsync(ownerAddressIfExists),
erc20Token.allowance.callAsync(
ownerAddressIfExists,
this._contractWrappers.contractAddresses.erc20Proxy,
),
]);
}
return [balance, allowance];

View File

@@ -1,5 +1,5 @@
import { getContractAddressesForNetworkOrThrow } from '@0x/contract-addresses';
import { ContractWrappers } from '@0x/contract-wrappers';
import { ContractWrappers, ERC20TokenContract } from '@0x/contract-wrappers';
import {
ledgerEthereumBrowserClientFactoryAsync,
LedgerSubprovider,
@@ -221,8 +221,9 @@ export class ConnectForm extends React.Component<Props, State> {
utils.assert(this._contractWrappers !== undefined, 'ContractWrappers must be instantiated.');
const contractAddresses = getContractAddressesForNetworkOrThrow(this.networkId);
const tokenAddress: string = contractAddresses.zrxToken;
const erc20Token = new ERC20TokenContract(tokenAddress, this._contractWrappers.getProvider());
try {
const amount = await this._contractWrappers.erc20Token.getBalanceAsync(tokenAddress, owner);
const amount = await erc20Token.balanceOf.callAsync(owner);
return amount;
} catch (error) {
return ZERO;

View File

@@ -1,4 +1,4 @@
import { ContractWrappersError } from '@0x/contract-wrappers';
import { ContractError } from '@0x/contract-wrappers';
import { assetDataUtils, TypedDataError } from '@0x/order-utils';
import { ExchangeContractErrs } from '@0x/types';
import { BigNumber } from '@0x/utils';
@@ -233,13 +233,13 @@ export const utils = {
const isUniqueSymbol = tokenWithSameSymbolIfExists === undefined;
return isUniqueName && isUniqueSymbol;
},
zeroExErrToHumanReadableErrMsg(error: ContractWrappersError | ExchangeContractErrs, takerAddress: string): string {
const ContractWrappersErrorToHumanReadableError: { [error: string]: string } = {
zeroExErrToHumanReadableErrMsg(error: ContractError | ExchangeContractErrs, takerAddress: string): string {
const ContractErrorToHumanReadableError: { [error: string]: string } = {
[BlockchainCallErrs.UserHasNoAssociatedAddresses]: 'User has no addresses available',
[TypedDataError.InvalidSignature]: 'Order signature is not valid',
[ContractWrappersError.ContractNotDeployedOnNetwork]: 'Contract is not deployed on the detected network',
[ContractWrappersError.InvalidJump]: 'Invalid jump occured while executing the transaction',
[ContractWrappersError.OutOfGas]: 'Transaction ran out of gas',
[ContractError.ContractNotDeployedOnNetwork]: 'Contract is not deployed on the detected network',
[ContractError.InvalidJump]: 'Invalid jump occured while executing the transaction',
[ContractError.OutOfGas]: 'Transaction ran out of gas',
};
const exchangeContractErrorToHumanReadableError: {
[error: string]: string;
@@ -269,7 +269,7 @@ export const utils = {
[ExchangeContractErrs.InsufficientRemainingFillAmount]: 'Insufficient remaining fill amount',
};
const humanReadableErrorMsg =
exchangeContractErrorToHumanReadableError[error] || ContractWrappersErrorToHumanReadableError[error];
exchangeContractErrorToHumanReadableError[error] || ContractErrorToHumanReadableError[error];
return humanReadableErrorMsg;
},
isParityNode(nodeVersion: string): boolean {