Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fill price js impl #1698

Merged
merged 9 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { ethers } from 'ethers';
import assertBn from '@synthetixio/core-utils/src/utils/assertions/assert-bignumber';
import { bn, bootstrapMarkets } from '../bootstrap';
import { OpenPositionData, openPosition } from '../helpers';
import { formatEther } from 'ethers/lib/utils';
import { wei } from '@synthetixio/wei';
import { calculateFillPrice } from '../helpers/fillPrice';
import { snapshotCheckpoint } from '@synthetixio/core-utils/utils/mocha/snapshot';

describe('PerpsMarketModule', () => {
Expand Down Expand Up @@ -96,29 +97,29 @@ describe('PerpsMarketModule', () => {
const tests = [
{
marketSkew: 0,
sizeAndExpectedPrice: [
{ size: 1, price: bn(1010), expectedPrice: bn(1010.0505) },
{ size: -1, price: bn(1010), expectedPrice: bn(1009.9495) },
sizeAndPrice: [
{ size: 1, price: 1010 },
{ size: -1, price: 1010 },
],
},
{
marketSkew: 10,
sizeAndExpectedPrice: [
{ size: 1, price: bn(1010), expectedPrice: bn(1011.0605) },
{ size: -1, price: bn(1010), expectedPrice: bn(1010.9595) },
{ size: -11, price: bn(1010), expectedPrice: bn(1010.4545) },
sizeAndPrice: [
{ size: 1, price: 1010 },
{ size: -1, price: 1010 },
{ size: -11, price: 1010 },
],
},
{
marketSkew: -10,
sizeAndExpectedPrice: [
{ size: 1, price: bn(1010), expectedPrice: bn(1009.0405) },
{ size: -1, price: bn(1010), expectedPrice: bn(1008.9395) },
{ size: 11, price: bn(1010), expectedPrice: bn(1009.5455) },
sizeAndPrice: [
{ size: 1, price: 1010 },
{ size: -1, price: 1010 },
{ size: 11, price: 1010 },
],
},
];
tests.forEach(({ marketSkew, sizeAndExpectedPrice }) => {
tests.forEach(({ marketSkew, sizeAndPrice }) => {
describe(`marketSkew ${marketSkew}`, () => {
const restoreMarketSkew = snapshotCheckpoint(provider);
before('create market skew', async () => {
Expand All @@ -129,10 +130,17 @@ describe('PerpsMarketModule', () => {
price: fixture.marketTokenPrice,
});
});
sizeAndExpectedPrice.forEach(({ size, price, expectedPrice }) => {
it(`fillPrice for size ${size} and price ${formatEther(price)}`, async () => {
const fillPrice = await systems().PerpsMarket.fillPrice(marketId, bn(size), price);
assertBn.equal(fillPrice, expectedPrice);
sizeAndPrice.forEach(({ size, price }) => {
it(`fillPrice for size ${size} and price ${price}`, async () => {
const fillPrice = await systems().PerpsMarket.fillPrice(marketId, bn(size), bn(price));
const expectedFillPrice = calculateFillPrice(
wei(marketSkew),
wei(fixture.skewScale),
wei(size),
wei(price)
).toBN();

assertBn.equal(fillPrice, expectedFillPrice);
});
});
after('restore market skew', restoreMarketSkew);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import assertBn from '@synthetixio/core-utils/utils/assertions/assert-bignumber'
import assertEvent from '@synthetixio/core-utils/utils/assertions/assert-event';
import assertRevert from '@synthetixio/core-utils/utils/assertions/assert-revert';
import { getTxTime } from '@synthetixio/core-utils/src/utils/hardhat/rpc';
import { calculateFillPrice } from '../helpers/fillPrice';
import { wei } from '@synthetixio/wei';
import { calcCurrentFundingVelocity } from '../helpers/funding-calcs';

Expand Down Expand Up @@ -382,10 +383,8 @@ describe('Settle Offchain Async Order test', () => {
});

it('emits event settle event', async () => {
// TODO Calculate the correct fill price instead of hardcoding

const accountId = 2;
const fillPrice = bn(1000.005);
const fillPrice = calculateFillPrice(wei(0), wei(100_000), wei(1), wei(1000)).toBN();
const pnl = 0;
const newPositionSize = bn(1);
const totalFees = DEFAULT_SETTLEMENT_STRATEGY.settlementReward;
Expand Down
19 changes: 19 additions & 0 deletions markets/perps-market/test/integration/helpers/fillPrice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Wei from '@synthetixio/wei';

// Calculates PD
const calculatePD = (skew: Wei, skewScale: Wei) => skew.div(skewScale);
// Calculates the price with pd applied
const calculateAdjustedPrice = (price: Wei, pd: Wei) => price.add(price.mul(pd));

export function calculateFillPrice(skew: Wei, skewScale: Wei, size: Wei, price: Wei) {
if (skewScale.eq(0)) {
return price;
}
const pdBefore = calculatePD(skew, skewScale);
const pdAfter = calculatePD(skew.add(size), skewScale);

const priceBefore = calculateAdjustedPrice(price, pdBefore);
const priceAfter = calculateAdjustedPrice(price, pdAfter);

return priceBefore.add(priceAfter).div(2);
}
Loading