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
npm install --save @magic-ext/tezos
#Yarn
yarn add @magic-ext/tezos
#Initializing Extension
#ES Modules/TypeScript
import { Magic } from 'magic-sdk';
import { TezosExtension } from '@magic-ext/tezos';
const magic = new Magic('YOUR_API_KEY', {
extensions: [
new TezosExtension({
rpcUrl: 'TEZOS_RPC_NODE_URL',
}),
],
});
#Get User Info
#Get Account
Using getAccount function to get Tezos public address for current user.
#ES Modules/TypeScript
import { Magic } from 'magic-sdk';
import { TezosExtension } from '@magic-ext/tezos';
const magic = new Magic('YOUR_API_KEY', {
extensions: [
new TezosExtension({
rpcUrl: 'TEZOS_RPC_NODE_URL',
}),
],
});
// Get user's Tezos public address
const publicAddress = await magic.tezos.getAccount();
console.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
import { Magic } from 'magic-sdk';
import { TezosExtension } from '@magic-ext/tezos';
const magic = new Magic('YOUR_API_KEY', {
extensions: [
new TezosExtension({
rpcUrl: 'TEZOS_RPC_NODE_URL',
}),
],
});
const result = await magic.tezos.sendTransactionOperation(
'tz1RVcUP9nUurgEJMDou8eW3bVDs6qmP5Lnc', // to address
500000, // amount
1500, // fee
'', // derivation path
);
console.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
import { Magic } from 'magic-sdk';
import { TezosExtension } from '@magic-ext/tezos';
const magic = new Magic('YOUR_API_KEY', {
extensions: [
new TezosExtension({
rpcUrl: 'TEZOS_RPC_NODE_URL',
}),
],
});
const contract = `[
{
"prim":"parameter",
"args":[ { "prim":"string" } ]
},
{
"prim":"storage",
"args":[ { "prim":"string" } ]
},
{
"prim":"code",
"args":[
[
{ "prim":"CAR" },
{ "prim":"NIL", "args":[ { "prim":"operation" } ] },
{ "prim":"PAIR" }
]
]
}
]`;
const storage = '{"string": "Sample"}';
const params = {
amount: 0,
delegate: undefined,
fee: 100000,
derivationPath: '',
storage_limit: 1000,
gas_limit: 100000,
code: contract,
storage,
codeFormat: 'micheline',
};
const result = await magic.tezos.sendContractOriginationOperation(
params.amount,
params.delegate,
params.fee,
params.derivationPath,
params.storage_limit,
params.gas_limit,
params.code,
params.storage,
params.codeFormat,
);
const operationGroupID = result.operationGroupID.trim();
setContractoperationGroupID(operationGroupID.substring(1, operationGroupID.length - 1));
console.log(`Injected operation group ID: ${result.operationGroupID}`, result);