Skip to content

Commit 83d4222

Browse files
authored
PKG -- [typedefs] Switch interaction-related constants to enums (#1816)
1 parent 699303c commit 83d4222

File tree

6 files changed

+139
-143
lines changed

6 files changed

+139
-143
lines changed

package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/sdk/src/build/build-authorizations.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import {pipe, prepAccount, AUTHORIZER} from "../interaction/interaction"
1+
import { TransactionRole } from "@onflow/typedefs"
2+
import {pipe, prepAccount} from "../interaction/interaction"
23

34
export function authorizations(ax = []) {
45
return pipe(
56
ax.map(authz => {
6-
return prepAccount(authz, {role: AUTHORIZER})
7+
return prepAccount(authz, {role: TransactionRole.AUTHORIZER})
78
})
89
)
910
}

packages/sdk/src/build/build-payer.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import {pipe, prepAccount, PAYER} from "../interaction/interaction"
1+
import { TransactionRole } from "@onflow/typedefs"
2+
import {pipe, prepAccount} from "../interaction/interaction"
23

34
export function payer(ax = []) {
45
if (!Array.isArray(ax)) ax = [ax]
56
return pipe(
67
ax.map(authz => {
7-
return prepAccount(authz, {role: PAYER})
8+
return prepAccount(authz, {role: TransactionRole.PAYER})
89
})
910
)
1011
}
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import {prepAccount, PROPOSER} from "../interaction/interaction"
1+
import { TransactionRole } from "@onflow/typedefs"
2+
import {prepAccount} from "../interaction/interaction"
23

34
export async function proposer(authz) {
4-
return prepAccount(authz, {role: PROPOSER})
5+
return prepAccount(authz, {role: TransactionRole.PROPOSER})
56
}

packages/sdk/src/interaction/interaction.ts

+44-57
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import {invariant} from "@onflow/util-invariant"
22
import {v4 as uuidv4} from "uuid"
33
import {log, LEVELS} from "@onflow/util-logger"
44

5-
import { InteractionAccount, ACCOUNT, PARAM, ARGUMENT, UNKNOWN, OK, Interaction, AUTHORIZER, PAYER, SCRIPT, TRANSACTION, GET_TRANSACTION_STATUS, GET_TRANSACTION, GET_ACCOUNT, GET_EVENTS, PING, GET_BLOCK, GET_BLOCK_HEADER, GET_COLLECTION, GET_NETWORK_PARAMETERS, BAD, PROPOSER } from "@onflow/typedefs";
5+
import { TransactionRole, Interaction, InteractionAccount, InteractionResolverKind, InteractionStatus, InteractionTag } from "@onflow/typedefs";
66

77
type AcctFn = (acct: InteractionAccount) => InteractionAccount;
88
type AccountFn = AcctFn & Partial<InteractionAccount>;
99

1010
const ACCT = `{
11-
"kind":"${ACCOUNT}",
11+
"kind":"${InteractionResolverKind.ACCOUNT}",
1212
"tempId":null,
1313
"addr":null,
1414
"keyId":null,
@@ -24,18 +24,8 @@ const ACCT = `{
2424
}
2525
}`
2626

27-
const PRM = `{
28-
"kind":"${PARAM}",
29-
"tempId":null,
30-
"key":null,
31-
"value":null,
32-
"asParam":null,
33-
"xform":null,
34-
"resolve": null
35-
}`
36-
3727
const ARG = `{
38-
"kind":"${ARGUMENT}",
28+
"kind":"${InteractionResolverKind.ARGUMENT}",
3929
"tempId":null,
4030
"value":null,
4131
"asArgument":null,
@@ -45,9 +35,9 @@ const ARG = `{
4535
}`
4636

4737
const IX = `{
48-
"tag":"${UNKNOWN}",
38+
"tag":"${InteractionTag.UNKNOWN}",
4939
"assigns":{},
50-
"status":"${OK}",
40+
"status":"${InteractionStatus.OK}",
5141
"reason":null,
5242
"accounts":{},
5343
"params":{},
@@ -118,17 +108,17 @@ export const isInteraction = (ix: Interaction) => {
118108
}
119109

120110
export const Ok = (ix: Interaction) => {
121-
ix.status = OK
111+
ix.status = InteractionStatus.OK
122112
return ix
123113
}
124114

125115
export const Bad = (ix: Interaction, reason: string) => {
126-
ix.status = BAD
116+
ix.status = InteractionStatus.BAD
127117
ix.reason = reason
128118
return ix
129119
}
130120

131-
const makeIx = (wat: string) => (ix: Interaction) => {
121+
const makeIx = (wat: InteractionTag) => (ix: Interaction) => {
132122
ix.tag = wat
133123
return Ok(ix)
134124
}
@@ -145,7 +135,7 @@ const prepAccountKeyId = (acct: Partial<InteractionAccount> | AccountFn): Partia
145135
}
146136

147137
interface IPrepAccountOpts {
148-
role?: typeof AUTHORIZER | typeof PAYER | typeof PROPOSER | null
138+
role?: TransactionRole | null
149139
}
150140

151141
export const initAccount = (): InteractionAccount => JSON.parse(ACCT)
@@ -187,9 +177,9 @@ export const prepAccount = (acct: InteractionAccount | AccountFn, opts: IPrepAcc
187177
},
188178
}
189179

190-
if (role === AUTHORIZER) {
180+
if (role === TransactionRole.AUTHORIZER) {
191181
ix.authorizations.push(tempId)
192-
} else if (role === PAYER) {
182+
} else if (role === TransactionRole.PAYER) {
193183
ix.payer.push(tempId)
194184
} else if (role) {
195185
ix[role] = tempId
@@ -215,41 +205,40 @@ export const makeArgument = (arg: Record<string, any>) => (ix: Interaction) =>
215205
return Ok(ix)
216206
}
217207

218-
export const makeUnknown /* */ = makeIx(UNKNOWN)
219-
export const makeScript /* */ = makeIx(SCRIPT)
220-
export const makeTransaction /* */ = makeIx(TRANSACTION)
221-
export const makeGetTransactionStatus /* */ = makeIx(GET_TRANSACTION_STATUS)
222-
export const makeGetTransaction /* */ = makeIx(GET_TRANSACTION)
223-
export const makeGetAccount /* */ = makeIx(GET_ACCOUNT)
224-
export const makeGetEvents /* */ = makeIx(GET_EVENTS)
225-
export const makePing /* */ = makeIx(PING)
226-
export const makeGetBlock /* */ = makeIx(GET_BLOCK)
227-
export const makeGetBlockHeader /* */ = makeIx(GET_BLOCK_HEADER)
228-
export const makeGetCollection /* */ = makeIx(GET_COLLECTION)
229-
export const makeGetNetworkParameters /* */ = makeIx(GET_NETWORK_PARAMETERS)
230-
231-
const is = (wat: string) => (ix: Interaction) => ix.tag === wat
232-
233-
export const isUnknown /* */ = is(UNKNOWN)
234-
export const isScript /* */ = is(SCRIPT)
235-
export const isTransaction /* */ = is(TRANSACTION)
236-
export const isGetTransactionStatus /* */ = is(GET_TRANSACTION_STATUS)
237-
export const isGetTransaction /* */ = is(GET_TRANSACTION)
238-
export const isGetAccount /* */ = is(GET_ACCOUNT)
239-
export const isGetEvents /* */ = is(GET_EVENTS)
240-
export const isPing /* */ = is(PING)
241-
export const isGetBlock /* */ = is(GET_BLOCK)
242-
export const isGetBlockHeader /* */ = is(GET_BLOCK_HEADER)
243-
export const isGetCollection /* */ = is(GET_COLLECTION)
244-
export const isGetNetworkParameters /* */ = is(GET_NETWORK_PARAMETERS)
245-
246-
export const isOk /* */ = (ix: Interaction) => ix.status === OK
247-
export const isBad /* */ = (ix: Interaction) => ix.status === BAD
208+
export const makeUnknown /* */ = makeIx(InteractionTag.UNKNOWN)
209+
export const makeScript /* */ = makeIx(InteractionTag.SCRIPT)
210+
export const makeTransaction /* */ = makeIx(InteractionTag.TRANSACTION)
211+
export const makeGetTransactionStatus /* */ = makeIx(InteractionTag.GET_TRANSACTION_STATUS)
212+
export const makeGetTransaction /* */ = makeIx(InteractionTag.GET_TRANSACTION)
213+
export const makeGetAccount /* */ = makeIx(InteractionTag.GET_ACCOUNT)
214+
export const makeGetEvents /* */ = makeIx(InteractionTag.GET_EVENTS)
215+
export const makePing /* */ = makeIx(InteractionTag.PING)
216+
export const makeGetBlock /* */ = makeIx(InteractionTag.GET_BLOCK)
217+
export const makeGetBlockHeader /* */ = makeIx(InteractionTag.GET_BLOCK_HEADER)
218+
export const makeGetCollection /* */ = makeIx(InteractionTag.GET_COLLECTION)
219+
export const makeGetNetworkParameters /* */ = makeIx(InteractionTag.GET_NETWORK_PARAMETERS)
220+
221+
const is = (wat: InteractionTag) => (ix: Interaction) => ix.tag === wat
222+
223+
export const isUnknown /* */ = is(InteractionTag.UNKNOWN)
224+
export const isScript /* */ = is(InteractionTag.SCRIPT)
225+
export const isTransaction /* */ = is(InteractionTag.TRANSACTION)
226+
export const isGetTransactionStatus /* */ = is(InteractionTag.GET_TRANSACTION_STATUS)
227+
export const isGetTransaction /* */ = is(InteractionTag.GET_TRANSACTION)
228+
export const isGetAccount /* */ = is(InteractionTag.GET_ACCOUNT)
229+
export const isGetEvents /* */ = is(InteractionTag.GET_EVENTS)
230+
export const isPing /* */ = is(InteractionTag.PING)
231+
export const isGetBlock /* */ = is(InteractionTag.GET_BLOCK)
232+
export const isGetBlockHeader /* */ = is(InteractionTag.GET_BLOCK_HEADER)
233+
export const isGetCollection /* */ = is(InteractionTag.GET_COLLECTION)
234+
export const isGetNetworkParameters /* */ = is(InteractionTag.GET_NETWORK_PARAMETERS)
235+
236+
export const isOk /* */ = (ix: Interaction) => ix.status === InteractionStatus.OK
237+
export const isBad /* */ = (ix: Interaction) => ix.status === InteractionStatus.BAD
248238
export const why /* */ = (ix: Interaction) => ix.reason
249239

250-
export const isAccount /* */ = (account: Record<string, any>) => account.kind === ACCOUNT
251-
export const isParam /* */ = (param: Record<string, any>) => param.kind === PARAM
252-
export const isArgument /* */ = (argument: Record<string, any>) => argument.kind === ARGUMENT
240+
export const isAccount /* */ = (account: Record<string, any>) => account.kind === InteractionResolverKind.ACCOUNT
241+
export const isArgument /* */ = (argument: Record<string, any>) => argument.kind === InteractionResolverKind.ARGUMENT
253242

254243
const hardMode = (ix: Interaction) => {
255244
for (let key of Object.keys(ix)) {
@@ -300,6 +289,4 @@ export const update = (key: string, fn = identity) => (ix: Interaction) => {
300289
export const destroy = (key: string) => (ix: Interaction) => {
301290
delete ix.assigns[key]
302291
return Ok(ix)
303-
}
304-
305-
export * from "@onflow/typedefs"
292+
}

0 commit comments

Comments
 (0)