Tezos

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.

note

You can skip straight to our kitchen sink example directly:

๐Ÿ‘‰ Tezos ConseilJS Example

npm install --save @magic-ext/tezos

Initializing Extension

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.

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

Getting Test XTZ

Before you can send transaction on the Tezos blockchain, you'll need to acquire some test XTZ (Tezos' native cryptocurrency for test network).

  1. Go to our Tezos Faucet
  2. Type in your email and click the "Sign Up / Login" button
  3. Go to your email and click on the magic link to login
  4. Once you are logged in, you will get 10 test XTZ
  5. Now you can use your test XTZ in our example app

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.

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

Getting Test XTZ

Before you can send transaction on the Tezos blockchain, you'll need to acquire some test XTZ (Tezos' native cryptocurrency for test network).

  1. Go to our Tezos Faucet
  2. Type in your email and click the "Sign Up / Login" button
  3. Go to your email and click on the magic link to login
  4. Once you are logged in, you will get 10 test XTZ
  5. Now you can use your test XTZ in our example app
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.

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
);