Skip to content
This repository has been archived by the owner on Jul 28, 2022. It is now read-only.

feat(memo): added memo handler to contributions #22

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions src/handlers/parachain-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(`Adding memo ${memo} for contributor ${contributor} at block ${blockNum}`);
await Storage.save('Contribution', latestContributionBeforeMemo);
};

export const updateCrowdloanStatus = async (block: SubstrateBlock) => {
const blockNum = block.block.header.number.toNumber();
const funds = (await Crowdloan.getByIsFinished(false)) || [];
Expand Down
5 changes: 3 additions & 2 deletions src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -36,6 +36,7 @@ const eventsMapping = {
'slots/Leased': handleSlotsLeased,
'slots/NewLeasePeriod': handleNewLeasePeriod,
'crowdloan/Contributed': handleCrowdloanContributed,
'crowdloan/MemoUpdated': handleCrowdloanMemo,
'crowdloan/Dissolved': handleCrowdloanDissolved
};

Expand Down
9 changes: 9 additions & 0 deletions src/types/models/Contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export class Contribution implements Entity {

public account: string;

public memo?: string;

public parachainId: string;

public fundId: string;
Expand Down Expand Up @@ -53,6 +55,13 @@ export class Contribution implements Entity {

}

static async getByMemo(memo: string): Promise<Contribution[] | undefined>{

const records = await store.getByField('Contribution', 'memo', memo);
return records.map(record => Contribution.create(record));

}

static async getByAmount(amount: bigint): Promise<Contribution[] | undefined>{

const records = await store.getByField('Contribution', 'amount', amount);
Expand Down