Skip to content

Commit

Permalink
feat(get-rmrks): [as-derivative]
Browse files Browse the repository at this point in the history
Get RMRKs from `utility.asDerivative` calls.
Attribute call to derivative account
  • Loading branch information
danforbes committed Jun 8, 2021
1 parent dee0587 commit 115598e
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/tools/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { SignedBlock } from "@polkadot/types/interfaces/runtime";
import { BlockCall, BlockCalls } from "./types";
import { Call as TCall } from "@polkadot/types/interfaces";
import { BlockHash } from "@polkadot/types/interfaces/chain";
import { encodeAddress } from "@polkadot/util-crypto";
import { encodeAddress, encodeDerivedAddress } from "@polkadot/util-crypto";
import { NFT } from "../rmrk1.0.0/classes/nft";

export const getApi = async (wsEndpoint: string): Promise<ApiPromise> => {
Expand Down Expand Up @@ -150,6 +150,22 @@ export const isUtilityBatch = (call: TCall) =>
call.section === "utility" &&
(call.method === "batch" || call.method === "batchAll");

export const hasInnerCall = (call: TCall): boolean =>
call.section === "utility" && call.method === "asDerivative";

export const getInnerCall = (call: TCall) => {
const sectionMethod = `${call.section}.${call.method}`;
switch (sectionMethod) {
case ("utility.asDerivative"): {
return { call: call.args[1], derivedAcct: parseInt(call.args[0].toString()) };
}

default: {
throw new Error(`Cannot get inner call from ${sectionMethod}.`);
}
}
};

export const getBlockCallsFromSignedBlock = async (
signedBlock: SignedBlock,
prefixes: string[],
Expand Down Expand Up @@ -226,6 +242,34 @@ export const getBlockCallsFromSignedBlock = async (

blockCalls = blockCalls.concat(batchRoot);
}
} else if (hasInnerCall(extrinsic.method as TCall)) {
const innerCall = getInnerCall(extrinsic.method as TCall);
const derivedAccts: number[] = [];
if (innerCall.derivedAcct !== undefined) {
derivedAccts.push(innerCall.derivedAcct);
}

let method = api.createType("Call", innerCall.call);
while (hasInnerCall(method)) {
const innerCall = getInnerCall(method);
method = api.createType("Call", innerCall.call);
if (innerCall.derivedAcct !== undefined) {
derivedAccts.push(innerCall.derivedAcct);
}
}

if (isSystemRemark(method, prefixes)) {
let address = encodeAddress(extrinsic.signer.toString(), ss58Format);
derivedAccts.forEach((acctIdx) => {
address = encodeDerivedAddress(address, acctIdx, ss58Format);
})

blockCalls.push({
call: "system.remark",
value: method.args.toString(),
caller: address,
});
}
}
extrinsicIndex++;
}
Expand Down

0 comments on commit 115598e

Please sign in to comment.