diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-06-01 03:15:15 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-06-01 03:15:15 +0800 |
commit | 28dee77db84549db24b95d9aaaca949a2fdec29c (patch) | |
tree | fd0c1466219a7caa070ebb1f11447a21bd59405a | |
parent | 886ed9180d4e59f6ef1e72509d003726a5a898e9 (diff) | |
download | dexon-sol-tools-28dee77db84549db24b95d9aaaca949a2fdec29c.tar.gz dexon-sol-tools-28dee77db84549db24b95d9aaaca949a2fdec29c.tar.zst dexon-sol-tools-28dee77db84549db24b95d9aaaca949a2fdec29c.zip |
Temp push
-rw-r--r-- | test/exchange_wrapper_test.ts | 49 | ||||
-rw-r--r-- | test/utils/order.ts | 64 |
2 files changed, 58 insertions, 55 deletions
diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 5138e140f..a5a327dc4 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -6,7 +6,7 @@ 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 {orderFactory} from './utils/order'; import {Token} from '../src/types'; import * as Web3 from 'web3'; @@ -101,34 +101,31 @@ describe('ExchangeWrapper', () => { }); describe('#fillOrderAsync', () => { let tokens: Token[]; + let addressBySymbol: {[symbol: string]: string} = {}; + let networkId: number; const setBalance = async (toAddress: string, amountInBaseUnits: BigNumber.BigNumber|number, - symbol: string) => { + tokenAddress: 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); - } + await zeroEx.token.transferAsync(tokenAddress, userAddresses[0], toAddress, amount); }; const setAllowance = async (ownerAddress: string, amountInBaseUnits: BigNumber.BigNumber|number, - symbol: string) => { + tokenAddress: 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); - } + await zeroEx.token.setProxyAllowanceAsync(tokenAddress, ownerAddress, amount); }; before('fetch tokens', async () => { tokens = await zeroEx.tokenRegistry.getTokensAsync(); + _.forEach(tokens, token => { + addressBySymbol[token.symbol] = token.address; + }); + networkId = await promisify(web3.version.getNetwork)(); }); describe('failed fills', () => { it('should throw when the fill amount is zero', async () => { - const signedOrder = await createSignedOrder(zeroEx, tokens, 5, 'MLN', 5, 'GNT'); + const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, userAddresses[0], + 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); const fillAmount = new BigNumber(0); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount)) .to.be.rejectedWith('This order has already been filled or cancelled'); @@ -139,13 +136,23 @@ describe('ExchangeWrapper', () => { 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 maker = userAddresses[0]; + const taker = userAddresses[1]; + await setAllowance(maker, 5, addressBySymbol.MLN); + await setBalance(taker, 5, addressBySymbol.GNT); + await setAllowance(taker, 5, addressBySymbol.GNT); + const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, + 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); const fillAmount = new BigNumber(5); - web3.eth.defaultAccount = userAddresses[1]; + web3.eth.defaultAccount = taker; + expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, maker)).to.be.bignumber.greaterThan(5); + expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, taker)).to.be.bignumber.equal(0); + expect(await zeroEx.token.getBalanceAsync(addressBySymbol.GNT, taker)).to.be.bignumber.equal(5); + expect(await zeroEx.token.getProxyAllowanceAsync(addressBySymbol.MLN, maker)).to.be.bignumber.equal(5); + expect(await zeroEx.token.getProxyAllowanceAsync(addressBySymbol.GNT, taker)).to.be.bignumber.equal(5); await zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount); + expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, taker)).to.be.bignumber.equal(5); + expect(await zeroEx.token.getBalanceAsync(addressBySymbol.GNT, taker)).to.be.bignumber.equal(0); }); }); }); diff --git a/test/utils/order.ts b/test/utils/order.ts index d775dc616..8a7d8430e 100644 --- a/test/utils/order.ts +++ b/test/utils/order.ts @@ -3,40 +3,36 @@ import * as BigNumber from 'bignumber.js'; import * as _ from 'lodash'; import {ZeroEx} from '../../src/0x.js'; import {constants} from './constants'; +import * as ExchangeArtifacts from '../../src/artifacts/Exchange.json'; -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`); +export const orderFactory = { + async createSignedOrderAsync( + zeroEx: ZeroEx, + networkId: number, + maker: string, + makerTokenAmount: BigNumber.BigNumber|number, + makerTokenAddress: string, + takerTokenAmount: BigNumber.BigNumber|number, + takerTokenAddress: string): Promise<SignedOrder> { + // TODO refactor and check + const exchangeAddress: string = (ExchangeArtifacts as any).networks[networkId].address; + const INF_TIMESTAMP = 2524604400; + const order = { + maker, + 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, + takerTokenAddress, + salt: ZeroEx.generatePseudoRandomSalt(), + feeRecipient: constants.NULL_ADDRESS, + expirationUnixTimestampSec: new BigNumber(INF_TIMESTAMP), + }; + const orderHash = ZeroEx.getOrderHashHex(exchangeAddress, order); + const ecSignature = await zeroEx.signOrderHashAsync(orderHash); + const signedOrder: SignedOrder = _.assign(order, {ecSignature}); + return signedOrder; } - 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; } |