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

feat: add transaction stats entity [SF-1135] #96

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

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

# TODO: add for APR
# TOOD: add for vol
Comment on lines +165 to +166
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please correct the spelling of "TODO" and provide more actionable details.

- # TOOD: add for vol
+ # TODO: Add functionality for calculating volume.

Also applies to: 166-166

Committable suggestion was skipped due to low confidence.

type TransactionStats @entity {
id: ID!
currency: Bytes!
maturity: BigInt!
priceChange: BigInt!
percentageChange: BigDecimal!
container: TransactionStatsContainer!
}
62 changes: 62 additions & 0 deletions src/helper/initializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
Protocol,
Transaction,
TransactionCandleStick,
TransactionStats,
TransactionStatsContainer,
Transfer,
User,
} from '../../generated/schema';
Expand Down Expand Up @@ -276,6 +278,66 @@ export const initTransfer = (
user.save();
};

export function updateTransactionStats(
currency: Bytes,
maturity: BigInt,
currentTimestamp: BigInt
): void {
const currentCandleStickId = getTransactionCandleStickEntityId(
currency,
maturity,
BigInt.fromI32(86400), // 1 day interval
currentTimestamp.div(BigInt.fromI32(86400))
);
const currentCandleStick =
TransactionCandleStick.load(currentCandleStickId);

if (currentCandleStick) {
const previousTimestamp = currentTimestamp.minus(BigInt.fromI32(86400));
const previousCandleStickId = getTransactionCandleStickEntityId(
currency,
maturity,
BigInt.fromI32(86400), // 1 day interval
previousTimestamp.div(BigInt.fromI32(86400))
);
const previousCandleStick = TransactionCandleStick.load(
previousCandleStickId
);

if (previousCandleStick) {
const currentPrice = currentCandleStick.close;
const previousPrice = previousCandleStick.close;

const priceChange = currentPrice.minus(previousPrice);
const percentageChange = priceChange
.times(BigInt.fromI32(10000))
.div(previousPrice);

const containerId = 'CONTAINER';
let container = TransactionStatsContainer.load(containerId);

if (!container) {
container = new TransactionStatsContainer(containerId);
}

const statsId = currency.toHexString() + '-' + maturity.toString();
let stats = TransactionStats.load(statsId);

if (!stats) {
stats = new TransactionStats(statsId);
stats.currency = currency;
stats.maturity = maturity;
stats.container = container.id;
stats.save();
}

stats.priceChange = priceChange;
stats.percentageChange = percentageChange.toBigDecimal();
stats.save();
}
}
}
Comment on lines +281 to +339
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a dynamic approach for generating containerId to handle multiple containers effectively.

- const containerId = 'CONTAINER';
+ const containerId = generateContainerId(currency, maturity); // Implement this function to generate unique IDs based on currency and maturity.
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export function updateTransactionStats(
currency: Bytes,
maturity: BigInt,
currentTimestamp: BigInt
): void {
const currentCandleStickId = getTransactionCandleStickEntityId(
currency,
maturity,
BigInt.fromI32(86400), // 1 day interval
currentTimestamp.div(BigInt.fromI32(86400))
);
const currentCandleStick =
TransactionCandleStick.load(currentCandleStickId);
if (currentCandleStick) {
const previousTimestamp = currentTimestamp.minus(BigInt.fromI32(86400));
const previousCandleStickId = getTransactionCandleStickEntityId(
currency,
maturity,
BigInt.fromI32(86400), // 1 day interval
previousTimestamp.div(BigInt.fromI32(86400))
);
const previousCandleStick = TransactionCandleStick.load(
previousCandleStickId
);
if (previousCandleStick) {
const currentPrice = currentCandleStick.close;
const previousPrice = previousCandleStick.close;
const priceChange = currentPrice.minus(previousPrice);
const percentageChange = priceChange
.times(BigInt.fromI32(10000))
.div(previousPrice);
const containerId = 'CONTAINER';
let container = TransactionStatsContainer.load(containerId);
if (!container) {
container = new TransactionStatsContainer(containerId);
}
const statsId = currency.toHexString() + '-' + maturity.toString();
let stats = TransactionStats.load(statsId);
if (!stats) {
stats = new TransactionStats(statsId);
stats.currency = currency;
stats.maturity = maturity;
stats.container = container.id;
stats.save();
}
stats.priceChange = priceChange;
stats.percentageChange = percentageChange.toBigDecimal();
stats.save();
}
}
}
export function updateTransactionStats(
currency: Bytes,
maturity: BigInt,
currentTimestamp: BigInt
): void {
const currentCandleStickId = getTransactionCandleStickEntityId(
currency,
maturity,
BigInt.fromI32(86400), // 1 day interval
currentTimestamp.div(BigInt.fromI32(86400))
);
const currentCandleStick =
TransactionCandleStick.load(currentCandleStickId);
if (currentCandleStick) {
const previousTimestamp = currentTimestamp.minus(BigInt.fromI32(86400));
const previousCandleStickId = getTransactionCandleStickEntityId(
currency,
maturity,
BigInt.fromI32(86400), // 1 day interval
previousTimestamp.div(BigInt.fromI32(86400))
);
const previousCandleStick = TransactionCandleStick.load(
previousCandleStickId
);
if (previousCandleStick) {
const currentPrice = currentCandleStick.close;
const previousPrice = previousCandleStick.close;
const priceChange = currentPrice.minus(previousPrice);
const percentageChange = priceChange
.times(BigInt.fromI32(10000))
.div(previousPrice);
const containerId = generateContainerId(currency, maturity); // Implement this function to generate unique IDs based on currency and maturity.
let container = TransactionStatsContainer.load(containerId);
if (!container) {
container = new TransactionStatsContainer(containerId);
}
const statsId = currency.toHexString() + '-' + maturity.toString();
let stats = TransactionStats.load(statsId);
if (!stats) {
stats = new TransactionStats(statsId);
stats.currency = currency;
stats.maturity = maturity;
stats.container = container.id;
stats.save();
}
stats.priceChange = priceChange;
stats.percentageChange = percentageChange.toBigDecimal();
stats.save();
}
}
}


export const initOrUpdateTransactionCandleStick = (
currency: Bytes,
maturity: BigInt,
Expand Down
7 changes: 7 additions & 0 deletions src/mappings/lending-market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
initOrUpdateTransactionCandleStick,
initOrder,
initTransaction,
updateTransactionStats,
} from '../helper/initializer';
import { getOrderEntityId } from '../utils/id-generation';

Expand Down Expand Up @@ -213,6 +214,12 @@ export function handlePositionUnwound(event: PositionUnwound): void {
BigInt.fromI32(intervals[i])
);
}
// TODO: check if this needs to be added in other handlers
updateTransactionStats(
event.params.ccy,
event.params.maturity,
event.block.timestamp
);
}
}

Expand Down
Loading