Portal
Search…
Send Transaction
Use Web3.js to Send Transaction on Clover
For our example, we only need a single JavaScript file (arbitrarily named transaction.js) to create and send the transaction, which we will run using the node command in the terminal. The script will transfer 100 CLV from the genesis account to another address. For simplicity, the file is divided into three sections: variable definition, create transaction, and send transaction.
We need to set a couple of values in the variable definitions, then construct and sign the transaction:
    1.
    Create your Web3 constructor (Web3).
    2.
    Specify the received address, CLV amount, gas price and gas limit.
    3.
    Sign the transaction and broadcast it the Clover chain
The code looks like:
1
const Web3 = require('web3')
2
3
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:9933'));
4
const GENESIS_ACCOUNT = '0xe6206C7f064c7d77C6d8e3eD8601c9AA435419cE';
5
6
async function webTransfer(account, etherValue) {
7
try {
8
const before = await web3.eth.getBalance(account);
9
console.log(`before transfer: ${account}, has balance ${web3.utils.fromWei(before, "ether")}`);
10
11
const nonce = await web3.eth.getTransactionCount(GENESIS_ACCOUNT);
12
const signedTransaction = await web3.eth.accounts.signTransaction({
13
from: GENESIS_ACCOUNT,
14
to: account,
15
value: web3.utils.toWei(etherValue.toString(), "ether"),
16
gasPrice: web3.utils.toWei("100", "gwei"),
17
gas: "0x5208",
18
nonce: nonce
19
}, 'your private key configured somewhere');
20
await web3.eth.sendSignedTransaction(signedTransaction.rawTransaction);
21
const after = await web3.eth.getBalance(account);
22
console.log(`after transfer: ${account}, has balance ${web3.utils.fromWei(after, "ether")}`);
23
return { success: true };
24
} catch (e) {
25
return { success: false, message: e.toString() };
26
}
27
}
28
29
webTransfer('0x1874FC5f915aa9Fd24C379fE7F9ec40607CEf78A', 100).then(console.log)
Copied!

Running the Script

You can run the above script using command :
1
node transaction.js
Copied!
Last modified 6mo ago
Copy link