Skip to content

Commit

Permalink
record share amount for stakers (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangjj9219 authored Oct 29, 2023
1 parent fef18f1 commit 369aa77
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
10 changes: 10 additions & 0 deletions packages/lsd-staking/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand Down
20 changes: 19 additions & 1 deletion packages/lsd-staking/src/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -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<NewPoolEvent['args']>
Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down
80 changes: 80 additions & 0 deletions packages/lsd-staking/src/types/models/Shares.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Auto-generated , DO NOT EDIT
import {Entity, FunctionPropertyNames} from "@subql/types";
import assert from 'assert';




export type SharesProps = Omit<Shares, NonNullable<FunctionPropertyNames<Shares>>| '_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<void>{
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<void>{
assert(id !== null, "Cannot remove Shares entity without an ID");
await store.remove('Shares', id.toString());
}

static async get(id:string): Promise<Shares | undefined>{
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;
}
}
2 changes: 2 additions & 0 deletions packages/lsd-staking/src/types/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

export {Pool} from "./Pool"

export {Shares} from "./Shares"

export {RewardRule} from "./RewardRule"

export {ClaimedReward} from "./ClaimedReward"
Expand Down

0 comments on commit 369aa77

Please sign in to comment.