forked from muttoni/fcl-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend-get-block-by-height.js
45 lines (36 loc) · 1.55 KB
/
send-get-block-by-height.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import {invariant} from "@onflow/util-invariant"
import {GetBlockByHeightRequest, AccessAPI} from "@onflow/protobuf"
import {response} from "../response/response.js"
import {unary} from "./unary"
const u8ToHex = u8 => Buffer.from(u8).toString("hex")
export async function sendGetBlockByHeight(ix, opts = {}) {
invariant(opts.node, `SDK Send Get Block By Height Error: opts.node must be defined.`)
ix = await ix
const req = new GetBlockByHeightRequest()
req.setHeight(Number(ix.block.height))
const res = await unary(opts.node, AccessAPI.GetBlockByHeight, req)
const block = res.getBlock()
const collectionGuarantees = block.getCollectionGuaranteesList()
const blockSeals = block.getBlockSealsList()
const signatures = block.getSignaturesList()
const ret = response()
ret.tag = ix.tag
ret.block = {
id: u8ToHex(block.getId_asU8()),
parentId: u8ToHex(block.getParentId_asU8()),
height: block.getHeight(),
timestamp: block.getTimestamp(),
collectionGuarantees: collectionGuarantees.map(collectionGuarantee => ({
collectionId: u8ToHex(collectionGuarantee.getCollectionId_asU8()),
signatures: collectionGuarantee.getSignaturesList(),
})),
blockSeals: blockSeals.map(blockSeal => ({
blockId: u8ToHex(blockSeal.getBlockId_asU8()),
executionReceiptId: u8ToHex(blockSeal.getExecutionReceiptId_asU8()),
executionReceiptSignatures: blockSeal.getExecutionReceiptSignaturesList(),
resultApprovalSignatures: blockSeal.getResultApprovalSignaturesList(),
})),
signatures: signatures,
}
return ret
}