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);
Installation
Magic interacts with the Zilliqa blockchain via Magic's extension NPM package @magic-ext/zilliqa
. The Zilliqa extension also lets you interact with the blockchain using methods from Zilliqa's Javascript SDK.
You can skip straight to our kitchen sink example directly:
#NPM
01npm install --save @magic-ext/zilliqa
#Yarn
01yarn add @magic-ext/zilliqa
#Initializing Extension
#ES Modules/TypeScript
01import { Magic } from 'magic-sdk';
02import { ZilliqaExtension } from '@magic-ext/zilliqa';
03
04const magic = new Magic('YOUR_API_KEY', {
05 extensions: [
06 new ZilliqaExtension({
07 rpcUrl: 'Zilliqa_RPC_NODE_URL',
08 }),
09 ],
10});
#Get User Wallet
#Get Wallet
Using getWallet function to get a Zilliqa wallet for the current user.
#ES Modules/TypeScript
01import { Magic } from 'magic-sdk';
02import { ZilliqaExtension } from '@magic-ext/zilliqa';
03
04const magic = new Magic('YOUR_API_KEY', {
05 extensions: [
06 new ZilliqaExtension({
07 rpcUrl: 'Zilliqa_RPC_NODE_URL',
08 }),
09 ],
10});
11
12// Get user's Zilliqa wallet info
13const wallet = await magic.zilliqa.getWallet();
14console.log('Zilliqa wallet: ', wallet);
#Send Transaction
#Getting Test ZIL
Before you can send transaction on the Zilliqa blockchain, you'll need to acquire some test ZIL (Zilliqa's native cryptocurrency for test network).
- Go to our Zilliqa Example application
- Login with your email address
- Copy your Zilliqa public address
- Go to the ZIL Faucet
- Paste your copied Zilliqa public address in the text input
- You can receive 300 test ZIL
- Now you can use your test ZIL in our example app
#Call Extension Method
To send a standard Zilliqa blockchain transaction, you can call the magic.Zilliqa.sendTransaction
method.
#ES Modules/TypeScript
01import { Magic } from 'magic-sdk';
02import { ZilliqaExtension } from '@magic-ext/zilliqa';
03const { BN, Long, bytes, units } = require('@zilliqa-js/util');
04
05const magic = new Magic('YOUR_API_KEY', {
06 extensions: [
07 new ZilliqaExtension({
08 rpcUrl: 'Zilliqa_RPC_NODE_URL',
09 }),
10 ],
11});
12
13const chainId = 333; // chainId of the developer testnet
14const msgVersion = 1; // current msgVersion
15const VERSION = bytes.pack(chainId, msgVersion);
16
17const myGasPrice = units.toQa('1000', units.Units.Li);
18
19const params = {
20 version: VERSION,
21 toAddr: 'zil14vut0rh7q78ydc0g7yt7e5zkfyrmmps00lk6r7',
22 amount: new BN(units.toQa('0.5', units.Units.Zil)),
23 gasPrice: myGasPrice,
24 gasLimit: Long.fromNumber(1),
25};
26
27const tx = await magic.zil.sendTransaction(params, false);
28// Send a transaction
29console.log('send transaction', tx);
#Smart Contract
#Deploy Contract
Getting Test ZIL
Before you can send transaction on the Zilliqa blockchain, you'll need to acquire some test ZIL (Zilliqa's native cryptocurrency for test network).
- Go to our Zilliqa Example application
- Login with your email address
- Copy your Zilliqa public address
- Go to the ZIL Faucet
- Paste your copied Zilliqa public address in the text input
- You can receive 300 test ZIL
- Now you can use your test ZIL in our example app
Call Extension Method
To deploy a smart contract, you can call the magic.zilliqa.deployContract
method.
#ES Modules/TypeScript
01import { Magic } from 'magic-sdk';
02import { ZilliqaExtension } from '@magic-ext/zilliqa';
03const { BN, Long, bytes, units } = require('@zilliqa-js/util');
04
05const magic = new Magic('YOUR_API_KEY', {
06 extensions: [
07 new ZilliqaExtension({
08 rpcUrl: 'Zilliqa_RPC_NODE_URL',
09 }),
10 ],
11});
12
13const wallet = await magic.zilliqa.getWallet();
14
15const address = wallet.address;
16
17const code = `scilla_version 0
18
19 (* HelloWorld contract *)
20
21 import ListUtils
22
23 (***************************************************)
24 (* Associated library *)
25 (***************************************************)
26 library HelloWorld
27
28 let not_owner_code = Int32 1
29 let set_hello_code = Int32 2
30
31 (***************************************************)
32 (* The contract definition *)
33 (***************************************************)
34
35 contract HelloWorld
36 (owner: ByStr20)
37
38 field welcome_msg : String = ""
39
40 transition setHello (msg : String)
41 is_owner = builtin eq owner _sender;
42 match is_owner with
43 | False =>
44 e = {_eventname : "setHello()"; code : not_owner_code};
45 event e
46 | True =>
47 welcome_msg := msg;
48 e = {_eventname : "setHello()"; code : set_hello_code};
49 event e
50 end
51 end
52
53
54 transition getHello ()
55 r <- welcome_msg;
56 e = {_eventname: "getHello()"; msg: r};
57 event e
58 end`;
59
60const init = [
61 // this parameter is mandatory for all init arrays
62 {
63 vname: '_scilla_version',
64 type: 'Uint32',
65 value: '0',
66 },
67 {
68 vname: 'owner',
69 type: 'ByStr20',
70 value: `${address}`,
71 },
72];
73
74const chainId = 333; // chainId of the developer testnet
75const msgVersion = 1; // current msgVersion
76const VERSION = bytes.pack(chainId, msgVersion);
77
78const myGasPrice = units.toQa('1000', units.Units.Li);
79
80const params = {
81 version: VERSION,
82 gasPrice: myGasPrice,
83 gasLimit: Long.fromNumber(10000),
84};
85
86const result = await magic.zil.deployContract(init, code, params, 33, 1000, false);
87
88console.log('deploy contract', result);