How to Integrate with the Celo Blockchain with Magic

How to Integrate with the Celo Blockchain with Magic

Celo implementation guide

How to use Magic with the Celo blockchain

View guide

Resources

Demo

Installation

Via Magic SDK, you can interact with Celo.

note

You can skip straight to our kitchen sink example directly:

šŸ‘‰ Celo Example

NPM

npm install --save magic-sdk npm install --save @celo/contractkit

Yarn

yarn add magic-sdk yarn add @celo/contractkit

Send Transaction

Getting Test CELO token

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

  1. Go to our Celo Example application
  2. Login with your email address
  3. Copy your Celo public address
  4. Go to the Celo Faucet
  5. Paste your copied Celo public address in the text input
  6. Now you can use your test CELO token in our Celo Example

Use Magic Rpc Provider

ES Modules/TypeScript

import { Magic } from 'magic-sdk'; import { newKitFromWeb3 } from '@celo/contractkit'; import Web3 from 'web3'; const magic = new Magic('YOUR_API_KEY', { network: { rpcUrl: 'https://alfajores-forno.celo-testnet.org', }, }); const web3 = new Web3(magic.rpcProvider); const kit = newKitFromWeb3(web3); const { publicAddress } = await magic.user.getMetadata(); kit.defaultAccount = publicAddress; const oneGold = kit.web3.utils.toWei('1', 'ether'); const tx = await kit.sendTransaction({ from: publicAddress, to: 'Destination Address', value: oneGold, gasPrice: 1000000000, }); const hash = await tx.getHash(); const receipt = await tx.waitReceipt(); console.log('transaction result: ', hash, receipt);

Smart Contract

Getting Test CELO token

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

  1. Go to our Celo Example application
  2. Login with your email address
  3. Copy your Celo public address
  4. Go to the Celo Faucet
  5. Paste your copied Celo public address in the text input
  6. Now you can use your test CELO token in our Celo Example

Contract Send

ES Modules/TypeScript
import { Magic } from 'magic-sdk'; import { newKitFromWeb3 } from '@celo/contractkit'; import Web3 from 'web3'; const magic = new Magic('YOUR_API_KEY', { network: { rpcUrl: 'https://alfajores-forno.celo-testnet.org', }, }); const contractAddress = '0xcf71aB733148F70647129F3006E92439d11946A9'; const abi = [ { constant: true, inputs: [], name: 'getName', outputs: [ { internalType: 'string', name: '', type: 'string', }, ], payable: false, stateMutability: 'view', type: 'function', }, { constant: false, inputs: [ { internalType: 'string', name: 'newName', type: 'string', }, ], name: 'setName', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function', }, ]; const { publicAddress } = await magic.user.getMetadata(); const web3 = new Web3(magic.rpcProvider); const kit = newKitFromWeb3(web3); let instance = new web3.eth.Contract(abi, contractAddress); const txObject = await instance.methods.setName('new name'); let tx = await kit.sendTransactionObject(txObject, { from: publicAddress, gasPrice: 1000000000 }); const hash = await tx.getHash(); let receipt = await tx.waitReceipt(); console.log('contract send result: ', hash, receipt);
How to Integrate with the Celo Blockchain with Magic