1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
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 { addressUtils, BigNumber } from '@0x/utils';
import * as chai from 'chai';
import * as _ from 'lodash';
import 'mocha';
import { ContractAddresses, ContractWrappers } from '../src';
import { chaiSetup } from './utils/chai_setup';
import { migrateOnceAsync } from './utils/migrate';
import { provider, web3Wrapper } from './utils/web3_wrapper';
chaiSetup.configure();
const expect = chai.expect;
const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
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)';
let signedOrderLeft: SignedOrder;
let signedOrderRight: SignedOrder;
let orderLeft = {};
let orderRight = {};
let matchOrdersTxData: string;
let contractAddresses: ContractAddresses;
let contractWrappers: ContractWrappers;
before(async () => {
// Create accounts
const accounts = await web3Wrapper.getAvailableAddressesAsync();
const [makerAddressLeft, makerAddressRight] = accounts;
const [privateKeyLeft, privateKeyRight] = constants.TESTRPC_PRIVATE_KEYS;
const exchangeAddress = addressUtils.generatePseudoRandomAddress();
const feeRecipientAddress = addressUtils.generatePseudoRandomAddress();
// Create orders to match.
// Values are arbitrary, with the exception of maker addresses (generated above).
orderLeft = {
makerAddress: makerAddressLeft,
makerAssetData: assetDataUtils.encodeERC20AssetData(defaultERC20MakerAssetAddress),
makerAssetAmount: new BigNumber(10),
takerAddress: '0x0000000000000000000000000000000000000000',
takerAssetData: assetDataUtils.encodeERC20AssetData(defaultERC20MakerAssetAddress),
takerAssetAmount: new BigNumber(1),
feeRecipientAddress,
makerFee: new BigNumber(0),
takerFee: new BigNumber(0),
senderAddress: '0x0000000000000000000000000000000000000000',
expirationTimeSeconds: new BigNumber(1549498915),
salt: new BigNumber(217),
};
orderRight = {
makerAddress: makerAddressRight,
makerAssetData: assetDataUtils.encodeERC20AssetData(defaultERC20MakerAssetAddress),
makerAssetAmount: new BigNumber(1),
takerAddress: '0x0000000000000000000000000000000000000000',
takerAssetData: assetDataUtils.encodeERC20AssetData(defaultERC20MakerAssetAddress),
takerAssetAmount: new BigNumber(8),
feeRecipientAddress,
makerFee: new BigNumber(0),
takerFee: new BigNumber(0),
senderAddress: '0x0000000000000000000000000000000000000000',
expirationTimeSeconds: new BigNumber(1549498915),
salt: new BigNumber(50010),
};
const orderFactoryLeft = new OrderFactory(privateKeyLeft, orderLeft);
signedOrderLeft = await orderFactoryLeft.newSignedOrderAsync({ exchangeAddress });
const orderFactoryRight = new OrderFactory(privateKeyRight, orderRight);
signedOrderRight = await orderFactoryRight.newSignedOrderAsync({ exchangeAddress });
// Encode match orders transaction
contractAddresses = await migrateOnceAsync();
await blockchainLifecycle.startAsync();
const config = {
networkId: constants.TESTRPC_NETWORK_ID,
contractAddresses,
blockPollingIntervalMs: 10,
};
contractWrappers = new ContractWrappers(provider, config);
const transactionEncoder = await contractWrappers.exchange.transactionEncoderAsync();
matchOrdersTxData = transactionEncoder.matchOrdersTx(signedOrderLeft, signedOrderRight);
});
describe('decode', () => {
it('should successfully decode DutchAuction.matchOrders calldata', async () => {
const contractName = 'DutchAuction';
const decodedTxData = contractWrappers
.getAbiDecoder()
.decodeCalldataOrThrow(matchOrdersTxData, contractName);
const expectedFunctionName = 'matchOrders';
const expectedFunctionArguments = {
buyOrder: orderLeft,
sellOrder: orderRight,
buySignature: signedOrderLeft.signature,
sellSignature: 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 successfully decode Exchange.matchOrders calldata (and distinguish from DutchAuction.matchOrders)', async () => {
const contractName = 'Exchange';
const decodedTxData = contractWrappers
.getAbiDecoder()
.decodeCalldataOrThrow(matchOrdersTxData, contractName);
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 calldata', async () => {
const badTxData = '0x01020304';
expect(() => {
contractWrappers.getAbiDecoder().decodeCalldataOrThrow(badTxData);
}).to.throw("No functions registered for selector '0x01020304'");
});
});
});
|