How to Integrate with the Tezos Blockchain with Magic and ConseilJS
How to Integrate with the Tezos Blockchain with Magic and ConseilJS
#Installation
Magic interacts with the Tezos blockchain via Magic's extension NPM package @magic-ext/tezos
. The Tezos extension also lets you interact with the blockchain using methods from Tezos's ConseilJS SDK.
You can skip straight to our kitchen sink example directly:
👉 Tezos Example
#NPM
01npm install --save @magic-ext/tezos
#Yarn
01yarn add @magic-ext/tezos
#Initializing Extension
#ES Modules/TypeScript
01import { Magic } from 'magic-sdk';
02import { TezosExtension } from '@magic-ext/tezos';
03
04const magic = new Magic('YOUR_API_KEY', {
05 extensions: [
06 new TezosExtension({
07 rpcUrl: 'TEZOS_RPC_NODE_URL',
08 }),
09 ],
10});
#Get User Info
#Get Account
Using getAccount function to get Tezos public address for current user.
#ES Modules/TypeScript
01import { Magic } from 'magic-sdk';
02import { TezosExtension } from '@magic-ext/tezos';
03
04const magic = new Magic('YOUR_API_KEY', {
05 extensions: [
06 new TezosExtension({
07 rpcUrl: 'TEZOS_RPC_NODE_URL',
08 }),
09 ],
10});
11
12// Get user's Tezos public address
13const publicAddress = await magic.tezos.getAccount();
14console.log('Tezos Public Address: ', publicAddress);
#Send Transaction
#Call Extension Method
Note that the Magic Tezos extension follows the method names and conventions by ConceilJS. To send a standard Tezos blockchain transaction, you can call the magic.tezos.sendTransactionOperation
method.
#ES Modules/TypeScript
01import { Magic } from 'magic-sdk';
02import { TezosExtension } from '@magic-ext/tezos';
03
04const magic = new Magic('YOUR_API_KEY', {
05 extensions: [
06 new TezosExtension({
07 rpcUrl: 'TEZOS_RPC_NODE_URL',
08 }),
09 ],
10});
11
12const result = await magic.tezos.sendTransactionOperation(
13 'tz1RVcUP9nUurgEJMDou8eW3bVDs6qmP5Lnc', // to address
14 500000, // amount
15 1500, // fee
16 '', // derivation path
17);
18console.log(`Injected operation group ID: ${result.operationGroupID}`);
#Smart Contract
#Deploy Contract
Call Extension Method
Note that the Magic Tezos extension follows the method names and conventions by ConceilJS. To deploy a Tezos smart contract, you can call the magic.tezos.sendContractOriginationOperation
method.
#ES Modules/TypeScript
01import { Magic } from 'magic-sdk';
02import { TezosExtension } from '@magic-ext/tezos';
03
04const magic = new Magic('YOUR_API_KEY', {
05 extensions: [
06 new TezosExtension({
07 rpcUrl: 'TEZOS_RPC_NODE_URL',
08 }),
09 ],
10});
11
12const contract = `[
13 {
14 "prim":"parameter",
15 "args":[ { "prim":"string" } ]
16 },
17 {
18 "prim":"storage",
19 "args":[ { "prim":"string" } ]
20 },
21 {
22 "prim":"code",
23 "args":[
24 [
25 { "prim":"CAR" },
26 { "prim":"NIL", "args":[ { "prim":"operation" } ] },
27 { "prim":"PAIR" }
28 ]
29 ]
30 }
31]`;
32
33const storage = '{"string": "Sample"}';
34
35const params = {
36 amount: 0,
37 delegate: undefined,
38 fee: 100000,
39 derivationPath: '',
40 storage_limit: 1000,
41 gas_limit: 100000,
42 code: contract,
43 storage,
44 codeFormat: 'micheline',
45};
46
47const result = await magic.tezos.sendContractOriginationOperation(
48 params.amount,
49 params.delegate,
50 params.fee,
51 params.derivationPath,
52 params.storage_limit,
53 params.gas_limit,
54 params.code,
55 params.storage,
56 params.codeFormat,
57);
58
59const operationGroupID = result.operationGroupID.trim();
60
61setContractoperationGroupID(operationGroupID.substring(1, operationGroupID.length - 1));
62
63console.log(`Injected operation group ID: ${result.operationGroupID}`, result);