Skip to content

Commit

Permalink
Merge pull request #51 from subquery/update/dictionary-mapping
Browse files Browse the repository at this point in the history
Convert Promise.all to for loop
  • Loading branch information
jiqiang90 authored Nov 8, 2023
2 parents 912322a + 01098dc commit eebb863
Show file tree
Hide file tree
Showing 28 changed files with 178 additions and 420 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea/
.idea/
.DS_Store
1 change: 1 addition & 0 deletions aleph-zero/.project-cid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Qmdna69rQRECMJRSPsqeRAaYdWoCGBQWp3F8u4jyKuoBQb
5 changes: 4 additions & 1 deletion aleph-zero/project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ version: 1.0.0
runner:
node:
name: '@subql/node'
version: '*'
version: '>=3.0.0'
options:
unfinalizedBlocks: false
historical: false
query:
name: '@subql/query'
version: '*'
Expand Down
33 changes: 18 additions & 15 deletions aleph-zero/src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,30 @@ export async function handleBlock(block: SubstrateBlock): Promise<void> {

// Process all events in block
const events = block.events
.filter(
(evt) =>
!(
evt.event.section === "system" &&
evt.event.method === "ExtrinsicSuccess"
)
)
.map((evt, idx) =>
handleEvent(block.block.header.number.toString(), idx, evt)
);
.filter(
(evt) =>
!(
evt.event.section === "system" &&
evt.event.method === "ExtrinsicSuccess"
)
)
.map((evt, idx) =>
handleEvent(block.block.header.number.toString(), idx, evt)
);

// Process all calls in block
const calls = wrapExtrinsics(block).map((ext, idx) =>
handleCall(`${block.block.header.number.toString()}-${idx}`, ext)
handleCall(`${block.block.header.number.toString()}-${idx}`, ext)
);

// Save all data
await Promise.all([
store.bulkCreate("Event", events),
store.bulkCreate("Extrinsic", calls),
]);
// All save order should always follow this structure
for (const event of events) {
await event.save()
}
for (const call of calls) {
await call.save()
}
}

