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
e5b73a9f
Unverified
Commit
e5b73a9f
authored
4 years ago
by
Maciej Hirsz
Committed by
GitHub
4 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #6 from paritytech/mh-extrinsics-extras
Extra data on blocks
parents
a9f03f87
bd9512e9
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/ApiHandler.ts
+55
-2
55 additions, 2 deletions
src/ApiHandler.ts
with
55 additions
and
2 deletions
src/ApiHandler.ts
+
55
−
2
View file @
e5b73a9f
...
...
@@ -16,6 +16,14 @@
import
{
ApiPromise
}
from
'
@polkadot/api
'
;
import
{
BlockHash
}
from
'
@polkadot/types/interfaces/rpc
'
;
import
{
Event
,
EventRecord
}
from
'
@polkadot/types/interfaces/system
'
;
import
{
blake2AsU8a
}
from
'
@polkadot/util-crypto
'
;
import
{
u8aToHex
}
from
'
@polkadot/util
'
;
interface
SantiziedEvent
{
method
:
string
;
data
:
any
;
}
export
default
class
ApiHandler
{
private
api
:
ApiPromise
;
...
...
@@ -26,7 +34,11 @@ export default class ApiHandler {
async
fetchBlock
(
hash
:
BlockHash
)
{
const
{
api
}
=
this
;
const
{
block
}
=
await
api
.
rpc
.
chain
.
getBlock
(
hash
);
const
[{
block
},
events
]
=
await
Promise
.
all
([
api
.
rpc
.
chain
.
getBlock
(
hash
),
this
.
fetchEvents
(
hash
),
]);
const
{
parentHash
,
number
,
stateRoot
,
extrinsicsRoot
}
=
block
.
header
;
const
logs
=
block
.
header
.
digest
.
logs
.
map
((
log
)
=>
{
...
...
@@ -34,17 +46,50 @@ export default class ApiHandler {
return
{
type
,
index
,
value
};
});
const
defaultSuccess
=
typeof
events
===
'
string
'
?
events
:
false
;
const
extrinsics
=
block
.
extrinsics
.
map
((
extrinsic
)
=>
{
const
{
method
,
nonce
,
signature
,
signer
,
isSigned
,
args
}
=
extrinsic
;
const
{
method
,
nonce
,
signature
,
signer
,
isSigned
,
tip
,
args
}
=
extrinsic
;
const
hash
=
u8aToHex
(
blake2AsU8a
(
extrinsic
.
toU8a
(),
256
));
return
{
method
:
`
${
method
.
sectionName
}
.
${
method
.
methodName
}
`
,
signature
:
isSigned
?
{
signature
,
signer
}
:
null
,
nonce
,
args
,
tip
,
hash
,
events
:
[]
as
SantiziedEvent
[],
success
:
defaultSuccess
,
};
});
if
(
Array
.
isArray
(
events
))
{
for
(
const
record
of
events
)
{
const
{
event
,
phase
}
=
record
;
if
(
phase
.
isApplyExtrinsic
)
{
const
extrinsicIdx
=
phase
.
asApplyExtrinsic
.
toNumber
();
const
extrinsic
=
extrinsics
[
extrinsicIdx
];
if
(
!
extrinsic
)
{
throw
new
Error
(
`Missing extrinsic
${
extrinsicIdx
}
in block
${
hash
}
`
);
}
const
method
=
`
${
event
.
section
}
.
${
event
.
method
}
`
;
if
(
method
===
'
system.ExtrinsicSuccess
'
)
{
extrinsic
.
success
=
true
;
}
extrinsic
.
events
.
push
({
method
:
`
${
event
.
section
}
.
${
event
.
method
}
`
,
data
:
event
.
data
,
});
}
}
}
return
{
number
,
hash
,
...
...
@@ -73,4 +118,12 @@ export default class ApiHandler {
return
{
at
,
free
,
reserved
,
locks
};
}
async
fetchEvents
(
hash
:
BlockHash
):
Promise
<
EventRecord
[]
|
string
>
{
try
{
return
await
await
this
.
api
.
query
.
system
.
events
.
at
(
hash
);
}
catch
(
_
)
{
return
'
Unable to fetch Events, cannot confirm extrinsic status. Check pruning settings on the node.
'
;
}
}
}
\ No newline at end of file
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