Flow

Flow

#Overview

Flow is an L1 blockchain featuring a distinctive multi-role architecture designed to increase throughput and efficiency without resorting to sharding. It adopts Cadence as its smart contract language and FCL (Flow Client Library) as the primary protocol for dapp, wallet, and user interactions with the chain.

#Magic Extension

This section will cover how to use our previous Flow integration via a custom extension that can be used alongside the core Magic SDK. You can use this extension to access Magic functionality alongside your FCL integration, however you do not need to use the authorization function shown here.

#Installation

Magic interacts with the Flow blockchain via Magic's extension NPM package @magic-ext/flow. The Flow extension also lets you interact with the blockchain using methods from Flow's Javascript SDK.

To get started, install the following dependencies for your project:

NPM
Yarn
01npm install @magic-ext/flow magic-sdk

#Initialization

The Magic class is the entry-point to the Magic SDK. It must be instantiated with a Magic publishable key.

note

If this is your first time using Magic with Flow, you may need to wait up to 30 seconds after clicking the magic link before your login completes because Magic has to wait for the Flow blockchain to confirm a transaction that creates your account.

ES Modules/TypeScript

Typescript
01import { Magic } from 'magic-sdk';
02import { FlowExtension } from '@magic-ext/flow';
03
04const magic = new Magic('YOUR_API_KEY', {
05  extensions: [
06    new FlowExtension({
07      // testnet or mainnet to connect different network
08      rpcUrl: 'https://rest-testnet.onflow.org',
09      network: 'testnet'
10    }),
11  ],
12});

#Login

You can use magic.flow.getAccount() method to let users login.

Typescript
01import * as fcl from '@onflow/fcl';
02
03import { Magic } from 'magic-sdk';
04import { FlowExtension } from '@magic-ext/flow';
05
06const magic = new Magic('YOUR_API_KEY', {
07  extensions: [
08    new FlowExtension({
09      rpcUrl: 'https://rest-testnet.onflow.org',
10      network: 'testnet' // testnet or mainnet to connect different network
11    }),
12  ],
13});
14
15const login = async () => {
16    const account = await magic.flow.getAccount();
17    console.log(account)
18}
19
20login()

#Common Methods

#Send Transaction

Getting Test Flow

Before you can send transaction on the Flow blockchain, you'll need to acquire some test Flow (Flow's native cryptocurrency for test network).

  1. Go to our Flow Example application
  2. Login with your email address
  3. Copy your Flow public address
  4. Go to the Flow Faucet
  5. Fill in the form and paste your copied Flow public address in the Address field
  6. You can receive 1000 test Flow
  7. Now you can use your test Flow in our example app

Call Extension Method

Note that the Magic Flow extension follows the method names and conventions of Flow's Javascript SDK. You can use the magic.flow.authorization() method to replace the fcl.authenticate().

ES Modules/TypeScript

Typescript
01import { Magic } from 'magic-sdk';
02import { FlowExtension } from '@magic-ext/flow';
03import * as fcl from '@onflow/fcl';
04
05const magic = new Magic('YOUR_API_KEY', {
06  extensions: [
07    new FlowExtension({
08      // testnet or mainnet to connect different network
09      rpcUrl: 'https://rest-testnet.onflow.org',
10      network: 'testnet'
11    }),
12  ],
13});
14
15// CONFIGURE ACCESS NODE
16fcl.config().put('accessNode.api', 'https://rest-testnet.onflow.org');
17
18// CONFIGURE WALLET
19// replace with your own wallets configuration
20// Below is the local environment configuration for the dev-wallet
21fcl.config().put('challenge.handshake', 'http://access-001.devnet9.nodes.onflow.org:8000');
22
23const AUTHORIZATION_FUNCTION = magic.flow.authorization;
24
25const verify = async () => {
26  try {
27    const getReferenceBlock = async () => {
28      const response = await fcl.send([fcl.getBlock()]);
29      const data = await fcl.decode(response);
30      return data.id;
31    };
32
33    console.log('SENDING TRANSACTION');
34    var response = await fcl.send([
35      fcl.transaction`
36      transaction {
37        var acct: AuthAccount
38
39        prepare(acct: AuthAccount) {
40          self.acct = acct
41        }
42
43        execute {
44          log(self.acct.address)
45        }
46      }
47    `,
48      fcl.ref(await getReferenceBlock()),
49      fcl.proposer(AUTHORIZATION_FUNCTION),
50      fcl.authorizations([AUTHORIZATION_FUNCTION]),
51      fcl.payer(AUTHORIZATION_FUNCTION),
52    ]);
53    console.log('TRANSACTION SENT');
54    console.log('TRANSACTION RESPONSE', response);
55
56    console.log('WAITING FOR TRANSACTION TO BE SEALED');
57    var data = await fcl.tx(response).onceSealed();
58    console.log('TRANSACTION SEALED', data);
59
60    if (data.status === 4 && data.statusCode === 0) {
61      console.log('Congrats!!! I Think It Works');
62    } else {
63      console.log(`Oh No: ${data.errorMessage}`);
64    }
65  } catch (error) {
66    console.error('FAILED TRANSACTION', error);
67  }
68};

#Resources