Celo implementation guide

How to use Magic with the Celo blockchain

Overview

Celo is a Layer 2 solution enhancing Ethereum by providing developers with faster and more cost-efficient transactions. For developers, its seamless compatibility with Ethereum’s ecosystem and tools simplifies dApp development. Celo is especially beneficial for those looking to boost performance while maintaining the decentralized principles of Ethereum. As Celo is EVM compatible, you can follow the Ethereumdocumentation to send your first transaction and utilize all other wallet features.

Installation

To install Magic and Celo, follow the instructions below.
npm install --save magic-sdk @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

JavaScript
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

JavaScript
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);

Compatibility

  • All Auth, User and most Wallet module methods*
  • All EVM Provider functionality to respond to supported RPC methods
*Some features are not yet compatible such as NFT Viewer and Fiat On-ramps. Need a feature or see a problem? File an issue on our github repo.

Resources & Tools