Ausnahme gefangen: SSL certificate problem: certificate is not yet valid ๐Ÿ“Œ How to Send Ethers Programmatically without Metamask

๐Ÿ  Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeitrรคge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden รœberblick รผber die wichtigsten Aspekte der IT-Sicherheit in einer sich stรคndig verรคndernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch รผbersetzen, erst Englisch auswรคhlen dann wieder Deutsch!

Google Android Playstore Download Button fรผr Team IT Security



๐Ÿ“š How to Send Ethers Programmatically without Metamask


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

Introduction

This tutorial will show you how to transfer Ethers using code rather than Metamask. The implementation makes use of the ethers library, which allows JavaScript to interact with Ethereum networks and contracts.

Upon completion, you will have gained a deeper understanding of how to programmatically transmit Ethers and will have a working sample for future reference.

If you're passionate about Web3 development, make sure to subscribe to my channel for a variety of educational videos. If you'd like to connect with me, take a look at my services.

Sending Ethers without Metamask

Here is a full code performing a programmatic transaction of ethers between accounts.

To dive deeper into the world of Web3 development, check out this video on how to build a decentralized autonomous organization.


Watch the video

Let's start by taking a closer look at the code and understanding how it works.

Clone this repository into your preferred working directory using the command below. Make sure you have NodeJs and Git Bash installed on your local machine before proceeding with these steps.

git clone https://github.com/Daltonic/tailwind_ethers_starter_kit <PROJECT NAME>

Next, you need to install the packages using npm install or yarn install on that project directory.

Next, create a file in the root of this project called transactions.js and start by importing the **ethers** library from hardhat:

const { ethers } = require('hardhat')

Next, it defines two helper functions, **toWei** and **fromWei**, to convert between Ether and its smallest unit, Wei. This is important because the Ethereum network operates in Wei, so we need to make sure the amounts we send are in Wei.

const toWei = (num) => ethers.utils.parseEther(num.toString())
const fromWei = (num) => ethers.utils.formatEther(num)

These two functions, **toWei** and **fromWei**, convert between ether and wei. **toWei** converts a value in ether to its equivalent in wei, and **fromWei** does the opposite. The **parseEther** and **formatEther** methods from the **ethers.utils** module are used for this conversion.

const sendEthers = async (recipientAddress, amountInWei) => {
  return new Promise(async (resolve, reject) => {
    const privateKey =
      '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80';
    const provider = new ethers.providers.JsonRpcProvider(
      'http://localhost:8545',
    );
    const wallet = new ethers.Wallet(privateKey, provider);
    const gasPrice = provider.getGasPrice();
    const nonce = provider.getTransactionCount(wallet.address, 'latest');
    const transaction = {
      to: recipientAddress,
      value: amountInWei,
      gasPrice,
      gasLimit: ethers.utils.hexlify(100000),
      nonce,
    };
    await wallet
      .sendTransaction(transaction)
      .then((transactionHash) => resolve(transactionHash))
      .catch((error) => reject(error));
  });
};

The **sendEthers** function is defined to send ethers from one wallet to another. It takes two parameters: **recipientAddress** and **amountInWei** which represent the address of the recipient and the amount to be sent, respectively.

A **privateKey** and a **provider** are created. The transaction is signed with the private key, and the provider communicates with the Ethereum network. Using the private key and the provider, a wallet instance is created.

The provider's getGasPrice and getTransactionCount methods are used to obtain the current gasPrice and nonce of the wallet, respectively. The gasPrice represents the cost of one unit of gas, and the nonce represents the number of transactions sent from the wallet.

A **transaction** object is created with the following parameters: **to** (recipientAddress), **value** (amountInWei), **gasPrice**, **gasLimit** (100000 in hex), and **nonce**. The **sendTransaction** method of the **wallet** instance is then used to send the transaction. If the transaction is successful, the promise resolves with the transaction hash, otherwise it rejects with an error.

const getBalance = async (walletAddress) => {
  return new Promise(async (resolve, reject) => {
    const provider = new ethers.providers.JsonRpcProvider(
      'http://localhost:8545',
    )
    await provider
      .getBalance(walletAddress)
      .then((balanceInWei) => resolve(balanceInWei))
      .catch((error) => reject(error))
  })
}

