Skip to content

Add Kusama #21

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
26 changes: 26 additions & 0 deletions commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,32 @@
"src/polkadot/parachains/hydration/main.ts"
]
},
"generateMeta:kusama:assethub": {
"description": "Generate metadata for Kusama AssetHub",
"cmd": [
"npx",
"squid-substrate-metadata-explorer",
"--rpc=wss://asset-hub-kusama-rpc.dwellir.com",
"--out=versions/kusama/assethub-versions.jsonl"
]
},
"typegen:kusama:assethub": {
"description": "Generate data access classes from metadata",
"cmd": [
"squid-substrate-typegen",
"./versions/kusama/typegen-assethub.json"
]
},
"process:kusama:assethub": {
"description": "Start the squid processor for Kusama AssetHub",
"deps": ["build"],
"cmd": [
"node",
"--require=ts-node/register",
"--require=dotenv/config",
"src/kusama/assethub/main.ts"
]
},
"serve": {
"description": "Start the GraphQL API server",
"cmd": ["squid-graphql-server"]
Expand Down
46 changes: 46 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,49 @@ type TransferStatusToEthereum @entity {
toBridgeHubOutboundQueue: OutboundMessageAcceptedOnBridgeHub
toDestination: InboundMessageDispatchedOnEthereum
}

"""
Transfers from Polkadot AssetHub to Kusama AssetHub
"""
type TransferStatusToKusama @entity {
id: ID!
messageId: String! @index
txHash: String! @index
blockNumber: Int! @index
timestamp: DateTime! @index
nonce: Int @index
status: Int @index
tokenAddress: String
tokenLocation: String
senderAddress: String
sourceParaId: Int
destinationAddress: String
amount: BigInt
channelId: String
toAssetHubMessageQueue: MessageProcessedOnPolkadot
toBridgeHubMessageQueue: MessageProcessedOnPolkadot
toDestination: MessageProcessedOnKusama
}

"""
Transfers from Kusama AssetHub to Polkadot AssetHub
"""
type TransferStatusFromKusama @entity {
id: ID!
messageId: String! @index
txHash: String! @index
blockNumber: Int! @index
timestamp: DateTime! @index
nonce: Int @index
status: Int @index
tokenAddress: String
tokenLocation: String
senderAddress: String
sourceParaId: Int
destinationAddress: String
amount: BigInt
channelId: String
fromKusamaMessageQueue: MessageProcessedOnKusama
toBridgeHubMessageQueue: MessageProcessedOnKusama
toDestination: MessageProcessedOnPolkadot
}
5 changes: 5 additions & 0 deletions squid.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ deploy:
RPC_MYTHOS: "wss://polkadot-mythos-rpc.polkadot.io"
START_BLOCK_MYTHOS: 2542302
cmd: ["sqd", "process:parachain:mythos"]
- name: kusama-assethub-processor
env:
RPC_KUSAMA_ASSETHUB: "wss://asset-hub-kusama-rpc.dwellir.com"
START_BLOCK_KUSAMA_ASSETHUB: 9337555
cmd: ["sqd", "process:kusama:assethub"]
- name: post-processor
cmd: ["sqd", "postprocess"]
api:
Expand Down
2 changes: 2 additions & 0 deletions src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export const BridgeHubParaId = 1002;

export const AssetHubParaId = 1000;

export const KusamaAssetHubParaId = 1000;

export const MoonBeamParaId = 2004;

export const HydrationParaId = 2034;
Expand Down
2 changes: 2 additions & 0 deletions src/kusama/assethub/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
import './main'
157 changes: 157 additions & 0 deletions src/kusama/assethub/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import { TypeormDatabase, Store } from "@subsquid/typeorm-store";
import { processor, ProcessorContext } from "./processor";
import {
MessageProcessedOnPolkadot,
TransferStatusToKusama,
TransferStatusFromKusama, TransferStatusToEthereum,
} from "../../model";
import { events } from "./types";
import { Bytes } from "./types/support";
import {
TransferStatusEnum,
BridgeHubParaId,
AssetHubParaId,
KusamaAssetHubParaId,
toSubscanEventID,
findTokenAddress,
} from "../../common";
import {V4Instruction, V4Location} from "../../polkadot/assethub/types/v1002000";

processor.run(
new TypeormDatabase({
supportHotBlocks: true,
stateSchema: "kusama_assethub_processor",
}),
async (ctx) => {
await processPolkadotToKusamaEvents(ctx);
}
);

async function processPolkadotToKusamaEvents(ctx: ProcessorContext<Store>) {
let transfers: TransferStatusFromKusama[] = [],
forwardMessages: MessageProcessedOnPolkadot[] = [];
for (let block of ctx.blocks) {
let xcmpMessageSent = false;
let messagesInBlock: MessageProcessedOnPolkadot[] = [];
for (let event of block.events) {
if (event.name == events.xcmpQueue.xcmpMessageSent.name) {
xcmpMessageSent = true;
} {
let rec: {
origin: V4Location;
destination: V4Location;
messageId: Bytes;
message: V4Instruction[];
};
if (events.polkadotXcm.sent.v1002000.is(event)) {
rec = events.polkadotXcm.sent.v1002000.decode(event);
} else {
console.log("skipping invalid spec event: {}", event);
continue;
}
if (
rec.destination.parents == 2 &&
rec.destination.interior.__kind == "X1" &&
rec.destination.interior.value[0].__kind == "GlobalConsensus" &&
rec.destination.interior.value[0].value.__kind == "Kusama"
) {
let amount: bigint = BigInt(0);
let senderAddress: Bytes = "";
let tokenAddress: Bytes = "";
let tokenLocation: Bytes = "";
let destinationAddress: Bytes = "";

let messageId = rec.messageId.toString().toLowerCase();
if (rec.origin.interior.__kind == "X1") {
let val = rec.origin.interior.value[0];
if (val.__kind == "AccountId32") {
senderAddress = val.id;
}
}

let instruction0 = rec.message[0];
if (instruction0.__kind == "ReserveAssetDeposited") {
let asset = instruction0.value[0];
tokenLocation = JSON.stringify(asset.id, (key, value) =>
typeof value === "bigint" ? value.toString() : value
);
if (asset.fun.__kind == "Fungible") {
amount = asset.fun.value;
// For ENA extract the token address
if (
asset.id.interior.__kind == "X2"
) {
let val = asset.id.interior.value[1];
if (val.__kind == "AccountKey20") {
tokenAddress = val.key;
}
}
// For native Ether
else if (
asset.id.interior.__kind == "X1"
) {
tokenAddress = "0x0000000000000000000000000000000000000000";
}
// For PNA retrieving from the static map, can be improved by using another indexer
else {
tokenAddress = findTokenAddress("polkadot", tokenLocation);
}
}
}

let instruction3 = rec.message[3];
if (instruction3.__kind == "DepositAsset") {
let beneficiary = instruction3.beneficiary;
if (beneficiary.interior.__kind == "X1") {
let val = beneficiary.interior.value[0];
if (val.__kind == "AccountId32") {
destinationAddress = val.id;
}
}
}

let transferToKusama = new TransferStatusFromKusama({
id: messageId,
txHash: event.extrinsic?.hash,
blockNumber: block.header.height,
timestamp: new Date(block.header.timestamp!),
messageId: messageId,
tokenAddress,
tokenLocation,
sourceParaId: AssetHubParaId,
senderAddress,
destinationAddress,
amount,
status: TransferStatusEnum.Pending,
});
transfers.push(transferToKusama);
}
}
}

// Start from Kusama AH
if (transfers.length) {
for (let transfer of transfers) {
let transferStatus = await ctx.store.findOneBy(
TransferStatusFromKusama,
{
id: transfer.messageId,
}
);
if (!transferStatus) {
transfers.push(transfer);
}
}
}
}

if (forwardMessages.length > 0) {
ctx.log.debug("saving forward messages from Kusama");
await ctx.store.save(forwardMessages);
}

if (transfers.length > 0) {
ctx.log.debug("saving transfer messages from Kusama to Polkadot");
await ctx.store.save(transfers);
}
}
70 changes: 70 additions & 0 deletions src/kusama/assethub/processor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { assertNotNull } from "@subsquid/util-internal";
import {
BlockHeader,
DataHandlerContext,
SubstrateBatchProcessor,
SubstrateBatchProcessorFields,
Event as _Event,
Call as _Call,
Extrinsic as _Extrinsic,
} from "@subsquid/substrate-processor";

import { events } from "./types";

const SUBSQUID_NETWORK =
process.env["SUBSQUID_NETWORK_KUSAMA_ASSETHUB"] || "asset-hub-kusama";

const START_BLOCK = process.env["START_BLOCK_KUSAMA_ASSETHUB"]
? parseInt(process.env["START_BLOCK_KUSAMA_ASSETHUB"])
: 8806488;

const RPC_URL =
process.env["RPC_KUSAMA_ASSETHUB"] || "wss://statemine.api.onfinality.io/public-ws";

export const processor = new SubstrateBatchProcessor()
// Lookup archive by the network name in Subsquid registry
// See https://docs.subsquid.io/substrate-indexing/supported-networks/
.setGateway(`https://v2.archive.subsquid.io/network/${SUBSQUID_NETWORK}`)
// Chain RPC endpoint is required on Substrate for metadata and real-time updates
.setRpcEndpoint({
// Set via .env for local runs or via secrets when deploying to Subsquid Cloud
// https://docs.subsquid.io/deploy-squid/env-variables/
url: RPC_URL,
// More RPC connection options at https://docs.subsquid.io/substrate-indexing/setup/general/#set-data-source
rateLimit: 10,
})
.setBlockRange({
from: START_BLOCK,
})
.addEvent({
name: [
events.messageQueue.processed.name,
events.messageQueue.processingFailed.name,
events.polkadotXcm.sent.name,
events.foreignAssets.burned.name,
events.foreignAssets.issued.name,
events.xcmpQueue.xcmpMessageSent.name,
],
extrinsic: true,
})
.setFields({
event: {
args: true,
},
extrinsic: {
hash: true,
fee: true,
},
block: {
timestamp: true,
},
});
// Uncomment to disable RPC ingestion and drastically reduce no of RPC calls
//.useArchiveOnly()

export type Fields = SubstrateBatchProcessorFields<typeof processor>;
export type Block = BlockHeader<Fields>;
export type Event = _Event<Fields>;
export type Call = _Call<Fields>;
export type Extrinsic = _Extrinsic<Fields>;
export type ProcessorContext<Store> = DataHandlerContext<Store, Fields>;
1 change: 1 addition & 0 deletions src/kusama/assethub/types/calls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as polkadotXcm from './polkadot-xcm/calls'
4 changes: 4 additions & 0 deletions src/kusama/assethub/types/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * as xcmpQueue from './xcmp-queue/events'
export * as polkadotXcm from './polkadot-xcm/events'
export * as foreignAssets from './foreign-assets/events'
export * as messageQueue from './message-queue/events'
Loading