aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contract-wrappers/test
diff options
context:
space:
mode:
authorJacob Evans <jacob@dekz.net>2018-08-17 11:03:07 +0800
committerJacob Evans <jacob@dekz.net>2018-08-18 07:32:29 +0800
commit1c68057999ad9cb8be5018bffeeb6c1e45e645ad (patch)
treed6eae448d703f75c8c2527d5b988bb408d48d8f9 /packages/contract-wrappers/test
parent36668f94090fa5c35f636f6b7ba597fcb89c068d (diff)
downloaddexon-sol-tools-1c68057999ad9cb8be5018bffeeb6c1e45e645ad.tar
dexon-sol-tools-1c68057999ad9cb8be5018bffeeb6c1e45e645ad.tar.gz
dexon-sol-tools-1c68057999ad9cb8be5018bffeeb6c1e45e645ad.tar.bz2
dexon-sol-tools-1c68057999ad9cb8be5018bffeeb6c1e45e645ad.tar.lz
dexon-sol-tools-1c68057999ad9cb8be5018bffeeb6c1e45e645ad.tar.xz
dexon-sol-tools-1c68057999ad9cb8be5018bffeeb6c1e45e645ad.tar.zst
dexon-sol-tools-1c68057999ad9cb8be5018bffeeb6c1e45e645ad.zip
Rename to Transaction Encoder.
Add tests for all encoding methods.
Diffstat (limited to 'packages/contract-wrappers/test')
-rw-r--r--packages/contract-wrappers/test/execute_transaction_encoder_test.ts113
-rw-r--r--packages/contract-wrappers/test/transaction_encoder_test.ts211
2 files changed, 211 insertions, 113 deletions
diff --git a/packages/contract-wrappers/test/execute_transaction_encoder_test.ts b/packages/contract-wrappers/test/execute_transaction_encoder_test.ts
deleted file mode 100644
index 13635dfb8..000000000
--- a/packages/contract-wrappers/test/execute_transaction_encoder_test.ts
+++ /dev/null
@@ -1,113 +0,0 @@
-import { BlockchainLifecycle } from '@0xproject/dev-utils';
-import { FillScenarios } from '@0xproject/fill-scenarios';
-import { assetDataUtils, ecSignOrderHashAsync, generatePseudoRandomSalt } from '@0xproject/order-utils';
-import { SignedOrder, SignerType } from '@0xproject/types';
-import { BigNumber } from '@0xproject/utils';
-import * as chai from 'chai';
-import 'mocha';
-
-import { ContractWrappers } from '../src';
-
-import { chaiSetup } from './utils/chai_setup';
-import { constants } from './utils/constants';
-import { tokenUtils } from './utils/token_utils';
-import { provider, web3Wrapper } from './utils/web3_wrapper';
-
-chaiSetup.configure();
-const expect = chai.expect;
-const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
-
-describe('ExecuteTransactionEncoder', () => {
- let contractWrappers: ContractWrappers;
- let userAddresses: string[];
- let zrxTokenAddress: string;
- let fillScenarios: FillScenarios;
- let exchangeContractAddress: string;
- let makerTokenAddress: string;
- let takerTokenAddress: string;
- let coinbase: string;
- let makerAddress: string;
- let senderAddress: string;
- let takerAddress: string;
- let makerAssetData: string;
- let takerAssetData: string;
- let feeRecipient: string;
- let txHash: string;
- const fillableAmount = new BigNumber(5);
- const takerTokenFillAmount = new BigNumber(5);
- let signedOrder: SignedOrder;
- let anotherSignedOrder: SignedOrder;
- const config = {
- networkId: constants.TESTRPC_NETWORK_ID,
- blockPollingIntervalMs: 0,
- };
- before(async () => {
- await blockchainLifecycle.startAsync();
- contractWrappers = new ContractWrappers(provider, config);
- exchangeContractAddress = contractWrappers.exchange.getContractAddress();
- userAddresses = await web3Wrapper.getAvailableAddressesAsync();
- zrxTokenAddress = tokenUtils.getProtocolTokenAddress();
- fillScenarios = new FillScenarios(
- provider,
- userAddresses,
- zrxTokenAddress,
- exchangeContractAddress,
- contractWrappers.erc20Proxy.getContractAddress(),
- contractWrappers.erc721Proxy.getContractAddress(),
- );
- [coinbase, makerAddress, takerAddress, feeRecipient, senderAddress] = userAddresses;
- [makerTokenAddress, takerTokenAddress] = tokenUtils.getDummyERC20TokenAddresses();
- [makerAssetData, takerAssetData] = [
- assetDataUtils.encodeERC20AssetData(makerTokenAddress),
- assetDataUtils.encodeERC20AssetData(takerTokenAddress),
- ];
- signedOrder = await fillScenarios.createFillableSignedOrderAsync(
- makerAssetData,
- takerAssetData,
- makerAddress,
- takerAddress,
- fillableAmount,
- );
- anotherSignedOrder = await fillScenarios.createFillableSignedOrderAsync(
- makerAssetData,
- takerAssetData,
- makerAddress,
- takerAddress,
- fillableAmount,
- );
- });
- after(async () => {
- await blockchainLifecycle.revertAsync();
- });
- beforeEach(async () => {
- await blockchainLifecycle.startAsync();
- });
- afterEach(async () => {
- await blockchainLifecycle.revertAsync();
- });
- describe('encoder', () => {
- describe('#fillOrderAsync', () => {
- it('should fill a valid order', async () => {
- const encoder = await contractWrappers.exchange.executeTransactionEncoderAsync();
- const salt = generatePseudoRandomSalt();
- const signerAddress = takerAddress;
- const data = encoder.fillOrder(signedOrder, takerTokenFillAmount);
- const encodedTransaction = encoder.getExecuteTransactionHex(data, salt, signerAddress);
- const signature = await ecSignOrderHashAsync(
- provider,
- encodedTransaction,
- signerAddress,
- SignerType.Default,
- );
- txHash = await contractWrappers.exchange.executeTransactionAsync(
- salt,
- signerAddress,
- data,
- signature,
- senderAddress,
- );
- await web3Wrapper.awaitTransactionSuccessAsync(txHash, constants.AWAIT_TRANSACTION_MINED_MS);
- });
- });
- });
-});
diff --git a/packages/contract-wrappers/test/transaction_encoder_test.ts b/packages/contract-wrappers/test/transaction_encoder_test.ts
new file mode 100644
index 000000000..10222dbc1
--- /dev/null
+++ b/packages/contract-wrappers/test/transaction_encoder_test.ts
@@ -0,0 +1,211 @@
+import { BlockchainLifecycle } from '@0xproject/dev-utils';
+import { FillScenarios } from '@0xproject/fill-scenarios';
+import { assetDataUtils, ecSignOrderHashAsync, generatePseudoRandomSalt, orderHashUtils } from '@0xproject/order-utils';
+import { SignedOrder, SignerType } from '@0xproject/types';
+import { BigNumber } from '@0xproject/utils';
+import 'mocha';
+
+import { ContractWrappers } from '../src';
+import { TransactionEncoder } from '../src/utils/transaction_encoder';
+
+import { constants } from './utils/constants';
+import { tokenUtils } from './utils/token_utils';
+import { provider, web3Wrapper } from './utils/web3_wrapper';
+
+const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
+
+describe('TransactionEncoder', () => {
+ let contractWrappers: ContractWrappers;
+ let userAddresses: string[];
+ let fillScenarios: FillScenarios;
+ let exchangeContractAddress: string;
+ let makerTokenAddress: string;
+ let takerTokenAddress: string;
+ let coinbase: string;
+ let makerAddress: string;
+ let senderAddress: string;
+ let takerAddress: string;
+ let makerAssetData: string;
+ let takerAssetData: string;
+ let txHash: string;
+ const fillableAmount = new BigNumber(5);
+ const takerTokenFillAmount = new BigNumber(5);
+ let signedOrder: SignedOrder;
+ const config = {
+ networkId: constants.TESTRPC_NETWORK_ID,
+ blockPollingIntervalMs: 0,
+ };
+ before(async () => {
+ await blockchainLifecycle.startAsync();
+ contractWrappers = new ContractWrappers(provider, config);
+ exchangeContractAddress = contractWrappers.exchange.getContractAddress();
+ userAddresses = await web3Wrapper.getAvailableAddressesAsync();
+ const zrxTokenAddress = tokenUtils.getProtocolTokenAddress();
+ fillScenarios = new FillScenarios(
+ provider,
+ userAddresses,
+ zrxTokenAddress,
+ exchangeContractAddress,
+ contractWrappers.erc20Proxy.getContractAddress(),
+ contractWrappers.erc721Proxy.getContractAddress(),
+ );
+ [coinbase, makerAddress, takerAddress, senderAddress] = userAddresses;
+ [makerTokenAddress, takerTokenAddress] = tokenUtils.getDummyERC20TokenAddresses();
+ [makerAssetData, takerAssetData] = [
+ assetDataUtils.encodeERC20AssetData(makerTokenAddress),
+ assetDataUtils.encodeERC20AssetData(takerTokenAddress),
+ ];
+ signedOrder = await fillScenarios.createFillableSignedOrderAsync(
+ makerAssetData,
+ takerAssetData,
+ makerAddress,
+ takerAddress,
+ fillableAmount,
+ );
+ });
+ after(async () => {
+ await blockchainLifecycle.revertAsync();
+ });
+ beforeEach(async () => {
+ await blockchainLifecycle.startAsync();
+ });
+ afterEach(async () => {
+ await blockchainLifecycle.revertAsync();
+ });
+ describe('encode and executeTransaction', () => {
+ const executeTransactionOrThrowAsync = async (
+ encoder: TransactionEncoder,
+ data: string,
+ signerAddress: string = takerAddress,
+ ): Promise<void> => {
+ const salt = generatePseudoRandomSalt();
+ const encodedTransaction = encoder.getTransactionHex(data, salt, signerAddress);
+ const signature = await ecSignOrderHashAsync(
+ provider,
+ encodedTransaction,
+ signerAddress,
+ SignerType.Default,
+ );
+ txHash = await contractWrappers.exchange.executeTransactionAsync(
+ salt,
+ signerAddress,
+ data,
+ signature,
+ senderAddress,
+ );
+ await web3Wrapper.awaitTransactionSuccessAsync(txHash, constants.AWAIT_TRANSACTION_MINED_MS);
+ };
+ describe('#fillOrderTx', () => {
+ it('should successfully execute the transaction', async () => {
+ const encoder = await contractWrappers.exchange.transactionEncoderAsync();
+ const data = encoder.fillOrderTx(signedOrder, takerTokenFillAmount);
+ await executeTransactionOrThrowAsync(encoder, data);
+ });
+ });
+ describe('#fillOrderNoThrowTx', () => {
+ it('should successfully execute the transaction', async () => {
+ const encoder = await contractWrappers.exchange.transactionEncoderAsync();
+ const data = encoder.fillOrderNoThrowTx(signedOrder, takerTokenFillAmount);
+ await executeTransactionOrThrowAsync(encoder, data);
+ });
+ });
+ describe('#fillOrKillOrderTx', () => {
+ it('should successfully execute the transaction', async () => {
+ const encoder = await contractWrappers.exchange.transactionEncoderAsync();
+ const data = encoder.fillOrKillOrderTx(signedOrder, takerTokenFillAmount);
+ await executeTransactionOrThrowAsync(encoder, data);
+ });
+ });
+ describe('#marketSellOrdersTx', () => {
+ it('should successfully execute the transaction', async () => {
+ const encoder = await contractWrappers.exchange.transactionEncoderAsync();
+ const data = encoder.marketSellOrdersTx([signedOrder], takerTokenFillAmount);
+ await executeTransactionOrThrowAsync(encoder, data);
+ });
+ });
+ describe('#marketSellOrdersNoThrowTx', () => {
+ it('should successfully execute the transaction', async () => {
+ const encoder = await contractWrappers.exchange.transactionEncoderAsync();
+ const data = encoder.marketSellOrdersNoThrowTx([signedOrder], takerTokenFillAmount);
+ await executeTransactionOrThrowAsync(encoder, data);
+ });
+ });
+ describe('#marketBuyOrdersTx', () => {
+ it('should successfully execute the transaction', async () => {
+ const encoder = await contractWrappers.exchange.transactionEncoderAsync();
+ const data = encoder.marketBuyOrdersTx([signedOrder], fillableAmount);
+ await executeTransactionOrThrowAsync(encoder, data);
+ });
+ });
+ describe('#marketBuyOrdersNoThrowTx', () => {
+ it('should successfully execute the transaction', async () => {
+ const encoder = await contractWrappers.exchange.transactionEncoderAsync();
+ const data = encoder.marketBuyOrdersNoThrowTx([signedOrder], fillableAmount);
+ await executeTransactionOrThrowAsync(encoder, data);
+ });
+ });
+ describe('#preSignTx', () => {
+ it('should successfully execute the transaction', async () => {
+ const encoder = await contractWrappers.exchange.transactionEncoderAsync();
+ const orderHash = orderHashUtils.getOrderHashHex(signedOrder);
+ const signature = signedOrder.signature;
+ const data = encoder.preSignTx(orderHash, makerAddress, signature);
+ await executeTransactionOrThrowAsync(encoder, data);
+ });
+ });
+ describe('#setSignatureValidatorApprovalTx', () => {
+ it('should successfully execute the transaction', async () => {
+ const encoder = await contractWrappers.exchange.transactionEncoderAsync();
+ const isApproved = true;
+ const data = encoder.setSignatureValidatorApprovalTx(senderAddress, isApproved);
+ await executeTransactionOrThrowAsync(encoder, data);
+ });
+ });
+ describe('#batchFillOrdersTx', () => {
+ it('should successfully execute the transaction', async () => {
+ const encoder = await contractWrappers.exchange.transactionEncoderAsync();
+ const data = encoder.batchFillOrdersTx([signedOrder], [takerTokenFillAmount]);
+ await executeTransactionOrThrowAsync(encoder, data);
+ });
+ });
+ describe('#batchFillOrKillOrdersTx', () => {
+ it('should successfully execute the transaction', async () => {
+ const encoder = await contractWrappers.exchange.transactionEncoderAsync();
+ const data = encoder.batchFillOrKillOrdersTx([signedOrder], [takerTokenFillAmount]);
+ await executeTransactionOrThrowAsync(encoder, data);
+ });
+ });
+ describe('#batchFillOrdersNoThrowTx', () => {
+ it('should successfully execute the transaction', async () => {
+ const encoder = await contractWrappers.exchange.transactionEncoderAsync();
+ const data = encoder.batchFillOrdersNoThrowTx([signedOrder], [takerTokenFillAmount]);
+ await executeTransactionOrThrowAsync(encoder, data);
+ });
+ });
+ describe('#batchCancelOrdersTx', () => {
+ it('should successfully execute the transaction', async () => {
+ const encoder = await contractWrappers.exchange.transactionEncoderAsync();
+ const data = encoder.batchCancelOrdersTx([signedOrder]);
+ const signerAddress = makerAddress;
+ await executeTransactionOrThrowAsync(encoder, data, signerAddress);
+ });
+ });
+ describe('#cancelOrderTx', () => {
+ it('should successfully execute the transaction', async () => {
+ const encoder = await contractWrappers.exchange.transactionEncoderAsync();
+ const data = encoder.cancelOrderTx(signedOrder);
+ const signerAddress = makerAddress;
+ await executeTransactionOrThrowAsync(encoder, data, signerAddress);
+ });
+ });
+ describe('#cancelOrdersUpToTx', () => {
+ it('should successfully execute the transaction', async () => {
+ const encoder = await contractWrappers.exchange.transactionEncoderAsync();
+ const targetEpoch = signedOrder.salt;
+ const data = encoder.cancelOrdersUpToTx(targetEpoch);
+ const signerAddress = makerAddress;
+ await executeTransactionOrThrowAsync(encoder, data, signerAddress);
+ });
+ });
+ });
+});