The **getBalance** function returns a promise that resolves with the balance of a wallet in wei. The function creates a JsonRpcProvider object and calls the **getBalance** method on it.

The method takes the address of the wallet as an argument. The **getBalance** method returns a Promise that resolves with the balance of the wallet in wei. If an error occurs, the Promise is rejected with the error.

Check out the video below to learn how to code a blog smart contract in solidity. This will give you a head start on how to advance in Web3 development.


Watch the video

async function main() {
  // the rest of the codes goes in here
}

The **main** function is defined to perform the transfer of ethers.

;[sender, receiver] = await ethers.getSigners()
const amountInWei = toWei(4.7)

console.log('\n')
let balance = await getBalance(sender.address)
console.log('Sender balance before transfer: ', fromWei(balance))
balance = await getBalance(receiver.address)
console.log('Receiver balance before transfer: ', fromWei(balance))
console.log('\n')

The **ethers.getSigners** method is called to obtain the sender and receiver addresses. The amount to be sent is converted to wei using the **toWei** function.

The balance of the sender and receiver is logged before the transfer by calling the **getBalance** function twice and passing the addresses of the sender and receiver as arguments.

await sendEthers(receiver.address, amountInWei).then((data) =>
  console.log('Transaction Details: ', data),
)
console.log('\n')

The **sendEthers** function is called to initiate the transfer of ethers. The function takes the recipient's address and the amount in wei as arguments.

The **sendEthers** function returns a Promise that resolves with the transaction details. This Promise is logged to the console.

balance = await getBalance(sender.address)
console.log('Sender balance after transfer: ', fromWei(balance))
balance = await getBalance(receiver.address)
console.log('Receiver balance after transfer: ', fromWei(balance))
console.log('\n')

The balance of the sender and receiver is logged again to show that the transfer was successful. The **getBalance** function is called twice to obtain the balance of the sender and receiver after the transfer. The result is logged onto the console.

Conclusion

In conclusion, this tutorial has shown how to send Ethers programmatically without the need for Metamask.

The code provided demonstrates the use of the **ethers** library in JavaScript to interact with the Ethereum network and perform a programmatic transaction of ethers between accounts.

The code includes helper functions to convert between Ether and its smallest unit, Wei, as well as functions to send ethers and get the balance of a wallet.

The code also demonstrates the use of private keys, providers, wallets, gas prices, nonces, and transactions in Ethereum.

The article has provided a good foundation for understanding how to send Ethers programmatically and serves as a useful reference for those starting out in this area.

My  latest book

Obtain a copy of my book, "Capturing Smart Contract Development", to attain the skills and knowledge needed to be a sought-after smart contract developer.

Remember to sign up for my channel to see various web3 development videos. Take care until our next tutorial.

About the Author

Gospel Darlington is a full-stack blockchain developer with 6+ years of experience in the software development industry.

By combining Software Development, writing, and teaching, he demonstrates how to build decentralized applications on EVM-compatible blockchain networks.

For more information about him, kindly visit and follow his page on Twitter, Github, LinkedIn, or his website.

...



๐Ÿ“Œ How to Send Ethers Programmatically without Metamask


๐Ÿ“ˆ 95.88 Punkte

๐Ÿ“Œ How to Load Unlimited Free Ethers to Metamask Wallet


๐Ÿ“ˆ 52.57 Punkte

๐Ÿ“Œ NameCheap's email hacked to send Metamask, DHL phishing emails


๐Ÿ“ˆ 31.93 Punkte

๐Ÿ“Œ NameCheap's Email Hacked To Send Metamask, DHL Phishing Emails


๐Ÿ“ˆ 31.93 Punkte

๐Ÿ“Œ EIP 712: A simple example using Ethers.js & Hardhat


๐Ÿ“ˆ 31.83 Punkte

๐Ÿ“Œ Interacting with Smart Contracts using ethers.js


๐Ÿ“ˆ 31.83 Punkte

๐Ÿ“Œ How to Restart your App Programmatically


