diff --git a/packages/lsd-staking/schema.graphql b/packages/lsd-staking/schema.graphql index e668840..0a7eedf 100644 --- a/packages/lsd-staking/schema.graphql +++ b/packages/lsd-staking/schema.graphql @@ -12,6 +12,16 @@ type Pool @entity { convertedExchangeRate: BigInt } +type Shares @entity { + id: ID! + + poolId: BigInt! + + who: String! + + stakedAmount: BigInt +} + type RewardRule @entity { id: ID! diff --git a/packages/lsd-staking/src/handlers/index.ts b/packages/lsd-staking/src/handlers/index.ts index 53755ac..6c44952 100644 --- a/packages/lsd-staking/src/handlers/index.ts +++ b/packages/lsd-staking/src/handlers/index.ts @@ -1,6 +1,6 @@ import { AcalaEvmEvent } from '@subql/acala-evm-processor'; import { NewPoolEvent, RewardsDeductionRateSetEvent, RewardRuleUpdateEvent, StakeEvent, UnstakeEvent, ClaimRewardEvent, LSTPoolConvertedEvent } from '../types/contracts/LsdAbi'; -import { Pool, RewardRule, ClaimedReward, RewardSupply, NewPoolRecord, RewardsDeductionRateSetRecord, RewardRuleUpdateRecord, StakeRecord, UnstakeRecord, ClaimRewardRecord, LSTPoolConvertedRecord } from '../types'; +import { Pool, Shares, RewardRule, ClaimedReward, RewardSupply, NewPoolRecord, RewardsDeductionRateSetRecord, RewardRuleUpdateRecord, StakeRecord, UnstakeRecord, ClaimRewardRecord, LSTPoolConvertedRecord } from '../types'; export async function handleNewPool( event: AcalaEvmEvent @@ -84,6 +84,15 @@ export async function handleStake( poolEntity.totalShare = poolEntity.totalShare + amount.toBigInt(); await poolEntity.save(); + const shareId = `${poolId}-${sender}`; + let sharesEntity = await Shares.get(shareId); + if (sharesEntity === undefined) { + sharesEntity = new Shares(shareId, poolId.toBigInt(), sender.toString()); + sharesEntity.stakedAmount = BigInt(0); + } + sharesEntity.stakedAmount = sharesEntity.stakedAmount + amount.toBigInt(); + await sharesEntity.save(); + const stakeRecordEntity = new StakeRecord(`${event.transactionHash}-${event.logIndex}`, event.blockTimestamp, event.from, sender.toString(), poolId.toBigInt(), amount.toBigInt()); await stakeRecordEntity.save(); } @@ -99,6 +108,15 @@ export async function handleUnstake( poolEntity.totalShare = poolEntity.totalShare - amount.toBigInt(); await poolEntity.save(); + const shareId = `${poolId}-${sender}`; + let sharesEntity = await Shares.get(shareId); + if (sharesEntity === undefined) { + sharesEntity = new Shares(shareId, poolId.toBigInt(), sender.toString()); + sharesEntity.stakedAmount = BigInt(0); + } + sharesEntity.stakedAmount = sharesEntity.stakedAmount - amount.toBigInt(); + await sharesEntity.save(); + const unstakeRecordEntity = new UnstakeRecord(`${event.transactionHash}-${event.logIndex}`, event.blockTimestamp, event.from, sender.toString(), poolId.toBigInt(), amount.toBigInt()); await unstakeRecordEntity.save(); } diff --git a/packages/lsd-staking/src/types/models/Shares.ts b/packages/lsd-staking/src/types/models/Shares.ts new file mode 100644 index 0000000..17b85de --- /dev/null +++ b/packages/lsd-staking/src/types/models/Shares.ts @@ -0,0 +1,80 @@ +// Auto-generated , DO NOT EDIT +import {Entity, FunctionPropertyNames} from "@subql/types"; +import assert from 'assert'; + + + + +export type SharesProps = Omit>| '_name'>; + +export class Shares implements Entity { + + constructor( + + id: string, + + poolId: bigint, + + who: string, + + + ) { + + this.id = id; + + this.poolId = poolId; + + this.who = who; + + } + + + public id: string; + + public poolId: bigint; + + public who: string; + + public stakedAmount?: bigint; + + + get _name(): string { + return 'Shares'; + } + + async save(): Promise{ + let id = this.id; + assert(id !== null, "Cannot save Shares entity without an ID"); + await store.set('Shares', id.toString(), this); + } + static async remove(id:string): Promise{ + assert(id !== null, "Cannot remove Shares entity without an ID"); + await store.remove('Shares', id.toString()); + } + + static async get(id:string): Promise{ + assert((id !== null && id !== undefined), "Cannot get Shares entity without an ID"); + const record = await store.get('Shares', id.toString()); + if (record){ + return this.create(record as SharesProps); + }else{ + return; + } + } + + + + static create(record: SharesProps): Shares { + assert(typeof record.id === 'string', "id must be provided"); + let entity = new this( + + record.id, + + record.poolId, + + record.who, + ); + Object.assign(entity,record); + return entity; + } +} diff --git a/packages/lsd-staking/src/types/models/index.ts b/packages/lsd-staking/src/types/models/index.ts index 90b35f3..e721df1 100644 --- a/packages/lsd-staking/src/types/models/index.ts +++ b/packages/lsd-staking/src/types/models/index.ts @@ -4,6 +4,8 @@ export {Pool} from "./Pool" +export {Shares} from "./Shares" + export {RewardRule} from "./RewardRule" export {ClaimedReward} from "./ClaimedReward"