aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/0x.js_test.ts47
-rw-r--r--test/exchange_wrapper_test.ts (renamed from test/exchange_wrapper.ts)64
-rw-r--r--test/utils/blockchain_lifecycle.ts2
-rw-r--r--test/utils/order.ts42
4 files changed, 124 insertions, 31 deletions
diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts
index 5d23d7094..a84785f4b 100644
--- a/test/0x.js_test.ts
+++ b/test/0x.js_test.ts
@@ -7,6 +7,7 @@ import * as Sinon from 'sinon';
import {ZeroEx} from '../src/0x.js';
import {constants} from './utils/constants';
import {web3Factory} from './utils/web3_factory';
+import {Order} from '../src/types';
// Use BigNumber chai add-on
chai.use(ChaiBigNumber());
@@ -43,38 +44,28 @@ describe('ZeroEx library', () => {
});
describe('#getOrderHash', () => {
const expectedOrderHash = '0x103a5e97dab5dbeb8f385636f86a7d1e458a7ccbe1bd194727f0b2f85ab116c7';
+ const order: Order = {
+ maker: constants.NULL_ADDRESS,
+ feeRecipient: constants.NULL_ADDRESS,
+ makerTokenAddress: constants.NULL_ADDRESS,
+ takerTokenAddress: constants.NULL_ADDRESS,
+ salt: new BigNumber(0),
+ makerFee: new BigNumber(0),
+ takerFee: new BigNumber(0),
+ makerTokenAmount: new BigNumber(0),
+ takerTokenAmount: new BigNumber(0),
+ expirationUnixTimestampSec: new BigNumber(0),
+ };
+ const exchangeAddress = constants.NULL_ADDRESS;
it('defaults takerAddress to NULL address', () => {
- const orderHash = ZeroEx.getOrderHashHex(
- constants.NULL_ADDRESS,
- constants.NULL_ADDRESS,
- '',
- constants.NULL_ADDRESS,
- constants.NULL_ADDRESS,
- constants.NULL_ADDRESS,
- new BigNumber(0),
- new BigNumber(0),
- new BigNumber(0),
- new BigNumber(0),
- new BigNumber(0),
- new BigNumber(0),
- );
+ const orderHash = ZeroEx.getOrderHashHex(exchangeAddress, order);
expect(orderHash).to.be.equal(expectedOrderHash);
});
it('calculates the order hash', () => {
- const orderHash = ZeroEx.getOrderHashHex(
- constants.NULL_ADDRESS,
- constants.NULL_ADDRESS,
- constants.NULL_ADDRESS,
- constants.NULL_ADDRESS,
- constants.NULL_ADDRESS,
- constants.NULL_ADDRESS,
- new BigNumber(0),
- new BigNumber(0),
- new BigNumber(0),
- new BigNumber(0),
- new BigNumber(0),
- new BigNumber(0),
- );
+ const orderWithZeroTaker = _.assign(order, {
+ taker: constants.NULL_ADDRESS,
+ });
+ const orderHash = ZeroEx.getOrderHashHex(exchangeAddress, orderWithZeroTaker);
expect(orderHash).to.be.equal(expectedOrderHash);
});
});
diff --git a/test/exchange_wrapper.ts b/test/exchange_wrapper_test.ts
index e42454089..5138e140f 100644
--- a/test/exchange_wrapper.ts
+++ b/test/exchange_wrapper_test.ts
@@ -2,16 +2,26 @@ import 'mocha';
import * as chai from 'chai';
import {web3Factory} from './utils/web3_factory';
import {ZeroEx} from '../src/0x.js';
+import promisify = require('es6-promisify');
+import * as _ from 'lodash';
import {BlockchainLifecycle} from './utils/blockchain_lifecycle';
+import * as BigNumber from 'bignumber.js';
+import {createSignedOrder} from './utils/order';
+import {Token} from '../src/types';
+import * as Web3 from 'web3';
const expect = chai.expect;
const blockchainLifecycle = new BlockchainLifecycle();
describe('ExchangeWrapper', () => {
let zeroEx: ZeroEx;
+ let userAddresses: string[];
+ let web3: Web3;
before(async () => {
- const web3 = web3Factory.create();
+ web3 = web3Factory.create();
zeroEx = new ZeroEx(web3);
+ userAddresses = await promisify(web3.eth.getAccounts)();
+ web3.eth.defaultAccount = userAddresses[0];
});
beforeEach(async () => {
await blockchainLifecycle.startAsync();
@@ -80,7 +90,7 @@ describe('ExchangeWrapper', () => {
expect(isValid).to.be.false;
});
it('should return false if the signature doesn\'t pertain to the dataHex & address', async () => {
- const wrongSignature = Object.assign({}, signature, {v: 28});
+ const wrongSignature = {...signature, v: 28};
const isValid = await zeroEx.exchange.isValidSignatureAsync(dataHex, wrongSignature, address);
expect(isValid).to.be.false;
});
@@ -89,4 +99,54 @@ describe('ExchangeWrapper', () => {
expect(isValid).to.be.true;
});
});
+ describe('#fillOrderAsync', () => {
+ let tokens: Token[];
+ const setBalance = async (toAddress: string,
+ amountInBaseUnits: BigNumber.BigNumber|number,
+ symbol: string) => {
+ const amount = _.isNumber(amountInBaseUnits) ? new BigNumber(amountInBaseUnits) : amountInBaseUnits;
+ const token = _.find(tokens, {symbol});
+ if (_.isUndefined(token)) {
+ throw new Error(`Token ${symbol} not found`);
+ } else {
+ await zeroEx.token.transferAsync(token.address, userAddresses[0], toAddress, amount);
+ }
+ };
+ const setAllowance = async (ownerAddress: string,
+ amountInBaseUnits: BigNumber.BigNumber|number,
+ symbol: string) => {
+ const amount = _.isNumber(amountInBaseUnits) ? new BigNumber(amountInBaseUnits) : amountInBaseUnits;
+ const token = _.find(tokens, {symbol});
+ if (_.isUndefined(token)) {
+ throw new Error(`Token ${symbol} not found`);
+ } else {
+ await zeroEx.token.setProxyAllowanceAsync(token.address, ownerAddress, amount);
+ }
+ };
+ before('fetch tokens', async () => {
+ tokens = await zeroEx.tokenRegistry.getTokensAsync();
+ });
+ describe('failed fills', () => {
+ it('should throw when the fill amount is zero', async () => {
+ const signedOrder = await createSignedOrder(zeroEx, tokens, 5, 'MLN', 5, 'GNT');
+ const fillAmount = new BigNumber(0);
+ expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount))
+ .to.be.rejectedWith('This order has already been filled or cancelled');
+ });
+ });
+ describe('successful fills', () => {
+ afterEach('reset default account', () => {
+ web3.eth.defaultAccount = userAddresses[0];
+ });
+ it('should fill the valid order', async () => {
+ await setAllowance(userAddresses[0], 5, 'MLN');
+ await setBalance(userAddresses[1], 5, 'GNT');
+ await setAllowance(userAddresses[1], 5, 'GNT');
+ const signedOrder = await createSignedOrder(zeroEx, tokens, 5, 'MLN', 5, 'GNT');
+ const fillAmount = new BigNumber(5);
+ web3.eth.defaultAccount = userAddresses[1];
+ await zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount);
+ });
+ });
+ });
});
diff --git a/test/utils/blockchain_lifecycle.ts b/test/utils/blockchain_lifecycle.ts
index 68e169ac0..50eb57b95 100644
--- a/test/utils/blockchain_lifecycle.ts
+++ b/test/utils/blockchain_lifecycle.ts
@@ -17,4 +17,4 @@ export class BlockchainLifecycle {
throw new Error(`Snapshot with id #${this.snapshotId} failed to revert`);
}
}
-};
+}
diff --git a/test/utils/order.ts b/test/utils/order.ts
new file mode 100644
index 000000000..d775dc616
--- /dev/null
+++ b/test/utils/order.ts
@@ -0,0 +1,42 @@
+import {SignedOrder, Token} from '../../src/types';
+import * as BigNumber from 'bignumber.js';
+import * as _ from 'lodash';
+import {ZeroEx} from '../../src/0x.js';
+import {constants} from './constants';
+
+export async function createSignedOrder(
+ zeroEx: ZeroEx,
+ tokens: Token[],
+ makerTokenAmount: BigNumber.BigNumber|number,
+ makerTokenSymbol: string,
+ takerTokenAmount: BigNumber.BigNumber|number,
+ takerTokenSymbol: string): Promise<SignedOrder> {
+ // TODO: fetch properly
+ const EXCHANGE_ADDRESS = '0xb69e673309512a9d726f87304c6984054f87a93b';
+ const INF_TIMESTAMP = 2524604400;
+ const makerToken = _.find(tokens, {symbol: makerTokenSymbol});
+ const takerToken = _.find(tokens, {symbol: takerTokenSymbol});
+ if (_.isUndefined(makerToken)) {
+ throw new Error(`Token ${makerTokenSymbol} not found`);
+ }
+ if (_.isUndefined(takerToken)) {
+ throw new Error(`Token ${takerTokenSymbol} not found`);
+ }
+ const order = {
+ maker: '0x5409ed021d9299bf6814279a6a1411a7e866a631',
+ taker: undefined,
+ makerFee: new BigNumber(0),
+ takerFee: new BigNumber(0),
+ makerTokenAmount: _.isNumber(makerTokenAmount) ? new BigNumber(makerTokenAmount) : makerTokenAmount,
+ takerTokenAmount: _.isNumber(takerTokenAmount) ? new BigNumber(takerTokenAmount) : takerTokenAmount,
+ makerTokenAddress: makerToken.address,
+ takerTokenAddress: takerToken.address,
+ salt: ZeroEx.generatePseudoRandomSalt(),
+ feeRecipient: constants.NULL_ADDRESS,
+ expirationUnixTimestampSec: new BigNumber(INF_TIMESTAMP),
+ };
+ const orderHash = ZeroEx.getOrderHashHex(EXCHANGE_ADDRESS, order);
+ const ecSignature = await zeroEx.signOrderHashAsync(orderHash);
+ const signedOrder: SignedOrder = _.assign(order, {ecSignature});
+ return signedOrder;
+}