Write tests for getUnavailableTakerAmountAsync, getFilledTakerAmountAsync and getCanceledTakerAmountAsync
This commit is contained in:
12
src/0x.js.ts
12
src/0x.js.ts
@@ -17,7 +17,7 @@ import {TokenRegistryWrapper} from './contract_wrappers/token_registry_wrapper';
|
|||||||
import {ecSignatureSchema} from './schemas/ec_signature_schema';
|
import {ecSignatureSchema} from './schemas/ec_signature_schema';
|
||||||
import {TokenWrapper} from './contract_wrappers/token_wrapper';
|
import {TokenWrapper} from './contract_wrappers/token_wrapper';
|
||||||
import {SolidityTypes, ECSignature, ZeroExError} from './types';
|
import {SolidityTypes, ECSignature, ZeroExError} from './types';
|
||||||
import {Order} from './types';
|
import {Order, SignedOrder} from './types';
|
||||||
import {orderSchema} from './schemas/order_schemas';
|
import {orderSchema} from './schemas/order_schemas';
|
||||||
import * as ExchangeArtifacts from './artifacts/Exchange.json';
|
import * as ExchangeArtifacts from './artifacts/Exchange.json';
|
||||||
|
|
||||||
@@ -116,17 +116,23 @@ export class ZeroEx {
|
|||||||
this.tokenRegistry.invalidateContractInstance();
|
this.tokenRegistry.invalidateContractInstance();
|
||||||
this.token.invalidateContractInstances();
|
this.token.invalidateContractInstances();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets default account for sending transactions.
|
* Sets default account for sending transactions.
|
||||||
*/
|
*/
|
||||||
public setTransactionSenderAccount(account: string): void {
|
public setTransactionSenderAccount(account: string): void {
|
||||||
this.web3Wrapper.setDefaultAccount(account);
|
this.web3Wrapper.setDefaultAccount(account);
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Get the default account set for sending transactions.
|
||||||
|
*/
|
||||||
|
public async getTransactionSenderAccountAsync(): Promise<string> {
|
||||||
|
const senderAccount = await this.web3Wrapper.getSenderAddressOrThrowAsync();
|
||||||
|
return senderAccount;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Computes the orderHash given the order parameters and returns it as a hex encoded string.
|
* Computes the orderHash given the order parameters and returns it as a hex encoded string.
|
||||||
*/
|
*/
|
||||||
public async getOrderHashHexAsync(order: Order): Promise<string> {
|
public async getOrderHashHexAsync(order: Order|SignedOrder): Promise<string> {
|
||||||
const exchangeContractAddr = await this.getExchangeAddressAsync();
|
const exchangeContractAddr = await this.getExchangeAddressAsync();
|
||||||
assert.doesConformToSchema('order',
|
assert.doesConformToSchema('order',
|
||||||
SchemaValidator.convertToJSONSchemaCompatibleObject(order as object),
|
SchemaValidator.convertToJSONSchemaCompatibleObject(order as object),
|
||||||
|
@@ -18,14 +18,20 @@ chai.use(ChaiBigNumber());
|
|||||||
const expect = chai.expect;
|
const expect = chai.expect;
|
||||||
const blockchainLifecycle = new BlockchainLifecycle();
|
const blockchainLifecycle = new BlockchainLifecycle();
|
||||||
|
|
||||||
|
const NON_EXISTENT_ORDER_HASH = '0x79370342234e7acd6bbeac335bd3bb1d368383294b64b8160a00f4060e4d3777';
|
||||||
|
|
||||||
describe('ExchangeWrapper', () => {
|
describe('ExchangeWrapper', () => {
|
||||||
let zeroEx: ZeroEx;
|
let zeroEx: ZeroEx;
|
||||||
let userAddresses: string[];
|
let userAddresses: string[];
|
||||||
let web3: Web3;
|
let web3: Web3;
|
||||||
|
let tokens: Token[];
|
||||||
|
let fillScenarios: FillScenarios;
|
||||||
before(async () => {
|
before(async () => {
|
||||||
web3 = web3Factory.create();
|
web3 = web3Factory.create();
|
||||||
zeroEx = new ZeroEx(web3);
|
zeroEx = new ZeroEx(web3);
|
||||||
userAddresses = await promisify(web3.eth.getAccounts)();
|
userAddresses = await promisify(web3.eth.getAccounts)();
|
||||||
|
tokens = await zeroEx.tokenRegistry.getTokensAsync();
|
||||||
|
fillScenarios = new FillScenarios(zeroEx, userAddresses, tokens);
|
||||||
});
|
});
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
await blockchainLifecycle.startAsync();
|
await blockchainLifecycle.startAsync();
|
||||||
@@ -104,24 +110,16 @@ describe('ExchangeWrapper', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('#fillOrderAsync', () => {
|
describe('#fillOrderAsync', () => {
|
||||||
let tokens: Token[];
|
|
||||||
let makerTokenAddress: string;
|
let makerTokenAddress: string;
|
||||||
let takerTokenAddress: string;
|
let takerTokenAddress: string;
|
||||||
let fillScenarios: FillScenarios;
|
|
||||||
let takerAddress: string;
|
let takerAddress: string;
|
||||||
const fillTakerAmountInBaseUnits = new BigNumber(5);
|
const fillTakerAmountInBaseUnits = new BigNumber(5);
|
||||||
const addressBySymbol: {[symbol: string]: string} = {};
|
|
||||||
const shouldCheckTransfer = false;
|
const shouldCheckTransfer = false;
|
||||||
before('fetch tokens', async () => {
|
before('fetch tokens', async () => {
|
||||||
takerAddress = userAddresses[1];
|
takerAddress = userAddresses[1];
|
||||||
tokens = await zeroEx.tokenRegistry.getTokensAsync();
|
|
||||||
_.forEach(tokens, token => {
|
|
||||||
addressBySymbol[token.symbol] = token.address;
|
|
||||||
});
|
|
||||||
const [makerToken, takerToken] = tokens;
|
const [makerToken, takerToken] = tokens;
|
||||||
makerTokenAddress = makerToken.address;
|
makerTokenAddress = makerToken.address;
|
||||||
takerTokenAddress = takerToken.address;
|
takerTokenAddress = takerToken.address;
|
||||||
fillScenarios = new FillScenarios(zeroEx, userAddresses, tokens);
|
|
||||||
});
|
});
|
||||||
afterEach('reset default account', () => {
|
afterEach('reset default account', () => {
|
||||||
zeroEx.setTransactionSenderAccount(userAddresses[0]);
|
zeroEx.setTransactionSenderAccount(userAddresses[0]);
|
||||||
@@ -185,4 +183,75 @@ describe('ExchangeWrapper', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
describe('tests that require partially filled order', () => {
|
||||||
|
let makerTokenAddress: string;
|
||||||
|
let takerTokenAddress: string;
|
||||||
|
let takerAddress: string;
|
||||||
|
before(() => {
|
||||||
|
takerAddress = userAddresses[1];
|
||||||
|
const [makerToken, takerToken] = tokens;
|
||||||
|
makerTokenAddress = makerToken.address;
|
||||||
|
takerTokenAddress = takerToken.address;
|
||||||
|
});
|
||||||
|
describe('#getUnavailableTakerAmountAsync', () => {
|
||||||
|
it ('should throw if passed an invalid orderHash', async () => {
|
||||||
|
const invalidOrderHashHex = '0x123';
|
||||||
|
expect(zeroEx.exchange.getUnavailableTakerAmountAsync(invalidOrderHashHex)).to.be.rejected();
|
||||||
|
});
|
||||||
|
it ('should return zero if passed a valid but non-existent orderHash', async () => {
|
||||||
|
const unavailableValueT = await zeroEx.exchange.getUnavailableTakerAmountAsync(NON_EXISTENT_ORDER_HASH);
|
||||||
|
expect(unavailableValueT).to.be.bignumber.equal(0);
|
||||||
|
});
|
||||||
|
it ('should return the unavailableValueT for a valid and partially filled orderHash', async () => {
|
||||||
|
const fillableAmount = new BigNumber(5);
|
||||||
|
const partialFillAmount = new BigNumber(2);
|
||||||
|
const signedOrder = await fillScenarios.createPartiallyFilledSignedOrderAsync(
|
||||||
|
makerTokenAddress, takerTokenAddress, takerAddress, fillableAmount, partialFillAmount,
|
||||||
|
);
|
||||||
|
const orderHash = await zeroEx.getOrderHashHexAsync(signedOrder);
|
||||||
|
const unavailableValueT = await zeroEx.exchange.getUnavailableTakerAmountAsync(orderHash);
|
||||||
|
expect(unavailableValueT).to.be.bignumber.equal(partialFillAmount);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('#getFilledTakerAmountAsync', () => {
|
||||||
|
it ('should throw if passed an invalid orderHash', async () => {
|
||||||
|
const invalidOrderHashHex = '0x123';
|
||||||
|
expect(zeroEx.exchange.getFilledTakerAmountAsync(invalidOrderHashHex)).to.be.rejected();
|
||||||
|
});
|
||||||
|
it ('should return zero if passed a valid but non-existent orderHash', async () => {
|
||||||
|
const filledValueT = await zeroEx.exchange.getFilledTakerAmountAsync(NON_EXISTENT_ORDER_HASH);
|
||||||
|
expect(filledValueT).to.be.bignumber.equal(0);
|
||||||
|
});
|
||||||
|
it ('should return the filledValueT for a valid and partially filled orderHash', async () => {
|
||||||
|
const fillableAmount = new BigNumber(5);
|
||||||
|
const partialFillAmount = new BigNumber(2);
|
||||||
|
const signedOrder = await fillScenarios.createPartiallyFilledSignedOrderAsync(
|
||||||
|
makerTokenAddress, takerTokenAddress, takerAddress, fillableAmount, partialFillAmount,
|
||||||
|
);
|
||||||
|
const orderHash = await zeroEx.getOrderHashHexAsync(signedOrder);
|
||||||
|
const filledValueT = await zeroEx.exchange.getFilledTakerAmountAsync(orderHash);
|
||||||
|
expect(filledValueT).to.be.bignumber.equal(partialFillAmount);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('#getCanceledTakerAmountAsync', () => {
|
||||||
|
it ('should throw if passed an invalid orderHash', async () => {
|
||||||
|
const invalidOrderHashHex = '0x123';
|
||||||
|
expect(zeroEx.exchange.getCanceledTakerAmountAsync(invalidOrderHashHex)).to.be.rejected();
|
||||||
|
});
|
||||||
|
it ('should return zero if passed a valid but non-existent orderHash', async () => {
|
||||||
|
const cancelledValueT = await zeroEx.exchange.getCanceledTakerAmountAsync(NON_EXISTENT_ORDER_HASH);
|
||||||
|
expect(cancelledValueT).to.be.bignumber.equal(0);
|
||||||
|
});
|
||||||
|
it ('should return the cancelledValueT for a valid and partially filled orderHash', async () => {
|
||||||
|
const fillableAmount = new BigNumber(5);
|
||||||
|
const partialFillAmount = new BigNumber(2);
|
||||||
|
const signedOrder = await fillScenarios.createPartiallyFilledSignedOrderAsync(
|
||||||
|
makerTokenAddress, takerTokenAddress, takerAddress, fillableAmount, partialFillAmount,
|
||||||
|
);
|
||||||
|
const orderHash = await zeroEx.getOrderHashHexAsync(signedOrder);
|
||||||
|
const cancelledValueT = await zeroEx.exchange.getCanceledTakerAmountAsync(orderHash);
|
||||||
|
expect(cancelledValueT).to.be.bignumber.equal(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@@ -26,4 +26,24 @@ export class FillScenarios {
|
|||||||
expirationUnixTimestampSec);
|
expirationUnixTimestampSec);
|
||||||
return signedOrder;
|
return signedOrder;
|
||||||
}
|
}
|
||||||
|
public async createPartiallyFilledSignedOrderAsync(makerTokenAddress: string, takerTokenAddress: string,
|
||||||
|
takerAddress: string, fillableAmount: BigNumber.BigNumber,
|
||||||
|
partialFillAmount: BigNumber.BigNumber) {
|
||||||
|
const prevSenderAccount = await this.zeroEx.getTransactionSenderAccountAsync();
|
||||||
|
const [makerAddress] = this.userAddresses;
|
||||||
|
await this.zeroEx.token.setProxyAllowanceAsync(makerTokenAddress, makerAddress, fillableAmount);
|
||||||
|
await this.zeroEx.token.transferAsync(takerTokenAddress, makerAddress, takerAddress, fillableAmount);
|
||||||
|
await this.zeroEx.token.setProxyAllowanceAsync(takerTokenAddress, takerAddress, fillableAmount);
|
||||||
|
|
||||||
|
const signedOrder = await orderFactory.createSignedOrderAsync(this.zeroEx, makerAddress,
|
||||||
|
takerAddress, fillableAmount, makerTokenAddress, fillableAmount, takerTokenAddress);
|
||||||
|
|
||||||
|
this.zeroEx.setTransactionSenderAccount(takerAddress);
|
||||||
|
const shouldCheckTransfer = false;
|
||||||
|
await this.zeroEx.exchange.fillOrderAsync(signedOrder, partialFillAmount, shouldCheckTransfer);
|
||||||
|
|
||||||
|
// Re-set sender account so as to avoid introducing side-effects
|
||||||
|
this.zeroEx.setTransactionSenderAccount(prevSenderAccount);
|
||||||
|
return signedOrder;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user