function handleEvent(
Expand Down
2 changes: 1 addition & 1 deletion astar/.project_shiden-cid
Original file line number Diff line number Diff line change
@@ -1 +1 @@
QmRrdfmc1p5FYQovtTPK3twvW2woT3EukRo38kU8hMSsYg
QmctexaEReioxaCK7FYwz653fU8iyByMFzf6Ztx9Yg1Z3w
5 changes: 4 additions & 1 deletion astar/project_shiden.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ version: 1.0.0
runner:
node:
name: '@subql/node'
version: '*'
version: '>=3.0.0'
options:
unfinalizedBlocks: false
historical: false
query:
name: '@subql/query'
version: '*'
Expand Down
29 changes: 19 additions & 10 deletions astar/src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,25 @@ export async function handleBlock(block: SubstrateBlock): Promise<void> {
// seems there is a concurrent limitation for promise.all and bulkCreate work together,
// the last entity upsertion are missed
// We will put them into two promise for now.
await Promise.all([
store.bulkCreate('Event', events),
store.bulkCreate('ContractEmitted', contractEmittedEvents),
store.bulkCreate('EvmLog', evmLogs),
]);
await Promise.all([
store.bulkCreate('Extrinsic', calls),
store.bulkCreate('ContractsCall', contractCalls),
store.bulkCreate('EvmTransaction', evmTransactions)
]);
// All save order should always follow this structure
for (const event of events) {
await event.save()
}
for (const call of calls) {
await call.save()
}
for (const evmLog of evmLogs) {
await evmLog.save()
}
for (const evmTransaction of evmTransactions) {
await evmTransaction.save()
}
for (const contractEmittedEvent of contractEmittedEvents) {
await contractEmittedEvent.save()
}
for (const contractCall of contractCalls) {
await contractCall.save()
}
}

export function handleEvent(event: SubstrateEvent): Event {
Expand Down
2 changes: 1 addition & 1 deletion bifrost-parachain/.project-cid
Original file line number Diff line number Diff line change
@@ -1 +1 @@
QmX5dnwJsZf1nvVTNwJKQBpsNmzRCAZRrQfPFT283Zm3i2
QmZGzbBmKYX2GLQQg2burWfPCgHpMy97Nb2x7Jbtr4znUQ
1 change: 1 addition & 0 deletions calamari/.project-cid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
QmcsYi6v2yCezutCt4p8gWHqn9NsFYEYY3pV4DudEZeAVZ
5 changes: 4 additions & 1 deletion calamari/project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ version: 1.0.0
runner:
node:
name: '@subql/node'
version: '*'
version: '>=3.0.0'
options:
unfinalizedBlocks: false
historical: false
query:
name: '@subql/query'
version: '*'
Expand Down
45 changes: 26 additions & 19 deletions calamari/src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ export async function handleBlock(block: SubstrateBlock): Promise<void> {

// Check for updates to Spec Version
if (!specVersion || specVersion.id !== block.specVersion.toString()) {
specVersion = new SpecVersion(block.specVersion.toString());
specVersion.blockHeight = block.block.header.number.toBigInt();
specVersion = SpecVersion.create({
id: block.specVersion.toString(),
blockHeight: block.block.header.number.toBigInt()
});
await specVersion.save();
}

Expand All @@ -33,33 +35,38 @@ export async function handleBlock(block: SubstrateBlock): Promise<void> {
);

// Save all data
await Promise.all([
store.bulkCreate("Event", events),
store.bulkCreate("Extrinsic", calls),
]);
// All save order should always follow this structure
for (const event of events) {
await event.save()
}
for (const call of calls) {
await call.save()
}
}

function handleEvent(
blockNumber: string,
eventIdx: number,
event: EventRecord
): Event {
const newEvent = new Event(`${blockNumber}-${eventIdx}`);
newEvent.blockHeight = BigInt(blockNumber);
newEvent.module = event.event.section;
newEvent.event = event.event.method;
return newEvent;
return Event.create({
id: `${blockNumber}-${eventIdx}`,
blockHeight : BigInt(blockNumber),
module : event.event.section,
event : event.event.method,
});
}

function handleCall(idx: string, extrinsic: SubstrateExtrinsic): Extrinsic {
const newExtrinsic = new Extrinsic(idx);
newExtrinsic.txHash = extrinsic.extrinsic.hash.toString();
newExtrinsic.module = extrinsic.extrinsic.method.section;
newExtrinsic.call = extrinsic.extrinsic.method.method;
newExtrinsic.blockHeight = extrinsic.block.block.header.number.toBigInt();
newExtrinsic.success = extrinsic.success;
newExtrinsic.isSigned = extrinsic.extrinsic.isSigned;
return newExtrinsic;
return Extrinsic.create({
id: idx,
txHash : extrinsic.extrinsic.hash.toString(),
module : extrinsic.extrinsic.method.section,
call : extrinsic.extrinsic.method.method,
blockHeight : extrinsic.block.block.header.number.toBigInt(),
success : extrinsic.success,
isSigned : extrinsic.extrinsic.isSigned,
});
}

function wrapExtrinsics(wrappedBlock: SubstrateBlock): SubstrateExtrinsic[] {
Expand Down
1 change: 1 addition & 0 deletions kilt-spiritnet/.project-cid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
QmXcbyJ77Zv2YPe2yRoMVwasCjYof6h2DoHPxvYzxb8wJt
5 changes: 4 additions & 1 deletion kilt-spiritnet/project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ version: 1.0.0
runner:
node:
name: '@subql/node'
version: '*'
version: '>=3.0.0'
options:
unfinalizedBlocks: false
historical: false
query:
name: '@subql/query'
version: '*'
Expand Down
11 changes: 7 additions & 4 deletions kilt-spiritnet/src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ export async function handleBlock(block: SubstrateBlock): Promise<void> {
);

// Save all data
await Promise.all([
store.bulkCreate("Event", events),
store.bulkCreate("Extrinsic", calls),
]);
// All save order should always follow this structure
for (const event of events) {
await event.save()
}
for (const call of calls) {
await call.save()
}
}

function handleEvent(
Expand Down
7 changes: 0 additions & 7 deletions kilt-spiritnet/src/types/index.ts

This file was deleted.

106 changes: 0 additions & 106 deletions kilt-spiritnet/src/types/models/Event.ts

This file was deleted.

Loading

0 comments on commit eebb863

Please sign in to comment.