Skip to content

Commit

Permalink
feat: 🎸 Add instruction attributes to AssetTransaction entity
Browse files Browse the repository at this point in the history
This adds `instruction` and `instruction_memo` to `AssetTransaction`
  • Loading branch information
prashantasdeveloper authored and quinndiggitypolymath committed Jan 18, 2024
1 parent 923a090 commit c15a7a8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
4 changes: 4 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2133,6 +2133,10 @@ type AssetTransaction @entity {
extrinsicIdx: Int
"`fundingRound` will only be present for the cases where Assets are issued with a fundingRound name"
fundingRound: String
"`instruction` will only be present for the cases where Assets are transferred once instruction is executed"
instruction: Instruction
"`instructionMemo` will only be present for the cases where memo is provided for the instruction which got executed"
instructionMemo: String
datetime: Date!
createdBlock: Block!
updatedBlock: Block!
Expand Down
35 changes: 33 additions & 2 deletions src/mappings/entities/mapAsset.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Option, u64, U8aFixed } from '@polkadot/types-codec';
import { Codec } from '@polkadot/types/types';
import { SubstrateBlock, SubstrateExtrinsic } from '@subql/types';
import {
Expand Down Expand Up @@ -28,7 +29,7 @@ import {
getTextValue,
serializeTicker,
} from '../util';
import { HandlerArgs, getAsset } from './common';
import { getAsset, HandlerArgs } from './common';

export const createFunding = (
blockId: string,
Expand Down Expand Up @@ -57,7 +58,14 @@ export const createAssetTransaction = (
datetime: Date,
details: Pick<
AssetTransaction,
'assetId' | 'toPortfolioId' | 'fromPortfolioId' | 'amount' | 'fundingRound' | 'nftIds'
| 'assetId'
| 'toPortfolioId'
| 'fromPortfolioId'
| 'amount'
| 'fundingRound'
| 'nftIds'
| 'instructionId'
| 'instructionMemo'
>,
extrinsic?: SubstrateExtrinsic
): Promise<void> => {
Expand Down Expand Up @@ -388,6 +396,8 @@ const handleAssetTransfer = async ({
toPortfolioId = null;
}

let instructionId: string;

if (fromPortfolioId && toPortfolioId) {
const asset = await getAsset(ticker);
asset.totalTransfers += BigInt(1);
Expand All @@ -406,6 +416,14 @@ const handleAssetTransfer = async ({
toHolder.amount = toHolder.amount + transferAmount;
toHolder.updatedBlockId = blockId;
promises.push(toHolder.save());

// For old `Transfer` events, `InstructionExecuted` event was separately emitted in the same block
const instructionExecutedEvent = block.events.find(
({ event }) => event.method === 'InstructionExecuted'
);
if (instructionExecutedEvent) {
instructionId = getTextValue(instructionExecutedEvent.event.data[1]);
}
}

promises.push(
Expand All @@ -418,6 +436,7 @@ const handleAssetTransfer = async ({
fromPortfolioId,
toPortfolioId,
amount: transferAmount,
instructionId,
},
extrinsic
)
Expand Down Expand Up @@ -445,6 +464,8 @@ const handleAssetBalanceUpdated = async ({
let fromPortfolioId: string;
let toPortfolioId: string;
let fundingRoundName: string;
let instructionId: string;
let instructionMemo: string;

const promises = [];

Expand Down Expand Up @@ -501,6 +522,14 @@ const handleAssetBalanceUpdated = async ({
asset.updatedBlockId = blockId;
promises.push(asset.save());
} else if (updateReason === 'transferred') {
const details = value as unknown as {
readonly instructionId: Option<u64>;
readonly instructionMemo: Option<U8aFixed>;
};

instructionId = getTextValue(details.instructionId);
instructionMemo = bytesToString(details.instructionMemo);

asset.totalTransfers += BigInt(1);
asset.updatedBlockId = blockId;
promises.push(asset.save());
Expand All @@ -517,6 +546,8 @@ const handleAssetBalanceUpdated = async ({
toPortfolioId,
amount: transferAmount,
fundingRound: fundingRoundName,
instructionId,
instructionMemo,
},
extrinsic
)
Expand Down
19 changes: 18 additions & 1 deletion src/mappings/entities/mapNfts.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { Option, u64, U8aFixed } from '@polkadot/types-codec';
import { Codec } from '@polkadot/types/types';
import { SubstrateBlock } from '@subql/types';
import { EventIdEnum, ModuleIdEnum, NftHolder } from '../../types';
import {
bytesToString,
getFirstKeyFromJson,
getFirstValueFromJson,
getNftId,
getPortfolioValue,
getTextValue,
serializeTicker,
} from '../util';
import { HandlerArgs, getAsset } from './common';
import { getAsset, HandlerArgs } from './common';
import { createAssetTransaction } from './mapAsset';

export const getNftHolder = async (
Expand Down Expand Up @@ -70,12 +73,16 @@ const handleNftPortfolioUpdates = async (

const did = getTextValue(rawId);
const reason = getFirstKeyFromJson(rawUpdateReason);
const value = getFirstValueFromJson(rawUpdateReason);

const { ticker, ids } = getNftId(rawNftId);

const asset = await getAsset(ticker);
asset.updatedBlockId = blockId;

let instructionId: string;
let instructionMemo: string;

if (reason === 'issued') {
asset.totalSupply += BigInt(ids.length);

Expand All @@ -93,6 +100,14 @@ const handleNftPortfolioUpdates = async (
promises.push(fromHolder.save(), toHolder.save());

asset.totalTransfers += BigInt(1);

const details = value as unknown as {
readonly instructionId: Option<u64>;
readonly instructionMemo: Option<U8aFixed>;
};

instructionId = getTextValue(details.instructionId);
instructionMemo = bytesToString(details.instructionMemo);
} else if (reason === 'redeemed') {
asset.totalSupply -= BigInt(ids.length);

Expand All @@ -108,6 +123,8 @@ const handleNftPortfolioUpdates = async (
fromPortfolioId,
toPortfolioId,
nftIds: ids.map(id => BigInt(id)),
instructionId,
instructionMemo,
})
);
promises.push(asset.save());
Expand Down

0 comments on commit c15a7a8

Please sign in to comment.