use ForwarderTestFactory for most forwarder tests + some comments in forwarder_test_factory

This commit is contained in:
Michael Zhu 2019-07-23 22:41:06 -07:00
parent af6243afb0
commit 26bfcccedc
2 changed files with 292 additions and 178 deletions

View File

@ -6,9 +6,7 @@ import {
chaiSetup, chaiSetup,
constants, constants,
ContractName, ContractName,
ERC20BalancesByOwner,
expectContractCreationFailedAsync, expectContractCreationFailedAsync,
expectTransactionFailedAsync,
OrderFactory, OrderFactory,
provider, provider,
sendTransactionResult, sendTransactionResult,
@ -23,13 +21,12 @@ import { Web3Wrapper } from '@0x/web3-wrapper';
import * as chai from 'chai'; import * as chai from 'chai';
import { TransactionReceiptWithDecodedLogs } from 'ethereum-types'; import { TransactionReceiptWithDecodedLogs } from 'ethereum-types';
import { artifacts, ForwarderContract, ForwarderWrapper } from '../src'; import { artifacts, ForwarderContract, ForwarderTestFactory, ForwarderWrapper } from '../src';
chaiSetup.configure(); chaiSetup.configure();
const expect = chai.expect; const expect = chai.expect;
const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper); const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
const DECIMALS_DEFAULT = 18; const DECIMALS_DEFAULT = 18;
// const MAX_WETH_FILL_PERCENTAGE = 95;
describe(ContractName.Forwarder, () => { describe(ContractName.Forwarder, () => {
let chainId: number; let chainId: number;
@ -42,7 +39,8 @@ describe(ContractName.Forwarder, () => {
let wethAssetData: string; let wethAssetData: string;
let weth: DummyERC20TokenContract; let weth: DummyERC20TokenContract;
let erc20TokenA: DummyERC20TokenContract; let erc20Token: DummyERC20TokenContract;
let secondErc20Token: DummyERC20TokenContract;
let erc721Token: DummyERC721TokenContract; let erc721Token: DummyERC721TokenContract;
let forwarderContract: ForwarderContract; let forwarderContract: ForwarderContract;
let wethContract: WETH9Contract; let wethContract: WETH9Contract;
@ -53,13 +51,11 @@ describe(ContractName.Forwarder, () => {
let orderWithPercentageFee: SignedOrder; let orderWithPercentageFee: SignedOrder;
let orderWithWethFee: SignedOrder; let orderWithWethFee: SignedOrder;
let orderFactory: OrderFactory; let orderFactory: OrderFactory;
let forwarderTestFactory: ForwarderTestFactory;
let erc20Wrapper: ERC20Wrapper; let erc20Wrapper: ERC20Wrapper;
let erc20Balances: ERC20BalancesByOwner;
let tx: TransactionReceiptWithDecodedLogs; let tx: TransactionReceiptWithDecodedLogs;
let erc721MakerAssetIds: BigNumber[]; let erc721MakerAssetIds: BigNumber[];
let takerEthBalanceBefore: BigNumber;
// let feePercentage: BigNumber;
let gasPrice: BigNumber; let gasPrice: BigNumber;
before(async () => { before(async () => {
@ -83,8 +79,8 @@ describe(ContractName.Forwarder, () => {
const erc721Wrapper = new ERC721Wrapper(provider, usedAddresses, owner); const erc721Wrapper = new ERC721Wrapper(provider, usedAddresses, owner);
erc20Wrapper = new ERC20Wrapper(provider, usedAddresses, owner); erc20Wrapper = new ERC20Wrapper(provider, usedAddresses, owner);
const numDummyErc20ToDeploy = 1; const numDummyErc20ToDeploy = 2;
[erc20TokenA] = await erc20Wrapper.deployDummyTokensAsync( [erc20Token, secondErc20Token] = await erc20Wrapper.deployDummyTokensAsync(
numDummyErc20ToDeploy, numDummyErc20ToDeploy,
constants.DUMMY_TOKEN_DECIMALS, constants.DUMMY_TOKEN_DECIMALS,
); );
@ -120,7 +116,7 @@ describe(ContractName.Forwarder, () => {
from: owner, from: owner,
}); });
defaultMakerAssetAddress = erc20TokenA.address; defaultMakerAssetAddress = erc20Token.address;
const defaultTakerAssetAddress = wethContract.address; const defaultTakerAssetAddress = wethContract.address;
const defaultOrderParams = { const defaultOrderParams = {
makerAddress, makerAddress,
@ -150,15 +146,27 @@ describe(ContractName.Forwarder, () => {
); );
forwarderContract = new ForwarderContract(forwarderInstance.address, provider); forwarderContract = new ForwarderContract(forwarderInstance.address, provider);
forwarderWrapper = new ForwarderWrapper(forwarderContract, provider); forwarderWrapper = new ForwarderWrapper(forwarderContract, provider);
await forwarderWrapper.approveMakerAssetProxyAsync(defaultOrderParams.makerAssetData, { from: takerAddress });
erc20Wrapper.addTokenOwnerAddress(forwarderInstance.address); erc20Wrapper.addTokenOwnerAddress(forwarderInstance.address);
forwarderTestFactory = new ForwarderTestFactory(
forwarderWrapper,
erc20Wrapper,
forwarderInstance.address,
makerAddress,
takerAddress,
orderFeeRecipientAddress,
forwarderFeeRecipientAddress,
weth.address,
gasPrice,
);
}); });
after(async () => { after(async () => {
await blockchainLifecycle.revertAsync(); await blockchainLifecycle.revertAsync();
}); });
beforeEach(async () => { beforeEach(async () => {
await blockchainLifecycle.startAsync(); await blockchainLifecycle.startAsync();
erc20Balances = await erc20Wrapper.getBalancesAsync();
takerEthBalanceBefore = await web3Wrapper.getBalanceInWeiAsync(takerAddress);
orderWithoutFee = await orderFactory.newSignedOrderAsync(); orderWithoutFee = await orderFactory.newSignedOrderAsync();
orderWithPercentageFee = await orderFactory.newSignedOrderAsync({ orderWithPercentageFee = await orderFactory.newSignedOrderAsync({
takerFee: Web3Wrapper.toBaseUnitAmount(new BigNumber(1), DECIMALS_DEFAULT), takerFee: Web3Wrapper.toBaseUnitAmount(new BigNumber(1), DECIMALS_DEFAULT),
@ -166,7 +174,7 @@ describe(ContractName.Forwarder, () => {
}); });
orderWithWethFee = await orderFactory.newSignedOrderAsync({ orderWithWethFee = await orderFactory.newSignedOrderAsync({
takerFee: Web3Wrapper.toBaseUnitAmount(new BigNumber(1), DECIMALS_DEFAULT), takerFee: Web3Wrapper.toBaseUnitAmount(new BigNumber(1), DECIMALS_DEFAULT),
takerFeeAssetData: assetDataUtils.encodeERC20AssetData(wethContract.address), takerFeeAssetData: wethAssetData,
}); });
}); });
afterEach(async () => { afterEach(async () => {
@ -195,169 +203,57 @@ describe(ContractName.Forwarder, () => {
}); });
describe('marketSellOrdersWithEth without extra fees', () => { describe('marketSellOrdersWithEth without extra fees', () => {
it('should fill a single order without a taker fee', async () => { it('should fill a single order without a taker fee', async () => {
const ordersWithoutFee = [orderWithoutFee]; await forwarderTestFactory.marketSellTestAsync([orderWithoutFee], new BigNumber(0.5), erc20Token);
const ethValue = orderWithoutFee.takerAssetAmount.dividedToIntegerBy(2);
tx = await forwarderWrapper.marketSellOrdersWithEthAsync(ordersWithoutFee, {
value: ethValue,
from: takerAddress,
});
const takerEthBalanceAfter = await web3Wrapper.getBalanceInWeiAsync(takerAddress);
const forwarderEthBalance = await web3Wrapper.getBalanceInWeiAsync(forwarderContract.address);
const newBalances = await erc20Wrapper.getBalancesAsync();
const primaryTakerAssetFillAmount = ethValue;
const makerAssetFillAmount = primaryTakerAssetFillAmount
.times(orderWithoutFee.makerAssetAmount)
.dividedToIntegerBy(orderWithoutFee.takerAssetAmount);
const totalEthSpent = primaryTakerAssetFillAmount.plus(gasPrice.times(tx.gasUsed));
expect(takerEthBalanceAfter).to.be.bignumber.equal(takerEthBalanceBefore.minus(totalEthSpent));
expect(newBalances[makerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal(
erc20Balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFillAmount),
);
expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal(
erc20Balances[takerAddress][defaultMakerAssetAddress].plus(makerAssetFillAmount),
);
expect(newBalances[makerAddress][weth.address]).to.be.bignumber.equal(
erc20Balances[makerAddress][weth.address].plus(primaryTakerAssetFillAmount),
);
expect(newBalances[forwarderContract.address][weth.address]).to.be.bignumber.equal(constants.ZERO_AMOUNT);
expect(newBalances[forwarderContract.address][defaultMakerAssetAddress]).to.be.bignumber.equal(
constants.ZERO_AMOUNT,
);
expect(forwarderEthBalance).to.be.bignumber.equal(constants.ZERO_AMOUNT);
}); });
it('should fill multiple orders without taker fees', async () => { it('should fill multiple orders without taker fees', async () => {
const secondOrderWithoutFee = await orderFactory.newSignedOrderAsync(); const secondOrderWithoutFee = await orderFactory.newSignedOrderAsync();
const ordersWithoutFee = [orderWithoutFee, secondOrderWithoutFee]; const orders = [orderWithoutFee, secondOrderWithoutFee];
const ethValue = ordersWithoutFee[0].takerAssetAmount.plus( await forwarderTestFactory.marketSellTestAsync(orders, new BigNumber(1.5), erc20Token);
ordersWithoutFee[1].takerAssetAmount.dividedToIntegerBy(2),
);
tx = await forwarderWrapper.marketSellOrdersWithEthAsync(ordersWithoutFee, {
value: ethValue,
from: takerAddress,
});
const takerEthBalanceAfter = await web3Wrapper.getBalanceInWeiAsync(takerAddress);
const forwarderEthBalance = await web3Wrapper.getBalanceInWeiAsync(forwarderContract.address);
const newBalances = await erc20Wrapper.getBalancesAsync();
const primaryTakerAssetFillAmount = ethValue;
const firstTakerAssetFillAmount = ordersWithoutFee[0].takerAssetAmount;
const secondTakerAssetFillAmount = primaryTakerAssetFillAmount.minus(firstTakerAssetFillAmount);
const makerAssetFillAmount = ordersWithoutFee[0].makerAssetAmount.plus(
ordersWithoutFee[1].makerAssetAmount
.times(secondTakerAssetFillAmount)
.dividedToIntegerBy(ordersWithoutFee[1].takerAssetAmount),
);
const totalEthSpent = primaryTakerAssetFillAmount.plus(gasPrice.times(tx.gasUsed));
expect(takerEthBalanceAfter).to.be.bignumber.equal(takerEthBalanceBefore.minus(totalEthSpent));
expect(newBalances[makerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal(
erc20Balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFillAmount),
);
expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal(
erc20Balances[takerAddress][defaultMakerAssetAddress].plus(makerAssetFillAmount),
);
expect(newBalances[makerAddress][weth.address]).to.be.bignumber.equal(
erc20Balances[makerAddress][weth.address].plus(primaryTakerAssetFillAmount),
);
expect(newBalances[forwarderContract.address][weth.address]).to.be.bignumber.equal(constants.ZERO_AMOUNT);
expect(newBalances[forwarderContract.address][defaultMakerAssetAddress]).to.be.bignumber.equal(
constants.ZERO_AMOUNT,
);
expect(forwarderEthBalance).to.be.bignumber.equal(constants.ZERO_AMOUNT);
}); });
it('should fill a single order with a percentage-based fee', async () => { it('should fill a single order with a percentage fee', async () => {
const ordersWithFee = [orderWithPercentageFee]; await forwarderTestFactory.marketSellTestAsync([orderWithPercentageFee], new BigNumber(0.5), erc20Token);
const ethValue = orderWithPercentageFee.takerAssetAmount.dividedToIntegerBy(2);
tx = await forwarderWrapper.marketSellOrdersWithEthAsync(ordersWithFee, {
value: ethValue,
from: takerAddress,
});
const takerEthBalanceAfter = await web3Wrapper.getBalanceInWeiAsync(takerAddress);
const forwarderEthBalance = await web3Wrapper.getBalanceInWeiAsync(forwarderContract.address);
const newBalances = await erc20Wrapper.getBalancesAsync();
const primaryTakerAssetFillAmount = ethValue;
const makerAssetFillAmount = primaryTakerAssetFillAmount
.times(orderWithPercentageFee.makerAssetAmount)
.dividedToIntegerBy(orderWithPercentageFee.takerAssetAmount);
const takerFeeAmount = primaryTakerAssetFillAmount
.times(orderWithPercentageFee.takerFee)
.dividedToIntegerBy(orderWithPercentageFee.takerAssetAmount);
const totalEthSpent = primaryTakerAssetFillAmount.plus(gasPrice.times(tx.gasUsed));
expect(takerEthBalanceAfter).to.be.bignumber.equal(takerEthBalanceBefore.minus(totalEthSpent));
expect(newBalances[makerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal(
erc20Balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFillAmount),
);
expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal(
erc20Balances[takerAddress][defaultMakerAssetAddress].plus(makerAssetFillAmount).minus(takerFeeAmount),
);
expect(newBalances[orderFeeRecipientAddress][defaultMakerAssetAddress]).to.be.bignumber.equal(
erc20Balances[orderFeeRecipientAddress][defaultMakerAssetAddress].plus(takerFeeAmount),
);
expect(newBalances[makerAddress][weth.address]).to.be.bignumber.equal(
erc20Balances[makerAddress][weth.address].plus(primaryTakerAssetFillAmount),
);
expect(newBalances[forwarderContract.address][weth.address]).to.be.bignumber.equal(constants.ZERO_AMOUNT);
expect(newBalances[forwarderContract.address][defaultMakerAssetAddress]).to.be.bignumber.equal(
constants.ZERO_AMOUNT,
);
expect(forwarderEthBalance).to.be.bignumber.equal(constants.ZERO_AMOUNT);
}); });
it('should fill multiple orders with percentage-based fees', async () => { it('should fill multiple orders with percentage fees', async () => {
const secondOrderWithPercentageFee = await orderFactory.newSignedOrderAsync({ const secondOrderWithPercentageFee = await orderFactory.newSignedOrderAsync({
takerFee: Web3Wrapper.toBaseUnitAmount(new BigNumber(2), DECIMALS_DEFAULT), takerFee: Web3Wrapper.toBaseUnitAmount(new BigNumber(2), DECIMALS_DEFAULT),
takerFeeAssetData: assetDataUtils.encodeERC20AssetData(defaultMakerAssetAddress), takerFeeAssetData: assetDataUtils.encodeERC20AssetData(defaultMakerAssetAddress),
}); });
const ordersWithFee = [orderWithPercentageFee, secondOrderWithPercentageFee]; const orders = [orderWithPercentageFee, secondOrderWithPercentageFee];
const ethValue = ordersWithFee[0].takerAssetAmount.plus( await forwarderTestFactory.marketSellTestAsync(orders, new BigNumber(1.5), erc20Token);
ordersWithFee[1].takerAssetAmount.dividedToIntegerBy(2), });
); it('should fail to fill an order with a percentage fee if the asset proxy is not yet approved', async () => {
const unapprovedAsset = assetDataUtils.encodeERC20AssetData(secondErc20Token.address);
const order = await orderFactory.newSignedOrderAsync({
makerAssetData: unapprovedAsset,
takerFee: Web3Wrapper.toBaseUnitAmount(new BigNumber(2), DECIMALS_DEFAULT),
takerFeeAssetData: unapprovedAsset,
});
tx = await forwarderWrapper.marketSellOrdersWithEthAsync(ordersWithFee, { const ethValue = order.takerAssetAmount;
const erc20Balances = await erc20Wrapper.getBalancesAsync();
const takerEthBalanceBefore = await web3Wrapper.getBalanceInWeiAsync(takerAddress);
// Execute test case
tx = await forwarderWrapper.marketSellOrdersWithEthAsync([order], {
value: ethValue, value: ethValue,
from: takerAddress, from: takerAddress,
}); });
const takerEthBalanceAfter = await web3Wrapper.getBalanceInWeiAsync(takerAddress); const takerEthBalanceAfter = await web3Wrapper.getBalanceInWeiAsync(takerAddress);
const forwarderEthBalance = await web3Wrapper.getBalanceInWeiAsync(forwarderContract.address); const forwarderEthBalance = await web3Wrapper.getBalanceInWeiAsync(forwarderContract.address);
const newBalances = await erc20Wrapper.getBalancesAsync(); const newBalances = await erc20Wrapper.getBalancesAsync();
const totalEthSpent = gasPrice.times(tx.gasUsed);
const primaryTakerAssetFillAmount = ethValue; // Validate test case
const firstTakerAssetFillAmount = ordersWithFee[0].takerAssetAmount;
const secondTakerAssetFillAmount = primaryTakerAssetFillAmount.minus(firstTakerAssetFillAmount);
const makerAssetFillAmount = ordersWithFee[0].makerAssetAmount.plus(
ordersWithFee[1].makerAssetAmount
.times(secondTakerAssetFillAmount)
.dividedToIntegerBy(ordersWithFee[1].takerAssetAmount),
);
const firstTakerFeeAmount = firstTakerAssetFillAmount
.times(orderWithPercentageFee.takerFee)
.dividedToIntegerBy(orderWithPercentageFee.takerAssetAmount);
const secondTakerFeeAmount = secondTakerAssetFillAmount
.times(secondOrderWithPercentageFee.takerFee)
.dividedToIntegerBy(secondOrderWithPercentageFee.takerAssetAmount);
const takerFeeAmount = firstTakerFeeAmount.plus(secondTakerFeeAmount);
const totalEthSpent = primaryTakerAssetFillAmount.plus(gasPrice.times(tx.gasUsed));
expect(takerEthBalanceAfter).to.be.bignumber.equal(takerEthBalanceBefore.minus(totalEthSpent)); expect(takerEthBalanceAfter).to.be.bignumber.equal(takerEthBalanceBefore.minus(totalEthSpent));
expect(newBalances[makerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( expect(newBalances[makerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal(
erc20Balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFillAmount), erc20Balances[makerAddress][defaultMakerAssetAddress],
); );
expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal(
erc20Balances[takerAddress][defaultMakerAssetAddress].plus(makerAssetFillAmount).minus(takerFeeAmount), erc20Balances[takerAddress][defaultMakerAssetAddress],
);
expect(newBalances[orderFeeRecipientAddress][defaultMakerAssetAddress]).to.be.bignumber.equal(
erc20Balances[orderFeeRecipientAddress][defaultMakerAssetAddress].plus(takerFeeAmount),
); );
expect(newBalances[makerAddress][weth.address]).to.be.bignumber.equal( expect(newBalances[makerAddress][weth.address]).to.be.bignumber.equal(
erc20Balances[makerAddress][weth.address].plus(primaryTakerAssetFillAmount), erc20Balances[makerAddress][weth.address],
); );
expect(newBalances[forwarderContract.address][weth.address]).to.be.bignumber.equal(constants.ZERO_AMOUNT); expect(newBalances[forwarderContract.address][weth.address]).to.be.bignumber.equal(constants.ZERO_AMOUNT);
expect(newBalances[forwarderContract.address][defaultMakerAssetAddress]).to.be.bignumber.equal( expect(newBalances[forwarderContract.address][defaultMakerAssetAddress]).to.be.bignumber.equal(
@ -365,9 +261,21 @@ describe(ContractName.Forwarder, () => {
); );
expect(forwarderEthBalance).to.be.bignumber.equal(constants.ZERO_AMOUNT); expect(forwarderEthBalance).to.be.bignumber.equal(constants.ZERO_AMOUNT);
}); });
it('should fill a single order with a WETH fee', async () => {
await forwarderTestFactory.marketSellTestAsync([orderWithWethFee], new BigNumber(0.5), erc20Token);
});
it('should fill multiple orders with WETH fees', async () => {
const secondOrderWithPercentageFee = await orderFactory.newSignedOrderAsync({
takerFee: Web3Wrapper.toBaseUnitAmount(new BigNumber(2), DECIMALS_DEFAULT),
takerFeeAssetData: assetDataUtils.encodeERC20AssetData(defaultMakerAssetAddress),
});
const orders = [orderWithWethFee, secondOrderWithPercentageFee];
await forwarderTestFactory.marketSellTestAsync(orders, new BigNumber(1.5), erc20Token);
});
it('should refund remaining ETH if amount is greater than takerAssetAmount', async () => { it('should refund remaining ETH if amount is greater than takerAssetAmount', async () => {
const ordersWithoutFee = [orderWithoutFee]; const ordersWithoutFee = [orderWithoutFee];
const ethValue = orderWithoutFee.takerAssetAmount.times(2); const ethValue = orderWithoutFee.takerAssetAmount.times(2);
const takerEthBalanceBefore = await web3Wrapper.getBalanceInWeiAsync(takerAddress);
tx = await forwarderWrapper.marketSellOrdersWithEthAsync(ordersWithoutFee, { tx = await forwarderWrapper.marketSellOrdersWithEthAsync(ordersWithoutFee, {
value: ethValue, value: ethValue,
@ -378,25 +286,226 @@ describe(ContractName.Forwarder, () => {
expect(takerEthBalanceAfter).to.be.bignumber.equal(takerEthBalanceBefore.minus(totalEthSpent)); expect(takerEthBalanceAfter).to.be.bignumber.equal(takerEthBalanceBefore.minus(totalEthSpent));
}); });
// it('should not fill orders with different makerAssetData than the first order', async () => { it('should not fill orders with different makerAssetData than the first order', async () => {
// const makerAssetId = erc721MakerAssetIds[0]; const makerAssetId = erc721MakerAssetIds[0];
// const erc721SignedOrder = await orderFactory.newSignedOrderAsync({ const erc721SignedOrder = await orderFactory.newSignedOrderAsync({
// makerAssetAmount: new BigNumber(1), makerAssetAmount: new BigNumber(1),
// makerAssetData: assetDataUtils.encodeERC721AssetData(erc721Token.address, makerAssetId), makerAssetData: assetDataUtils.encodeERC721AssetData(erc721Token.address, makerAssetId),
// }); });
// const erc20SignedOrder = await orderFactory.newSignedOrderAsync(); const erc20SignedOrder = await orderFactory.newSignedOrderAsync();
// const ordersWithoutFee = [erc20SignedOrder, erc721SignedOrder]; const orders = [erc20SignedOrder, erc721SignedOrder];
// const ethValue = erc20SignedOrder.takerAssetAmount.plus(erc721SignedOrder.takerAssetAmount);
// await forwarderTestFactory.marketSellTestAsync(orders, new BigNumber(2), erc20Token, {
// tx = await forwarderWrapper.marketSellOrdersWithEthAsync(ordersWithoutFee, { revertReason: RevertReason.MakerAssetMismatch,
// value: ethValue, });
// from: takerAddress, });
// }); });
// const takerEthBalanceAfter = await web3Wrapper.getBalanceInWeiAsync(takerAddress); describe('marketSellOrdersWithEth with extra fees', () => {
// const totalEthSpent = erc20SignedOrder.takerAssetAmount.plus(gasPrice.times(tx.gasUsed)); it('should fill the order and send fee to feeRecipient', async () => {
// await forwarderTestFactory.marketSellTestAsync([orderWithoutFee], new BigNumber(0.5), erc20Token, {
// expect(takerEthBalanceAfter).to.be.bignumber.equal(takerEthBalanceBefore.minus(totalEthSpent)); forwarderFeePercentage: new BigNumber(2),
// }); });
});
it('should fail if the fee is set too high', async () => {
await forwarderTestFactory.marketSellTestAsync([orderWithoutFee], new BigNumber(0.5), erc20Token, {
forwarderFeePercentage: new BigNumber(6),
revertReason: RevertReason.FeePercentageTooLarge,
});
});
});
describe('marketBuyOrdersWithEth without extra fees', () => {
it('should buy the exact amount of makerAsset in a single order', async () => {
await forwarderTestFactory.marketBuyTestAsync([orderWithoutFee], new BigNumber(0.5), erc20Token);
});
it('should buy the exact amount of makerAsset in multiple orders', async () => {
const secondOrderWithoutFee = await orderFactory.newSignedOrderAsync();
const orders = [orderWithoutFee, secondOrderWithoutFee];
await forwarderTestFactory.marketBuyTestAsync(orders, new BigNumber(1.5), erc20Token);
});
it('should buy the exact amount of makerAsset and return excess ETH', async () => {
await forwarderTestFactory.marketBuyTestAsync([orderWithoutFee], new BigNumber(0.5), erc20Token, {
ethValueAdjustment: orderWithoutFee.takerAssetAmount.dividedToIntegerBy(2),
});
});
it('should buy the exact amount of makerAsset from a single order with a WETH fee', async () => {
await forwarderTestFactory.marketBuyTestAsync([orderWithWethFee], new BigNumber(0.5), erc20Token);
});
it('should buy the exact amount of makerAsset from a single order with a percentage fee', async () => {
await forwarderTestFactory.marketBuyTestAsync([orderWithPercentageFee], new BigNumber(0.5), erc20Token);
});
it('should revert if the amount of ETH sent is too low to fill the makerAssetAmount', async () => {
await forwarderTestFactory.marketBuyTestAsync([orderWithoutFee], new BigNumber(0.5), erc20Token, {
ethValueAdjustment: new BigNumber(-1),
revertReason: RevertReason.CompleteFillFailed,
});
});
it('should buy an ERC721 asset from a single order', async () => {
const makerAssetId = erc721MakerAssetIds[0];
const erc721Order = await orderFactory.newSignedOrderAsync({
makerAssetAmount: new BigNumber(1),
makerAssetData: assetDataUtils.encodeERC721AssetData(erc721Token.address, makerAssetId),
});
await forwarderTestFactory.marketBuyTestAsync([erc721Order], new BigNumber(1), erc721Token, {
makerAssetId,
});
});
it('should revert if buying an ERC721 asset when later orders contain different makerAssetData', async () => {
const makerAssetId = erc721MakerAssetIds[0];
const erc721Order = await orderFactory.newSignedOrderAsync({
makerAssetAmount: new BigNumber(1),
makerAssetData: assetDataUtils.encodeERC721AssetData(erc721Token.address, makerAssetId),
});
const differentMakerAssetDataOrder = await orderFactory.newSignedOrderAsync();
const orders = [erc721Order, differentMakerAssetDataOrder];
await forwarderTestFactory.marketBuyTestAsync(orders, new BigNumber(2), erc721Token, {
makerAssetId,
revertReason: RevertReason.MakerAssetMismatch,
});
});
it('should buy an ERC721 asset and pay WETH fees from a single fee order', async () => {
const makerAssetId = erc721MakerAssetIds[0];
const erc721orderWithWethFee = await orderFactory.newSignedOrderAsync({
makerAssetAmount: new BigNumber(1),
makerAssetData: assetDataUtils.encodeERC721AssetData(erc721Token.address, makerAssetId),
takerFee: Web3Wrapper.toBaseUnitAmount(new BigNumber(1), DECIMALS_DEFAULT),
takerFeeAssetData: wethAssetData,
});
await forwarderTestFactory.marketBuyTestAsync([erc721orderWithWethFee], new BigNumber(1), erc721Token, {
makerAssetId,
});
});
it('Should buy slightly greater MakerAsset when exchange rate is rounded', async () => {
// The 0x Protocol contracts round the exchange rate in favor of the Maker.
// In this case, the taker must round up how much they're going to spend, which
// in turn increases the amount of MakerAsset being purchased.
// Example:
// The taker wants to buy 5 units of the MakerAsset at a rate of 3M/2T.
// For every 2 units of TakerAsset, the taker will receive 3 units of MakerAsset.
// To purchase 5 units, the taker must spend 10/3 = 3.33 units of TakerAssset.
// However, the Taker can only spend whole units.
// Spending floor(10/3) = 3 units will yield a profit of Floor(3*3/2) = Floor(4.5) = 4 units of MakerAsset.
// Spending ceil(10/3) = 4 units will yield a profit of Floor(4*3/2) = 6 units of MakerAsset.
//
// The forwarding contract will opt for the second option, which overbuys, to ensure the taker
// receives at least the amount of MakerAsset they requested.
//
// Construct test case using values from example above
orderWithoutFee = await orderFactory.newSignedOrderAsync({
makerAssetAmount: new BigNumber('30'),
takerAssetAmount: new BigNumber('20'),
makerAssetData: assetDataUtils.encodeERC20AssetData(erc20Token.address),
takerAssetData: assetDataUtils.encodeERC20AssetData(weth.address),
makerFee: new BigNumber(0),
takerFee: new BigNumber(0),
});
const ordersWithoutFee = [orderWithoutFee];
const desiredMakerAssetFillAmount = new BigNumber('5');
const makerAssetFillAmount = new BigNumber('6');
const ethValue = new BigNumber('4');
const erc20Balances = await erc20Wrapper.getBalancesAsync();
const takerEthBalanceBefore = await web3Wrapper.getBalanceInWeiAsync(takerAddress);
// Execute test case
tx = await forwarderWrapper.marketBuyOrdersWithEthAsync(ordersWithoutFee, desiredMakerAssetFillAmount, {
value: ethValue,
from: takerAddress,
});
// Fetch end balances and construct expected outputs
const takerEthBalanceAfter = await web3Wrapper.getBalanceInWeiAsync(takerAddress);
const forwarderEthBalance = await web3Wrapper.getBalanceInWeiAsync(forwarderContract.address);
const newBalances = await erc20Wrapper.getBalancesAsync();
const primaryTakerAssetFillAmount = ethValue;
const totalEthSpent = primaryTakerAssetFillAmount.plus(gasPrice.times(tx.gasUsed));
// Validate test case
expect(makerAssetFillAmount).to.be.bignumber.greaterThan(desiredMakerAssetFillAmount);
expect(takerEthBalanceAfter).to.be.bignumber.equal(takerEthBalanceBefore.minus(totalEthSpent));
expect(newBalances[makerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal(
erc20Balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFillAmount),
);
expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal(
erc20Balances[takerAddress][defaultMakerAssetAddress].plus(makerAssetFillAmount),
);
expect(newBalances[makerAddress][weth.address]).to.be.bignumber.equal(
erc20Balances[makerAddress][weth.address].plus(primaryTakerAssetFillAmount),
);
expect(newBalances[forwarderContract.address][weth.address]).to.be.bignumber.equal(constants.ZERO_AMOUNT);
expect(newBalances[forwarderContract.address][defaultMakerAssetAddress]).to.be.bignumber.equal(
constants.ZERO_AMOUNT,
);
expect(forwarderEthBalance).to.be.bignumber.equal(constants.ZERO_AMOUNT);
});
it('Should buy slightly greater MakerAsset when exchange rate is rounded (Regression Test)', async () => {
// Order taken from a transaction on mainnet that failed due to a rounding error.
orderWithoutFee = await orderFactory.newSignedOrderAsync({
makerAssetAmount: new BigNumber('268166666666666666666'),
takerAssetAmount: new BigNumber('219090625878836371'),
makerAssetData: assetDataUtils.encodeERC20AssetData(erc20Token.address),
takerAssetData: assetDataUtils.encodeERC20AssetData(weth.address),
makerFee: new BigNumber(0),
takerFee: new BigNumber(0),
});
const ordersWithoutFee = [orderWithoutFee];
const erc20Balances = await erc20Wrapper.getBalancesAsync();
const takerEthBalanceBefore = await web3Wrapper.getBalanceInWeiAsync(takerAddress);
// The taker will receive more than the desired amount of makerAsset due to rounding
const desiredMakerAssetFillAmount = new BigNumber('5000000000000000000');
const ethValue = new BigNumber('4084971271824171');
const makerAssetFillAmount = ethValue
.times(orderWithoutFee.makerAssetAmount)
.dividedToIntegerBy(orderWithoutFee.takerAssetAmount);
// Execute test case
tx = await forwarderWrapper.marketBuyOrdersWithEthAsync(ordersWithoutFee, desiredMakerAssetFillAmount, {
value: ethValue,
from: takerAddress,
});
// Fetch end balances and construct expected outputs
const takerEthBalanceAfter = await web3Wrapper.getBalanceInWeiAsync(takerAddress);
const forwarderEthBalance = await web3Wrapper.getBalanceInWeiAsync(forwarderContract.address);
const newBalances = await erc20Wrapper.getBalancesAsync();
const primaryTakerAssetFillAmount = ethValue;
const totalEthSpent = primaryTakerAssetFillAmount.plus(gasPrice.times(tx.gasUsed));
// Validate test case
expect(makerAssetFillAmount).to.be.bignumber.greaterThan(desiredMakerAssetFillAmount);
expect(takerEthBalanceAfter).to.be.bignumber.equal(takerEthBalanceBefore.minus(totalEthSpent));
expect(newBalances[makerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal(
erc20Balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFillAmount),
);
expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal(
erc20Balances[takerAddress][defaultMakerAssetAddress].plus(makerAssetFillAmount),
);
expect(newBalances[makerAddress][weth.address]).to.be.bignumber.equal(
erc20Balances[makerAddress][weth.address].plus(primaryTakerAssetFillAmount),
);
expect(newBalances[forwarderContract.address][weth.address]).to.be.bignumber.equal(constants.ZERO_AMOUNT);
expect(newBalances[forwarderContract.address][defaultMakerAssetAddress]).to.be.bignumber.equal(
constants.ZERO_AMOUNT,
);
expect(forwarderEthBalance).to.be.bignumber.equal(constants.ZERO_AMOUNT);
});
});
describe('marketBuyOrdersWithEth with extra fees', () => {
it('should buy the asset and send fee to feeRecipient', async () => {
await forwarderTestFactory.marketBuyTestAsync([orderWithoutFee], new BigNumber(0.5), erc20Token, {
forwarderFeePercentage: new BigNumber(2),
});
});
it('should fail if the fee is set too high', async () => {
await forwarderTestFactory.marketBuyTestAsync([orderWithoutFee], new BigNumber(0.5), erc20Token, {
forwarderFeePercentage: new BigNumber(6),
revertReason: RevertReason.FeePercentageTooLarge,
});
});
it('should fail if there is not enough ETH remaining to pay the fee', async () => {
await forwarderTestFactory.marketBuyTestAsync([orderWithoutFee], new BigNumber(0.5), erc20Token, {
ethValueAdjustment: new BigNumber(-1),
forwarderFeePercentage: new BigNumber(2),
revertReason: RevertReason.InsufficientEthRemaining,
});
});
}); });
}); });
// tslint:disable:max-file-line-count // tslint:disable:max-file-line-count

View File

@ -19,6 +19,7 @@ import { ForwarderWrapper } from './forwarder_wrapper';
chaiSetup.configure(); chaiSetup.configure();
const expect = chai.expect; const expect = chai.expect;
// Necessary bookkeeping to validate Forwarder results
interface ForwarderFillState { interface ForwarderFillState {
takerAssetFillAmount: BigNumber; takerAssetFillAmount: BigNumber;
makerAssetFillAmount: BigNumber; makerAssetFillAmount: BigNumber;
@ -28,6 +29,9 @@ interface ForwarderFillState {
maxOverboughtMakerAsset: BigNumber; maxOverboughtMakerAsset: BigNumber;
} }
// Simulates filling some orders via the Forwarder contract. For example, if
// orders = [A, B, C, D] and fractionalNumberOfOrdersToFill = 2.3, then
// we simulate A and B being completely filled, and 0.3 * C being filled.
function computeExpectedResults(orders: SignedOrder[], fractionalNumberOfOrdersToFill: BigNumber): ForwarderFillState { function computeExpectedResults(orders: SignedOrder[], fractionalNumberOfOrdersToFill: BigNumber): ForwarderFillState {
const currentState = { const currentState = {
takerAssetFillAmount: constants.ZERO_AMOUNT, takerAssetFillAmount: constants.ZERO_AMOUNT,
@ -71,7 +75,7 @@ function computeExpectedResults(orders: SignedOrder[], fractionalNumberOfOrdersT
currentState.percentageFees = currentState.percentageFees.plus(takerFee); currentState.percentageFees = currentState.percentageFees.plus(takerFee);
} else if (order.takerFeeAssetData === order.takerAssetData) { } else if (order.takerFeeAssetData === order.takerAssetData) {
currentState.wethFees = currentState.wethFees.plus(takerFee); currentState.wethFees = currentState.wethFees.plus(takerFee);
// Up to 1 wei worth of WETH will be oversold per order // Up to 1 wei worth of WETH will be oversold per order due to rounding
currentState.maxOversoldWeth = currentState.maxOversoldWeth.plus(new BigNumber(1)); currentState.maxOversoldWeth = currentState.maxOversoldWeth.plus(new BigNumber(1));
// Equivalently, up to 1 wei worth of maker asset will be overbought per order // Equivalently, up to 1 wei worth of maker asset will be overbought per order
currentState.maxOverboughtMakerAsset = currentState.maxOversoldWeth currentState.maxOverboughtMakerAsset = currentState.maxOversoldWeth
@ -86,6 +90,7 @@ function computeExpectedResults(orders: SignedOrder[], fractionalNumberOfOrdersT
return currentState; return currentState;
} }
// Since bignumber is not compatible with chai's within
function expectBalanceWithin(balance: BigNumber, low: BigNumber, high: BigNumber): void { function expectBalanceWithin(balance: BigNumber, low: BigNumber, high: BigNumber): void {
expect(balance).to.be.bignumber.gte(low); expect(balance).to.be.bignumber.gte(low);
expect(balance).to.be.bignumber.lte(high); expect(balance).to.be.bignumber.lte(high);
@ -99,8 +104,8 @@ export class ForwarderTestFactory {
private readonly _takerAddress: string; private readonly _takerAddress: string;
private readonly _orderFeeRecipientAddress: string; private readonly _orderFeeRecipientAddress: string;
private readonly _forwarderFeeRecipientAddress: string; private readonly _forwarderFeeRecipientAddress: string;
private readonly _gasPrice: BigNumber;
private readonly _wethAddress: string; private readonly _wethAddress: string;
private readonly _gasPrice: BigNumber;
public static getPercentageOfValue(value: BigNumber, percentage: BigNumber): BigNumber { public static getPercentageOfValue(value: BigNumber, percentage: BigNumber): BigNumber {
const numerator = constants.PERCENTAGE_DENOMINATOR.times(percentage).dividedToIntegerBy(100); const numerator = constants.PERCENTAGE_DENOMINATOR.times(percentage).dividedToIntegerBy(100);
@ -116,8 +121,8 @@ export class ForwarderTestFactory {
takerAddress: string, takerAddress: string,
orderFeeRecipientAddress: string, orderFeeRecipientAddress: string,
forwarderFeeRecipientAddress: string, forwarderFeeRecipientAddress: string,
gasPrice: BigNumber,
wethAddress: string, wethAddress: string,
gasPrice: BigNumber,
) { ) {
this._forwarderWrapper = forwarderWrapper; this._forwarderWrapper = forwarderWrapper;
this._erc20Wrapper = erc20Wrapper; this._erc20Wrapper = erc20Wrapper;
@ -126,8 +131,8 @@ export class ForwarderTestFactory {
this._takerAddress = takerAddress; this._takerAddress = takerAddress;
this._orderFeeRecipientAddress = orderFeeRecipientAddress; this._orderFeeRecipientAddress = orderFeeRecipientAddress;
this._forwarderFeeRecipientAddress = forwarderFeeRecipientAddress; this._forwarderFeeRecipientAddress = forwarderFeeRecipientAddress;
this._gasPrice = gasPrice;
this._wethAddress = wethAddress; this._wethAddress = wethAddress;
this._gasPrice = gasPrice;
} }
public async marketBuyTestAsync( public async marketBuyTestAsync(
@ -135,7 +140,7 @@ export class ForwarderTestFactory {
fractionalNumberOfOrdersToFill: BigNumber, fractionalNumberOfOrdersToFill: BigNumber,
makerAssetContract: DummyERC20TokenContract | DummyERC721TokenContract, makerAssetContract: DummyERC20TokenContract | DummyERC721TokenContract,
options: { options: {
ethValueAdjustment?: BigNumber; ethValueAdjustment?: BigNumber; // Used to provided insufficient/excess ETH
forwarderFeePercentage?: BigNumber; forwarderFeePercentage?: BigNumber;
makerAssetId?: BigNumber; makerAssetId?: BigNumber;
revertReason?: RevertReason; revertReason?: RevertReason;