Example of request transaction
Learn how to send transactions to blockchain network using the EQ Hub API.
Before to start
- An API Key is required to use the API provided by EQ Hub. For information on how to check the API Key of your project, refer to here.
- In order to send a transaction, the microchain ID of a specific blockchain network is required. When sending transactions to the blockchain network you have built, you can check the blockchain network list and information in EQ Hub - [Network] - [Network] - [Network list].
- The web3.js version used in the example is 1.10.0. If the module does not operate normally, be sure to check the version. If you use web3.js version 4.x, please click here to check the changes.The library information used in the example is as follows.
- axios: ^1.4.0
web3.js: ^1.10.0
In this guide, we will look at the entire example code for sending a transaction by using the contents of the [Course of request transaction] and [Course for request transaction with Smart contract] guides.
The examples covered in this guide are:
- Transferring coins
- Sending Tokens
Transferring coins
Coin transfer is a transaction that does not go through a smart contract, and in this case, it does not go through the process of factor encoding.
import axios from "axios";
const Web3 = require("web3");
const WEB3 = new Web3();
const main = async () => {
const API_KEY = "YOUR_API_KEY";
const HOST = "https://ag.eqhub.eqbr.com";
let server;
server = axios.create({
baseURL: HOST,
});
server.defaults.headers.common["x-eq-ag-api-key"] = API_KEY;
const SENDER_ADDRESS = "YOUR_WALLET_ADDRESS";
const SENDER_PRIVATE_KEY = "YOUR_WALLET_PRIVATE_KEY";
const RECEIVER_ADDRESS = "RECEIVER_WALLET_ADDRESS";
const SEND_AMOUNT = 80;
//This is an arbitrarily set micro chain id.
const MY_MICRO_CHAIN_ID = 2000;
//Inquiring Nonce
const nonce = await server.get(`/api/v2/request/nonce?microChainId=${MY_MICRO_CHAIN_ID}&address=${SENDER_ADDRESS}`).then((response) => response.data.nonce);
//Inquiring Gas price & Gas
const gasPrice = await server.get(`/api/v2/micro-chains/${MY_MICRO_CHAIN_ID}/gas-price`).then((response) => response.data.gas_price);
const estimateGasBody = {
to: RECEIVER_ADDRESS,
from: SENDER_ADDRESS,
gasPrice: WEB3.utils.fromDecimal(gasPrice),
value: WEB3.utils.toHex(WEB3.utils.toWei(SEND_AMOUNT.toString())),
};
const gas = await server.post(`/api/v2/request/estimate-gas?microChainId=${MY_MICRO_CHAIN_ID}`, estimateGasBody).then((response) => response.data.gas);
const transaction = {
nonce,
to: RECEIVER_ADDRESS,
chainId: MY_MICRO_CHAIN_ID.toString(),
gasPrice: WEB3.utils.fromDecimal(gasPrice),
gas: WEB3.utils.fromDecimal(gas),
value: WEB3.utils.toHex(WEB3.utils.toWei(SEND_AMOUNT.toString())),
};
const { rawTransaction } = await WEB3.eth.accounts.signTransaction(transaction, SENDER_PRIVATE_KEY);
const sendTransactionBody = {
rawTransaction,
};
const transactionHash = await server
.post(`/api/v2/request/transaction?microChainId=${MY_MICRO_CHAIN_ID}`, sendTransactionBody)
.then((response) => response.data.transaction_hash);
};
main().then().catch();
Sending Tokens
Token transfer is the act of sending a transaction using the "send" function defined in the smart contract. In token smart contracts conforming to the ERC-20 standard, the name of the "transfer" function is "transfer".
import axios from "axios";
const Web3 = require("web3");
const WEB3 = new Web3();
const main = async () => {
const API_KEY = "YOUR_API_KEY";
const HOST = "https://ag.eqhub.eqbr.com";
let server;
server = axios.create({
baseURL: HOST,
});
server.defaults.headers.common["x-eq-ag-api-key"] = API_KEY;
const SENDER_ADDRESS = "YOUR_WALLET_ADDRESS";
const SENDER_PRIVATE_KEY = "YOUR_WALLET_PRIVATE_KEY";
const RECEIVER_ADDRESS = "RECEIVER_WALLET_ADDRESS";
const SEND_AMOUNT = 220;
//This is an arbitrarily set micro chain id.
const MY_MICRO_CHAIN_ID = 2000;
const CONTRACT_ADDRESS = "TOKEN_CONTRACT_ADDRESS";
const FUNCTION_NAME = "transfer";
const ABICode = await server
.get(`/api/v2/contracts/address/${CONTRACT_ADDRESS}/abi-code?microChainId=${MY_MICRO_CHAIN_ID}`)
.then((response) => response.data);
const contract = new WEB3.eth.Contract(ABICode);
const transferParameter = [RECEIVER_ADDRESS, WEB3.utils.toHex(WEB3.utils.toWei(SEND_AMOUNT.toString()))];
const encodeParameter = contract.methods[FUNCTION_NAME](...transferParameter).encodeABI();
// Inquiring Nonce
const nonce = await server.get(`/api/v2/request/nonce?microChainId=${MY_MICRO_CHAIN_ID}&address=${SENDER_ADDRESS}`).then((response) => response.data.nonce);
//Inquiring Gas price & Gas
const gasPrice = await server.get(`/api/v2/micro-chains/${MY_MICRO_CHAIN_ID}/gas-price`).then((response) => response.data.gas_price);
const estimateGasBody = {
to: CONTRACT_ADDRESS,
from: SENDER_ADDRESS,
gasPrice: WEB3.utils.fromDecimal(gasPrice),
data: encodeParameter,
};
const gas = await server.post(`/api/v2/request/estimate-gas?microChainId=${MY_MICRO_CHAIN_ID}`, estimateGasBody).then((response) => response.data.gas);
const transaction = {
nonce,
to: CONTRACT_ADDRESS,
chainId: MY_MICRO_CHAIN_ID.toString(),
gasPrice: WEB3.utils.fromDecimal(gasPrice),
gas: WEB3.utils.fromDecimal(gas),
data: encodeParameter,
};
const { rawTransaction } = await WEB3.eth.accounts.signTransaction(transaction, SENDER_PRIVATE_KEY);
const sendTransactionBody = {
rawTransaction,
};
const transactionHash = await server
.post(`/api/v2/request/transaction?microChainId=${MY_MICRO_CHAIN_ID}`, sendTransactionBody)
.then((response) => response.data.transaction_hash);
};
main().then().catch();
Minting Tokens
Token mint is the act of sending a transaction using the "mint" function defined in the smart contract. In token smart contracts conforming to the ERC-20 standard, the name of the "mint" function is "mint".
import axios from "axios";
const Web3 = require("web3");
const WEB3 = new Web3();
const main = async () => {
const API_KEY = "YOUR_API_KEY";
const HOST = "https://ag.eqhub.eqbr.com";
let server;
server = axios.create({
baseURL: HOST,
});
server.defaults.headers.common["x-eq-ag-api-key"] = API_KEY;
const RECIPIENT_ADDRESS = "RECIPIENT_WALLET_ADDRESS";
const RECIPIENT_PRIVATE_KEY = "RECIPIENT_WALLET_PRIVATE_KEY";
const MINT_AMOUNT = 1000;
//This is an arbitrarily set micro chain id.
const MY_MICRO_CHAIN_ID = 2000;
const TOKEN_CONTRACT_ADDRESS = "TOKEN_CONTRACT_ADDRESS";
const FUNCTION_NAME = "mint";
const ABICode = await server.get(`/api/v2/contracts/address/${TOKEN_CONTRACT_ADDRESS}/abi-code?microChainId=${MY_MICRO_CHAIN_ID}`).then((response) => response.data);
const contract = new WEB3.eth.Contract(ABICode);
const mintParameter = [RECIPIENT_ADDRESS, WEB3.utils.toHex(WEB3.utils.toWei(MINT_AMOUNT.toString()))];
const encodeParameter = contract.methods[FUNCTION_NAME](...mintParameter).encodeABI();
// Inquiring Nonce
const nonce = await server
.get(`/api/v2/request/nonce?microChainId=${MY_MICRO_CHAIN_ID}&address=${RECIPIENT_ADDRESS}`)
.then((response) => response.data.nonce);
//Inquiring Gas price & Gas
const gasPrice = await server.get(`/api/v2/micro-chains/${MY_MICRO_CHAIN_ID}/gas-price`).then((response) => response.data.gas_price);
const estimateGasBody = {
to: TOKEN_CONTRACT_ADDRESS,
from: RECIPIENT_ADDRESS,
gasPrice: WEB3.utils.fromDecimal(gasPrice),
data: encodeParameter,
};
const gas = await server.post(`/api/v2/request/estimate-gas?microChainId=${MY_MICRO_CHAIN_ID}`, estimateGasBody).then((response) => response.data.gas);
const transaction = {
nonce,
to: TOKEN_CONTRACT_ADDRESS,
chainId: MY_MICRO_CHAIN_ID.toString(),
gasPrice: WEB3.utils.fromDecimal(gasPrice),
gas: WEB3.utils.fromDecimal(gas),
data: encodeParameter,
};
const { rawTransaction } = await WEB3.eth.accounts.signTransaction(transaction, RECIPIENT_PRIVATE_KEY);
const sendTransactionBody = {
rawTransaction,
};
const transactionHash = await server
.post(`/api/v2/request/transaction?microChainId=${MY_MICRO_CHAIN_ID}`, sendTransactionBody)
.then((response) => response.data.transaction_hash);
};
main().then().catch();
Burning Tokens
Token burn is the act of burning a transaction using the "burn" function defined in the smart contract. In token smart contracts conforming to the ERC-20 standard, the name of the "burn" function is "burn".
import axios from "axios";
const Web3 = require("web3");
const WEB3 = new Web3();
const main = async () => {
const API_KEY = "YOUR_API_KEY";
const HOST = "https://ag.eqhub.eqbr.com";
let server;
server = axios.create({
baseURL: HOST,
});
server.defaults.headers.common["x-eq-ag-api-key"] = API_KEY;
const RECIPIENT_ADDRESS = "RECIPIENT_WALLET_ADDRESS";
const RECIPIENT_PRIVATE_KEY = "RECIPIENT_WALLET_PRIVATE_KEY";
const BURN_AMOUNT = 1000;
//This is an arbitrarily set micro chain id.
const MY_MICRO_CHAIN_ID = 2000;
const TOKEN_CONTRACT_ADDRESS = "TOKEN_CONTRACT_ADDRESS";
const FUNCTION_NAME = "burn";
const ABICode = await server.get(`/api/v2/contracts/address/${TOKEN_CONTRACT_ADDRESS}/abi-code?microChainId=${MY_MICRO_CHAIN_ID}`).then((response) => response.data);
const contract = new WEB3.eth.Contract(ABICode);
const burnParameter = [RECIPIENT_ADDRESS, WEB3.utils.toHex(WEB3.utils.toWei(BURN_AMOUNT.toString()))];
const encodeParameter = contract.methods[FUNCTION_NAME](...burnParameter).encodeABI();
// Inquiring Nonce
const nonce = await server
.get(`/api/v2/request/nonce?microChainId=${MY_MICRO_CHAIN_ID}&address=${RECIPIENT_ADDRESS}`)
.then((response) => response.data.nonce);
//Inquiring Gas price & Gas
const gasPrice = await server.get(`/api/v2/micro-chains/${MY_MICRO_CHAIN_ID}/gas-price`).then((response) => response.data.gas_price);
const estimateGasBody = {
to: TOKEN_CONTRACT_ADDRESS,
from: RECIPIENT_ADDRESS,
gasPrice: WEB3.utils.fromDecimal(gasPrice),
data: encodeParameter,
};
const gas = await server.post(`/api/v2/request/estimate-gas?microChainId=${MY_MICRO_CHAIN_ID}`, estimateGasBody).then((response) => response.data.gas);
const transaction = {
nonce,
to: TOKEN_CONTRACT_ADDRESS,
chainId: MY_MICRO_CHAIN_ID.toString(),
gasPrice: WEB3.utils.fromDecimal(gasPrice),
gas: WEB3.utils.fromDecimal(gas),
data: encodeParameter,
};
const { rawTransaction } = await WEB3.eth.accounts.signTransaction(transaction, RECIPIENT_PRIVATE_KEY);
const sendTransactionBody = {
rawTransaction,
};
const transactionHash = await server
.post(`/api/v2/request/transaction?microChainId=${MY_MICRO_CHAIN_ID}`, sendTransactionBody)
.then((response) => response.data.transaction_hash);
};
main().then().catch();
Concluding
Through the above process, the transaction has been successfully sent to the blockchain network. The API that sends the transaction returns the hash value(identification value) of the transaction in response. You can search the result of the transaction with the returned hash value.
For information on how to check the transaction result through the transaction hash value, refer to [here].
Updated over 1 year ago