This example aims to demonstrate how to send a simple transaction that is manually signed.
Short overview of example:
- Prerequisites
- Build example
- Registering the Tolar plugin with a web3.js instance
- Get nonce
- Send transaction
- NodeJS (LTS/Iron)
- npm
- web3js 4.0 or higher
# Install
npm install
# Build
npm run build
# Run
npm run dev
First, we create a web3js object and set the provider to the local Tolar Hashnet network.
The next step is to register the Tolar Hashnet plugin in accordance with the web3JS plugin guide.
const web3 = new Web3("http://127.0.0.1:8200/jsonrpc");
web3.registerPlugin(new TolarPlugin());
Nonce is obtained by calling Tolar specific getNonce method. Nonce returns an INVALID_NONCE value for genesis addresses that are used for the first time.
let nonce = await web3.tolar.getNonce(senderAddress);
if(nonce === INVALID_NONCE) {
nonce = 0n;
}
First, we create a transaction object called TolTx
that sets all provided transaction fields, serializes and signs the transaction date.
Tolar transactions are serialized using protocol buffer format, and then the resulting binary data is converted to a hex string.
const tx = TolTx.fromTransaction(
{from: senderAddress, to: receiver, value: 1000000000000000000n, networkId: NetworkId.Local, nonce: nonce},
senderPrivateKey
);
const txHash = await web3.tolar.sendSignedTransaction(tx.rawSignedTx);