aboutsummaryrefslogtreecommitdiffstats
path: root/packages/order-utils/test
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2018-06-12 01:21:32 +0800
committerFabio Berger <me@fabioberger.com>2018-06-12 01:21:32 +0800
commit21f7722f1023cc9d1848737b7c986f7df7a07122 (patch)
treea4b9c57fa75fb8c92bf44ed968df8b7112f478de /packages/order-utils/test
parente4afe603f9c4dd5128f2446f9fe075516a3d4894 (diff)
downloaddexon-sol-tools-21f7722f1023cc9d1848737b7c986f7df7a07122.tar
dexon-sol-tools-21f7722f1023cc9d1848737b7c986f7df7a07122.tar.gz
dexon-sol-tools-21f7722f1023cc9d1848737b7c986f7df7a07122.tar.bz2
dexon-sol-tools-21f7722f1023cc9d1848737b7c986f7df7a07122.tar.lz
dexon-sol-tools-21f7722f1023cc9d1848737b7c986f7df7a07122.tar.xz
dexon-sol-tools-21f7722f1023cc9d1848737b7c986f7df7a07122.tar.zst
dexon-sol-tools-21f7722f1023cc9d1848737b7c986f7df7a07122.zip
Move OrderValidationUtils (+ tests) and ExchangeTransferSimulator to order-utils
Diffstat (limited to 'packages/order-utils/test')
-rw-r--r--packages/order-utils/test/exchange_transfer_simulator_test.ts164
-rw-r--r--packages/order-utils/test/global_hooks_test.ts45
-rw-r--r--packages/order-utils/test/order_validation_utils_test.ts70
-rw-r--r--packages/order-utils/test/utils/simple_erc20_balance_and_proxy_allowance_fetcher.ts26
4 files changed, 305 insertions, 0 deletions
diff --git a/packages/order-utils/test/exchange_transfer_simulator_test.ts b/packages/order-utils/test/exchange_transfer_simulator_test.ts
new file mode 100644
index 000000000..3e35f900e
--- /dev/null
+++ b/packages/order-utils/test/exchange_transfer_simulator_test.ts
@@ -0,0 +1,164 @@
+import { BlockchainLifecycle } from '@0xproject/dev-utils';
+import { ExchangeContractErrs, Token } from '@0xproject/types';
+import { BigNumber } from '@0xproject/utils';
+import * as chai from 'chai';
+import { BlockParamLiteral } from 'ethereum-types';
+import * as _ from 'lodash';
+import 'make-promises-safe';
+
+import { artifacts } from '../src/artifacts';
+import { constants } from '../src/constants';
+import { ExchangeTransferSimulator } from '../src/exchange_transfer_simulator';
+import { DummyERC20TokenContract } from '../src/generated_contract_wrappers/dummy_e_r_c20_token';
+import { BalanceAndProxyAllowanceLazyStore } from '../src/store/balance_and_proxy_allowance_lazy_store';
+import { TradeSide, TransferType } from '../src/types';
+
+import { chaiSetup } from './utils/chai_setup';
+import { SimpleERC20BalanceAndProxyAllowanceFetcher } from './utils/simple_erc20_balance_and_proxy_allowance_fetcher';
+import { provider, web3Wrapper } from './utils/web3_wrapper';
+
+chaiSetup.configure();
+const expect = chai.expect;
+const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
+
+describe('ExchangeTransferSimulator', async () => {
+ const transferAmount = new BigNumber(5);
+ let userAddresses: string[];
+ let dummyERC20Token: DummyERC20TokenContract;
+ let coinbase: string;
+ let sender: string;
+ let recipient: string;
+ let exampleTokenAddress: string;
+ let exchangeTransferSimulator: ExchangeTransferSimulator;
+ let txHash: string;
+ let erc20ProxyAddress: string;
+ before(async () => {
+ userAddresses = await web3Wrapper.getAvailableAddressesAsync();
+ [coinbase, sender, recipient] = userAddresses;
+
+ erc20ProxyAddress = getAddressFromArtifact(artifacts.ERC20Proxy, constants.TESTRPC_NETWORK_ID);
+
+ const wethArtifact = artifacts.DummyERC20Token;
+ const wethAddress = getAddressFromArtifact(wethArtifact, constants.TESTRPC_NETWORK_ID);
+ dummyERC20Token = new DummyERC20TokenContract(
+ artifacts.DummyERC20Token.compilerOutput.abi,
+ wethAddress,
+ provider,
+ );
+ exampleTokenAddress = dummyERC20Token.address;
+ });
+ beforeEach(async () => {
+ await blockchainLifecycle.startAsync();
+ });
+ afterEach(async () => {
+ await blockchainLifecycle.revertAsync();
+ });
+ describe('#transferFromAsync', function(): void {
+ // HACK: For some reason these tests need a slightly longer timeout
+ const mochaTestTimeoutMs = 3000;
+ this.timeout(mochaTestTimeoutMs);
+
+ beforeEach(() => {
+ const simpleERC20BalanceAndProxyAllowanceFetcher = new SimpleERC20BalanceAndProxyAllowanceFetcher(
+ dummyERC20Token,
+ erc20ProxyAddress,
+ );
+ const balanceAndProxyAllowanceLazyStore = new BalanceAndProxyAllowanceLazyStore(
+ simpleERC20BalanceAndProxyAllowanceFetcher,
+ );
+ exchangeTransferSimulator = new ExchangeTransferSimulator(balanceAndProxyAllowanceLazyStore);
+ });
+ it("throws if the user doesn't have enough allowance", async () => {
+ return expect(
+ exchangeTransferSimulator.transferFromAsync(
+ exampleTokenAddress,
+ sender,
+ recipient,
+ transferAmount,
+ TradeSide.Taker,
+ TransferType.Trade,
+ ),
+ ).to.be.rejectedWith(ExchangeContractErrs.InsufficientTakerAllowance);
+ });
+ it("throws if the user doesn't have enough balance", async () => {
+ txHash = await dummyERC20Token.approve.sendTransactionAsync(erc20ProxyAddress, transferAmount, {
+ from: sender,
+ });
+ await web3Wrapper.awaitTransactionSuccessAsync(txHash);
+ return expect(
+ exchangeTransferSimulator.transferFromAsync(
+ exampleTokenAddress,
+ sender,
+ recipient,
+ transferAmount,
+ TradeSide.Maker,
+ TransferType.Trade,
+ ),
+ ).to.be.rejectedWith(ExchangeContractErrs.InsufficientMakerBalance);
+ });
+ it('updates balances and proxyAllowance after transfer', async function(): Promise<void> {
+ txHash = await dummyERC20Token.transfer.sendTransactionAsync(sender, transferAmount, {
+ from: coinbase,
+ });
+ await web3Wrapper.awaitTransactionSuccessAsync(txHash);
+
+ txHash = await dummyERC20Token.approve.sendTransactionAsync(erc20ProxyAddress, transferAmount, {
+ from: sender,
+ });
+ await web3Wrapper.awaitTransactionSuccessAsync(txHash);
+
+ await exchangeTransferSimulator.transferFromAsync(
+ exampleTokenAddress,
+ sender,
+ recipient,
+ transferAmount,
+ TradeSide.Taker,
+ TransferType.Trade,
+ );
+ const store = (exchangeTransferSimulator as any)._store;
+ const senderBalance = await store.getBalanceAsync(exampleTokenAddress, sender);
+ const recipientBalance = await store.getBalanceAsync(exampleTokenAddress, recipient);
+ const senderProxyAllowance = await store.getProxyAllowanceAsync(exampleTokenAddress, sender);
+ expect(senderBalance).to.be.bignumber.equal(0);
+ expect(recipientBalance).to.be.bignumber.equal(transferAmount);
+ expect(senderProxyAllowance).to.be.bignumber.equal(0);
+ });
+ it("doesn't update proxyAllowance after transfer if unlimited", async () => {
+ txHash = await dummyERC20Token.transfer.sendTransactionAsync(sender, transferAmount, {
+ from: coinbase,
+ });
+ await web3Wrapper.awaitTransactionSuccessAsync(txHash);
+ txHash = await dummyERC20Token.approve.sendTransactionAsync(
+ erc20ProxyAddress,
+ constants.UNLIMITED_ALLOWANCE_IN_BASE_UNITS,
+ {
+ from: sender,
+ },
+ );
+ await web3Wrapper.awaitTransactionSuccessAsync(txHash);
+ await exchangeTransferSimulator.transferFromAsync(
+ exampleTokenAddress,
+ sender,
+ recipient,
+ transferAmount,
+ TradeSide.Taker,
+ TransferType.Trade,
+ );
+ const store = (exchangeTransferSimulator as any)._store;
+ const senderBalance = await store.getBalanceAsync(exampleTokenAddress, sender);
+ const recipientBalance = await store.getBalanceAsync(exampleTokenAddress, recipient);
+ const senderProxyAllowance = await store.getProxyAllowanceAsync(exampleTokenAddress, sender);
+ expect(senderBalance).to.be.bignumber.equal(0);
+ expect(recipientBalance).to.be.bignumber.equal(transferAmount);
+ expect(senderProxyAllowance).to.be.bignumber.equal(constants.UNLIMITED_ALLOWANCE_IN_BASE_UNITS);
+ });
+ });
+});
+
+function getAddressFromArtifact(artifact: any, networkId: number): string {
+ if (_.isUndefined(artifact.networks[networkId])) {
+ throw new Error(`Contract ${artifact.contractName} not deployed to network ${networkId}`);
+ }
+ const contractAddress = artifact.networks[networkId].address.toLowerCase();
+ return contractAddress;
+}
diff --git a/packages/order-utils/test/global_hooks_test.ts b/packages/order-utils/test/global_hooks_test.ts
new file mode 100644
index 000000000..662a2cb0f
--- /dev/null
+++ b/packages/order-utils/test/global_hooks_test.ts
@@ -0,0 +1,45 @@
+import { devConstants } from '@0xproject/dev-utils';
+import { ArtifactWriter } from '@0xproject/migrations';
+import { BigNumber } from '@0xproject/utils';
+
+import { artifacts } from '../src/artifacts';
+import { constants } from '../src/constants';
+import { DummyERC20TokenContract } from '../src/generated_contract_wrappers/dummy_e_r_c20_token';
+import { ERC20ProxyContract } from '../src/generated_contract_wrappers/e_r_c20_proxy';
+
+import { provider } from './utils/web3_wrapper';
+
+before('migrate contracts', async function(): Promise<void> {
+ // HACK: Since contract migrations take longer then our global mocha timeout limit
+ // we manually increase it for this before hook.
+ const mochaTestTimeoutMs = 20000;
+ this.timeout(mochaTestTimeoutMs);
+
+ const txDefaults = {
+ gas: devConstants.GAS_LIMIT,
+ from: devConstants.TESTRPC_FIRST_ADDRESS,
+ };
+
+ const networkId = constants.TESTRPC_NETWORK_ID;
+ const artifactsDir = `lib/src/artifacts`;
+ const artifactsWriter = new ArtifactWriter(artifactsDir, networkId);
+
+ const erc20proxy = await ERC20ProxyContract.deployFrom0xArtifactAsync(artifacts.ERC20Proxy, provider, txDefaults);
+ artifactsWriter.saveArtifact(erc20proxy);
+
+ const totalSupply = new BigNumber(100000000000000000000);
+ const name = 'Test';
+ const symbol = 'TST';
+ const decimals = new BigNumber(18);
+ // tslint:disable-next-line:no-unused-variable
+ const dummyErc20Token = await DummyERC20TokenContract.deployFrom0xArtifactAsync(
+ artifacts.DummyERC20Token,
+ provider,
+ txDefaults,
+ name,
+ symbol,
+ decimals,
+ totalSupply,
+ );
+ artifactsWriter.saveArtifact(dummyErc20Token);
+});
diff --git a/packages/order-utils/test/order_validation_utils_test.ts b/packages/order-utils/test/order_validation_utils_test.ts
new file mode 100644
index 000000000..d3ff867d7
--- /dev/null
+++ b/packages/order-utils/test/order_validation_utils_test.ts
@@ -0,0 +1,70 @@
+import { BigNumber } from '@0xproject/utils';
+import * as chai from 'chai';
+import 'mocha';
+
+import { OrderValidationUtils } from '../src/order_validation_utils';
+
+import { chaiSetup } from './utils/chai_setup';
+
+chaiSetup.configure();
+const expect = chai.expect;
+
+describe('OrderValidationUtils', () => {
+ describe('#isRoundingError', () => {
+ it('should return false if there is a rounding error of 0.1%', async () => {
+ const numerator = new BigNumber(20);
+ const denominator = new BigNumber(999);
+ const target = new BigNumber(50);
+ // rounding error = ((20*50/999) - floor(20*50/999)) / (20*50/999) = 0.1%
+ const isRoundingError = OrderValidationUtils.isRoundingError(numerator, denominator, target);
+ expect(isRoundingError).to.be.false();
+ });
+
+ it('should return false if there is a rounding of 0.09%', async () => {
+ const numerator = new BigNumber(20);
+ const denominator = new BigNumber(9991);
+ const target = new BigNumber(500);
+ // rounding error = ((20*500/9991) - floor(20*500/9991)) / (20*500/9991) = 0.09%
+ const isRoundingError = OrderValidationUtils.isRoundingError(numerator, denominator, target);
+ expect(isRoundingError).to.be.false();
+ });
+
+ it('should return true if there is a rounding error of 0.11%', async () => {
+ const numerator = new BigNumber(20);
+ const denominator = new BigNumber(9989);
+ const target = new BigNumber(500);
+ // rounding error = ((20*500/9989) - floor(20*500/9989)) / (20*500/9989) = 0.011%
+ const isRoundingError = OrderValidationUtils.isRoundingError(numerator, denominator, target);
+ expect(isRoundingError).to.be.true();
+ });
+
+ it('should return true if there is a rounding error > 0.1%', async () => {
+ const numerator = new BigNumber(3);
+ const denominator = new BigNumber(7);
+ const target = new BigNumber(10);
+ // rounding error = ((3*10/7) - floor(3*10/7)) / (3*10/7) = 6.67%
+ const isRoundingError = OrderValidationUtils.isRoundingError(numerator, denominator, target);
+ expect(isRoundingError).to.be.true();
+ });
+
+ it('should return false when there is no rounding error', async () => {
+ const numerator = new BigNumber(1);
+ const denominator = new BigNumber(2);
+ const target = new BigNumber(10);
+
+ const isRoundingError = OrderValidationUtils.isRoundingError(numerator, denominator, target);
+ expect(isRoundingError).to.be.false();
+ });
+
+ it('should return false when there is rounding error <= 0.1%', async () => {
+ // randomly generated numbers
+ const numerator = new BigNumber(76564);
+ const denominator = new BigNumber(676373677);
+ const target = new BigNumber(105762562);
+ // rounding error = ((76564*105762562/676373677) - floor(76564*105762562/676373677)) /
+ // (76564*105762562/676373677) = 0.0007%
+ const isRoundingError = OrderValidationUtils.isRoundingError(numerator, denominator, target);
+ expect(isRoundingError).to.be.false();
+ });
+ });
+});
diff --git a/packages/order-utils/test/utils/simple_erc20_balance_and_proxy_allowance_fetcher.ts b/packages/order-utils/test/utils/simple_erc20_balance_and_proxy_allowance_fetcher.ts
new file mode 100644
index 000000000..29b9a128b
--- /dev/null
+++ b/packages/order-utils/test/utils/simple_erc20_balance_and_proxy_allowance_fetcher.ts
@@ -0,0 +1,26 @@
+import { BigNumber } from '@0xproject/utils';
+
+import { AbstractBalanceAndProxyAllowanceFetcher } from '../../src/abstract/abstract_balance_and_proxy_allowance_fetcher';
+
+import { ERC20TokenContract } from '../../src/generated_contract_wrappers/e_r_c20_token';
+
+export class SimpleERC20BalanceAndProxyAllowanceFetcher implements AbstractBalanceAndProxyAllowanceFetcher {
+ private _erc20TokenContract: ERC20TokenContract;
+ private _erc20ProxyAddress: string;
+ constructor(erc20TokenWrapper: ERC20TokenContract, erc20ProxyAddress: string) {
+ this._erc20TokenContract = erc20TokenWrapper;
+ this._erc20ProxyAddress = erc20ProxyAddress;
+ }
+ public async getBalanceAsync(assetData: string, userAddress: string): Promise<BigNumber> {
+ // HACK: We cheat and don't pass in the userData since it's always the same token used
+ // in our tests.
+ const balance = await this._erc20TokenContract.balanceOf.callAsync(userAddress);
+ return balance;
+ }
+ public async getProxyAllowanceAsync(assetData: string, userAddress: string): Promise<BigNumber> {
+ // HACK: We cheat and don't pass in the userData since it's always the same token used
+ // in our tests.
+ const proxyAllowance = await this._erc20TokenContract.allowance.callAsync(userAddress, this._erc20ProxyAddress);
+ return proxyAllowance;
+ }
+}