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

Sf 1135 #98

Closed
wants to merge 8 commits into from
Closed
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
17 changes: 17 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,20 @@ type TransactionCandleStick @entity {
volumeInFV: BigInt!
lendingMarket: LendingMarket!
}

type DailyTransactionStatsContainer @entity {
id: ID!
stats: [DailyTransactionStats!]! @derivedFrom(field: "container")
}

type DailyTransactionStats @entity {
id: ID!
currency: Bytes!
maturity: BigInt!
timestamp: BigInt!
close: BigInt!
open: BigInt!
container: DailyTransactionStatsContainer!
change: BigInt!
percentageChange: BigInt!
}
56 changes: 56 additions & 0 deletions src/helper/initializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
log,
} from '@graphprotocol/graph-ts';
import {
DailyTransactionStats,
DailyTransactionStatsContainer,
DailyVolume,
Deposit,
LendingMarket,
Expand All @@ -18,6 +20,7 @@ import {
User,
} from '../../generated/schema';
import {
getDailyTransactionEntityId,
getDailyVolumeEntityId,
getTransactionCandleStickEntityId,
} from '../utils/id-generation';
Expand Down Expand Up @@ -276,6 +279,59 @@ export const initTransfer = (
user.save();
};

export function initOrUpdateDailyTransactionStats(
currency: Bytes,
maturity: BigInt,
currentTimestamp: BigInt
): void {
const currentCandleStickId = getTransactionCandleStickEntityId(
currency,
maturity,
BigInt.fromI32(86400), // 24 hours in seconds
currentTimestamp.div(BigInt.fromI32(86400))
);
const currentCandleStick =
TransactionCandleStick.load(currentCandleStickId);

const containerId = 'TRANSACTION_STATS_CONTAINER';
let container = DailyTransactionStatsContainer.load(containerId);

if (!container) {
container = new DailyTransactionStatsContainer(containerId);
container.save();
}

const statsId = getDailyTransactionEntityId(currency, maturity);
let stats = DailyTransactionStats.load(statsId);

if (!stats) {
stats = new DailyTransactionStats(statsId);
}

stats.currency = currency;
stats.maturity = maturity;
stats.container = container.id;
stats.timestamp = currentTimestamp;

if (currentCandleStick) {
stats.open = currentCandleStick.open;
stats.close = currentCandleStick.close;
stats.change = currentCandleStick.close.minus(currentCandleStick.open);
stats.percentageChange = currentCandleStick.open.isZero()
? BigInt.fromI32(0)
: stats.change
.times(BigInt.fromI32(10000))
.div(currentCandleStick.open);
} else {
stats.open = BigInt.fromI32(0);
stats.close = BigInt.fromI32(0);
stats.change = BigInt.fromI32(0);
stats.percentageChange = BigInt.fromI32(0);
}

stats.save();
}

export const initOrUpdateTransactionCandleStick = (
currency: Bytes,
maturity: BigInt,
Expand Down
18 changes: 18 additions & 0 deletions src/mappings/lending-market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ItayoseExecuted } from '../../generated/templates/OrderBookLogic/OrderB
import {
getOrInitDailyVolume,
getOrInitLendingMarket,
initOrUpdateDailyTransactionStats,
initOrUpdateTransactionCandleStick,
initOrder,
initTransaction,
Expand Down Expand Up @@ -112,6 +113,12 @@ export function handleOrderExecuted(event: OrderExecuted): void {
BigInt.fromI32(intervals[i])
);
}

initOrUpdateDailyTransactionStats(
event.params.ccy,
event.params.maturity,
event.block.timestamp
);
}
}

Expand Down Expand Up @@ -213,6 +220,12 @@ export function handlePositionUnwound(event: PositionUnwound): void {
BigInt.fromI32(intervals[i])
);
}

initOrUpdateDailyTransactionStats(
event.params.ccy,
event.params.maturity,
event.block.timestamp
);
}
}

Expand Down Expand Up @@ -321,6 +334,11 @@ export function handleItayoseExecuted(event: ItayoseExecuted): void {
BigInt.fromI32(intervals[i])
);
}
initOrUpdateDailyTransactionStats(
event.params.ccy,
event.params.maturity,
event.block.timestamp
);
}

function addToTransactionVolume(
Expand Down
7 changes: 7 additions & 0 deletions src/utils/id-generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@ export function getTransactionCandleStickEntityId(
): string {
return `${ccy.toString()}-${maturity.toString()}-${interval.toString()}-${epochTime.toString()}`;
}

export function getDailyTransactionEntityId(
ccy: Bytes,
maturity: BigInt
): string {
return `${ccy.toHexString()}-${maturity.toString()}`;
}
193 changes: 193 additions & 0 deletions test/lending-market.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
handlePreOrderExecuted,
} from '../src/mappings/lending-market';
import {
getDailyTransactionEntityId,
getDailyVolumeEntityId,
getOrderEntityId,
getTransactionCandleStickEntityId,
Expand Down Expand Up @@ -1911,6 +1912,7 @@ describe('Transaction Candle Stick', () => {
'interval',
interval.toString()
);

assert.fieldEquals(
'TransactionCandleStick',
id,
Expand Down Expand Up @@ -2479,3 +2481,194 @@ describe('Transaction Candle Stick', () => {
}
});
});

