86 lines
No EOL
3.6 KiB
TypeScript
86 lines
No EOL
3.6 KiB
TypeScript
import {
|
|
PositionCreated as PositionCreatedEvent,
|
|
PositionRemoved as PositionRemovedEvent,
|
|
PositionTaxPaid as PositionTaxPaidEvent,
|
|
PositionShrunk as PositionShrunkEvent,
|
|
PositionRateHiked as PositionRateHikedEvent,
|
|
} from "../generated/Stake/Stake";
|
|
import { Harb } from "../generated/Harb/Harb";
|
|
import { Stake } from "../generated/Stake/Stake";
|
|
import { getSubgraphConfig, SubgraphConfig } from './chains';
|
|
import { BigDecimal, BigInt, Bytes } from "@graphprotocol/graph-ts";
|
|
import { Position, Stats } from "../generated/schema";
|
|
|
|
let decimals: BigDecimal = BigDecimal.fromString("1000000000000000000");
|
|
let totalSupply: BigDecimal = BigDecimal.fromString("10000000000000000000000000");
|
|
let taxRates: Array<string> = ["0.01", "0.03", "0.05", "0.08", "0.12", "0.18", "0.24", "0.30", "0.40", "0.50", "0.60", "0.80", "1", "1.3", "1.8", "2.5", "3.2", "4.2", "5.4", "7", "9.2", "12", "16", "20", "26", "34", "44", "57", "75", "97"];
|
|
|
|
export function handlePositionCreated(event: PositionCreatedEvent): void {
|
|
let position = new Position(Bytes.fromI32(event.params.positionId.toI32()));
|
|
position.owner = event.params.owner;
|
|
position.share = event.params.share.toBigDecimal().div(totalSupply);
|
|
position.creationTime = event.block.timestamp.toI32();
|
|
position.lastTaxTime = event.block.timestamp.toI32();
|
|
position.taxRate = BigDecimal.fromString(taxRates[event.params.taxRate.toI32()]);
|
|
position.harbDeposit = event.params.harbDeposit;
|
|
position.status = "Active";
|
|
position.snatched = 0;
|
|
position.payout = BigInt.fromString("0");
|
|
position.taxPaid = BigInt.fromString("0");
|
|
let harb = Harb.bind(getSubgraphConfig().harbergAddress);
|
|
position.totalSupplyInit = harb.totalSupply();
|
|
position.save();
|
|
let stake = Stake.bind(event.address);
|
|
let stats = Stats.load(Bytes.fromHexString("0x01") as Bytes);
|
|
if (stats != null) {
|
|
stats.outstandingStake = stake.outstandingStake();
|
|
stats.save();
|
|
}
|
|
}
|
|
|
|
export function handlePositionRemoved(event: PositionRemovedEvent): void {
|
|
let position = Position.load(Bytes.fromI32(event.params.positionId.toI32()));
|
|
if (position != null) {
|
|
position.status = "Closed";
|
|
let harb = Harb.bind(getSubgraphConfig().harbergAddress);
|
|
position.totalSupplyEnd = harb.totalSupply();
|
|
position.payout = position.payout.plus(event.params.harbPayout);
|
|
position.save();
|
|
}
|
|
let stake = Stake.bind(event.address);
|
|
let stats = Stats.load(Bytes.fromHexString("0x01") as Bytes);
|
|
if (stats != null) {
|
|
stats.outstandingStake = stake.outstandingStake();
|
|
stats.save();
|
|
}
|
|
}
|
|
|
|
export function handlePositionShrunk(event: PositionShrunkEvent): void {
|
|
let position = Position.load(Bytes.fromI32(event.params.positionId.toI32()));
|
|
if (position != null) {
|
|
position.share = event.params.newShares.toBigDecimal().div(totalSupply);
|
|
position.harbDeposit = position.harbDeposit.minus(event.params.harbPayout);
|
|
position.snatched = position.snatched++;
|
|
|
|
position.payout = position.payout.plus(event.params.harbPayout);
|
|
position.save();
|
|
}
|
|
}
|
|
|
|
export function handleTaxPaid(event: PositionTaxPaidEvent): void {
|
|
let position = Position.load(Bytes.fromI32(event.params.positionId.toI32()));
|
|
if (position != null) {
|
|
position.taxPaid = position.taxPaid.plus(event.params.taxPaid);
|
|
position.taxRate = BigDecimal.fromString(taxRates[event.params.taxRate.toI32()]);
|
|
position.lastTaxTime = event.block.timestamp.toI32();
|
|
position.save();
|
|
}
|
|
}
|
|
|
|
export function handlePositionRateHiked(event: PositionRateHikedEvent): void {
|
|
let position = Position.load(Bytes.fromI32(event.params.positionId.toI32()));
|
|
if (position != null) {
|
|
position.taxRate = BigDecimal.fromString(taxRates[event.params.newTaxRate.toI32()]);
|
|
position.save();
|
|
}
|
|
} |