Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
substrate-api-sidecar
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Contributor analytics
CI/CD analytics
Repository analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
parity
Mirrored projects
substrate-api-sidecar
Commits
1cbaccb2
Commit
1cbaccb2
authored
4 years ago
by
Zeke Mostov
Browse files
Options
Downloads
Patches
Plain Diff
first try at using args struct
parent
d7a5be2f
Branches
Branches containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/ApiHandler.ts
+83
-43
83 additions, 43 deletions
src/ApiHandler.ts
with
83 additions
and
43 deletions
src/ApiHandler.ts
+
83
−
43
View file @
1cbaccb2
...
...
@@ -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
:
labelled
Args
,
args
:
new
Args
,
};
}
// 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,
// };
// }
}
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment