From 81aca43d1987a96078355edb978de0d28f8511ac Mon Sep 17 00:00:00 2001 From: Theodore Bugnet Date: Mon, 6 Sep 2021 16:00:32 +0100 Subject: [PATCH 1/2] feat(memo): added memo handler to contributions --- schema.graphql | 1 + src/handlers/parachain-handler.ts | 19 +++++++++++++++++++ src/mappings/mappingHandlers.ts | 5 +++-- src/types/models/Contribution.ts | 9 +++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/schema.graphql b/schema.graphql index 482238b..74a9590 100644 --- a/schema.graphql +++ b/schema.graphql @@ -104,6 +104,7 @@ type Bid @entity { type Contribution @entity { id: ID! account: String! @index + memo: String @index parachain: Parachain! fund: Crowdloan! amount: BigInt! @index diff --git a/src/handlers/parachain-handler.ts b/src/handlers/parachain-handler.ts index 556825b..bee6565 100644 --- a/src/handlers/parachain-handler.ts +++ b/src/handlers/parachain-handler.ts @@ -3,6 +3,7 @@ import * as Storage from '../services/storage'; import { Crowdloan } from '../types/models/Crowdloan'; import { parseNumber } from '../utils'; import { CrowdloanStatus } from '../types'; +import {Contribution} from '../types/index'; interface ParaInfo { manager: string; @@ -65,6 +66,24 @@ export const handleCrowdloanContributed = async (substrateEvent: SubstrateEvent) await Storage.save('Contribution', contribution); }; +export const handleCrowdloanMemo = async (substrateEvent: SubstrateEvent) => { + const {event, block} = substrateEvent; + const {block: rawBlock} = block; + + const blockNum = rawBlock.header.number.toNumber(); + const [contributor, fundIdx, memo] = event.data.toJSON() as [string, number, string]; + await Storage.ensureParachain(fundIdx); + await Storage.ensureFund(fundIdx); + + const contributions = await Contribution.getByAccount(contributor); + const latestContributionBeforeMemo = contributions.filter((contrib) => contrib.blockNum <= blockNum).sort((a, b) => a.blockNum - b.blockNum).pop(); + + if (!latestContributionBeforeMemo) return; + latestContributionBeforeMemo.memo = memo; + logger.info(`memo for ${JSON.stringify(latestContributionBeforeMemo, null, 2)}`); + await Storage.save('Contribution', latestContributionBeforeMemo); +}; + export const updateCrowdloanStatus = async (block: SubstrateBlock) => { const blockNum = block.block.header.number.toNumber(); const funds = (await Crowdloan.getByIsFinished(false)) || []; diff --git a/src/mappings/mappingHandlers.ts b/src/mappings/mappingHandlers.ts index d6154b5..c8dc2b6 100644 --- a/src/mappings/mappingHandlers.ts +++ b/src/mappings/mappingHandlers.ts @@ -1,11 +1,11 @@ -import { SignedBlock } from '@polkadot/types/interfaces'; -import { SubstrateExtrinsic, SubstrateEvent } from '@subql/types'; +import { SubstrateEvent } from '@subql/types'; import { SubstrateBlock } from '@subql/types'; import { handleCrowdloanContributed, handleCrowdloanCreated, handleCrowdloanDissolved, + handleCrowdloanMemo, handleParachainRegistered, updateCrowdloanStatus } from '../handlers/parachain-handler'; @@ -36,6 +36,7 @@ const eventsMapping = { 'slots/Leased': handleSlotsLeased, 'slots/NewLeasePeriod': handleNewLeasePeriod, 'crowdloan/Contributed': handleCrowdloanContributed, + 'crowdloan/MemoUpdated': handleCrowdloanMemo, 'crowdloan/Dissolved': handleCrowdloanDissolved }; diff --git a/src/types/models/Contribution.ts b/src/types/models/Contribution.ts index 6e78c6b..02c190b 100644 --- a/src/types/models/Contribution.ts +++ b/src/types/models/Contribution.ts @@ -14,6 +14,8 @@ export class Contribution implements Entity { public account: string; + public memo?: string; + public parachainId: string; public fundId: string; @@ -53,6 +55,13 @@ export class Contribution implements Entity { } + static async getByMemo(memo: string): Promise{ + + const records = await store.getByField('Contribution', 'memo', memo); + return records.map(record => Contribution.create(record)); + + } + static async getByAmount(amount: bigint): Promise{ const records = await store.getByField('Contribution', 'amount', amount); From 766f7be51cf298be6d0e6b38e74775dd2af0c04a Mon Sep 17 00:00:00 2001 From: Gregory Hill Date: Thu, 6 Jan 2022 15:32:38 +0000 Subject: [PATCH 2/2] fix: memo logging Signed-off-by: Gregory Hill --- src/handlers/parachain-handler.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/handlers/parachain-handler.ts b/src/handlers/parachain-handler.ts index bee6565..6f7cfec 100644 --- a/src/handlers/parachain-handler.ts +++ b/src/handlers/parachain-handler.ts @@ -3,7 +3,7 @@ import * as Storage from '../services/storage'; import { Crowdloan } from '../types/models/Crowdloan'; import { parseNumber } from '../utils'; import { CrowdloanStatus } from '../types'; -import {Contribution} from '../types/index'; +import { Contribution } from '../types/index'; interface ParaInfo { manager: string; @@ -67,8 +67,8 @@ export const handleCrowdloanContributed = async (substrateEvent: SubstrateEvent) }; export const handleCrowdloanMemo = async (substrateEvent: SubstrateEvent) => { - const {event, block} = substrateEvent; - const {block: rawBlock} = block; + const { event, block } = substrateEvent; + const { block: rawBlock } = block; const blockNum = rawBlock.header.number.toNumber(); const [contributor, fundIdx, memo] = event.data.toJSON() as [string, number, string]; @@ -80,7 +80,7 @@ export const handleCrowdloanMemo = async (substrateEvent: SubstrateEvent) => { if (!latestContributionBeforeMemo) return; latestContributionBeforeMemo.memo = memo; - logger.info(`memo for ${JSON.stringify(latestContributionBeforeMemo, null, 2)}`); + logger.info(`Adding memo ${memo} for contributor ${contributor} at block ${blockNum}`); await Storage.save('Contribution', latestContributionBeforeMemo); };