Skip to content

Commit

Permalink
add tests for flipping pos
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Elsasser committed Sep 27, 2018
1 parent 8077f4a commit e803713
Showing 1 changed file with 130 additions and 0 deletions.
130 changes: 130 additions & 0 deletions test/MarketCollateralPoolAccounting.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,5 +308,135 @@ contract('MarketCollateralPool.Accounting', function(accounts) {
assert.equal(takerLastPosition[0].toNumber(), entryOrderPrice, 'Taker should be short 1 from entryOrderPrice');
assert.equal(takerLastPosition[1].toNumber(), -1, 'Taker should be short 1 from entryOrderPrice');
});

it('Flipping a position works correctly', async function() {

// Confirm we have expected positions from previous test.
var makerNetPos = await collateralPool.getUserNetPosition.call(marketContract.address, accountMaker);
var takerNetPos = await collateralPool.getUserNetPosition.call(marketContract.address, accountTaker);
assert.equal(makerNetPos.toNumber(), 1, 'Maker should be long 1');
assert.equal(takerNetPos.toNumber(), -1, 'Taker should be short 1');

var makerPosCount = await collateralPool.getUserPositionCount.call(marketContract.address, accountMaker);
var takerPosCount = await collateralPool.getUserPositionCount.call(marketContract.address, accountTaker);
assert.equal(makerPosCount.toNumber(), 1, 'Maker should have one position struct');
assert.equal(takerPosCount.toNumber(), 1, 'Taker should have one position struct');

var makerPos = await collateralPool.getUserPosition.call(marketContract.address, accountMaker, 0);
var takerPos = await collateralPool.getUserPosition.call(marketContract.address, accountTaker, 0);

assert.equal(
makerPos[0].toNumber(),
entryOrderPrice,
'Maker should have one position from entryOrderPrice'
);

assert.equal(
takerPos[0].toNumber(),
entryOrderPrice,
'Maker should have one position from entryOrderPrice'
);

assert.equal(makerPos[1].toNumber(), 1, 'Maker should have one position, long +1');
assert.equal(takerPos[1].toNumber(), -1, 'Taker should have one position, short -1');

// Create a new position from new price
const timeStamp = new Date().getTime() / 1000 + 60 * 5; // order expires 5 minute from now.
const orderAddresses = [accountMaker, accountTaker, accounts[2]];
const secondEntryOrderPrice = entryOrderPrice - 100;
const secondUnsignedOrderValues = [0, 0, secondEntryOrderPrice, timeStamp, 1]; // second order with new price.
var orderQty = 2;

const secondOrderHash = await orderLib._createOrderHash.call(
marketContract.address,
orderAddresses,
secondUnsignedOrderValues,
orderQty
);

// create approval and deposit collateral tokens for trading.
const amountToDeposit = 5000000;
await collateralToken.approve(collateralPool.address, amountToDeposit, { from: accounts[0] });
await collateralToken.approve(collateralPool.address, amountToDeposit, { from: accounts[1] });

// move tokens to the collateralPool
await collateralPool.depositTokensForTrading(collateralToken.address, amountToDeposit, { from: accounts[0] });
await collateralPool.depositTokensForTrading(collateralToken.address, amountToDeposit, { from: accounts[1] });

makerAccountBalanceBeforeTrade = await collateralPool.getUserUnallocatedBalance.call(collateralToken.address,
accounts[0]
);
takerAccountBalanceBeforeTrade = await collateralPool.getUserUnallocatedBalance.call(collateralToken.address,
accounts[1]
);

// Create a second position, from a new price.
qtyToFill = 2;
orderSignature = utility.signMessage(web3, accountMaker, secondOrderHash);
await marketContract.tradeOrder(
orderAddresses,
secondUnsignedOrderValues,
orderQty,
qtyToFill,
orderSignature[0], // v
orderSignature[1], // r
orderSignature[2], // s
{ from: accountTaker }
);

makerNetPos = await collateralPool.getUserNetPosition.call(marketContract.address, accountMaker);
takerNetPos = await collateralPool.getUserNetPosition.call(marketContract.address, accountTaker);
assert.equal(makerNetPos.toNumber(), 3, 'Maker should be long 3');
assert.equal(takerNetPos.toNumber(), -3, 'Taker should be short 3');

makerPosCount = await collateralPool.getUserPositionCount.call(marketContract.address, accountMaker);
takerPosCount = await collateralPool.getUserPositionCount.call(marketContract.address, accountTaker);
assert.equal(makerPosCount.toNumber(), 2, 'Maker should have 2 position structs');
assert.equal(takerPosCount.toNumber(), 2, 'Taker should have 2 position structs');

var makerLastPosition = await collateralPool.getUserPosition.call(marketContract.address, accountMaker, 1);
assert.equal(makerLastPosition[0].toNumber(), secondEntryOrderPrice, 'Maker should be long 2 from secondEntryOrderPrice');
assert.equal(makerLastPosition[1].toNumber(), qtyToFill, 'Maker should be long 2 from secondEntryOrderPrice');

var takerLastPosition = await collateralPool.getUserPosition.call(marketContract.address, accountTaker, 1);
assert.equal(takerLastPosition[0].toNumber(), secondEntryOrderPrice, 'Taker should be short 2 from secondEntryOrderPrice');
assert.equal(takerLastPosition[1].toNumber(), qtyToFill * -1, 'Taker should be short 2 from secondEntryOrderPrice');

// We are now going to create a trade that will flip all parties open positions, exiting a trade and entering new one
const exitOrderPrice = secondEntryOrderPrice - 100;
const unsignedExitOrderValues = [0, 0, exitOrderPrice, timeStamp, 1]; // second order with new price.
const exitOrderQty = -5;
const exitOrderHash = await orderLib._createOrderHash.call(
marketContract.address,
orderAddresses,
unsignedExitOrderValues,
exitOrderQty
);
orderSignature = utility.signMessage(web3, accountMaker, exitOrderHash);

await marketContract.tradeOrder(
orderAddresses,
unsignedExitOrderValues,
exitOrderQty,
exitOrderQty,
orderSignature[0], // v
orderSignature[1], // r
orderSignature[2], // s
{ from: accountTaker }
);

makerPosCount = await collateralPool.getUserPositionCount.call(marketContract.address, accountMaker);
takerPosCount = await collateralPool.getUserPositionCount.call(marketContract.address, accountTaker);
assert.equal(makerPosCount.toNumber(), 1, 'Maker should have 1 position struct');
assert.equal(takerPosCount.toNumber(), 1, 'Taker should have 1 position struct');

makerLastPosition = await collateralPool.getUserPosition.call(marketContract.address, accountMaker, 0);
assert.equal(makerLastPosition[0].toNumber(), exitOrderPrice, 'Maker should be short -2 from exitOrderPrice');
assert.equal(makerLastPosition[1].toNumber(), -2, 'Maker should be short -2 from exitOrderPrice');

var takerLastPosition = await collateralPool.getUserPosition.call(marketContract.address, accountTaker, 0);
assert.equal(takerLastPosition[0].toNumber(), exitOrderPrice, 'Taker should be long 2 from exitOrderPrice');
assert.equal(takerLastPosition[1].toNumber(), 2, 'Taker should be long 2 from exitOrderPrice');
});
});

0 comments on commit e803713

Please sign in to comment.