๐Ÿ“ˆ 24.02 Punkte

๐Ÿ“Œ Editing Configuration Files Programmatically


๐Ÿ“ˆ 24.02 Punkte

๐Ÿ“Œ How could i start a software and inject a String so i can later programmatically fetch it (i'll use python)


๐Ÿ“ˆ 24.02 Punkte

๐Ÿ“Œ How do tools like drozer etc. find the publicly exported activities of an app. How can this be done programmatically?


๐Ÿ“ˆ 24.02 Punkte

๐Ÿ“Œ How to ( programmatically ) tell who is running "the" window manager?


๐Ÿ“ˆ 24.02 Punkte

๐Ÿ“Œ Resolv.conf not modifying programmatically but has no issues on manual editing.


๐Ÿ“ˆ 24.02 Punkte

๐Ÿ“Œ How to check programmatically when a filesystem goes south.


๐Ÿ“ˆ 24.02 Punkte

๐Ÿ“Œ Security Leaders are Calling for Industry to Take Action and Programmatically Improve Secure Coding Education


๐Ÿ“ˆ 24.02 Punkte

๐Ÿ“Œ Creating videos programmatically with Remotion on The Download


๐Ÿ“ˆ 24.02 Punkte

๐Ÿ“Œ Configure XFCE 4 programmatically with the help of watch-xfce-xfconf


๐Ÿ“ˆ 24.02 Punkte

๐Ÿ“Œ Programmatically Setting File Inputs in JavaScript


๐Ÿ“ˆ 24.02 Punkte

๐Ÿ“Œ Programmatically Setting File Inputs in JavaScript


๐Ÿ“ˆ 24.02 Punkte

๐Ÿ“Œ Programmatically deploy your GitHub Repo on Netlify


๐Ÿ“ˆ 24.02 Punkte

๐Ÿ“Œ How to Programmatically Cancel MRP Records in Odoo


๐Ÿ“ˆ 24.02 Punkte

๐Ÿ“Œ Programmatically change your system&#8217;s mic and speakers with NirCmd and Elgato StreamDeck


๐Ÿ“ˆ 24.02 Punkte

๐Ÿ“Œ Analyze your builds programmatically with the C++ Build Insights SDK


๐Ÿ“ˆ 24.02 Punkte

๐Ÿ“Œ Programmatically Navigate Using React Router and Hooks


๐Ÿ“ˆ 24.02 Punkte

๐Ÿ“Œ Programmatically Disconnect Device From Another Device


๐Ÿ“ˆ 24.02 Punkte

๐Ÿ“Œ MySQL Dual Passwords โ€“ How To Manage Them Programmatically


๐Ÿ“ˆ 24.02 Punkte

๐Ÿ“Œ Implementing Wildcard Subdomain (Part 2) - Creating subdomain programmatically


๐Ÿ“ˆ 24.02 Punkte

๐Ÿ“Œ Add alternate contacts to AWS Organization member accounts programmatically


๐Ÿ“ˆ 24.02 Punkte

๐Ÿ“Œ This is the Send, encrypted end-to-end, this is the Send, my Mozillan friend


๐Ÿ“ˆ 22.39 Punkte

๐Ÿ“Œ Send | Share Self-Destructing File Online FREE Using Firefox Send


๐Ÿ“ˆ 22.39 Punkte

๐Ÿ“Œ Heard you can send pictures in the comments now. Send pics of Linux setups


๐Ÿ“ˆ 22.39 Punkte

๐Ÿ“Œ How to build a Telegram Bot that Send Quote after every 10 Send


๐Ÿ“ˆ 22.39 Punkte

๐Ÿ“Œ Send to Kindle 1.1.1.254 - Send your personal documents to your Kindle.


๐Ÿ“ˆ 22.39 Punkte

๐Ÿ“Œ Google entfernt Ethereum Webwallet Metamask vom Playstore


๐Ÿ“ˆ 20.74 Punkte

๐Ÿ“Œ MetaMask phishing steals cryptocurrency wallets via Google ads


๐Ÿ“ˆ 20.74 Punkte











matomo