Skip to content

Commit

Permalink
Merge pull request #836 from autonomys/feat/keepImprovingStakingSquid
Browse files Browse the repository at this point in the history
Staking-squid v24
  • Loading branch information
marc-aurele-besner authored Sep 4, 2024
2 parents 74ab199 + 36fdbcc commit 1120ad1
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 87 deletions.
36 changes: 23 additions & 13 deletions indexers/staking-squid/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,6 @@ type Operator @entity {
signingKey: String! @index
minimumNominatorStake: BigInt!
nominationTax: Int!
name: String! @index
description: String!
icon: String!
banner: String!
website: String!
websiteVerified: Boolean!
email: String!
emailVerified: Boolean!
discord: String!
github: String!
twitter: String!
currentTotalStake: BigInt!
currentStorageFeeDeposit: BigInt!
currentEpochRewards: BigInt!
Expand Down Expand Up @@ -136,6 +125,27 @@ type Operator @entity {
updatedAt: Int! @index
}

type OperatorProfile @entity {
id: ID! @index
operatorId: String! @index
accountId: String! @index
name: String! @index
description: String!
icon: String!
banner: String!
website: String!
websiteVerified: Boolean!
email: String!
emailVerified: Boolean!
discord: String!
github: String!
twitter: String!
proofMessage: String!
proofSignature: String!
createdAt: Int! @index
updatedAt: Int! @index
}

type DomainBlock @entity {
id: ID! @index
domainId: String! @index
Expand Down Expand Up @@ -215,7 +225,7 @@ enum NominatorStatus {
enum NominatorPendingAction {
NO_ACTION_REQUIRED
PENDING_EPOCH_CHANGE
PENDING_DEREGISTRATION_LOCK
PENDING_LOCK_PERIOD
READY_TO_UNLOCK_ALL_FUNDS
}

Expand Down Expand Up @@ -312,7 +322,7 @@ type Withdrawal @entity {
updatedAt: Int! @index
}

type RewardEvent @entity {
type Reward @entity {
id: ID! @index
domainId: String! @index
operatorId: String! @index
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: 21
version: 24
description: Autonomys - Astral - Staking Indexer
build:
deploy:
Expand Down
15 changes: 12 additions & 3 deletions indexers/staking-squid/src/events/epoch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export async function processEpochTransitionEvent(
_domain.runtimeId = pDomain.domainConfig.runtimeId;

const stringifiedRuntime = JSON.stringify(pDomain.domainRuntimeInfo);
_domain.runtime = stringifiedRuntime.includes("AutoId")
_domain.runtime = stringifiedRuntime.includes("autoId")
? DomainRuntime.AutoId
: DomainRuntime.EVM;
_domain.runtimeInfo = stringifiedRuntime;
Expand Down Expand Up @@ -158,8 +158,7 @@ export async function processEpochTransitionEvent(
)
.forEach((n) => {
n.status = NominatorStatus.PENDING;
n.pendingAction =
NominatorPendingAction.PENDING_DEREGISTRATION_LOCK;
n.pendingAction = NominatorPendingAction.PENDING_LOCK_PERIOD;
n.updatedAt = currentBlockNumber;
cache.nominators.set(n.id, n);
});
Expand All @@ -185,15 +184,25 @@ export async function processEpochTransitionEvent(
}
domain.currentTotalStake = BigInt(0);
domain.currentStorageFeeDeposit = BigInt(0);
domain.currentTotalShares = BigInt(0);
domain.currentSharePrice = BigInt(0);
domain.accumulatedEpochStake = BigInt(0);
domain.accumulatedEpochStorageFeeDeposit = BigInt(0);

allOperators.forEach((o) => {
domain.currentTotalStake += o.operatorDetails.currentTotalStake;
domain.currentStorageFeeDeposit += o.operatorDetails.totalStorageFeeDeposit;
domain.currentTotalShares += o.operatorDetails.currentTotalShares;
domain.currentSharePrice =
domain.currentTotalShares > BigInt(0)
? (domain.currentTotalStake * SHARES_CALCULATION_MULTIPLIER) /
domain.currentTotalShares
: BigInt(0);
domain.accumulatedEpochStake += o.operatorDetails.currentTotalStake;
domain.accumulatedEpochStorageFeeDeposit +=
o.operatorDetails.totalStorageFeeDeposit;
domain.accumulatedEpochRewards += domain.totalRewardsCollected;
domain.accumulatedEpochShares += o.operatorDetails.currentTotalShares;
});

domain.completedEpoch = completedEpoch;
Expand Down
6 changes: 0 additions & 6 deletions indexers/staking-squid/src/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import {
import {
processFundsUnlockedEvent,
processNominatedStakedUnlockedEvent,
processNominatorUnlockedEvent,
processOperatorUnlockedEvent,
processStorageFeeUnlockedEvent,
} from "./unlock";
import { processWithdrewStakeEvent } from "./withdraw";

Expand Down Expand Up @@ -79,10 +77,6 @@ async function processEvent(
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:
Expand Down
7 changes: 2 additions & 5 deletions indexers/staking-squid/src/events/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,12 @@ export function processOperatorRewardedEvent(
domain.totalRewardsCollected += amount;
cache.domains.set(domain.id, domain);

const operatorRewardedEvent = createRewardEvent(block, extrinsic, {
const operatorRewardedEvent = createRewardEvent(block, extrinsic, event, {
operatorId: operator.id,
domainId: operator.domainId,
amount,
});
cache.operatorRewardedEvents.set(
operatorRewardedEvent.id,
operatorRewardedEvent
);
cache.operatorRewards.set(operatorRewardedEvent.id, operatorRewardedEvent);

cache.isModified = true;

Expand Down
51 changes: 16 additions & 35 deletions indexers/staking-squid/src/events/unlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,17 @@ export function processFundsUnlockedEvent(
});

domain.totalWithdrawals += amountBigInt;
cache.domains.set(domain.id, domain);

account.totalWithdrawals += amountBigInt;
cache.accounts.set(account.id, account);

operator.totalWithdrawals += amountBigInt;
cache.operators.set(operator.id, operator);

nominator.totalWithdrawals += amountBigInt;
nominator.pendingAction = NominatorPendingAction.NO_ACTION_REQUIRED;
cache.nominators.set(nominator.id, nominator);

cache.isModified = true;

Expand Down Expand Up @@ -206,44 +214,17 @@ export function processNominatedStakedUnlockedEvent(
});

domain.totalWithdrawals += unlockedAmountBigInt;
account.totalWithdrawals += unlockedAmountBigInt;
operator.totalWithdrawals += unlockedAmountBigInt;
nominator.totalWithdrawals += unlockedAmountBigInt;

cache.isModified = true;

return cache;
}

export function processNominatorUnlockedEvent(
cache: Cache,
block: CtxBlock,
extrinsic: CtxExtrinsic,
event: CtxEvent
) {
const { operatorId, nominatorId } = event.args;
const operatorIdNum = Number(operatorId);
const nominatorIdNum = Number(nominatorId);
const address = getCallSigner(extrinsic.call);
const blockNumber = getBlockNumber(block);
cache.domains.set(domain.id, domain);

cache.isModified = true;
account.totalWithdrawals += unlockedAmountBigInt;
cache.accounts.set(account.id, account);

return cache;
}
operator.totalWithdrawals += unlockedAmountBigInt;
cache.operators.set(operator.id, operator);

export function processStorageFeeUnlockedEvent(
cache: Cache,
block: CtxBlock,
extrinsic: CtxExtrinsic,
event: CtxEvent
) {
const { operatorId, nominatorId, storageFee } = event.args;
const operatorIdNum = Number(operatorId);
const nominatorIdNum = Number(nominatorId);
const address = getCallSigner(extrinsic.call);
const blockNumber = getBlockNumber(block);
const storageFeeBigInt = BigInt(storageFee);
nominator.totalWithdrawals += unlockedAmountBigInt;
nominator.pendingAction = NominatorPendingAction.NO_ACTION_REQUIRED;
cache.nominators.set(nominator.id, nominator);

cache.isModified = true;

Expand Down
2 changes: 2 additions & 0 deletions indexers/staking-squid/src/events/withdraw.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { NominatorPendingAction } from "../model";
import type { CtxBlock, CtxEvent, CtxExtrinsic } from "../processor";
import {
createWithdrawal,
Expand Down Expand Up @@ -64,6 +65,7 @@ export function processWithdrewStakeEvent(

nominator.totalWithdrawalsCount++;
nominator.totalEstimatedWithdrawals += estimatedAmount;
nominator.pendingAction = NominatorPendingAction.PENDING_LOCK_PERIOD;
nominator.updatedAt = blockNumber;
cache.nominators.set(nominator.id, nominator);

Expand Down
2 changes: 2 additions & 0 deletions indexers/staking-squid/src/extrinsics/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ export function processDeregisterOperator(
cache.withdrawals.set(w.id, w);

n.status = NominatorStatus.PENDING;
n.pendingAction = NominatorPendingAction.PENDING_LOCK_PERIOD;
n.totalWithdrawalsCount++;
n.totalEstimatedWithdrawals += estimatedAmount;
n.updatedAt = blockNumber;
cache.nominators.set(n.id, n);

Expand Down
2 changes: 1 addition & 1 deletion indexers/staking-squid/src/storage/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const createBundleAuthor = (
props?: Partial<BundleAuthor>
): BundleAuthor =>
new BundleAuthor({
id: randomUUID(),
id: bundleUID(domainId, bundleId, operatorId),
domainId,
accountId,
operatorId,
Expand Down
24 changes: 7 additions & 17 deletions indexers/staking-squid/src/storage/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {
Operator,
OperatorPendingAction,
OperatorStatus,
RewardEvent,
Reward,
} from "../model";
import type { CtxBlock, CtxExtrinsic } from "../processor";
import type { CtxBlock, CtxEvent, CtxExtrinsic } from "../processor";
import { getBlockNumber, getTimestamp, operatorUID } from "../utils";
import { Cache } from "../utils/cache";

Expand All @@ -22,17 +22,6 @@ export const createOperator = (
signingKey: "0x",
minimumNominatorStake: BigInt(0),
nominationTax: 0,
name: "",
description: "",
icon: "",
banner: "",
website: "",
websiteVerified: false,
email: "",
emailVerified: false,
discord: "",
github: "",
twitter: "",
currentTotalStake: BigInt(0),
currentStorageFeeDeposit: BigInt(0),
currentEpochRewards: BigInt(0),
Expand Down Expand Up @@ -88,10 +77,11 @@ export const getOrCreateOperator = (
export const createRewardEvent = (
block: CtxBlock,
extrinsic: CtxExtrinsic,
props: Partial<RewardEvent>
): RewardEvent =>
new RewardEvent({
id: randomUUID(),
event: CtxEvent,
props: Partial<Reward>
): Reward =>
new Reward({
id: event.id,
...props,
blockNumber: getBlockNumber(block),
timestamp: getTimestamp(block),
Expand Down
18 changes: 12 additions & 6 deletions indexers/staking-squid/src/utils/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
DomainEpoch,
Nominator,
Operator,
RewardEvent,
Reward,
Stats,
StatsPerAccount,
StatsPerDomain,
Expand All @@ -34,7 +34,7 @@ export type TemporaryCache = {
bundles: Map<string, Bundle>;
bundleAuthors: Map<string, BundleAuthor>;
domainBlocks: Map<string, DomainBlock>;
operatorRewardedEvents: Map<string, RewardEvent>;
operatorRewards: Map<string, Reward>;
stats: Map<string, Stats>;
statsPerDomain: Map<string, StatsPerDomain>;
statsPerOperator: Map<string, StatsPerOperator>;
Expand Down Expand Up @@ -73,7 +73,7 @@ export const initTemporaryCache: TemporaryCache = {
bundles: new Map(),
bundleAuthors: new Map(),
domainBlocks: new Map(),
operatorRewardedEvents: new Map(),
operatorRewards: new Map(),
stats: new Map(),
statsPerDomain: new Map(),
statsPerOperator: new Map(),
Expand Down Expand Up @@ -143,7 +143,13 @@ const saveEntry = async <E extends Entity>(
const entity = cache[name] as unknown as Map<string, E>;
if (entity.size === 0) return;

await ctx.store.save(Array.from(entity.values()));
const entitiesArray = Array.from(entity.values()) as E[];
const batchSize = 300;

for (let i = 0; i < entitiesArray.length; i += batchSize) {
const batch = entitiesArray.slice(i, i + batchSize);
await ctx.store.save(batch);
}
} catch (e) {
console.error(`Failed to save ${name} with error:`, e);
}
Expand All @@ -167,7 +173,7 @@ export const save = async (ctx: Ctx<Store>, cache: Cache) => {
let logTemp = logEntry("bundles", cache.bundles);
logTemp += logEntry("bundleAuthors", cache.bundleAuthors);
logTemp += logEntry("domainBlocks", cache.domainBlocks);
logTemp += logEntry("operatorRewardedEvents", cache.operatorRewardedEvents);
logTemp += logEntry("operatorRewards", cache.operatorRewards);
logTemp += logEntry("stats", cache.stats);
logTemp += logEntry("statsPerDomain", cache.statsPerDomain);
logTemp += logEntry("statsPerOperator", cache.statsPerOperator);
Expand All @@ -189,7 +195,7 @@ export const save = async (ctx: Ctx<Store>, cache: Cache) => {
cache.internalKeyStore.clear();
cache.bundles.clear();
cache.bundleAuthors.clear();
cache.operatorRewardedEvents.clear();
cache.operatorRewards.clear();
cache.domainBlocks.clear();
cache.stats.clear();
cache.statsPerDomain.clear();
Expand Down

0 comments on commit 1120ad1

Please sign in to comment.