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
#Resources
#Installation
Via Magic SDK, you can interact with Celo.
You can skip straight to our kitchen sink example directly:
👉 Celo Example
#NPM
01npm install --save magic-sdk
02npm install --save @celo/contractkit
#Yarn
01yarn add magic-sdk
02yarn 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).
- Go to our Celo Example application
- Login with your email address
- Copy your Celo public address
- Go to the Celo Faucet
- Paste your copied Celo public address in the text input
- Now you can use your test CELO token in our Celo Example
#Use Magic Rpc Provider
ES Modules/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).
- Go to our Celo Example application
- Login with your email address
- Copy your Celo public address
- Go to the Celo Faucet
- Paste your copied Celo public address in the text input
- Now you can use your test CELO token in our Celo Example
Contract Send
ES Modules/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);