Flow

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.

note

You can skip straight to our kitchen sink example directly:

👉 Flow Example

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.

npm install --save @magic-ext/flow

Initializing Extension

import { Magic } from 'magic-sdk';
import { FlowExtension } from '@magic-ext/flow';
const magic = new Magic('YOUR_API_KEY', {
extensions: [
new FlowExtension({
rpcUrl: 'https://access-testnet.onflow.org'
})
]
});
warning

You have to use the Magic LIVE API KEY with Flow Mainnet and Magic TEST API KEY with Flow Testnet, because which network to create your account according to the API KEY.

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().

import { Magic } from 'magic-sdk';
import { FlowExtension } from '@magic-ext/flow';
import * as fcl from "@onflow/fcl";
const magic = new Magic('YOUR_API_KEY', {
extensions: [
new FlowExtension({
rpcUrl: 'https://access-testnet.onflow.org'
})
]
});
// CONFIGURE ACCESS NODE
fcl.config().put("accessNode.api", "https://access-testnet.onflow.org");
// CONFIGURE WALLET
// replace with your own wallets configuration
// Below is the local environment configuration for the dev-wallet
fcl
.config()
.put("challenge.handshake", "http://access-001.devnet9.nodes.onflow.org:8000");
const AUTHORIZATION_FUNCTION = magic.flow.authorization;
const verify = async () => {
try {
const getReferenceBlock = async () => {
const response = await fcl.send([fcl.getLatestBlock()]);
const data = await fcl.decode(response);
return data.id
};
console.log("SENDING TRANSACTION");
var response = await fcl.send([
fcl.transaction`
transaction {
var acct: AuthAccount
prepare(acct: AuthAccount) {
self.acct = acct
}
execute {
log(self.acct.address)
}
}
`,
fcl.ref(await getReferenceBlock()),
fcl.proposer(AUTHORIZATION_FUNCTION),
fcl.authorizations([AUTHORIZATION_FUNCTION]),
fcl.payer(AUTHORIZATION_FUNCTION)
]);
console.log("TRANSACTION SENT");
console.log("TRANSACTION RESPONSE", response);
console.log("WAITING FOR TRANSACTION TO BE SEALED");
var data = await fcl.tx(response).onceSealed();
console.log("TRANSACTION SEALED", data);
if (data.status === 4 && data.statusCode === 0) {
console.log("Congrats!!! I Think It Works");
} else {
console.log(`Oh No: ${data.errorMessage}`);
}
} catch (error) {
console.error("FAILED TRANSACTION", error);
}
};