Skip to content

Commit

Permalink
✨ new storage and stakeFrom info
Browse files Browse the repository at this point in the history
  • Loading branch information
PsicoThePato committed Oct 11, 2024
1 parent a645db1 commit 9df068c
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 119 deletions.
5 changes: 2 additions & 3 deletions apps/commune-worker/src/db/type-transformations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ export function SubspaceModuleToDatabase(
incentive: module.incentive ?? null,
dividend: module.dividends ?? null,
delegationFee: module.delegationFee ?? null,
totalStaked: null,
totalStakers: null,
totalRewards: null,
totalStaked: module.totalStaked,
totalStakers: module.totalStakers,
moduleId: module.uid,
isWhitelisted: whitelisted,
};
Expand Down
1 change: 1 addition & 0 deletions apps/commune-worker/src/workers/module-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export async function moduleFetcherWorker(props: WorkerProps) {
const modules = await queryRegisteredModulesInfo(
lastBlock.apiAtBlock,
NETUID_ZERO,
props.lastBlock.blockNumber,
);
const modulesData = modules.map((module) =>
SubspaceModuleToDatabase(
Expand Down
39 changes: 31 additions & 8 deletions packages/subspace/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
checkSS58,
GOVERNANCE_CONFIG_SCHEMA,
isSS58,
STAKE_FROM_SCHEMA,
MODULE_BURN_CONFIG_SCHEMA,
NetworkSubnetConfigSchema,
SUBSPACE_MODULE_SCHEMA,
Expand All @@ -34,6 +35,7 @@ import {
standardizeUidToSS58address,
} from "@commune-ts/utils";


export { ApiPromise };

// == chain ==
Expand Down Expand Up @@ -557,9 +559,26 @@ export async function querySubnetParams(api: Api): Promise<NetworkSubnetConfig[]
return subnets;
}

export function keyStakeFrom(
targetKey: SS58Address,
stakeFromStorage: Map<SS58Address, Map<SS58Address, bigint>>
) {
const stakerMap = stakeFromStorage.get(targetKey);
let totalStake = 0n;
if (stakerMap === undefined) {
console.log("could not find map for key: ", targetKey);
return totalStake;
}
for (const stake of stakerMap.values()) {
totalStake += stake;
}
return totalStake;
}

export async function queryRegisteredModulesInfo(
api: Api,
netuid: number,
blockNumber: number,
): Promise<SubspaceModule[]> {
console.log("Fetching module keys from the chain...");
const keyQuery: { subspaceModule: SubspaceStorageName[] } = {
Expand All @@ -576,13 +595,15 @@ export async function queryRegisteredModulesInfo(
"emission",
"incentive",
"dividends",
];
const extraPropsQuery: { subspaceModule: SubspaceStorageName[] } = {
subspaceModule: moduleProps,
};
"delegationFee",
"stakeFrom",
]

const extraPropsQuery: { subspaceModule: SubspaceStorageName[] } = { subspaceModule: moduleProps }
const modulesInfo = await queryChain(api, extraPropsQuery, netuid);
const processedModules = standardizeUidToSS58address(modulesInfo, uidToSS58);
const moduleMap: SubspaceModule[] = [];
const parsedStakeFromStorage = STAKE_FROM_SCHEMA.parse({ stakeFromStorage: processedModules.stakeFrom });

for (const uid of Object.keys(uidToSS58)) {
const moduleKey = uidToSS58[uid];
Expand All @@ -599,13 +620,15 @@ export async function queryRegisteredModulesInfo(
registrationBlock: processedModules.registrationBlock[moduleKey],
metadata: processedModules.metadata[moduleKey],
lastUpdate: processedModules.lastUpdate[moduleKey],
atBlock: 0,
atBlock: blockNumber,
emission: processedModules.emission[moduleKey],
incentive: processedModules.incentive[moduleKey],
dividends: processedModules.dividends[moduleKey],
delegationFee: 0,
stakeFrom: 0n,
});
delegationFee: processedModules.delegationFee[moduleKey],
totalStaked: keyStakeFrom(moduleKey, parsedStakeFromStorage.stakeFromStorage),
totalStakers: parsedStakeFromStorage.stakeFromStorage.get(moduleKey)?.size ?? 0,

})
moduleMap.push(module);
}
return moduleMap;
Expand Down
26 changes: 22 additions & 4 deletions packages/types/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import { assert } from "tsafe";
import { z } from "zod";

import type {
Codec,
DaoApplicationStatus,
OptionalProperties,
Proposal,
SS58Address,
SubspaceModule,
} from "./types";

export function checkSS58(value: string | SS58Address): SS58Address {
Expand Down Expand Up @@ -145,6 +142,26 @@ export const NUMBER_SCHEMA = z.coerce.number();
export const SUBSPACE_MODULE_REGISTRATION_BLOCK_SCHEMA = z.coerce.number();
export const SUBSPACE_MODULE_METADATA_SCHEMA = z.string(); // TODO: validate it's a valid ipfs hash or something (?)
export const SUBSPACE_MODULE_LAST_UPDATE_SCHEMA = z.any();
export const STAKE_FROM_SCHEMA = z.object({
stakeFromStorage: z.record(
ADDRESS_SCHEMA,
z.record(ADDRESS_SCHEMA, z.coerce.bigint())
).transform(
(val) => {
const map = new Map<SS58Address, Map<SS58Address, bigint>>();
const stakeMapEntries = Object.entries(val) as [SS58Address, Record<SS58Address, bigint>][];
for (const [stakedInto, stakerMap] of stakeMapEntries) {
const innerMap = new Map<SS58Address, bigint>();
const stakers = Object.entries(stakerMap) as [SS58Address, bigint][];
for (const [staker, stake] of stakers) {
innerMap.set(staker, BigInt(stake));
}
map.set(stakedInto, innerMap);
}
return map;
},
),
});

export const SUBSPACE_MODULE_SCHEMA = z.object({
netuid: z.coerce.number(),
Expand All @@ -162,5 +179,6 @@ export const SUBSPACE_MODULE_SCHEMA = z.object({
dividends: z.coerce.bigint().optional(),
delegationFee: z.coerce.number().optional(),

stakeFrom: z.bigint().optional(),
totalStaked: z.coerce.bigint(),
totalStakers: z.coerce.number(),
});
Loading

0 comments on commit 9df068c

Please sign in to comment.