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
npm install --save @magic-ext/harmony
#Yarn
yarn add @magic-ext/harmony
#Initializing Extension
#ES Modules/TypeScript
import { Magic } from 'magic-sdk';
import { HarmonyExtension } from '@magic-ext/harmony';
const { Harmony: Index } = require('@harmony-js/core');
const { ChainID, ChainType } = require('@harmony-js/utils');
const magic = new Magic('YOUR_API_KEY', {
extensions: [
new HarmonyExtension({
rpcUrl: 'https://api.s0.b.hmny.io',
chainId: ChainID.HmyTestnet,
}),
],
});
#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
import { Magic } from 'magic-sdk';
import { HarmonyExtension } from '@magic-ext/harmony';
const { Harmony: Index } = require('@harmony-js/core');
const { ChainID, ChainType } = require('@harmony-js/utils');
const magic = new Magic('YOUR_API_KEY', {
extensions: [
new HarmonyExtension({
rpcUrl: 'https://api.s0.b.hmny.io',
chainId: ChainID.HmyTestnet,
}),
],
});
const params = {
// token send to
to: 'one1jzxhswufkh7wgyq7s49u3rvp9vlts8wcwsq8y2',
// amount to send
value: '50000',
// gas limit, you can use string
gasLimit: '210000',
// send token from shardID
shardID: 0,
// send token to toShardID
toShardID: 0,
// gas Price, you can use Unit class, and use Gwei, then remember to use toWei(), which will be transformed to BN
gasPrice: 1000000000,
};
const txHash = await magic.harmony.sendTransaction(params);
console.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
import { Magic } from 'magic-sdk';
import { HarmonyExtension } from '@magic-ext/harmony';
const { Harmony: Index } = require('@harmony-js/core');
const { ChainID, ChainType } = require('@harmony-js/utils');
const magic = new Magic('YOUR_API_KEY', {
extensions: [
new HarmonyExtension({
rpcUrl: 'https://api.s0.b.hmny.io',
chainId: ChainID.HmyTestnet,
}),
],
});
const bin =
'608060405234801561001057600080fd5b5060c68061001f6000396000f3fe6080604052348015600f576000' +
'80fd5b506004361060325760003560e01c80636057361d146037578063b05784b8146062575b600080fd5b6060600480' +
'36036020811015604b57600080fd5b8101908080359060200190929190505050607e565b005b60686088565b60405180' +
'82815260200191505060405180910390f35b8060008190555050565b6000805490509056fea265627a7a723158209e86' +
'9bf97eba094ccf7533f0f92b4de32cf3cce7d7cff974769bca975e178b0164736f6c63430005110032';
const contractBytecode = {
data: `0x${bin}`,
gasLimit: '210000',
// send token from shardID
shardID: 0,
// send token to toShardID
toShardID: 0,
// gas Price, you can use Unit class, and use Gwei, then remember to use toWei(), which will be transformed to BN
gasPrice: 1000000000,
arguments: [],
};
const txHash = await magic.harmony.sendTransaction(contractBytecode);
console.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
import { Magic } from 'magic-sdk';
import { HarmonyExtension } from '@magic-ext/harmony';
const { Harmony: Index } = require('@harmony-js/core');
const { ChainID, ChainType } = require('@harmony-js/utils');
const magic = new Magic('YOUR_API_KEY', {
extensions: [
new HarmonyExtension({
rpcUrl: 'https://api.s0.b.hmny.io',
chainId: ChainID.HmyTestnet,
}),
],
});
const harmony = new Index(
// rpc url
'https://api.s0.b.hmny.io',
{
// chainType set to Index
chainType: ChainType.Harmony,
// chainType set to HmyLocal
chainId: ChainID.HmyTestnet,
},
);
let contractAddress = '0x67a3f8db0c98524e8e4513f95cd68f7fbbca7f06';
const contractAbi = [
{
constant: false,
inputs: [
{
internalType: 'uint256',
name: 'num',
type: 'uint256',
},
],
name: 'store',
outputs: [],
payable: false,
stateMutability: 'nonpayable',
type: 'function',
},
{
constant: true,
inputs: [],
name: 'retreive',
outputs: [
{
internalType: 'uint256',
name: '',
type: 'uint256',
},
],
payable: false,
stateMutability: 'view',
type: 'function',
},
];
const deployedContract = harmony.contracts.createContract(contractAbi, contractAddress);
const tx = await deployedContract.methods.store(900);
let { txPayload } = tx.transaction;
txPayload.from = (await magic.user.getMetadata()).publicAddress;
txPayload.gasLimit = '210000';
txPayload.gasPrice = '1000000000';
const txHash = await magic.harmony.sendTransaction(txPayload);
console.log('transaction hash', txHash);