Merged tx decoder into AbiDecoder in utils and merged zeroex tx decoder into ContractWrappers.

This commit is contained in:
Greg Hysen
2019-02-06 23:47:40 -08:00
parent 6bde77bb57
commit 6406126ae3
10 changed files with 145 additions and 339 deletions

View File

@@ -2,14 +2,12 @@ import { constants, OrderFactory } from '@0x/contracts-test-utils';
import { BlockchainLifecycle } from '@0x/dev-utils';
import { assetDataUtils } from '@0x/order-utils';
import { SignedOrder } from '@0x/types';
import { AbiEncoder, addressUtils, BigNumber } from '@0x/utils';
import { addressUtils, BigNumber } from '@0x/utils';
import * as chai from 'chai';
import { MethodAbi } from 'ethereum-types';
import * as _ from 'lodash';
import 'mocha';
import { ContractAddresses, ContractWrappers } from '../src';
import { ZeroExTransactionDecoder } from '../src/utils/zeroex_transaction_decoder';
import { chaiSetup } from './utils/chai_setup';
import { migrateOnceAsync } from './utils/migrate';
@@ -20,7 +18,7 @@ const expect = chai.expect;
const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
describe.only('ZeroExTransactionDecoder', () => {
describe('ABI Decoding Calldata', () => {
const defaultERC20MakerAssetAddress = addressUtils.generatePseudoRandomAddress();
const matchOrdersSignature =
'matchOrders((address,address,address,address,uint256,uint256,uint256,uint256,uint256,uint256,bytes,bytes),(address,address,address,address,uint256,uint256,uint256,uint256,uint256,uint256,bytes,bytes),bytes,bytes)';
@@ -30,6 +28,7 @@ describe.only('ZeroExTransactionDecoder', () => {
let orderRight = {};
let matchOrdersTxData: string;
let contractAddresses: ContractAddresses;
let contractWrappers: ContractWrappers;
before(async () => {
// Create accounts
@@ -80,15 +79,15 @@ describe.only('ZeroExTransactionDecoder', () => {
contractAddresses,
blockPollingIntervalMs: 10,
};
const contractWrappers = new ContractWrappers(provider, config);
contractWrappers = new ContractWrappers(provider, config);
const transactionEncoder = await contractWrappers.exchange.transactionEncoderAsync();
matchOrdersTxData = transactionEncoder.matchOrdersTx(signedOrderLeft, signedOrderRight);
});
describe('decode', () => {
it('should successfully decode DutchAuction.matchOrders txData', async () => {
it('should successfully decode DutchAuction.matchOrders calldata', async () => {
const contractName = 'DutchAuction';
const decodedTxData = ZeroExTransactionDecoder.decode(matchOrdersTxData, { contractName });
const decodedTxData = contractWrappers.getAbiDecoder().tryDecodeCalldata(matchOrdersTxData, contractName);
const expectedFunctionName = 'matchOrders';
const expectedFunctionArguments = {
buyOrder: orderLeft,
@@ -100,9 +99,9 @@ describe.only('ZeroExTransactionDecoder', () => {
expect(decodedTxData.functionSignature).to.be.equal(matchOrdersSignature);
expect(decodedTxData.functionArguments).to.be.deep.equal(expectedFunctionArguments);
});
it('should successfully decode Exchange.matchOrders txData (and distinguish from DutchAuction.matchOrders)', async () => {
it('should successfully decode Exchange.matchOrders calldata (and distinguish from DutchAuction.matchOrders)', async () => {
const contractName = 'Exchange';
const decodedTxData = ZeroExTransactionDecoder.decode(matchOrdersTxData, { contractName });
const decodedTxData = contractWrappers.getAbiDecoder().tryDecodeCalldata(matchOrdersTxData, contractName);
const expectedFunctionName = 'matchOrders';
const expectedFunctionArguments = {
leftOrder: orderLeft,
@@ -114,77 +113,11 @@ describe.only('ZeroExTransactionDecoder', () => {
expect(decodedTxData.functionSignature).to.be.equal(matchOrdersSignature);
expect(decodedTxData.functionArguments).to.be.deep.equal(expectedFunctionArguments);
});
it('should successfully decode Exchange.matchOrders, using exchange address to identify the exchange contract', async () => {
const contractAddress = contractAddresses.exchange;
const decodedTxData = ZeroExTransactionDecoder.decode(matchOrdersTxData, { contractAddress });
const expectedFunctionName = 'matchOrders';
const expectedFunctionArguments = {
leftOrder: orderLeft,
rightOrder: orderRight,
leftSignature: signedOrderLeft.signature,
rightSignature: signedOrderRight.signature,
};
expect(decodedTxData.functionName).to.be.equal(expectedFunctionName);
expect(decodedTxData.functionSignature).to.be.equal(matchOrdersSignature);
expect(decodedTxData.functionArguments).to.be.deep.equal(expectedFunctionArguments);
});
it('should throw if cannot decode txData', async () => {
const contractAddress = contractAddresses.exchange;
it('should throw if cannot decode calldata', async () => {
const badTxData = '0x01020304';
expect(() => {
ZeroExTransactionDecoder.decode(badTxData, { contractAddress });
contractWrappers.getAbiDecoder().tryDecodeCalldata(badTxData);
}).to.throw("No functions registered for selector '0x01020304'");
});
});
describe('addABI', () => {
it('should successfully add a new ABI', async () => {
// Add new ABI
const abi: MethodAbi = {
name: 'foobar',
type: 'function',
inputs: [
{
name: 'addr',
type: 'address',
},
],
outputs: [
{
name: 'butter',
type: 'string',
},
],
constant: false,
payable: false,
stateMutability: 'pure',
};
const contractName = 'newContract';
const contractAddress = addressUtils.generatePseudoRandomAddress();
const networkId = 1;
const contractInfo = [
{
contractAddress,
networkId,
},
];
ZeroExTransactionDecoder.addABI([abi], contractName, contractInfo);
// Create some tx data
const foobarEncoder = new AbiEncoder.Method(abi);
const foobarSignature = foobarEncoder.getSignature();
const foobarTxData = foobarEncoder.encode([contractAddress]);
// Decode tx data using contract name
const decodedTxData = ZeroExTransactionDecoder.decode(foobarTxData, { contractName });
const expectedFunctionName = abi.name;
const expectedFunctionArguments = {
addr: contractAddress,
};
expect(decodedTxData.functionName).to.be.equal(expectedFunctionName);
expect(decodedTxData.functionSignature).to.be.equal(foobarSignature);
expect(decodedTxData.functionArguments).to.be.deep.equal(expectedFunctionArguments);
// Decode tx data using contract address
const decodedTxDataDecodedWithAddress = ZeroExTransactionDecoder.decode(foobarTxData, { contractAddress });
expect(decodedTxDataDecodedWithAddress).to.be.deep.equal(decodedTxData);
});
});
});