describe('Daily Transaction Stats', () => {
beforeEach(() => {
clearStore();
createLendingMarket(ccy, maturity);
});

test('order executed creates and updates daily stats correctly', () => {
const placedOrderId = BigInt.fromI32(0);
const filledAmount = BigInt.fromI32(81);
const filledUnitPrice = unitPrice;
const filledAmountInFV = BigInt.fromI32(90);
const totalAmount = filledAmount.plus(amount);
const feeInFV = BigInt.fromI32(1);

const event = createOrderExecutedEvent(
ALICE,
borrow,
ccy,
maturity,
totalAmount,
BigInt.fromI32(0),
filledAmount,
filledUnitPrice,
filledAmountInFV,
feeInFV,
placedOrderId,
BigInt.fromI32(0),
BigInt.fromI32(0),
true
);
handleOrderExecuted(event);

const id = getDailyTransactionEntityId(ccy, maturity);

assert.fieldEquals(
'DailyTransactionStats',
id,
'currency',
ccy.toHexString()
);
assert.fieldEquals(
'DailyTransactionStats',
id,
'maturity',
maturity.toString()
);
assert.fieldEquals(
'DailyTransactionStats',
id,
'open',
filledUnitPrice.toString()
);
assert.fieldEquals(
'DailyTransactionStats',
id,
'close',
filledUnitPrice.toString()
);
assert.fieldEquals(
'DailyTransactionStats',
id,
'change',
BigInt.fromI32(0).toString()
);
assert.fieldEquals(
'DailyTransactionStats',
id,
'percentageChange',
BigInt.fromI32(0).toString()
);
});

test('position unwound should update the daily stats', () => {
const futureValue = BigInt.fromI32(250);
const filledAmount = BigInt.fromI32(225);
const filledUnitPrice = unitPrice;
const filledAmountInFV = BigInt.fromI32(250);

const event = createPositionUnwoundEvent(
BOB,
lend,
ccy,
maturity,
futureValue,
filledAmount,
filledUnitPrice,
filledAmountInFV,
BigInt.fromI32(0),
false,
timestamp
);
handlePositionUnwound(event);

const id = getDailyTransactionEntityId(ccy, maturity);

assert.fieldEquals(
'DailyTransactionStats',
id,
'currency',
ccy.toHexString()
);
assert.fieldEquals(
'DailyTransactionStats',
id,
'maturity',
maturity.toString()
);
assert.fieldEquals(
'DailyTransactionStats',
id,
'open',
filledUnitPrice.toString()
);
assert.fieldEquals(
'DailyTransactionStats',
id,
'close',
filledUnitPrice.toString()
);
assert.fieldEquals(
'DailyTransactionStats',
id,
'change',
BigInt.fromI32(0).toString()
);
assert.fieldEquals(
'DailyTransactionStats',
id,
'percentageChange',
BigInt.fromI32(0).toString()
);
});

test('itayose executed event should update the daily stats', () => {
const openingUnitPrice = BigInt.fromI32(8050);
const lastLendUnitPrice = BigInt.fromI32(8100);
const lastBorrowUnitPrice = BigInt.fromI32(8000);
const offsetAmount = BigInt.fromI32(300);
const offsetAmountInFV = BigInt.fromI32(372);
const itayoseExecutedEvent = createItayoseExecutedEvent(
ccy,
maturity,
openingUnitPrice,
lastLendUnitPrice,
lastBorrowUnitPrice,
offsetAmount,
timestamp
);
handleItayoseExecuted(itayoseExecutedEvent);

const id = getDailyTransactionEntityId(ccy, maturity);

assert.fieldEquals(
'DailyTransactionStats',
id,
'currency',
ccy.toHexString()
);
assert.fieldEquals(
'DailyTransactionStats',
id,
'maturity',
maturity.toString()
);
assert.fieldEquals(
'DailyTransactionStats',
id,
'open',
openingUnitPrice.toString()
);
assert.fieldEquals(
'DailyTransactionStats',
id,
'close',
openingUnitPrice.toString()
);
assert.fieldEquals(
'DailyTransactionStats',
id,
'change',
BigInt.fromI32(0).toString()
);
assert.fieldEquals(
'DailyTransactionStats',
id,
'percentageChange',
BigInt.fromI32(0).toString()
);
});
});
Loading