Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add balancesdeposit data0 in events that trigger balance update #1140

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions indexers/consensus/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ type Transfer @entity {
extrinsicId: String!
eventId: String!
from: String!
fromChain: String!
to: String!
toChain: String!
value: BigInt!
fee: BigInt!
success: Boolean!
Expand Down
36 changes: 15 additions & 21 deletions indexers/consensus/src/mappings/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ export function createBlock(
voteRewardValue: bigint,
authorId: string
) {
const id = height.toString();
const sortId = getSortId(height);
return {
id,
sortId,
id: height.toString(),
sortId: getSortId(height),
height,
hash,
timestamp,
Expand All @@ -54,19 +52,17 @@ export function createBlock(
};
}

export function createAndSaveLog(
export function createLog(
blockHeight: bigint,
blockHash: string,
indexInBlock: number,
kind: string,
value: string,
timestamp: Date
) {
const id = `${blockHeight}-${indexInBlock}`;
const sortId = getSortId(blockHeight, BigInt(indexInBlock));
return {
id,
sortId,
id: blockHeight + "-" + indexInBlock,
sortId: getSortId(blockHeight, BigInt(indexInBlock)),
blockHeight,
blockHash,
indexInBlock,
Expand Down Expand Up @@ -96,11 +92,9 @@ export function createExtrinsic(
pos: number,
cid?: string
) {
const extrinsicId = `${blockHeight}-${indexInBlock}`;
const sortId = getSortId(blockHeight, BigInt(indexInBlock));
return {
id: extrinsicId,
sortId,
id: blockHeight + "-" + indexInBlock,
sortId: getSortId(blockHeight, BigInt(indexInBlock)),
hash,
blockHeight,
blockHash,
Expand Down Expand Up @@ -137,11 +131,9 @@ export function createEvent(
args: string,
cid?: string
) {
const id = `${blockHeight}-${indexInBlock.toString()}`;
const sortId = getSortId(blockHeight, BigInt(indexInBlock));
return {
id,
sortId,
id: blockHeight + "-" + indexInBlock.toString(),
sortId: getSortId(blockHeight, indexInBlock),
blockHeight,
blockHash,
extrinsicId,
Expand Down Expand Up @@ -185,21 +177,24 @@ export function createTransfer(
extrinsicId: string,
eventId: string,
from: string,
fromChain: string,
to: string,
toChain: string,
value: bigint,
fee: bigint,
success: boolean,
timestamp: Date
) {
const id = extrinsicId + "-" + eventId;
return {
id,
id: extrinsicId + "-" + eventId,
blockHeight,
blockHash,
extrinsicId,
eventId,
from,
fromChain,
to,
toChain,
value,
fee,
success,
Expand All @@ -217,9 +212,8 @@ export function createReward(
amount: bigint,
timestamp: Date
) {
const id = accountId + "-" + eventId;
return {
id,
id: accountId + "-" + eventId,
blockHeight,
blockHash,
extrinsicId,
Expand Down
66 changes: 56 additions & 10 deletions indexers/consensus/src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ import { SubstrateBlock } from "@subql/types";
import { Entity } from "@subql/types-core";
import {
createAccountHistory,
createAndSaveLog,
createBlock,
createEvent,
createExtrinsic,
createLog,
createReward,
createTransfer,
} from "./db";
import { getBlockAuthor, parseDataToCid } from "./helper";
import { ExtrinsicPrimitive, LogValue } from "./types";

const CONSENSUS_CHAIN_TYPE = "consensus";

export async function handleBlock(_block: SubstrateBlock): Promise<void> {
const {
block: {
Expand Down Expand Up @@ -133,7 +135,7 @@ export async function handleBlock(_block: SubstrateBlock): Promise<void> {

// Detect data storage extrinsics and parse args to cid
let cid: string | undefined = "";
let args: string = stringify(extrinsicMethodToPrimitive.args);
let extrinsicArgs: string = stringify(extrinsicMethodToPrimitive.args);
if (
(extrinsic.method.section === "historySeeding" &&
extrinsic.method.method === "seedHistory") ||
Expand All @@ -144,7 +146,7 @@ export async function handleBlock(_block: SubstrateBlock): Promise<void> {
const parsedArgs = parseDataToCid(extrinsicMethodToPrimitive.args.remark);
cid = parsedArgs.cid;
// The args parameter will be replaced by `{ "cid": "bafkr6i..." }` to minimize the size of the db
args = parsedArgs.modifiedArgs ?? args;
extrinsicArgs = parsedArgs.modifiedArgs ?? extrinsicArgs;
}

newExtrinsics.push(
Expand All @@ -161,7 +163,7 @@ export async function handleBlock(_block: SubstrateBlock): Promise<void> {
extrinsicSigner,
extrinsic.signature.toString(),
extrinsicEvents.length,
args,
extrinsicArgs,
error,
BigInt(extrinsic.tip.toString()),
fee,
Expand All @@ -179,15 +181,15 @@ export async function handleBlock(_block: SubstrateBlock): Promise<void> {

// Detect data storage extrinsics and parse args to cid
let cid: string | undefined = "";
let args: string = stringify(event.event.data);
let eventsArgs: string = stringify(event.event.data);
if (
event.event.section === "system" &&
event.event.method === "Remarked"
) {
const parsedArgs = parseDataToCid(event.event.data[1].toString());
cid = parsedArgs.cid;
// The args parameter will be replaced by `{ "cid": "bafkr6i..." }` to minimize the size of the db
args = parsedArgs.modifiedArgs ?? args;
eventsArgs = parsedArgs.modifiedArgs ?? eventsArgs;
}

newEvents.push(
Expand All @@ -202,13 +204,13 @@ export async function handleBlock(_block: SubstrateBlock): Promise<void> {
blockTimestamp,
event.phase.type,
pos,
args,
eventsArgs,
cid
)
);

// Process specific events
switch (`${event.event.section}.${event.event.method}`) {
switch (event.event.section + "." + event.event.method) {
case "balances.Transfer": {
const from = event.event.data[0].toString();
const to = event.event.data[1].toString();
Expand All @@ -225,7 +227,9 @@ export async function handleBlock(_block: SubstrateBlock): Promise<void> {
extrinsicId,
height + "-" + eventIndex,
from,
CONSENSUS_CHAIN_TYPE,
to,
CONSENSUS_CHAIN_TYPE,
amount,
fee,
successEvent ? true : false,
Expand All @@ -235,6 +239,44 @@ export async function handleBlock(_block: SubstrateBlock): Promise<void> {

break;
}
case "transporter.OutgoingTransferInitiated": {
const [chainType, domainId] = Object.entries(
extrinsicMethodToPrimitive.args.dst_location.chainId
)[0] as [string, string | undefined];
const [_, to] = Object.entries(
extrinsicMethodToPrimitive.args.dst_location.accountId
)[0] as [string, string];
const amount = BigInt(
extrinsicMethodToPrimitive.args.amount.toString()
);

addressToUpdate.add(extrinsicSigner);
if (chainType === CONSENSUS_CHAIN_TYPE) addressToUpdate.add(to);

totalTransferValue += amount;

const newTransfer = createTransfer(
height,
blockHash,
extrinsicId,
height + "-" + eventIndex,
extrinsicSigner,
CONSENSUS_CHAIN_TYPE,
to,
chainType + ":" + domainId,
amount,
fee,
successEvent ? true : false,
blockTimestamp
);
newTransfers.push(newTransfer);

break;
}
case "balances.Burned": {
addressToUpdate.add(event.event.data[0].toString());
break;
}
default:
break;
}
Expand Down Expand Up @@ -264,7 +306,7 @@ export async function handleBlock(_block: SubstrateBlock): Promise<void> {
);

// Process specific events
switch (`${event.event.section}.${event.event.method}`) {
switch (event.event.section + "." + event.event.method) {
case "rewards.VoteReward": {
const voter = event.event.data[0].toString();
const reward = BigInt(event.event.data[1].toString());
Expand Down Expand Up @@ -315,6 +357,10 @@ export async function handleBlock(_block: SubstrateBlock): Promise<void> {

break;
}
case "balances.Deposit": {
addressToUpdate.add(event.event.data[0].toString());
break;
}
default:
break;
}
Expand All @@ -335,7 +381,7 @@ export async function handleBlock(_block: SubstrateBlock): Promise<void> {
? { data: _value[1], engine: _value[0] }
: { data: _value };

return createAndSaveLog(
return createLog(
height,
blockHash,
i,
Expand Down
2 changes: 2 additions & 0 deletions indexers/db/docker-entrypoint-initdb.d/init-db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,9 @@ CREATE TABLE consensus.transfers (
extrinsic_id TEXT NOT NULL,
event_id TEXT NOT NULL,
"from" TEXT NOT NULL,
from_chain TEXT NOT NULL,
"to" TEXT NOT NULL,
to_chain TEXT NOT NULL,
value NUMERIC NOT NULL,
fee NUMERIC NOT NULL,
success BOOLEAN NOT NULL,
Expand Down