@onflow/fcl
A high level abstraction (built on top of @onflow/sdk) that enables development of browser based dApps.
Status
- Interface Stable (low risk of change)
- Realization Unstable (high risk of change)
We are currently confident in how to consume this package and how to build it, but this module is currently in an incomplete state and not everything works yet.
Install
npm install --save @onflow/fcl
You will probably also want: @onflow/sdk
and @onflow/types
Overview
Usage
Authentication Example
import React, {useState, useEffect} from "react"
import * as fcl from "@onflow/fcl"
fcl.config()
.put("challenge.scope", "email+publicKey")
export const Profile = () => {
const [user, setUser] = useState(null)
useEffect(() => fcl.currentUser().subscribe(setUser), [])
if (user == null) return <div>Loading...</div>
return !user.loggedIn
? <div>
<button onClick={fcl.authenticate}>Sign In</button>
<button onClick={fcl.authenticate}>Sign Up</button>
</div>
: <div>
<div>
<img src={user.avatar || "http://placekitten.com/g/100/100"} width="100" height="100"/>
{user.name || "Anonymous"}
</div>
<button onClick={fcl.unauthenticate}>Sign Out</button>
</div>
}
Transaction Example
import * as fcl from "@onflow/fcl"
import * as sdk from "@onflow/sdk"
import * as six from "@onflow/six"
import * as t from "@onflow/types"
fcl.config()
.put("send.node", "https://accessNodeUrl")
const response = await fcl.send([
sdk.transaction(six.SEND_FLOW_TOKENS),
sdk.params([
fcl.user(toAddress).param(),
sdk.param(amount, t.UFix64),
]),
sdk.payer(fcl.currentUser().payerAuthorization),
sdk.proposer(fcl.currentUser().proposerAuthorization),
sdk.authorizations([
fcl.currentUser().authorization
]),
])
const unsub = fcl.transaction(response).subscribe(status => {
if (sdk.isSealed(status)) unsub()
console.log(status)
})
Script
import * as fcl from "@onflow/fcl"
import * as sdk from "@onflow/sdk"
import * as t from "@onflow/types"
fcl.config()
.put("decoder.SomeNFT", d => new SomeToken(d))
const response = await fcl.send([
sdk.script`
import SomeNFT, getAllForAddress from 0x....
pub fun main(addr: Address): @[SomeNFT] {
let nfts: [SomeNFT] = getAllForAddress(addr: Address)
return nfts
}
`,
sdk.params([
fcl.currentUser().param()
])
])
const results = await fcl.decode(response)