Skip to content

Commit

Permalink
feat: calculating tvl
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasbn committed Jul 27, 2021
1 parent 843cdf0 commit 17ddc62
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
25 changes: 25 additions & 0 deletions subgraph/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
DtokenBalance,
SLA,
SLI,
TVL,
User,
Withdrawal,
} from './generated/schema';
Expand Down Expand Up @@ -102,6 +103,14 @@ export function handleStake(event: Stake): void {
}
user.deposits = user.deposits.concat([deposit.id]);
user.save();

let tvl = TVL.load('0');
if (!tvl) {
tvl = new TVL('0');
}
tvl.amount = tvl.amount.plus(event.params.amount);
tvl.deposits = tvl.deposits.concat([deposit.id]);
tvl.save();
}

export function handleProviderWithdraw(event: ProviderWithdraw): void {
Expand All @@ -121,6 +130,14 @@ export function handleProviderWithdraw(event: ProviderWithdraw): void {
}
user.withdrawals = user.withdrawals.concat([withdrawal.id]);
user.save();

let tvl = TVL.load('0');
if (!tvl) {
tvl = new TVL('0');
}
tvl.amount = tvl.amount.minus(event.params.amount);
tvl.withdrawals = tvl.withdrawals.concat([withdrawal.id]);
tvl.save();
}

export function handleUserWithdraw(event: ProviderWithdraw): void {
Expand All @@ -140,6 +157,14 @@ export function handleUserWithdraw(event: ProviderWithdraw): void {
}
user.withdrawals = user.withdrawals.concat([withdrawal.id]);
user.save();

let tvl = TVL.load('0');
if (!tvl) {
tvl = new TVL('0');
}
tvl.amount = tvl.amount.minus(event.params.amount);
tvl.withdrawals = tvl.withdrawals.concat([withdrawal.id]);
tvl.save();
}

export function handleSLORegistered(event: SLORegistered): void {
Expand Down
7 changes: 7 additions & 0 deletions subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ type Withdrawal @entity {
slaAddress: Bytes!
}

type TVL @entity {
id: ID!
amount: BigInt
deposits: [Deposit!]
withdrawals: [Withdrawal!]
}

type User @entity {
id: ID!
slas: [SLA!]
Expand Down

0 comments on commit 17ddc62

Please sign in to comment.