How to Integrate with the Harmony Blockchain with Magic
How to Integrate with the Harmony Blockchain with Magic
#Installation
Magic interacts with the Harmony blockchain via Magic's extension NPM package @magic-ext/harmony
. The Harmony extension also lets you interact with the blockchain using methods from Harmony's Javascript SDK.
You can skip straight to our kitchen sink example directly:
👉 Harmony Example
#NPM
01npm install --save @magic-ext/harmony
#Yarn
01yarn add @magic-ext/harmony
#Initializing Extension
#ES Modules/TypeScript
01import { Magic } from 'magic-sdk';
02import { HarmonyExtension } from '@magic-ext/harmony';
03const { Harmony: Index } = require('@harmony-js/core');
04const { ChainID, ChainType } = require('@harmony-js/utils');
05
06const magic = new Magic('YOUR_API_KEY', {
07 extensions: [
08 new HarmonyExtension({
09 rpcUrl: 'https://api.s0.b.hmny.io',
10 chainId: ChainID.HmyTestnet,
11 }),
12 ],
13});
#Send Transaction
#Getting Test ONE token
Before you can send transaction on the Harmony blockchain, you'll need to acquire some test ONE token (Harmony's native cryptocurrency for test network).
- Go to our Harmony Example application
- Login with your email address
- Copy your Harmony public address
- Go to the Harmony Faucet
- Paste your copied Harmony public address in the text input
- Now you can use your test ONE token in our example app
#Call Extension Method
Note that the Magic Harmony extension follows the method names and conventions by Harmony's Javascript SDK. To send a standard Harmony blockchain transaction, you can call the magic.harmony.sendTransaction
method.
#ES Modules/TypeScript
01import { Magic } from 'magic-sdk';
02import { HarmonyExtension } from '@magic-ext/harmony';
03const { Harmony: Index } = require('@harmony-js/core');
04const { ChainID, ChainType } = require('@harmony-js/utils');
05
06const magic = new Magic('YOUR_API_KEY', {
07 extensions: [
08 new HarmonyExtension({
09 rpcUrl: 'https://api.s0.b.hmny.io',
10 chainId: ChainID.HmyTestnet,
11 }),
12 ],
13});
14
15const params = {
16 // token send to
17 to: 'one1jzxhswufkh7wgyq7s49u3rvp9vlts8wcwsq8y2',
18 // amount to send
19 value: '50000',
20 // gas limit, you can use string
21 gasLimit: '210000',
22 // send token from shardID
23 shardID: 0,
24 // send token to toShardID
25 toShardID: 0,
26 // gas Price, you can use Unit class, and use Gwei, then remember to use toWei(), which will be transformed to BN
27 gasPrice: 1000000000,
28};
29
30const txHash = await magic.harmony.sendTransaction(params);
31
32console.log('transaction hash', txHash);
#Smart Contract
#Deploy Contract
#Getting Test ONE token
Before you can send transaction on the Harmony blockchain, you'll need to acquire some test ONE token (Harmony's native cryptocurrency for test network).
- Go to our Harmony Example application
- Login with your email address
- Copy your Harmony public address
- Go to the Harmony Faucet
- Paste your copied Harmony public address in the text input
- Now you can use your test ONE token in our example app
Call Extension Method
Note that the Magic Harmony extension follows the method names and conventions by Harmony's Javascript SDK. To deploy an Harmony smart contract, you can call the magic.harmony.sendTransaction
method to send deploy contract transaction.
#ES Modules/TypeScript
01import { Magic } from 'magic-sdk';
02import { HarmonyExtension } from '@magic-ext/harmony';
03const { Harmony: Index } = require('@harmony-js/core');
04const { ChainID, ChainType } = require('@harmony-js/utils');
05
06const magic = new Magic('YOUR_API_KEY', {
07 extensions: [
08 new HarmonyExtension({
09 rpcUrl: 'https://api.s0.b.hmny.io',
10 chainId: ChainID.HmyTestnet,
11 }),
12 ],
13});
14
15const bin =
16 '608060405234801561001057600080fd5b5060c68061001f6000396000f3fe6080604052348015600f576000' +
17 '80fd5b506004361060325760003560e01c80636057361d146037578063b05784b8146062575b600080fd5b6060600480' +
18 '36036020811015604b57600080fd5b8101908080359060200190929190505050607e565b005b60686088565b60405180' +
19 '82815260200191505060405180910390f35b8060008190555050565b6000805490509056fea265627a7a723158209e86' +
20 '9bf97eba094ccf7533f0f92b4de32cf3cce7d7cff974769bca975e178b0164736f6c63430005110032';
21
22const contractBytecode = {
23 data: `0x${bin}`,
24 gasLimit: '210000',
25 // send token from shardID
26 shardID: 0,
27 // send token to toShardID
28 toShardID: 0,
29 // gas Price, you can use Unit class, and use Gwei, then remember to use toWei(), which will be transformed to BN
30 gasPrice: 1000000000,
31 arguments: [],
32};
33
34const txHash = await magic.harmony.sendTransaction(contractBytecode);
35
36console.log('transaction hash', txHash);
#Contract Send
#Getting Test ONE token
Before you can send transaction on the Harmony blockchain, you'll need to acquire some test ONE token (Harmony's native cryptocurrency for test network).
- Go to our Harmony Example application
- Login with your email address
- Copy your Harmony public address
- Go to the Harmony Faucet
- Paste your copied Harmony public address in the text input
- Now you can use your test ONE token in our example app
Call Extension Method
Note that the Magic Harmony extension follows the method names and conventions by Harmony's Javascript SDK. To call an Harmony smart contract function, you can call the magic.harmony.sendTransaction
method to send contract transaction.
#ES Modules/TypeScript
01import { Magic } from 'magic-sdk';
02import { HarmonyExtension } from '@magic-ext/harmony';
03const { Harmony: Index } = require('@harmony-js/core');
04const { ChainID, ChainType } = require('@harmony-js/utils');
05
06const magic = new Magic('YOUR_API_KEY', {
07 extensions: [
08 new HarmonyExtension({
09 rpcUrl: 'https://api.s0.b.hmny.io',
10 chainId: ChainID.HmyTestnet,
11 }),
12 ],
13});
14
15const harmony = new Index(
16 // rpc url
17 'https://api.s0.b.hmny.io',
18 {
19 // chainType set to Index
20 chainType: ChainType.Harmony,
21 // chainType set to HmyLocal
22 chainId: ChainID.HmyTestnet,
23 },
24);
25
26let contractAddress = '0x67a3f8db0c98524e8e4513f95cd68f7fbbca7f06';
27
28const contractAbi = [
29 {
30 constant: false,
31 inputs: [
32 {
33 internalType: 'uint256',
34 name: 'num',
35 type: 'uint256',
36 },
37 ],
38 name: 'store',
39 outputs: [],
40 payable: false,
41 stateMutability: 'nonpayable',
42 type: 'function',
43 },
44 {
45 constant: true,
46 inputs: [],
47 name: 'retreive',
48 outputs: [
49 {
50 internalType: 'uint256',
51 name: '',
52 type: 'uint256',
53 },
54 ],
55 payable: false,
56 stateMutability: 'view',
57 type: 'function',
58 },
59];
60
61const deployedContract = harmony.contracts.createContract(contractAbi, contractAddress);
62
63const tx = await deployedContract.methods.store(900);
64
65let { txPayload } = tx.transaction;
66
67txPayload.from = (await magic.user.getMetadata()).publicAddress;
68txPayload.gasLimit = '210000';
69txPayload.gasPrice = '1000000000';
70
71const txHash = await magic.harmony.sendTransaction(txPayload);
72
73console.log('transaction hash', txHash);