Skip to main content

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 ethers

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 { newKit } from '@celo/contractkit';
import { parseEther } from 'ethers';

const rpcUrl = 'https://alfajores-forno.celo-testnet.org';
const magic = new Magic('YOUR_API_KEY', {
  network: {
    rpcUrl,
  },
});

const kit = newKit(rpcUrl);
kit.connection.web3.setProvider(magic.rpcProvider);

const { publicAddress } = await magic.user.getMetadata();

kit.defaultAccount = publicAddress;

const oneGold = parseEther('1').toString();

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 { BrowserProvider, Contract } from 'ethers';

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 provider = new BrowserProvider(magic.rpcProvider);
const signer = await provider.getSigner();
const instance = new Contract(contractAddress, abi, signer);

const tx = await instance.setName('new name');
const receipt = await tx.wait();

console.log('contract send result: ', tx.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