-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathresolve.js
122 lines (110 loc) · 3.9 KB
/
resolve.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import {pipe, isTransaction} from "../interaction/interaction"
import {config} from "@onflow/config"
import {invariant} from "@onflow/util-invariant"
import {Buffer} from "@onflow/rlp"
import {send as defaultSend} from "@onflow/transport-http"
import * as ixModule from "../interaction/interaction"
import {response} from "../response/response"
import {build} from "../build/build.js"
import {getBlock} from "../build/build-get-block.js"
import {getAccount} from "../build/build-get-account.js"
import {decodeResponse as decode} from "../decode/decode.js"
import {resolveCadence} from "./resolve-cadence.js"
import {resolveArguments} from "./resolve-arguments.js"
import {resolveAccounts} from "./resolve-accounts"
import {resolveSignatures} from "./resolve-signatures"
import {resolveValidators} from "./resolve-validators.js"
import {resolveFinalNormalization} from "./resolve-final-normalization.js"
import {resolveVoucherIntercept} from "./resolve-voucher-intercept.js"
import {resolveComputeLimit} from "./resolve-compute-limit.js"
const noop = v => v
const debug =
(key, fn = noop) =>
async ix => {
const take = (obj, keys = []) => {
if (typeof keys === "string") keys = keys.split(" ")
keys.reduce((acc, key) => ({...acc, [key]: obj[key]}), {})
}
const accts = ix =>
[
"\nAccounts:",
{
proposer: ix.proposer,
authorizations: ix.authorizations,
payer: ix.payer,
},
"\n\nDetails:",
ix.accounts,
].filter(Boolean)
const log = (...msg) => {
console.log(`debug[${key}] ---\n`, ...msg, "\n\n\n---")
}
if (await config.get(`debug.${key}`)) await fn(ix, log, accts)
return ix
}
export const resolve = pipe([
resolveCadence,
debug("cadence", (ix, log) => log(ix.message.cadence)),
resolveComputeLimit,
debug("compute limit", (ix, log) => log(ix.message.computeLimit)),
resolveArguments,
debug("arguments", (ix, log) => log(ix.message.arguments, ix.message)),
resolveAccounts,
debug("accounts", (ix, log, accts) => log(...accts(ix))),
/* special */ execFetchRef,
/* special */ execFetchSequenceNumber,
resolveSignatures,
debug("signatures", (ix, log, accts) => log(...accts(ix))),
resolveFinalNormalization,
resolveValidators,
resolveVoucherIntercept,
debug("resolved", (ix, log) => log(ix)),
])
async function execFetchRef(ix) {
if (isTransaction(ix) && ix.message.refBlock == null) {
const node = await config().get("accessNode.api")
const sendFn = await config.first(
["sdk.transport", "sdk.send"],
defaultSend
)
invariant(
sendFn,
`Required value for sdk.transport is not defined in config. See: ${"https://github.com/onflow/fcl-js/blob/master/packages/sdk/CHANGELOG.md#0057-alpha1----2022-01-21"}`
)
ix.message.refBlock = (
await sendFn(
build([getBlock()]),
{config, response, Buffer, ix: ixModule},
{node}
).then(decode)
).id
}
return ix
}
async function execFetchSequenceNumber(ix) {
if (isTransaction(ix)) {
var acct = Object.values(ix.accounts).find(a => a.role.proposer)
invariant(acct, `Transactions require a proposer`)
if (acct.sequenceNum == null) {
const node = await config().get("accessNode.api")
const sendFn = await config.first(
["sdk.transport", "sdk.send"],
defaultSend
)
invariant(
sendFn,
`Required value for sdk.transport is not defined in config. See: ${"https://github.com/onflow/fcl-js/blob/master/packages/sdk/CHANGELOG.md#0057-alpha1----2022-01-21"}`
)
ix.accounts[acct.tempId].sequenceNum = await sendFn(
await build([getAccount(acct.addr)]),
{config, response, Buffer, ix: ixModule},
{node}
)
.then(decode)
.then(acct => acct.keys)
.then(keys => keys.find(key => key.index === acct.keyId))
.then(key => key.sequenceNumber)
}
}
return ix
}