FCL
This module provides a high level opinionated use of various parts of the Flow SDK.
config/1
and config/2
Key/Value Store used to configure the internals of fcl.
Progress
config/1
Asynchronously fetches the config value
config/2
Sets the value for a given key in the config.
import { config } from "@onflow/fcl"
config("foo", "bar")
await config("foo")
config("foo", undefined)
await config("foo")
currentUser/0
fcl.currentUser/0
returns things you can do with the current user.
Progress
fcl.currentUser/0.subscribe/1
Reactively calls the callback with the identity of the current user anytime the knowledge of the currentUser changes. Returns an unsubscribe function.
import React, {useState, useEffect} from "react"
import {currentUser} from "@onflow/fcl"
export const CurrentUser = () => {
const [user, setUser] = useState({})
useEffect(() => currentUser.subscribe(setUser), [])
return user.DID == null
? null
: <div>
<img src={user.avatar || "https://fallback.avatar.com"}/>
<div>{user.name || "Anonymous"}</div>
</div>
}
fcl.currentUser/0.authorization/n
and fcl.currentUser/0.payerAuthorization/n
Used in conjunction with fcl.send/n
to specify a required authorization by the currentUser.
import * as fcl from "@onflow/fcl"
import * as sdk from "@onflow/sdk"
import * as t from "@onflow/types"
const runTransaction = async (to, amount) =>
fcl.send([
sdk.transaction`
transaction(to: Address, amount: UFix64) {
prepare(from: AuthAccount) { … }
execute { … }
}
`,
sdk.params([
fcl.user(to).param,
sdk.param(amount, t.UFix64),
]),
sdk.authorizations([
fcl.currentUser().authorization
]),
sdk.payer(fcl.currentUser().payerAuthorization)
])
user/1
fcl.user/1
returns things you can do with the supplied user.
Progress
fcl.user/1.subscribe/1
Reactively calls the callback with the identity of the supplied user anytime the knowledge of the supplied user changes. Returns an unsubscribe function.
import React, {useState, useEffect} from "react"
import * as fcl from "@onflow/fcl"
export const User = ({ flowAcctNumber }) => {
const [user, setUser] = userState({})
useEffect(() => fcl.user(flowAcctNumber).subscribe(setUser), [])
return user.DID == null
? null
: <div>
<img src={user.avatar || "https://fallback.avatar.com"}/>
<div>{user.name || "Anonymous"}</div>
</div>
}
fcl.user/1.authorization/n
and fcl.user/1.payerAuthorization
Used in conjunction with fcl.send/n
to specify a required authorization by a supplied user.
import * as fcl from "@onflow/fcl"
import * as sdk from "@onflow/sdk"
import * as t from "@onflow/types"
const runTransaction = async (from, amount) =>
fcl.send([
sdk.transaction`
transaction(to: Address, amount: UFix64) {
prepare(from: AuthAccount) { … }
execute { … }
}
`,
sdk.params([
fcl.currentUser().param,
sdk.param(amount, t.UFix64),
]),
sdk.authorizations([
fcl.user(from).authorization,
]),
sdk.payer(fcl.user(from).payerAuthorization)
])
transaction/1
EARLY WIP
Progress
event/1
, event/2
and event/3
EARLY WIP
Progress
authenticate/0
and unauthenticate/0
Authenticates and unauthenticates the currentUser.
Progress
import React, {useState, useEffect} from "react"
import { currentUser, authenticate, unauthenticate } from "@onflow/fcl"
export const AuthButton = () => {
const [user, setUser] = useState({})
useEffect(() => currentUser().subscribe(setUser), [])
return user.DID != null
? <button onClick={unauthenticate}>Log Out</button>
: <button onClick={authenticate}>Log In</button>
}
send/1
and send/2
An opinionated use of the build/resolve/send pipeline. Comes preconfigured to work with params, async remote signing and payer.
Progress
import * as fcl from "@onflow/fcl"
import * as sdk from "@onflow/sdk"
import * as t from "@onflow/types"
fcl.config("accessNode.api", "https://my.access.node")
fcl.config("accessNode.key", process.env.FLOW_ACCESS_NODE_API_KEY)
const runTransaction = async (to, amount) =>
fcl.send([
sdk.transaction`
transaction(to: Address, amount: UFix64) {
prepare(from: AuthAccount) { … }
execute { … }
}
`,
sdk.params([
fcl.user(to).param,
sdk.param(amount, t.UFix64),
]),
sdk.authorizations([
fcl.currentUser().authorization
]),
sdk.payer(fcl.currentUser().payerAuthorization)
])
decode/1
An opinionated use of the decode function. Allows for global configuration of custom resources.
Progress
import * as fcl from "@onflow/fcl"
import * as sdk from "@onflow/sdk"
class MyCustomResource {
consturctor(data) {
this.name = data.name || "Anonymous"
this.age = data.age || 0
}
}
fcl.config("decode.MyCustomResource", data => new MyCustomResource(data))
const getStuff = async () => {
const response = await fcl.send([
sdk.script`
import MyCustomResource from 0x___
pub fun main(): @[MyCustomResource] {
...
}
`
])
const arrayOfMyCustomResource = await fcl.decode(response)
…
}