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

feat: added memo handler to contributions #9

Open
wants to merge 3 commits into
base: master
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
10 changes: 10 additions & 0 deletions project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ dataSources:
filter:
module: crowdloan
method: Contributed
- handler: handleCrowdloanMemo
kind: substrate/EventHandler
filter:
module: crowdloan
method: MemoUpdated
# - handler: handleCrowdloanAllRefunded
# kind: substrate/EventHandler
# filter:
Expand Down Expand Up @@ -90,6 +95,11 @@ dataSources:
filter:
module: crowdloan
method: Contributed
- handler: handleCrowdloanMemo
kind: substrate/EventHandler
filter:
module: crowdloan
method: MemoUpdated
# - handler: handleCrowdloanAllRefunded
# kind: substrate/EventHandler
# filter:
Expand Down
1 change: 1 addition & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type CrowdloanSequence @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-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Chronicle } from '../types/models/Chronicle';
import { ChronicleKey } from '../constants';
import { parseNumber } from '../utils';
import { CrowdloanStatus } from '../types';
import { Contribution } from '../types/index';

interface ParaInfo {
manager: string;
Expand Down Expand Up @@ -69,6 +70,24 @@ export const onCrowdloanContributed = async (substrateEvent: SubstrateEvent) =>
await Storage.save('Contribution', contribution);
};

export const onCrowdloanMemo = 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 onCrowdloanDissolved = async (substrateEvent: SubstrateEvent) => {
const { event, block } = substrateEvent;
const { timestamp, block: rawBlock } = block;
Expand Down
6 changes: 6 additions & 0 deletions src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
onParachainRegistered,
onCrowdloanCreated,
onCrowdloanContributed,
onCrowdloanMemo,
onCrowdloanDissolved,
} from '../handlers/parachain-handlers';
import { onSlotsLeased, onNewLeasePeriod } from '../handlers/lease-handlers';
Expand All @@ -33,6 +34,11 @@ export async function handleCrowdloanContributed(substrateEvent: SubstrateEvent)
// await onCrowdloanAllRefunded(substrateEvent);
// }

export async function handleCrowdloanMemo(substrateEvent: SubstrateEvent): Promise<void> {
getEventLogger(substrateEvent);
await onCrowdloanMemo(substrateEvent);
}

export async function handleCrowdloanDissolved(substrateEvent: SubstrateEvent): Promise<void> {
getEventLogger(substrateEvent);
await onCrowdloanDissolved(substrateEvent);
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 getByParachainId(parachainId: string): Promise<Contribution[] | undefined>{

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