Quick Integration
Requirements
Node Version >= 16It is recommended to usenvmornvm-windows(for Windows) to manage Node versions.
tip
- This chapter introduces how to quickly install, import, create, and initiate transfer transactions using hymatrix-js.
Installation
Use yarn or npm to install. If installation fails, please check your network and try again.
- yarn
- npm
yarn add hymatrix-js
npm install hymatrix-js
Import
- ES Modules
- CommonJS
import HyMatrix from 'hymatrix-js'
const hyMatrix = new HyMatrix()
const HyMatrix = require('hymatrix-js')
const hyMatrix = new HyMatrix()
Creating a HyMatrix Instance
Ethereum Wallet Connection Method
- You can optionally inject configurations such as
signerorprivateKeywhen creating the instance. - Please download
MetaMaskor other Ethereum wallets.
1. Using a Package Manager
(1) Install hymatrix-js
- yarn
- npm
yarn add hymatrix-js
npm install hymatrix-js
(2) Import hymatrix-js and create an instance.
import HyMatrix, { Web3Provider } from 'hymatrix-js'
const accid = ''
// Browser Ethereum extension wallet
const provider = new Web3Provider(window.ethereum)
const hyMatrix = new HyMatrix({
debug: true, // Enable hymatrix dev environment
accid: accid, // Account address
signer: provider // eth signer provider
})
// Ethereum private key
const privateKey = '0x...'
const hyMatrix2 = new HyMatrix({
debug: true, // Enable hymatrix dev environment
accid: accid, // Account address
privateKey: privateKey // 0x + 64-digit private key, total 66 characters
})
tip
- In a WEB environment where multiple Ethereum wallets coexist, such as
MetaMask,OKX Wallet,TokenPocket Wallet, you can useeip6963:announceProviderandeip6963:requestProviderto distinguish them. - Related docs: https://eips.ethereum.org/EIPS/eip-6963#announce-and-request-events
Arweave Wallet Connection Method
- Download
Wanderwallet. - You need to inject
arJWKwhen creating:- When using the browser Wander wallet, simply inject
arJWK: 'use_wallet'. - When using Arweave JWKInterface JSON file, import the file. See
arweave Docs - Sample JWK.
- When using the browser Wander wallet, simply inject
1. Using a Package Manager
(1) Install hymatrix-js
- yarn
- npm
yarn add hymatrix-js
npm install hymatrix-js
(2) Import hymatrix-js and create an instance.
import HyMatrix from 'hymatrix-js'
// Browser Arweave extension wallet
const arAddress = await window.arweaveWallet.getActiveAddress()
const hyMatrix = new HyMatrix({
debug: true, // Enable hymatrix dev environment
accid: arAddress,
arJWK: 'use_wallet'
})
// JWKInterface KEY JSON // requires file import
// import jwkInterface from 'arweave-key-file.json'
const accid = ''
const hyMatrix2 = new HyMatrix({
debug: true, // Enable hymatrix dev environment
accid: accid, // Account address
arJWK: jwkInterface
})
tip
JWKInterface method: Refer to arweave Docs - Sample JWK. The private key format of arweave is a KEY JSON.
Success!
- Following the above steps, we have successfully created a HyMatrix instance. Let's initiate a transfer transaction next!
Transfer Transaction
After creating the HyMatrix instance successfully, you can use the sendMessage method to transfer assets (token) owned by the current account (accid) on the HyMatrix network.
Fill in the required processId, tags, and data, then call the interface below to complete the transaction:
processId: TheprocessIdto receive notifications.tags:Tagfields. To initiate a transfer, you must include at least the parametersAction,Recipient, andQuantity.
data: Signed data, optional.
// Transfer example using hmAR token
// 1. Confirm the transfer processId
const processId = 'GuuH1wCOBatG-JoKu42NkJMC7Cx-rjD8F5EEICLTNP8'
// 2. Confirm recipient address and amount
const recipient = '' // ethAddress or arAddress
const amt = '100' // transfer amount
const tags = [
{ name: 'Action', value: 'Transfer' },
{ name: 'Recipient', value: recipient },
{ name: 'Quantity', value: amt }
]
// 3. Obtain the URL node by processId to initiate the transaction successfully.
// If the URL is already known, this step can be skipped to save time.
const urls = await new HyMatrix().getNodesByProcess(processId)
// 4. Create HyMatrix instance
const hyMatrix = new HyMatrix({
arJWK: 'use_wallet',
url: urls[0]?.URL // Subsequent requests from hyMatrix will use this URL.
})
// 5. Create parameter structure
const params = {
tags,
processId,
data: '' // Optional, defaults to empty string
}
// 6. Initiate the transaction
const result = await hyMatrix.sendMessage(params)
console.log(result.id) // Get messageID, use it to check the transaction record
// wait a moment...
// 7. Query transaction record. Tip: Request will be made at the specified URL node.
const resultTx = await hyMatrix.getResult(processId, result.id)
console.log(resultTx) // Get detailed transaction info