I am trying to check if I am able to send one test ether from one Wallet (Wallet 2) to another Wallet (Wallet 1) using JavaScript. I tried to do this using the codes below but for some reason, this is not working with me and I do not understand the error. I am new to this, so please bear with me as I am trying to introduce my code.
First of all I start as simple as this.
Please note that in order to perform this code you will need to create your own 1. Infura API key, 2. Mnemonic Phrase of Wallet 1 (which contains no test ether), 3. The private key of Wallet 2 (which contain 15 test Ether).
// Get references to the modules (or the module content) that we need
const ethers = require('ethers');
// Get references to the modules (or the module content) that we need
var Web3 = require('web3');
// Use your Project ID Infura API using the rinkeby testnet
const Project_ID_Infura = '';
// This represent the API which is important to get connected to the provider (i.e. the blockchain testnet)
// This apikey is recieved from Infura which I did a new project to get the API key!
const apiKey = 'https://rinkeby.infura.io/v3/' + Project_ID_Infura;
// This represent the API which is important to get connected to the provider (i.e. the blockchain testnet)
// This apikey is recieved from Infura which I did a new project to get the API key!
const apiKey = 'https://rinkeby.infura.io/v3/' + Project_ID_Infura;
// Creating a new instance of the Web3 and ensuring that it is connected to the rinkeby testnet using Infura API
const web3 = new Web3(new Web3.providers.HttpProvider(apiKey));
var util = require('ethereumjs-util');
// Introducing the same mnemonic phrase as the pre-recorded lecture of this course
const Mnemonic_Phrase = 'Create your own Mnemonic';
// Create a Wallet from a Mnemonic phrase
const Wallet_1 = ethers.Wallet.fromMnemonic(Mnemonic_Phrase);
var Private_key_Wallet_2 = '0xc7fdadfda...';
// Creating a Wallet based on private key!
const Wallet_2 = new ethers.Wallet(Private_key_Wallet_2);
// Let us create a function that enables us to get the money from a defined wallet
async function Check_balance(wallet) {
Ballance_wallet = await web3.eth.getBalance(wallet.address);
Ballance_wallet = web3.utils.fromWei(Ballance_wallet, 'ether');
return Ballance_wallet;
}
// Creating transaction with variable inputs
function tx_vriable_input(gasPrice_1, gasLimit_1, to_wallet) {
// Creating a transaction by sending from an EOA to another!
var TxData = {
nonce: 1,
gasPrice: gasPrice_1,
gasLimit: gasLimit_1,
to: to_wallet.address,
value: 1,
};
return TxData;
}
async function test1() {
Balance_Wallet_1 = await Check_balance(Wallet_1);
Balance_Wallet_2 = await Check_balance(Wallet_2);
console.log('This is the balance of the Wallet 1: ', Balance_Wallet_1); // 0
console.log('This is the balance of the Wallet 2: ', Balance_Wallet_2); //18.751
// Creating a transaction
// Get the current gas price
Current_gas_price = await web3.eth.getGasPrice();
// Do some modification to incentivise the miner to mine your transaction
Acceptable_gas_price = Number.parseInt(Current_gas_price) * 1.4;
// Get the latest average gas limit of the latest block
var block = await web3.eth.getBlock('latest');
number_of_transactions_per_latest_block = block.transactions.length;
gas_limit_of_all_the_block = block.gasLimit;
average_gas_limit_per_transaction =
gas_limit_of_all_the_block / number_of_transactions_per_latest_block;
// console.log('Hello ', average_gas_limit_per_transaction);
// Get the nonce of the wallet
let nonce = await web3.eth.getTransactionCount(Wallet_2.address);
console.log('Nonce of Wallet 2: ', nonce + 1);
// Import the transaction method
const EthereumTx = require('ethereumjs-tx').Transaction;
tx_data = tx_vriable_input(
Acceptable_gas_price,
average_gas_limit_per_transaction,
Wallet_1
);
console.log(tx_data);
// Create the first transaction
var first_tx1 = new EthereumTx(tx_data, { chain: 'rinkeby' });
console.log('Before Signing');
// Now we need to sign this contract using Wallet 2 private key
var Private_key_to_sign_Wallet_2 = new Buffer.from(
Wallet_2.privateKey.replace('0x', ''),
'hex'
);
// Sign first_tx1 transaction
first_tx1.sign(Private_key_to_sign_Wallet_2);
console.log('After Signing');
// Also known as raw transaction in the Bitcoin blockchain framework!
serializedTx = first_tx1.serialize();
signedTransaction = '0x' + serializedTx.toString('hex');
web3.eth.sendSignedTransaction(signedTransaction).then(receipt => {
console.log('receipt', receipt);
this.setState({ rawReceipt: receipt });
});
Balance_Wallet_1_new = await Check_balance(Wallet_1);
console.log('The new balance of Wallet 1 is: ', Balance_Wallet_1_new);
}
test1();
This will give some understanding of my code whereby I failed to do the simple task of sending test Ether from one EOA (Wallet 2) to another EOA (Wallet 1). Remember that I took into account most of the things I have seen online but if there is something I am not aware of please let me be aware of it.
nonce: 1
hardcoded it should beweb3.eth.getTransactionCount(sender)
instead.