mirror of
https://github.com/pezkuwichain/pezkuwi-subquery.git
synced 2026-04-22 05:27:57 +00:00
fix: noter bot queries staking data from Asset Hub instead of Relay Chain
Staking pallet moved from RC to AH. Updated noter to query assetHubApi.query.staking.* for direct staking data.
This commit is contained in:
+61
-28
@@ -1,12 +1,16 @@
|
|||||||
/**
|
/**
|
||||||
* Pezkuwi Noter Bot
|
* Pezkuwi Noter Bot
|
||||||
*
|
*
|
||||||
* Collects staking data from Relay Chain and Asset Hub, then submits
|
* Collects staking data from Asset Hub (direct staking + nomination pools),
|
||||||
* to People Chain via receive_staking_details() as a noter-authorized account.
|
* then submits to People Chain via receive_staking_details() as a noter-authorized account.
|
||||||
|
*
|
||||||
|
* NOTE: NPoS staking moved from Relay Chain to Asset Hub.
|
||||||
|
* RC no longer has pallet_staking (removed via RemovePallet migration).
|
||||||
|
* Direct staking (bond/nominate/unbond) is now on AH as pallet_staking_async.
|
||||||
*
|
*
|
||||||
* Workflow:
|
* Workflow:
|
||||||
* 1. Listen for ScoreTrackingStarted events on People Chain
|
* 1. Listen for ScoreTrackingStarted events on People Chain
|
||||||
* 2. On event → query staking data from Relay Chain (+ Asset Hub pools)
|
* 2. On event → query staking data from Asset Hub (direct staking + pools)
|
||||||
* 3. Submit receive_staking_details() signed by noter account
|
* 3. Submit receive_staking_details() signed by noter account
|
||||||
* 4. Periodically scan all tracked accounts for staking changes
|
* 4. Periodically scan all tracked accounts for staking changes
|
||||||
*
|
*
|
||||||
@@ -90,20 +94,25 @@ async function connectApi(endpoint, name) {
|
|||||||
// ========================================
|
// ========================================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get staking data from Relay Chain for a single account.
|
* Get direct staking data from Asset Hub for a single account.
|
||||||
|
* NPoS staking moved from Relay Chain to Asset Hub (pallet_staking_async).
|
||||||
* Returns { stakedAmount, nominationsCount, unlockingChunksCount }
|
* Returns { stakedAmount, nominationsCount, unlockingChunksCount }
|
||||||
*/
|
*/
|
||||||
async function getRelayStakingData(relayApi, address) {
|
async function getAssetHubStakingData(assetHubApi, address) {
|
||||||
try {
|
try {
|
||||||
|
if (!assetHubApi.query.staking) {
|
||||||
|
return { stakedAmount: 0n, nominationsCount: 0, unlockingChunksCount: 0 };
|
||||||
|
}
|
||||||
|
|
||||||
// Try direct ledger query (stash == controller in modern Substrate)
|
// Try direct ledger query (stash == controller in modern Substrate)
|
||||||
let ledgerResult = await relayApi.query.staking.ledger(address);
|
let ledgerResult = await assetHubApi.query.staking.ledger(address);
|
||||||
|
|
||||||
// Fallback: check bonded controller
|
// Fallback: check bonded controller
|
||||||
if (ledgerResult.isNone) {
|
if (ledgerResult.isNone) {
|
||||||
const bonded = await relayApi.query.staking.bonded(address);
|
const bonded = await assetHubApi.query.staking.bonded(address);
|
||||||
if (bonded.isSome) {
|
if (bonded.isSome) {
|
||||||
const controller = bonded.unwrap().toString();
|
const controller = bonded.unwrap().toString();
|
||||||
ledgerResult = await relayApi.query.staking.ledger(controller);
|
ledgerResult = await assetHubApi.query.staking.ledger(controller);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,7 +124,7 @@ async function getRelayStakingData(relayApi, address) {
|
|||||||
const stakedAmount = ledger.active.toBigInt();
|
const stakedAmount = ledger.active.toBigInt();
|
||||||
|
|
||||||
// Get nominations count
|
// Get nominations count
|
||||||
const nominations = await relayApi.query.staking.nominators(address);
|
const nominations = await assetHubApi.query.staking.nominators(address);
|
||||||
const nominationsCount = nominations.isSome
|
const nominationsCount = nominations.isSome
|
||||||
? nominations.unwrap().targets.length
|
? nominations.unwrap().targets.length
|
||||||
: 0;
|
: 0;
|
||||||
@@ -125,7 +134,7 @@ async function getRelayStakingData(relayApi, address) {
|
|||||||
|
|
||||||
return { stakedAmount, nominationsCount, unlockingChunksCount };
|
return { stakedAmount, nominationsCount, unlockingChunksCount };
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log('ERROR', `Failed to get relay staking data for ${address}`, { error: err.message });
|
log('ERROR', `Failed to get AH staking data for ${address}`, { error: err.message });
|
||||||
return { stakedAmount: 0n, nominationsCount: 0, unlockingChunksCount: 0 };
|
return { stakedAmount: 0n, nominationsCount: 0, unlockingChunksCount: 0 };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -274,32 +283,56 @@ async function submitStakingDetails(peopleApi, noterKeypair, updates) {
|
|||||||
async function processAccount(relayApi, assetHubApi, peopleApi, noterKeypair, address) {
|
async function processAccount(relayApi, assetHubApi, peopleApi, noterKeypair, address) {
|
||||||
const updates = [];
|
const updates = [];
|
||||||
|
|
||||||
// 1. Relay Chain staking
|
// 1. Asset Hub direct staking (NPoS staking moved from RC to AH)
|
||||||
const relayData = await getRelayStakingData(relayApi, address);
|
const ahStakingData = await getAssetHubStakingData(assetHubApi, address);
|
||||||
const relayCached = await getCachedData(peopleApi, address, 'RelayChain');
|
const ahStakingCached = await getCachedData(peopleApi, address, 'AssetHub');
|
||||||
|
|
||||||
if (hasDataChanged(relayData, relayCached)) {
|
if (hasDataChanged(ahStakingData, ahStakingCached)) {
|
||||||
updates.push({ address, source: 'RelayChain', data: relayData });
|
updates.push({ address, source: 'AssetHub', data: ahStakingData });
|
||||||
const stakedHEZ = Number(relayData.stakedAmount / UNITS);
|
const stakedHEZ = Number(ahStakingData.stakedAmount / UNITS);
|
||||||
log('INFO', `Relay data changed for ${address.slice(0, 8)}...`, {
|
log('INFO', `AH staking data changed for ${address.slice(0, 8)}...`, {
|
||||||
stakedHEZ, noms: relayData.nominationsCount, unlocking: relayData.unlockingChunksCount
|
stakedHEZ, noms: ahStakingData.nominationsCount, unlocking: ahStakingData.unlockingChunksCount
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Asset Hub nomination pools
|
// 2. Asset Hub nomination pools (separate from direct staking)
|
||||||
const assetHubData = await getAssetHubPoolData(assetHubApi, address);
|
const poolData = await getAssetHubPoolData(assetHubApi, address);
|
||||||
const assetHubCached = await getCachedData(peopleApi, address, 'AssetHub');
|
|
||||||
|
|
||||||
if (assetHubData.stakedAmount > 0n || assetHubCached !== null) {
|
// If user has pool membership, merge pool stake into AH data
|
||||||
// Only submit if there's actual pool membership or we need to clear old data
|
if (poolData.stakedAmount > 0n) {
|
||||||
if (hasDataChanged(assetHubData, assetHubCached)) {
|
// Combine direct staking + pool stake for total AH stake
|
||||||
updates.push({ address, source: 'AssetHub', data: assetHubData });
|
const combinedData = {
|
||||||
const stakedHEZ = Number(assetHubData.stakedAmount / UNITS);
|
stakedAmount: ahStakingData.stakedAmount + poolData.stakedAmount,
|
||||||
log('INFO', `Asset Hub data changed for ${address.slice(0, 8)}...`, { stakedHEZ });
|
nominationsCount: ahStakingData.nominationsCount,
|
||||||
|
unlockingChunksCount: ahStakingData.unlockingChunksCount + poolData.unlockingChunksCount,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (hasDataChanged(combinedData, ahStakingCached)) {
|
||||||
|
// Replace previous AH update with combined data
|
||||||
|
const existingIdx = updates.findIndex(u => u.source === 'AssetHub');
|
||||||
|
if (existingIdx >= 0) {
|
||||||
|
updates[existingIdx].data = combinedData;
|
||||||
|
} else {
|
||||||
|
updates.push({ address, source: 'AssetHub', data: combinedData });
|
||||||
|
}
|
||||||
|
const stakedHEZ = Number(combinedData.stakedAmount / UNITS);
|
||||||
|
log('INFO', `AH combined (staking+pool) for ${address.slice(0, 8)}...`, { stakedHEZ });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Submit all updates in a single batch
|
// 3. Clear old RelayChain cache if it exists (staking moved to AH)
|
||||||
|
const relayCached = await getCachedData(peopleApi, address, 'RelayChain');
|
||||||
|
if (relayCached !== null && relayCached.stakedAmount > 0n) {
|
||||||
|
// Submit zero to clear old RC cache entry
|
||||||
|
updates.push({
|
||||||
|
address,
|
||||||
|
source: 'RelayChain',
|
||||||
|
data: { stakedAmount: 0n, nominationsCount: 0, unlockingChunksCount: 0 }
|
||||||
|
});
|
||||||
|
log('INFO', `Clearing old RC cache for ${address.slice(0, 8)}...`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Submit all updates in a single batch
|
||||||
if (updates.length > 0) {
|
if (updates.length > 0) {
|
||||||
await submitStakingDetails(peopleApi, noterKeypair, updates);
|
await submitStakingDetails(peopleApi, noterKeypair, updates);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user