diff --git a/packages/fcl-core/src/exec/mutate.js b/packages/fcl-core/src/exec/mutate.ts similarity index 55% rename from packages/fcl-core/src/exec/mutate.js rename to packages/fcl-core/src/exec/mutate.ts index c2744eef4..45b99267f 100644 --- a/packages/fcl-core/src/exec/mutate.js +++ b/packages/fcl-core/src/exec/mutate.ts @@ -1,31 +1,42 @@ +import type {AccountAuthorization} from "@onflow/sdk" import * as sdk from "@onflow/sdk" -import {normalizeArgs} from "./utils/normalize-args" -import {getCurrentUser} from "../current-user" -import {prepTemplateOpts} from "./utils/prep-template-opts.js" -import {preMutate} from "./utils/pre.js" +import {CurrentUserService, getCurrentUser} from "../current-user" import {isNumber} from "../utils/is" +import type {ArgsFn} from "./args" +import {normalizeArgs} from "./utils/normalize-args" +import {preMutate} from "./utils/pre" +import {prepTemplateOpts} from "./utils/prep-template-opts" + +export interface MutateOptions { + cadence?: string + args?: ArgsFn + template?: any + limit?: number + authz?: AccountAuthorization + proposer?: AccountAuthorization + payer?: AccountAuthorization + authorizations?: AccountAuthorization[] +} /** - * @description - * Factory function that returns a mutate function for a given currentUser. + * @description Factory function that returns a mutate function for a given currentUser. * - * @param {ReturnType | import("../current-user").CurrentUserConfig} currentUserOrConfig - CurrentUser actor or configuration + * @param currentUserOrConfig CurrentUser actor or configuration */ -export const getMutate = currentUserOrConfig => { +export const getMutate = (currentUserOrConfig: CurrentUserService) => { /** - * @description - * Allows you to submit transactions to the blockchain to potentially mutate the state. + * @description Allows you to submit transactions to the blockchain to potentially mutate the state. * - * @param {object} [opts] - Mutation Options and configuration - * @param {string} [opts.cadence] - Cadence Transaction used to mutate Flow - * @param {import("./args").ArgsFn} [opts.args] - Arguments passed to cadence transaction - * @param {object | string} [opts.template] - Interaction Template for a transaction - * @param {number} [opts.limit] - Compute Limit for transaction - * @param {Function} [opts.authz] - Authorization function for transaction - * @param {Function} [opts.proposer] - Proposer Authorization function for transaction - * @param {Function} [opts.payer] - Payer Authorization function for transaction - * @param {Array} [opts.authorizations] - Authorizations function for transaction - * @returns {Promise} Transaction Id + * @param opts Mutation Options and configuration + * @param opts.cadence Cadence Transaction used to mutate Flow + * @param opts.args Arguments passed to cadence transaction + * @param opts.template Interaction Template for a transaction + * @param opts.limit Compute Limit for transaction + * @param opts.authz Authorization function for transaction + * @param opts.proposer Proposer Authorization function for transaction + * @param opts.payer Payer Authorization function for transaction + * @param opts.authorizations Authorizations function for transaction + * @returns Transaction Id * * @example * fcl.mutate({ @@ -59,7 +70,7 @@ export const getMutate = currentUserOrConfig => { * authorizations: [AuthzFn], // an array of authorization functions used as authorizations signatory roles * } */ - const mutate = async (opts = {}) => { + const mutate = async (opts: MutateOptions = {}): Promise => { var txid try { await preMutate(opts) @@ -67,17 +78,17 @@ export const getMutate = currentUserOrConfig => { // Allow for a config to overwrite the authorization function. // prettier-ignore const currentUser = typeof currentUserOrConfig === "function" ? currentUserOrConfig : getCurrentUser(currentUserOrConfig) - const authz = await sdk + const authz: any = await sdk .config() .get("fcl.authz", currentUser().authorization) txid = sdk .send([ - sdk.transaction(opts.cadence), + sdk.transaction(opts.cadence!), sdk.args(normalizeArgs(opts.args || [])), - opts.limit && isNumber(opts.limit) && sdk.limit(opts.limit), + opts.limit && isNumber(opts.limit) && (sdk.limit(opts.limit!) as any), // opts.proposer > opts.authz > authz sdk.proposer(opts.proposer || opts.authz || authz), diff --git a/packages/fcl-core/src/exec/query.js b/packages/fcl-core/src/exec/query.js deleted file mode 100644 index bee2ebcba..000000000 --- a/packages/fcl-core/src/exec/query.js +++ /dev/null @@ -1,48 +0,0 @@ -import * as sdk from "@onflow/sdk" -import * as config from "@onflow/config" -import {normalizeArgs} from "./utils/normalize-args" -import {prepTemplateOpts} from "./utils/prep-template-opts.js" -import {preQuery} from "./utils/pre.js" - -/** - * @description - * Allows you to submit scripts to query the blockchain. - * - * @param {object} opts - Query Options and configuration - * @param {string} opts.cadence - Cadence Script used to query Flow - * @param {import("./args").ArgsFn} [opts.args] - Arguments passed to cadence script - * @param {object | string} [opts.template] - Interaction Template for a script - * @param {boolean} [opts.isSealed] - Block Finality - * @param {number} [opts.limit] - Compute Limit for Query - * @returns {Promise} - * - * @example - * const cadence = ` - * cadence: ` - * access(all) fun main(a: Int, b: Int, c: Address): Int { - * log(c) - * return a + b - * } - * `.trim() - * - * const args = (arg, t) => [ - * arg(5, t.Int), - * arg(7, t.Int), - * arg("0xb2db43ad6bc345fec9", t.Address), - * ] - * - * await query({ cadence, args }) - */ -export async function query(opts = {}) { - await preQuery(opts) - opts = await prepTemplateOpts(opts) - - return sdk - .send([ - sdk.script(opts.cadence), - sdk.args(normalizeArgs(opts.args || [])), - sdk.atLatestBlock(opts.isSealed ?? false), - opts.limit && typeof opts.limit === "number" && sdk.limit(opts.limit), - ]) - .then(sdk.decode) -} diff --git a/packages/fcl-core/src/exec/query.ts b/packages/fcl-core/src/exec/query.ts new file mode 100644 index 000000000..db7d44723 --- /dev/null +++ b/packages/fcl-core/src/exec/query.ts @@ -0,0 +1,57 @@ +import * as sdk from "@onflow/sdk" +import type {ArgsFn} from "./args" +import {normalizeArgs} from "./utils/normalize-args" +import {preQuery} from "./utils/pre" +import {prepTemplateOpts} from "./utils/prep-template-opts" + +export interface QueryOptions { + cadence?: string + args?: ArgsFn + template?: any + isSealed?: boolean + limit?: number +} + +/** + * @description Allows you to submit scripts to query the blockchain. + * + * @param opts Query Options and configuration + * @param opts.cadence Cadence Script used to query Flow + * @param opts.args Arguments passed to cadence script + * @param opts.template Interaction Template for a script + * @param opts.isSealed Block Finality + * @param opts.limit Compute Limit for Query + * @returns A promise that resolves to the query result + * + * @example + * const cadence = ` + * cadence: ` + * access(all) fun main(a: Int, b: Int, c: Address): Int { + * log(c) + * return a + b + * } + * `.trim() + * + * const args = (arg, t) => [ + * arg(5, t.Int), + * arg(7, t.Int), + * arg("0xb2db43ad6bc345fec9", t.Address), + * ] + * + * await query({ cadence, args }) + */ +export async function query(opts: QueryOptions = {}): Promise { + await preQuery(opts) + opts = await prepTemplateOpts(opts) + + return sdk + .send([ + sdk.script(opts.cadence!), + sdk.args(normalizeArgs(opts.args || [])), + sdk.atLatestBlock(opts.isSealed ?? false), + opts.limit && + typeof opts.limit === "number" && + (sdk.limit(opts.limit!) as any), + ]) + .then(sdk.decode) +} diff --git a/packages/fcl-core/src/exec/utils/normalize-args.js b/packages/fcl-core/src/exec/utils/normalize-args.js deleted file mode 100644 index b0cc38589..000000000 --- a/packages/fcl-core/src/exec/utils/normalize-args.js +++ /dev/null @@ -1,8 +0,0 @@ -import {isFunc} from "../../utils/is" -import * as sdk from "@onflow/sdk" -import * as t from "@onflow/types" - -export function normalizeArgs(ax) { - if (isFunc(ax)) return ax(sdk.arg, t) - return [] -} diff --git a/packages/fcl-core/src/exec/utils/normalize-args.ts b/packages/fcl-core/src/exec/utils/normalize-args.ts new file mode 100644 index 000000000..401843156 --- /dev/null +++ b/packages/fcl-core/src/exec/utils/normalize-args.ts @@ -0,0 +1,9 @@ +import {isFunc} from "../../utils/is" +import * as sdk from "@onflow/sdk" +import * as t from "@onflow/types" +import type {ArgsFn} from "../args" + +export function normalizeArgs(ax: ArgsFn | any[] | undefined): any[] { + if (isFunc(ax)) return (ax as ArgsFn)(sdk.arg, t) + return [] +} diff --git a/packages/fcl-core/src/exec/utils/pre.js b/packages/fcl-core/src/exec/utils/pre.ts similarity index 79% rename from packages/fcl-core/src/exec/utils/pre.js rename to packages/fcl-core/src/exec/utils/pre.ts index a92962836..3a807b2e9 100644 --- a/packages/fcl-core/src/exec/utils/pre.js +++ b/packages/fcl-core/src/exec/utils/pre.ts @@ -2,7 +2,12 @@ import {invariant} from "@onflow/util-invariant" import * as sdk from "@onflow/sdk" import {isRequired, isObject, isString} from "../../utils/is" -async function pre(type, opts) { +export interface PreOptions { + cadence?: string + template?: any +} + +async function pre(type: string, opts: PreOptions): Promise { // prettier-ignore invariant(isRequired(opts), `${type}(opts) -- opts is required`) // prettier-ignore @@ -23,10 +28,10 @@ async function pre(type, opts) { ) } -export async function preMutate(opts) { +export async function preMutate(opts: PreOptions): Promise { return pre("mutate", opts) } -export async function preQuery(opts) { +export async function preQuery(opts: PreOptions): Promise { return pre("query", opts) } diff --git a/packages/fcl-core/src/exec/utils/prep-template-opts.test.js b/packages/fcl-core/src/exec/utils/prep-template-opts.test.ts similarity index 98% rename from packages/fcl-core/src/exec/utils/prep-template-opts.test.js rename to packages/fcl-core/src/exec/utils/prep-template-opts.test.ts index d6d2218a8..8520c514e 100644 --- a/packages/fcl-core/src/exec/utils/prep-template-opts.test.js +++ b/packages/fcl-core/src/exec/utils/prep-template-opts.test.ts @@ -1,5 +1,5 @@ import {config} from "@onflow/config" -import {prepTemplateOpts} from "./prep-template-opts.js" +import {prepTemplateOpts} from "./prep-template-opts" describe("Prepare template options for template version 1.0.0", () => { // NOTE: template10 and template11 copied from packages\fcl-core\src\interaction-template-utils\derive-cadence-by-network\derive-cadence-by-network.test.js @@ -175,7 +175,7 @@ describe("Prepare template options for template version 1.1.0", () => { const test = async () => await prepTemplateOpts({ - template: template, + template: template11, }) await expect(test()).rejects.toThrow(Error) diff --git a/packages/fcl-core/src/exec/utils/prep-template-opts.js b/packages/fcl-core/src/exec/utils/prep-template-opts.ts similarity index 68% rename from packages/fcl-core/src/exec/utils/prep-template-opts.js rename to packages/fcl-core/src/exec/utils/prep-template-opts.ts index ca48549a3..96b314c68 100644 --- a/packages/fcl-core/src/exec/utils/prep-template-opts.js +++ b/packages/fcl-core/src/exec/utils/prep-template-opts.ts @@ -1,9 +1,16 @@ import {retrieve} from "../../document/document" -import {deriveCadenceByNetwork} from "../../interaction-template-utils/derive-cadence-by-network/derive-cadence-by-network.js" +import {deriveCadenceByNetwork} from "../../interaction-template-utils/derive-cadence-by-network/derive-cadence-by-network" import {isString} from "../../utils/is" import {getChainId} from "../../utils" -export async function prepTemplateOpts(opts) { +export interface TemplateOptions { + cadence?: string + template?: any +} + +export async function prepTemplateOpts( + opts: TemplateOptions +): Promise { if (isString(opts?.template)) { opts.template = await retrieve({url: opts?.template}) } diff --git a/packages/fcl-core/src/exec/verify.js b/packages/fcl-core/src/exec/verify.ts similarity index 74% rename from packages/fcl-core/src/exec/verify.js rename to packages/fcl-core/src/exec/verify.ts index 7c64450af..7f0484d40 100644 --- a/packages/fcl-core/src/exec/verify.js +++ b/packages/fcl-core/src/exec/verify.ts @@ -2,16 +2,14 @@ import {log} from "@onflow/util-logger" import {verifyUserSignatures as verify} from "../app-utils" /** - * Verify a valid signature/s for an account on Flow. - * + * @description Verify a valid signature/s for an account on Flow. * @deprecated since version '1.0.0-alpha.0', use AppUtils.verifyUserSignatures instead - * */ export const verifyUserSignatures = log.deprecate({ pkg: "FCL", subject: "fcl.verifyUserSignatures()", message: "Please use fcl.AppUtils.verifyUserSignatures()", - callback: function verifyUserSignatures(message, compSigs) { + callback: function verifyUserSignatures(message: any, compSigs: any) { return verify(message, compSigs) }, }) diff --git a/packages/fcl-core/src/interaction-template-utils/get-interaction-template-audits.js b/packages/fcl-core/src/interaction-template-utils/get-interaction-template-audits.js index 8c59d595a..633f8e3d1 100644 --- a/packages/fcl-core/src/interaction-template-utils/get-interaction-template-audits.js +++ b/packages/fcl-core/src/interaction-template-utils/get-interaction-template-audits.js @@ -1,6 +1,6 @@ import {config, invariant} from "@onflow/sdk" import {log, LEVELS} from "@onflow/util-logger" -import {query} from "../exec/query.js" +import {query} from "../exec/query" import {generateTemplateId} from "./generate-template-id/generate-template-id.js" import {getChainId} from "../utils" diff --git a/packages/sdk/src/sdk.ts b/packages/sdk/src/sdk.ts index ca50e8058..0aa14d78f 100644 --- a/packages/sdk/src/sdk.ts +++ b/packages/sdk/src/sdk.ts @@ -43,8 +43,12 @@ export { } from "./interaction/interaction" import type {CadenceArgument} from "./interaction/interaction" export {CadenceArgument} // Workaround for babel https://github.com/babel/babel/issues/8361 -import type {InteractionBuilderFn} from "./interaction/interaction" -export {InteractionBuilderFn} +import type { + InteractionBuilderFn, + AccountAuthorization, + AuthorizationFn, +} from "./interaction/interaction" +export {InteractionBuilderFn, AccountAuthorization, AuthorizationFn} export {createSignableVoucher, voucherToTxId} from "./resolve/voucher" export {encodeMessageFromSignable} from "./wallet-utils/encode-signable"