-
Notifications
You must be signed in to change notification settings - Fork 128
Refactored fcl-core exec folder files to TypeScript #2473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
ca519d7
19cacff
103a499
19f18b9
9c91841
c4e9d99
d5151a5
33b17bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import * as sdk from "@onflow/sdk" | ||
import {normalizeArgs} from "./utils/normalize-args" | ||
import {getCurrentUser} from "../current-user" | ||
import {prepTemplateOpts} from "./utils/prep-template-opts" | ||
import {preMutate} from "./utils/pre" | ||
import {isNumber} from "../utils/is" | ||
import type {ArgsFn} from "./args" | ||
import type { | ||
Account, | ||
CompositeSignature, | ||
CurrentUser, | ||
Service, | ||
} from "@onflow/typedefs" | ||
import type {AccountAuthorization} from "@onflow/sdk" | ||
import {StorageProvider} from "../utils/storage" | ||
|
||
// TODO: Substitute with CurrentUser interface from current-user refactoring once merged | ||
export interface CurrentUserConfig { | ||
platform: string | ||
discovery?: object | undefined | ||
getStorageProvider?: () => Promise<StorageProvider> | ||
} | ||
|
||
// TODO: Substitute with CurrentUser interface from current-user refactoring once merged | ||
export interface AuthenticationOptions { | ||
service?: Service | ||
redir?: boolean | ||
forceReauth?: boolean | ||
} | ||
|
||
// TODO: Substitute with CurrentUser interface from current-user refactoring once merged | ||
export interface CurrentUserFull extends CurrentUser { | ||
(): CurrentUserFull | ||
authenticate: ({ | ||
service, | ||
redir, | ||
forceReauth, | ||
}?: AuthenticationOptions) => Promise<CurrentUser> | ||
unauthenticate: () => void | ||
authorization: (account: Account) => Promise<Account> | ||
signUserMessage: (msg: string) => Promise<CompositeSignature[]> | ||
subscribe: (callback: (user: CurrentUser) => void) => () => void | ||
snapshot: () => Promise<CurrentUser> | ||
resolveArgument: () => Promise<string> | ||
} | ||
|
||
export interface MutateOptions { | ||
cadence?: string | ||
args?: ArgsFn | any[] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Compatibility with other not yet refactored folders, cleaned all up now! |
||
template?: string | any | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Compatibility with other not yet refactored folders, cleaned all up now! |
||
limit?: number | ||
authz?: AccountAuthorization | ||
proposer?: AccountAuthorization | ||
payer?: AccountAuthorization | ||
authorizations?: AccountAuthorization[] | ||
[key: string]: any | ||
jribbink marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* @description Factory function that returns a mutate function for a given currentUser. | ||
* | ||
* @param currentUserOrConfig CurrentUser actor or configuration | ||
*/ | ||
export const getMutate = ( | ||
currentUserOrConfig: CurrentUserFull | CurrentUserConfig | ||
) => { | ||
/** | ||
* @description Allows you to submit transactions to the blockchain to potentially mutate the state. | ||
* | ||
* @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({ | ||
* cadence: ` | ||
* transaction(a: Int, b: Int, c: Address) { | ||
* prepare(acct: AuthAccount) { | ||
* log(acct) | ||
* log(a) | ||
* log(b) | ||
* log(c) | ||
* } | ||
* } | ||
* `, | ||
* args: (arg, t) => [ | ||
* arg(6, t.Int), | ||
* arg(7, t.Int), | ||
* arg("0xba1132bc08f82fe2", t.Address), | ||
* ], | ||
* }) | ||
* | ||
* | ||
* Options: | ||
* type Options = { | ||
* template: InteractionTemplate | String // InteractionTemplate or url to one | ||
* cadence: String!, | ||
* args: (arg, t) => Array<Arg>, | ||
* limit: Number, | ||
* authz: AuthzFn, // will overload the trinity of signatory roles | ||
* proposer: AuthzFn, // will overload the proposer signatory role | ||
* payer: AuthzFn, // will overload the payer signatory role | ||
* authorizations: [AuthzFn], // an array of authorization functions used as authorizations signatory roles | ||
* } | ||
*/ | ||
const mutate = async (opts: MutateOptions = {}): Promise<string> => { | ||
var txid | ||
try { | ||
await preMutate(opts) | ||
opts = await prepTemplateOpts(opts) | ||
// Allow for a config to overwrite the authorization function. | ||
// prettier-ignore | ||
const currentUser = typeof currentUserOrConfig === "function" ? currentUserOrConfig : getCurrentUser(currentUserOrConfig) | ||
const authz: any = await sdk | ||
.config() | ||
.get("fcl.authz", currentUser().authorization) | ||
|
||
txid = sdk | ||
.send([ | ||
sdk.transaction(opts.cadence!), | ||
|
||
sdk.args(normalizeArgs(opts.args || [])), | ||
|
||
opts.limit && isNumber(opts.limit) && (sdk.limit(opts.limit!) as any), | ||
|
||
// opts.proposer > opts.authz > authz | ||
sdk.proposer(opts.proposer || opts.authz || authz), | ||
|
||
// opts.payer > opts.authz > authz | ||
sdk.payer(opts.payer || opts.authz || authz), | ||
|
||
// opts.authorizations > [opts.authz > authz] | ||
sdk.authorizations(opts.authorizations || [opts.authz || authz]), | ||
]) | ||
.then(sdk.decode) | ||
|
||
return txid | ||
} catch (error) { | ||
throw error | ||
} | ||
} | ||
|
||
return mutate | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
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 | any[] | ||
template?: string | any | ||
isSealed?: boolean | ||
limit?: number | ||
[key: string]: any | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the same thing apply here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same, updated and improved! |
||
} | ||
|
||
/** | ||
* @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<any> { | ||
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) | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 [] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which pr needs to be reviewed first to unblock this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one #2454