Zilliqa
Zilliqa
#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:
👉 Zilliqa Example
01npm install --save @magic-ext/zilliqa
#Initialization
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});
#CDN
01<script src="https://auth.magic.link/sdk"></script>
02<script type="text/javascript" src="https://auth.magic.link/sdk/extension/zilliqa"></script>
03
04<script>
05 const magic = new window.Magic("YOUR_API_KEY", {
06 extensions: [
07 new MagicZilliqaExtension({
08 rpcUrl: 'ZILLIQA_RPC_NODE_URL'
09 })
10 ]
11 });
12</script>
#Common Methods
#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
#Get User Wallet
Using getWallet
function to get a Zilliqa wallet for the current user.
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
To send a standard Zilliqa blockchain transaction, you can call the magic.Zilliqa.sendTransaction
method.
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
To deploy a smart contract, you can call the magic.zilliqa.deployContract
method.
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);