Replace assetDataUtils with DevUtilsContract wherever possible (#2304)

* Replace assetDataUtils with DevUtilsContract wherever possible

Does not replace from @0x/instant and some @0x/order-utils uses

* Add revertIfInvalidAssetData to LibAssetData

This is needed to replace `assetDataUtils.decodeAssetDataOrThrow`.
Because it's used in packages and not only contracts, we should wait
to deploy the updated contract so we can update `@0x/contract-artifacts`,
`@0x/abi-gen-wrappers`, and `@0x/contract-wrappers` first.

* remove usages of signatureUtils

* fix test for  optimised encoding

* refactor @0x/contracts-integrations

* update changelogs

* Move @0x/contracts-dev-utils from devDependencies to dependencies
It is exported as part of the package
This commit is contained in:
Xianny
2019-11-06 19:40:20 -08:00
committed by GitHub
parent ec26cff656
commit 6a852ab0ed
56 changed files with 1408 additions and 970 deletions

View File

@@ -73,6 +73,7 @@
"dependencies": {
"@0x/base-contract": "^5.5.0-beta.0",
"@0x/contracts-asset-proxy": "^2.3.0-beta.0",
"@0x/contracts-dev-utils": "^0.1.0-beta.0",
"@0x/contracts-erc20": "^2.3.0-beta.0",
"@0x/contracts-erc721": "^2.2.0-beta.0",
"@0x/contracts-exchange": "^2.2.0-beta.0",

View File

@@ -1,6 +1,7 @@
import { DevUtilsContract } from '@0x/contracts-dev-utils';
import { ExchangeContract, ExchangeWrapper } from '@0x/contracts-exchange';
import { BlockchainLifecycle } from '@0x/dev-utils';
import { assetDataUtils, ExchangeRevertErrors } from '@0x/order-utils';
import { ExchangeRevertErrors } from '@0x/order-utils';
import { Order, RevertReason, SignedOrder } from '@0x/types';
import { BigNumber, providerUtils } from '@0x/utils';
import { Web3Wrapper } from '@0x/web3-wrapper';
@@ -52,6 +53,7 @@ describe(ContractName.BalanceThresholdFilter, () => {
let zrxToken: DummyERC20TokenContract;
let exchangeInstance: ExchangeContract;
let exchangeWrapper: ExchangeWrapper;
let devUtils: DevUtilsContract;
let orderFactory: OrderFactory;
let orderFactory2: OrderFactory;
@@ -105,6 +107,7 @@ describe(ContractName.BalanceThresholdFilter, () => {
const makerPrivateKey = constants.TESTRPC_PRIVATE_KEYS[accounts.indexOf(validMakerAddress)];
const secondMakerPrivateKey = constants.TESTRPC_PRIVATE_KEYS[accounts.indexOf(validMakerAddress2)];
const invalidAddressPrivateKey = constants.TESTRPC_PRIVATE_KEYS[accounts.indexOf(invalidAddress)];
devUtils = new DevUtilsContract(constants.NULL_ADDRESS, provider);
// Create wrappers
erc20Wrapper = new ERC20Wrapper(provider, usedAddresses, owner);
const validAddresses = _.cloneDeepWith(usedAddresses);
@@ -123,7 +126,7 @@ describe(ContractName.BalanceThresholdFilter, () => {
);
defaultMakerAssetAddress = erc20TokenA.address;
defaultTakerAssetAddress = erc20TokenB.address;
zrxAssetData = assetDataUtils.encodeERC20AssetData(zrxToken.address);
zrxAssetData = await devUtils.encodeERC20AssetData.callAsync(zrxToken.address);
// Create proxies
const erc20Proxy = await erc20Wrapper.deployProxyAsync();
await erc20Wrapper.setBalancesAndAllowancesAsync();
@@ -170,8 +173,8 @@ describe(ContractName.BalanceThresholdFilter, () => {
// Default order parameters
defaultOrderParams = {
feeRecipientAddress,
makerAssetData: assetDataUtils.encodeERC20AssetData(defaultMakerAssetAddress),
takerAssetData: assetDataUtils.encodeERC20AssetData(defaultTakerAssetAddress),
makerAssetData: await devUtils.encodeERC20AssetData.callAsync(defaultMakerAssetAddress),
takerAssetData: await devUtils.encodeERC20AssetData.callAsync(defaultTakerAssetAddress),
makerAssetAmount,
takerAssetAmount,
makerFee: Web3Wrapper.toBaseUnitAmount(new BigNumber(100), DECIMALS_DEFAULT),
@@ -1236,8 +1239,8 @@ describe(ContractName.BalanceThresholdFilter, () => {
feeRecipientAddress,
});
const signedOrderRight = await orderFactory2.newSignedOrderAsync({
makerAssetData: assetDataUtils.encodeERC20AssetData(defaultTakerAssetAddress),
takerAssetData: assetDataUtils.encodeERC20AssetData(defaultMakerAssetAddress),
makerAssetData: await devUtils.encodeERC20AssetData.callAsync(defaultTakerAssetAddress),
takerAssetData: await devUtils.encodeERC20AssetData.callAsync(defaultMakerAssetAddress),
makerAssetAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber(75), 0),
takerAssetAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber(13), 0),
makerFee: Web3Wrapper.toBaseUnitAmount(new BigNumber(1), 18),

View File

@@ -1,4 +1,5 @@
import { ERC20Wrapper, ERC721Wrapper } from '@0x/contracts-asset-proxy';
import { DevUtilsContract } from '@0x/contracts-dev-utils';
import { DummyERC20TokenContract } from '@0x/contracts-erc20';
import { DummyERC721TokenContract } from '@0x/contracts-erc721';
import { ExchangeContract, ExchangeWrapper } from '@0x/contracts-exchange';
@@ -59,6 +60,8 @@ describe(ContractName.DutchAuction, () => {
let dutchAuctionTestWrapper: DutchAuctionTestWrapper;
let defaultERC20MakerAssetData: string;
const devUtils = new DevUtilsContract(constants.NULL_ADDRESS, provider);
before(async () => {
await blockchainLifecycle.startAsync();
@@ -86,7 +89,7 @@ describe(ContractName.DutchAuction, () => {
wethContract = await WETH9Contract.deployFrom0xArtifactAsync(artifacts.WETH9, provider, txDefaults, artifacts);
erc20Wrapper.addDummyTokenContract(wethContract as any);
const zrxAssetData = assetDataUtils.encodeERC20AssetData(zrxToken.address);
const zrxAssetData = await devUtils.encodeERC20AssetData.callAsync(zrxToken.address);
const exchangeInstance = await ExchangeContract.deployFrom0xArtifactAsync(
artifacts.Exchange,
provider,
@@ -153,11 +156,11 @@ describe(ContractName.DutchAuction, () => {
// taker address or sender address should be set to the ducth auction contract
takerAddress: dutchAuctionContract.address,
makerAssetData: assetDataUtils.encodeDutchAuctionAssetData(
assetDataUtils.encodeERC20AssetData(defaultMakerAssetAddress),
await devUtils.encodeERC20AssetData.callAsync(defaultMakerAssetAddress),
auctionBeginTimeSeconds,
auctionBeginAmount,
),
takerAssetData: assetDataUtils.encodeERC20AssetData(defaultTakerAssetAddress),
takerAssetData: await devUtils.encodeERC20AssetData.callAsync(defaultTakerAssetAddress),
makerAssetAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber(200), DECIMALS_DEFAULT),
takerAssetAmount: auctionEndAmount,
expirationTimeSeconds: auctionEndTimeSeconds,
@@ -179,7 +182,7 @@ describe(ContractName.DutchAuction, () => {
const takerPrivateKey = constants.TESTRPC_PRIVATE_KEYS[accounts.indexOf(takerAddress)];
sellerOrderFactory = new OrderFactory(makerPrivateKey, sellerDefaultOrderParams);
buyerOrderFactory = new OrderFactory(takerPrivateKey, buyerDefaultOrderParams);
defaultERC20MakerAssetData = assetDataUtils.encodeERC20AssetData(defaultMakerAssetAddress);
defaultERC20MakerAssetData = await devUtils.encodeERC20AssetData.callAsync(defaultMakerAssetAddress);
});
after(async () => {
await blockchainLifecycle.revertAsync();
@@ -318,7 +321,7 @@ describe(ContractName.DutchAuction, () => {
});
it('asset data contains auction parameters', async () => {
sellOrder = await sellerOrderFactory.newSignedOrderAsync({
makerAssetData: assetDataUtils.encodeERC20AssetData(defaultMakerAssetAddress),
makerAssetData: await devUtils.encodeERC20AssetData.callAsync(defaultMakerAssetAddress),
});
const tx = dutchAuctionTestWrapper.matchOrdersAsync(buyOrder, sellOrder, takerAddress);
return expect(tx).to.revertWith(RevertReason.InvalidAssetData);
@@ -327,7 +330,10 @@ describe(ContractName.DutchAuction, () => {
describe('ERC721', () => {
it('should match orders when ERC721', async () => {
const makerAssetId = erc721MakerAssetIds[0];
const erc721MakerAssetData = assetDataUtils.encodeERC721AssetData(erc721Token.address, makerAssetId);
const erc721MakerAssetData = await devUtils.encodeERC721AssetData.callAsync(
erc721Token.address,
makerAssetId,
);
const makerAssetData = assetDataUtils.encodeDutchAuctionAssetData(
erc721MakerAssetData,
auctionBeginTimeSeconds,

View File

@@ -4,6 +4,7 @@ import {
ERC20Wrapper,
ERC721ProxyContract,
} from '@0x/contracts-asset-proxy';
import { DevUtilsContract } from '@0x/contracts-dev-utils';
import { DummyERC20TokenContract } from '@0x/contracts-erc20';
import { artifacts as erc721Artifacts, DummyERC721TokenContract } from '@0x/contracts-erc721';
import { ExchangeContract, ExchangeWrapper } from '@0x/contracts-exchange';
@@ -20,7 +21,7 @@ import {
web3Wrapper,
} from '@0x/contracts-test-utils';
import { BlockchainLifecycle } from '@0x/dev-utils';
import { assetDataUtils, ExchangeRevertErrors } from '@0x/order-utils';
import { ExchangeRevertErrors } from '@0x/order-utils';
import { RevertReason } from '@0x/types';
import { BigNumber, providerUtils } from '@0x/utils';
import { Web3Wrapper } from '@0x/web3-wrapper';
@@ -62,6 +63,7 @@ describe('OrderMatcher', () => {
let defaultERC20MakerAssetAddress: string;
let defaultERC20TakerAssetAddress: string;
const devUtils = new DevUtilsContract(constants.NULL_ADDRESS, provider, txDefaults);
before(async () => {
await blockchainLifecycle.startAsync();
});
@@ -112,7 +114,7 @@ describe('OrderMatcher', () => {
provider,
txDefaults,
artifacts,
assetDataUtils.encodeERC20AssetData(zrxToken.address),
await devUtils.encodeERC20AssetData.callAsync(zrxToken.address),
new BigNumber(chainId),
);
exchangeWrapper = new ExchangeWrapper(exchange);
@@ -136,8 +138,8 @@ describe('OrderMatcher', () => {
// Set default addresses
defaultERC20MakerAssetAddress = erc20TokenA.address;
defaultERC20TakerAssetAddress = erc20TokenB.address;
leftMakerAssetData = assetDataUtils.encodeERC20AssetData(defaultERC20MakerAssetAddress);
leftTakerAssetData = assetDataUtils.encodeERC20AssetData(defaultERC20TakerAssetAddress);
leftMakerAssetData = await devUtils.encodeERC20AssetData.callAsync(defaultERC20MakerAssetAddress);
leftTakerAssetData = await devUtils.encodeERC20AssetData.callAsync(defaultERC20TakerAssetAddress);
// Set OrderMatcher balances and allowances
await web3Wrapper.awaitTransactionSuccessAsync(
await erc20TokenA.setBalance.sendTransactionAsync(orderMatcher.address, constants.INITIAL_ERC20_BALANCE, {
@@ -743,7 +745,7 @@ describe('OrderMatcher', () => {
await erc721Token.mint.sendTransactionAsync(orderMatcher.address, tokenId, { from: owner }),
constants.AWAIT_TRANSACTION_MINED_MS,
);
const assetData = assetDataUtils.encodeERC721AssetData(erc721Token.address, tokenId);
const assetData = await devUtils.encodeERC721AssetData.callAsync(erc721Token.address, tokenId);
const withdrawAmount = new BigNumber(1);
await web3Wrapper.awaitTransactionSuccessAsync(
await orderMatcher.withdrawAsset.sendTransactionAsync(assetData, withdrawAmount, { from: owner }),
@@ -782,7 +784,10 @@ describe('OrderMatcher', () => {
constants.DUMMY_TOKEN_NAME,
constants.DUMMY_TOKEN_SYMBOL,
);
const assetData = assetDataUtils.encodeERC721AssetData(erc721Token.address, constants.ZERO_AMOUNT);
const assetData = await devUtils.encodeERC721AssetData.callAsync(
erc721Token.address,
constants.ZERO_AMOUNT,
);
const allowance = new BigNumber(1);
await web3Wrapper.awaitTransactionSuccessAsync(
await orderMatcher.approveAssetProxy.sendTransactionAsync(assetData, allowance, { from: owner }),
@@ -800,7 +805,10 @@ describe('OrderMatcher', () => {
constants.DUMMY_TOKEN_NAME,
constants.DUMMY_TOKEN_SYMBOL,
);
const assetData = assetDataUtils.encodeERC721AssetData(erc721Token.address, constants.ZERO_AMOUNT);
const assetData = await devUtils.encodeERC721AssetData.callAsync(
erc721Token.address,
constants.ZERO_AMOUNT,
);
const allowance = new BigNumber(2);
await web3Wrapper.awaitTransactionSuccessAsync(
await orderMatcher.approveAssetProxy.sendTransactionAsync(assetData, allowance, { from: owner }),