We've updated our Terms of Service. By continuing to use our services, you agree to the updated Terms.

Celo

Celo

Celo implementation guide

How to use Magic with the Celo blockchain

View guide

#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 Ethereum documentation to send your first transaction and utilize all other wallet features.

#Installation

To install Magic and Celo, follow the instructions below.

NPM
Yarn
01npm 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

ES Modules/TypeScript

Typescript
01import { Magic } from 'magic-sdk';
02import { newKitFromWeb3 } from '@celo/contractkit';
03import Web3 from 'web3';
04
05const magic = new Magic('YOUR_API_KEY', {
06  network: {
07    rpcUrl: 'https://alfajores-forno.celo-testnet.org',
08  },
09});
10
11const web3 = new Web3(magic.rpcProvider);
12const kit = newKitFromWeb3(web3);
13
14const { publicAddress } = await magic.user.getMetadata();
15
16kit.defaultAccount = publicAddress;
17
18const oneGold = kit.web3.utils.toWei('1', 'ether');
19
20const tx = await kit.sendTransaction({
21  from: publicAddress,
22  to: 'Destination Address',
23  value: oneGold,
24  gasPrice: 1000000000,
25});
26
27const hash = await tx.getHash();
28const receipt = await tx.waitReceipt();
29
30console.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

Typescript
01import { Magic } from 'magic-sdk';
02import { newKitFromWeb3 } from '@celo/contractkit';
03import Web3 from 'web3';
04
05const magic = new Magic('YOUR_API_KEY', {
06  network: {
07    rpcUrl: 'https://alfajores-forno.celo-testnet.org',
08  },
09});
10
11const contractAddress = '0xcf71aB733148F70647129F3006E92439d11946A9';
12
13const abi = [
14  {
15    constant: true,
16    inputs: [],
17    name: 'getName',
18    outputs: [
19      {
20        internalType: 'string',
21        name: '',
22        type: 'string',
23      },
24    ],
25    payable: false,
26    stateMutability: 'view',
27    type: 'function',
28  },
29  {
30    constant: false,
31    inputs: [
32      {
33        internalType: 'string',
34        name: 'newName',
35        type: 'string',
36      },
37    ],
38    name: 'setName',
39    outputs: [],
40    payable: false,
41    stateMutability: 'nonpayable',
42    type: 'function',
43  },
44];
45const { publicAddress } = await magic.user.getMetadata();
46
47const web3 = new Web3(magic.rpcProvider);
48const kit = newKitFromWeb3(web3);
49
50let instance = new web3.eth.Contract(abi, contractAddress);
51
52const txObject = await instance.methods.setName('new name');
53
54let tx = await kit.sendTransactionObject(txObject, { from: publicAddress, gasPrice: 1000000000 });
55
56const hash = await tx.getHash();
57
58let receipt = await tx.waitReceipt();
59
60console.log('contract send result: ', hash, receipt);

#Compatibility

*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