Skip to content

Commit

Permalink
Merge pull request #815 from autonomys/feat/stakingSquidV15
Browse files Browse the repository at this point in the history
Staking squid v15
  • Loading branch information
marc-aurele-besner authored Aug 20, 2024
2 parents e2cf8bc + 74ec636 commit 429cec5
Show file tree
Hide file tree
Showing 27 changed files with 1,913 additions and 169 deletions.

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion indexers/staking-squid/gemini3h.jsonl

Large diffs are not rendered by default.

71 changes: 52 additions & 19 deletions indexers/staking-squid/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type Domain @entity {
completedEpoch: Int! @index
lastDomainBlockNumber: Int! @index
totalDeposits: BigInt!
totalEstimatedWithdrawals: BigInt!
totalWithdrawals: BigInt!
totalTaxCollected: BigInt!
totalRewardsCollected: BigInt!
totalTransfersIn: BigInt!
Expand All @@ -30,6 +32,8 @@ type Domain @entity {
totalBurnedBalance: BigInt!
currentTotalStake: BigInt!
currentStorageFeeDeposit: BigInt!
accumulatedEpochStake: BigInt!
accumulatedEpochStorageFeeDeposit: BigInt!
bundleCount: Int!
lastBundleAt: Int!
createdAt: Int! @index
Expand All @@ -39,6 +43,8 @@ type Domain @entity {
type Account @entity {
id: ID! @index
totalDeposits: BigInt!
totalEstimatedWithdrawals: BigInt!
totalWithdrawals: BigInt!
totalTaxCollected: BigInt!
createdAt: Int! @index
updatedAt: Int! @index
Expand All @@ -48,10 +54,17 @@ enum OperatorStatus {
PENDING
REGISTERED
DEREGISTERED
READY_TO_UNLOCK
SLASHED
}

enum OperatorPendingAction {
NO_ACTION_REQUIRED
PENDING_REGISTRATION
WAITING_TO_UNLOCK_NOMINATOR
READY_FOR_UNLOCK_NOMINATOR
READY_FOR_UNLOCK_FUNDS
}

type Operator @entity {
id: ID! @index
sortId: Int! @index
Expand All @@ -75,8 +88,11 @@ type Operator @entity {
currentStorageFeeDeposit: BigInt!
currentEpochRewards: BigInt!
currentTotalShares: BigInt!
currentSharePrice: BigInt!
rawStatus: String!
totalDeposits: BigInt!
totalEstimatedWithdrawals: BigInt!
totalWithdrawals: BigInt!
totalTaxCollected: BigInt!
totalRewardsCollected: BigInt!
totalTransfersIn: BigInt!
Expand All @@ -91,9 +107,12 @@ type Operator @entity {
totalConsensusStorageFee: BigInt!
totalDomainExecutionFee: BigInt!
totalBurnedBalance: BigInt!
accumulatedEpochShares: BigInt!
accumulatedEpochStorageFeeDeposit: BigInt!
activeEpochCount: Int!
bundleCount: Int!
status: OperatorStatus! @index
pendingAction: OperatorPendingAction! @index
lastBundleAt: Int!
createdAt: Int! @index
updatedAt: Int! @index
Expand Down Expand Up @@ -146,10 +165,16 @@ type BundleAuthor @entity {

enum NominatorStatus {
PENDING
STAKING
DEREGISTERED
READY_TO_UNLOCK
STAKED
SLASHED
WITHDRAW
}

enum NominatorPendingAction {
NO_ACTION_REQUIRED
PENDING_EPOCH_CHANGE
PENDING_DEREGISTRATION_LOCK
READY_TO_UNLOCK_ALL_FUNDS
}

type Nominator @entity {
Expand All @@ -168,7 +193,15 @@ type Nominator @entity {
pendingShares: BigInt!
pendingStorageFeeRefund: BigInt!
totalDeposits: BigInt!
totalEstimatedWithdrawals: BigInt!
totalWithdrawals: BigInt!
totalDepositsCount: Int!
totalWithdrawalsCount: Int!
accumulatedEpochShares: BigInt!
accumulatedEpochStorageFeeDeposit: BigInt!
activeEpochCount: Int!
status: NominatorStatus! @index
pendingAction: NominatorPendingAction! @index
createdAt: Int! @index
updatedAt: Int! @index
}
Expand Down Expand Up @@ -197,9 +230,11 @@ type Deposit @entity {
}

enum WithdrawalStatus {
PENDING
PENDING_LOCK
PENDING_OPERATOR
READY
WITHDRAW
SLASHED
}

type Withdrawal @entity {
Expand All @@ -209,6 +244,9 @@ type Withdrawal @entity {
operatorId: String! @index
nominatorId: String! @index
shares: BigInt!
estimatedAmount: BigInt!
unlockedAmount: BigInt!
unlockedStorageFee: BigInt!
status: WithdrawalStatus! @index
timestamp: DateTime! @index
withdrawExtrinsicHash: String! @index
Expand All @@ -221,17 +259,6 @@ type Withdrawal @entity {
updatedAt: Int! @index
}

type OperatorUnlockedFunds @entity {
id: ID! @index
blockNumber: Int! @index
operator: Operator!
nominator: Nominator!
nominatorAccount: String!
timestamp: DateTime! @index
extrinsicHash: String! @index
amount: BigInt!
}

type RewardEvent @entity {
id: ID! @index
domainId: String! @index
Expand All @@ -246,7 +273,8 @@ type Stats @entity {
id: ID! @index
blockNumber: Int! @index
totalStaked: BigInt!
totalFees: BigInt!
totalTaxCollected: BigInt!
totalRewardsCollected: BigInt!
totalDeposits: BigInt!
totalWithdrawals: BigInt!
allTimeHighStaked: BigInt!
Expand All @@ -265,7 +293,8 @@ type StatsPerDomain @entity {
domainId: String! @index
blockNumber: Int! @index
totalStaked: BigInt!
totalFees: BigInt!
totalTaxCollected: BigInt!
totalRewardsCollected: BigInt!
totalDeposits: BigInt!
totalWithdrawals: BigInt!
allTimeHighStaked: BigInt!
Expand All @@ -284,10 +313,14 @@ type StatsPerOperator @entity {
operatorId: String! @index
blockNumber: Int! @index
totalStaked: BigInt!
totalFees: BigInt!
totalShares: BigInt!
totalTaxCollected: BigInt!
totalRewardsCollected: BigInt!
totalDeposits: BigInt!
totalWithdrawals: BigInt!
currentSharePrice: BigInt!
allTimeHighStaked: BigInt!
allTimeHighSharePrice: BigInt!
nominatorsCount: Int!
depositsCount: Int!
withdrawalsCount: Int!
Expand Down
2 changes: 1 addition & 1 deletion indexers/staking-squid/squid.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
manifestVersion: subsquid.io/v0.1
name: staking-squid
version: 14
version: 15
description: Autonomys - Astral - Staking Indexer
build:
deploy:
Expand Down
49 changes: 40 additions & 9 deletions indexers/staking-squid/src/events/epoch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import type { ApiPromise } from "@autonomys/auto-utils";
import {
DepositStatus,
DomainRuntime,
NominatorPendingAction,
NominatorStatus,
OperatorPendingAction,
OperatorStatus,
WithdrawalStatus,
} from "../model";
Expand All @@ -21,7 +23,7 @@ import {
getOrCreateNominator,
getOrCreateOperator,
} from "../storage";
import { getBlockNumber } from "../utils";
import { getBlockNumber, SHARES_CALCULATION_MULTIPLIER } from "../utils";
import { Cache } from "../utils/cache";

export async function processEpochTransitionEvent(
Expand Down Expand Up @@ -114,6 +116,17 @@ export async function processEpochTransitionEvent(
op.rawStatus = rawStatus;
op.updatedAt = currentBlockNumber;

if (op.currentTotalShares > BigInt(0)) {
op.currentSharePrice =
(op.currentTotalStake * SHARES_CALCULATION_MULTIPLIER) /
op.currentTotalShares;
} else {
op.currentSharePrice = BigInt(0);
}

op.accumulatedEpochShares += op.currentTotalShares;
op.accumulatedEpochStorageFeeDeposit += op.currentStorageFeeDeposit;

cache.operators.set(op.id, op);

try {
Expand All @@ -130,28 +143,33 @@ export async function processEpochTransitionEvent(
_status.deregistered.unlockAtConfirmedDomainBlockNumber <=
domain.lastDomainBlockNumber
) {
op.status = OperatorStatus.READY_TO_UNLOCK;
op.status = OperatorStatus.DEREGISTERED;
op.pendingAction = OperatorPendingAction.READY_FOR_UNLOCK_NOMINATOR;
cache.operators.set(op.id, op);

Array.from(cache.nominators.values())
.filter(
(n) =>
n.status === NominatorStatus.STAKING && n.operatorId === op.id
(n.status === NominatorStatus.PENDING ||
n.status === NominatorStatus.STAKED) &&
n.operatorId === op.id
)
.forEach((n) => {
n.status = NominatorStatus.READY_TO_UNLOCK;
n.status = NominatorStatus.PENDING;
n.pendingAction =
NominatorPendingAction.PENDING_DEREGISTRATION_LOCK;
n.updatedAt = currentBlockNumber;
cache.nominators.set(n.id, n);
});

Array.from(cache.withdrawals.values())
.filter(
(w) =>
w.status === WithdrawalStatus.PENDING && w.domainId === domain.id
w.status === WithdrawalStatus.PENDING_LOCK &&
w.domainId === domain.id
)
.forEach((w) => {
w.status = WithdrawalStatus.READY;
w.readyAt = currentBlockNumber;
w.status = WithdrawalStatus.PENDING_OPERATOR;
w.updatedAt = currentBlockNumber;
cache.withdrawals.set(w.id, w);
});
Expand All @@ -176,6 +194,9 @@ export async function processEpochTransitionEvent(
domain.completedEpoch = completedEpoch;
domain.updatedAt = currentBlockNumber;

domain.accumulatedEpochStake += domain.currentTotalStake;
domain.accumulatedEpochStorageFeeDeposit += domain.currentStorageFeeDeposit;

cache.domains.set(domain.id, domain);

Array.from(cache.operators.values())
Expand All @@ -194,6 +215,7 @@ export async function processEpochTransitionEvent(
)
.forEach((o) => {
o.status = OperatorStatus.REGISTERED;
o.pendingAction = OperatorPendingAction.NO_ACTION_REQUIRED;
o.updatedAt = currentBlockNumber;
cache.operators.set(o.id, o);
});
Expand All @@ -203,7 +225,8 @@ export async function processEpochTransitionEvent(
(n) => n.status === NominatorStatus.PENDING && n.domainId === domain.id
)
.forEach((n) => {
n.status = NominatorStatus.STAKING;
n.status = NominatorStatus.STAKED;
n.pendingAction = NominatorPendingAction.NO_ACTION_REQUIRED;
n.updatedAt = currentBlockNumber;
cache.nominators.set(n.id, n);
});
Expand All @@ -227,7 +250,10 @@ export async function processEpochTransitionEvent(
cache.statsPerDomain.set(statsPerDomain.id, statsPerDomain);

const operators = Array.from(cache.operators.values()).filter(
(o) => o.domainId === domain.id && o.status === OperatorStatus.REGISTERED
(o) =>
o.domainId === domain.id &&
(o.status === OperatorStatus.REGISTERED ||
o.status === OperatorStatus.DEREGISTERED)
);

for (const operator of operators) {
Expand Down Expand Up @@ -269,6 +295,11 @@ export async function processEpochTransitionEvent(
deposit.pending?.storageFeeDeposit ?? BigInt(0);
nominator.pendingEffectiveDomainEpoch =
deposit.pending?.effectiveDomainEpoch ?? 0;

nominator.accumulatedEpochShares += nominator.knownShares;
nominator.accumulatedEpochStorageFeeDeposit +=
nominator.knownStorageFeeDeposit;
nominator.activeEpochCount++;
nominator.updatedAt = currentBlockNumber;

cache.nominators.set(nominator.id, nominator);
Expand Down
33 changes: 28 additions & 5 deletions indexers/staking-squid/src/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import {
processOperatorSlashedEvent,
processOperatorTaxCollectedEvent,
} from "./operator";
import {
processFundsUnlockedEvent,
processNominatedStakedUnlockedEvent,
processNominatorUnlockedEvent,
processOperatorUnlockedEvent,
processStorageFeeUnlockedEvent,
} from "./unlock";
import { processWithdrewStakeEvent } from "./withdraw";

export async function processEvents(
Expand Down Expand Up @@ -52,22 +59,38 @@ async function processEvent(
case events.domains.operatorNominated.name:
return processOperatorNominatedEvent(cache, block, extrinsic, event);

// deposit and stake
case events.domains.withdrewStake.name:
return processWithdrewStakeEvent(cache, block, extrinsic, event);

// bundle
case events.domains.bundleStored.name:
return processBundleStoredEvent(cache, block, extrinsic, event);

// deposit and stake
case events.domains.withdrewStake.name:
return processWithdrewStakeEvent(cache, block, extrinsic, event);
// unlock
case events.domains.operatorUnlocked.name:
return processOperatorUnlockedEvent(cache, block, extrinsic, event);
case events.domains.fundsUnlocked.name:
return processFundsUnlockedEvent(cache, block, extrinsic, event);
case events.domains.nominatedStakedUnlocked.name:
return processNominatedStakedUnlockedEvent(
cache,
block,
extrinsic,
event
);
case events.domains.nominatorUnlocked.name:
return processNominatorUnlockedEvent(cache, block, extrinsic, event);
case events.domains.storageFeeUnlocked.name:
return processStorageFeeUnlockedEvent(cache, block, extrinsic, event);

// rewards and slashing
case events.domains.operatorRewarded.name:
return processOperatorRewardedEvent(cache, block, extrinsic, event);

case events.domains.operatorSlashed.name:
return processOperatorSlashedEvent(cache, block, extrinsic, event);

// tax on fees
// tax
case events.domains.operatorTaxCollected.name:
return processOperatorTaxCollectedEvent(cache, block, extrinsic, event);

Expand Down
Loading

0 comments on commit 429cec5

Please sign in to comment.