Skip to content
Snippets Groups Projects
Commit 1cbaccb2 authored by Zeke Mostov's avatar Zeke Mostov
Browse files

first try at using args struct

parent d7a5be2f
Branches
No related merge requests found
......@@ -26,6 +26,7 @@ import { u32 } from '@polkadot/types/primitive';
import { DispatchInfo } from '@polkadot/types/interfaces';
import { CalcFee } from '@polkadot/calc-fee'
import { Codec } from '@polkadot/types/types';
import { Struct } from '@polkadot/types';
interface SanitizedEvent {
method: string;
......@@ -501,59 +502,98 @@ export default class ApiHandler {
return api;
}
private isGenericCallArray(arrayToCheck: any): boolean {
return Array.isArray(arrayToCheck) && arrayToCheck.every((element: any) =>
element instanceof GenericCall
)
}
private parseGenericCall(genericCall: GenericCall): SanitizedCall {
const { args, sectionName, methodName, callIndex } = genericCall;
// Pull out the labelled args since we want the labels
let labelledArgs = JSON.parse(genericCall.toString()).args;
// If labelledArgs has a `call` or `calls` field, we then go look for it in
// the original args field where it will be an instance of a GenericCall or
// an array of GenericCall's. The GenericCall(s) are then recursively
// sanitized by this function.
// A possible pitfall to look out for here is if a call or array of calls
// does not have the parameter name `call` or `calls`.
if (labelledArgs.call !== undefined) {
const genericCallArg = args.find((argument: any): boolean =>
argument instanceof GenericCall
);
if (genericCallArg !== undefined) {
labelledArgs.call = this.parseGenericCall(genericCallArg);
} else {
console.error(
`A Call was expected as an argument to ${sectionName}.${methodName}, but could not be found.`
)
private parseArrayGenericCalls(argsArray: any[]): any[] {
return argsArray.map((argument) => {
if (argument instanceof GenericCall){
return this.parseGenericCall(argument);
}
}
if (labelledArgs.calls !== undefined) {
const genericCallsArg = args.find((argument: Codec): boolean =>
this.isGenericCallArray(argument)
)
return argument;
})
}
if (genericCallsArg !== undefined && Array.isArray(genericCallsArg)) {
labelledArgs.calls = genericCallsArg.map(
(c: GenericCall): SanitizedCall => this.parseGenericCall(c)
);
} else {
console.error(
`An array of Calls was expected as an argument to ${sectionName}.${methodName}, but could not be found.`
)
private parseGenericCall(genericCall: GenericCall): SanitizedCall {
const {sectionName, methodName, callIndex } = genericCall;
const newArgs = {};
// Pull out the struct of arguments to this call
const callArgs = genericCall.get("args") as Struct;
if (callArgs && callArgs.defKeys){
// paramName is a string
for (const paramName of callArgs.defKeys) {
const argument = callArgs.get(paramName);
if (Array.isArray(argument)) {
newArgs[paramName] = this.parseArrayGenericCalls(argument);
} else if (argument instanceof GenericCall) {
newArgs[paramName] = this.parseGenericCall(argument);
} else {
newArgs[paramName] = argument;
}
}
}
return {
method: `${sectionName}.${methodName}`,
callIndex,
args: labelledArgs,
args: newArgs,
};
}
// private isGenericCallArray(arrayToCheck: any): boolean {
// return Array.isArray(arrayToCheck) && arrayToCheck.every((element: any) =>
// element instanceof GenericCall
// )
// }
// private parseGenericCall(genericCall: GenericCall): SanitizedCall {
// const { args, sectionName, methodName, callIndex } = genericCall;
// // Pull out the labelled args since we want the labels
// let labelledArgs = JSON.parse(genericCall.toString()).args;
// // If labelledArgs has a `call` or `calls` field, we then go look for it in
// // the original args field where it will be an instance of a GenericCall or
// // an array of GenericCall's. The GenericCall(s) are then recursively
// // sanitized by this function.
// // A possible pitfall to look out for here is if a call or array of calls
// // does not have the parameter name `call` or `calls`.
// if (labelledArgs.call !== undefined) {
// const genericCallArg = args.find((argument: any): boolean =>
// argument instanceof GenericCall
// );
// if (genericCallArg instanceof GenericCall) {
// debugger
// labelledArgs.call = this.parseGenericCall(genericCallArg);
// } else {
// console.error(
// `A Call was expected as an argument to ${sectionName}.${methodName}, but could not be found.`
// )
// }
// }
// if (labelledArgs.calls !== undefined) {
// const genericCallsArg = args.find((argument: Codec): boolean =>
// this.isGenericCallArray(argument)
// )
// if (genericCallsArg !== undefined && Array.isArray(genericCallsArg)) {
// labelledArgs.calls = genericCallsArg.map(
// (c: GenericCall): SanitizedCall => this.parseGenericCall(c)
// );
// } else {
// console.error(
// `An array of Calls was expected as an argument to ${sectionName}.${methodName}, but could not be found.`
// )
// }
// }
// return {
// method: `${sectionName}.${methodName}`,
// callIndex,
// args: labelledArgs,
// };
// }
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment