We've updated our Terms of Service. By continuing to use our services, you agree to the updated Terms.

Polkadot

Polkadot

#Installation

Magic interacts with the Polkadot blockchain via Magic's extension NPM package @magic-ext/polkadot. The Polkadot extension also lets you interact with the blockchain using methods from polkadot-js.

note

You can skip straight to our kitchen sink example directly:

👉 Polkadot Example

NPM
Yarn
01npm install --save @magic-ext/polkadot

#Initialization

Typescript
01import { Magic } from 'magic-sdk';
02import { PolkadotExtension } from '@magic-ext/polkadot';
03
04const magic = new Magic('YOUR_API_KEY', {
05  extensions: [
06    new PolkadotExtension({
07      rpcUrl: 'polkadot rpc url',
08    }),
09  ],
10});

#Common Methods

#Get User Info

Using getAccount function to get Polkadot public address for current user.

Typescript
01import { Magic } from 'magic-sdk';
02import { PolkadotExtension } from '@magic-ext/polkadot';
03
04const magic = new Magic('YOUR_API_KEY', {
05  extensions: [
06    new PolkadotExtension({
07      rpcUrl: 'polkadot rpc url',
08    }),
09  ],
10});
11
12// Get user's Polkadot public address
13const publicAddress = await magic.polkadot.getAccount();
14console.log('Polkadot Public Address: ', publicAddress);

#Send Transaction

To send a standard Polkadot blockchain transaction, you can call the magic.polkadot.sendTransaction method.

Typescript
01import { Magic } from 'magic-sdk';
02import { PolkadotExtension } from '@magic-ext/polkadot';
03
04const magic = new Magic('YOUR_API_KEY', {
05  extensions: [
06    new PolkadotExtension({
07      rpcUrl: 'polkadot rpc url',
08    }),
09  ],
10});
11
12const tx = await magic.polkadot.sendTransaction(
13  // to address
14  '5H3pELHbg9skXE2HfLqP23UPgrgu2Juj55CH6sdDGWc2HKNs',
15  1000000000000000, // amount
16);
17
18console.log('transaction hash', tx);

#Smart Contract

To call a Polkadot blockchain contract as transaction, you can call the magic.polkadot.contractCall method.

Typescript
01import { Magic } from 'magic-sdk';
02import { PolkadotExtension } from '@magic-ext/polkadot';
03
04const magic = new Magic('YOUR_API_KEY', {
05  extensions: [
06    new PolkadotExtension({
07      rpcUrl: 'polkadot rpc url',
08    }),
09  ],
10});
11
12const api = await ApiPromise.create({
13  provider: new WsProvider('polkadot rpc url'),
14});
15await api.isReady;
16const abi = new Abi(api.registry, contractABI);
17
18const data = abi.messages.flip();
19
20const tx = await magic.polkadot.contractCall(
21  // contract address
22  '5C52CfgkwANdFuN3VgPSprQwNWKfkLWMHJbMRzp12h4YarCa',
23  0, // value
24  1000000, // max gas
25  data, // contract data
26);
27
28console.log('transaction hash', tx);

#Deploy ink contract on substrate

Please follow substrate documention to create and deploy contract on substrate.

#Resources