Tutorial
Run a simple dApp that uses Blocto wallet service
Hello World
Let's build a simple project that sends a simple transaction on Flow testnet with Blocto FCL wallet.
Step 1 - Create app and setup dependencies
$ npx create-react-app hello-worldIn the hello-world folder you've just created, install dependencies necessary for this project.
$ yarn add @blocto/fcl@^1.4.0
$ yarn add styled-componentsFCL is under heavy developments and the versions are not always backward compatible. We recommend that you use @blocto/fcl@^1.4.0 for now.
You can start the app and see it running on http://localhost:3000
$ yarn startStep 2 - Read from Flow
Create
src/GetLatestBlock.jsAdd the component to
src/App.jsAdd config for FCL in
src/index.jsso FCL knows which access node to read data from
import React, {useState} from "react"
import * as fcl from "@blocto/fcl"
import styled from 'styled-components'
const Card = styled.div`
margin: 10px 5px;
padding: 10px;
border: 1px solid #c0c0c0;
border-radius: 5px;
`
const Code = styled.pre`
background: #f0f0f0;
border-radius: 5px;
max-height: 150px;
overflow-y: auto;
padding: 5px;
`
const GetLatestBlock = () => {
const [data, setData] = useState(null)
const runGetLatestBlock = async (event) => {
event.preventDefault()
const response = await fcl.send([
fcl.getBlock(true)
])
setData(await fcl.decode(response))
}
return (
<Card>
<button onClick={runGetLatestBlock}>
Get Latest Block
</button>
{data && <Code>{JSON.stringify(data, null, 2)}</Code>}
</Card>
)
}
export default GetLatestBlockWhen user clicks the button, runGetLatestBlock sends a request to get information for the latest block on Flow and display the result in <Code> block.
Step 3 - Connect to Blocto wallet
Now, let's add login functionality to your dApp.
Create
src/Authenticate.jsAdd the component to
src/App.jsAdd config for FCL in
src/index.jsso FCL knows which wallet to use
When user clicks the login button, FCL calls out to Blocto wallet and the user can either register a new Blocto account or login to their existing Blocto accounts. Once the login process completes and user chooses to use the Blocto account on the dApp, the dApp gets the connected account information and show it in <UserProfile>.
Step 4 - Send a simple transaction
Finally, we can use the connected Blocto Flow account to send a transaction.
Create
src/SendTransaction.jsAdd the component to
src/App.js
When user clicks the send button, FCL summons Blocto wallet and prompts user to either approve the transaction or reject it. If user approves of the transaction, Blocto wallet signs the message with the key in custodial and pass the signature back to FCL, where the transaction and the signature is sent to Flow network.
Other Resources
Flow App Quickstart: https://docs.onflow.org/fcl/tutorials/flow-app-quickstart/
Last updated
Was this helpful?