🧰 Web3 [Serie Part 6/10] - Send Ether to a Smart Contract
Introduction
Sometimes it's necessary to load the Smart Contract with Ethers. Thus, we need to be able to send some token to our Smart Contract.
Since we are modifying the Blockchain, it will have to be mined, and by consequence it will cost some gas fees.
Actually, there are two ways to send Ether to a Smart Contract.
Method #1: Execute a Function
We will be using a different Smart Contract code for this new part. Thus, let's create a new file 'MyContract2.sol' in our contracts directory, and copy the code below. Comments are following.
We define a new contract MyContract2 including 2 functions.
We declare a public variable called message as a string type.
Function sendEther() is simulating a payment, while the second function is a fallback function. It will be detailed in section.
Don't forget to adapt the migration file (2_contract_deploy.js) accordingly.
We simply added the MyContract2 to be deployed. If you want things cleaner, you can drop the MyContract since we won't use it anymore. It's up to you.
Now, we can migrate our new MyContract2.sol by running the following command in our terminal:
truffle migrate --reset
Among the results, you should see our MyContract2 deployment feedback:
Now, that we deployed our new MyContract2.sol successfully, we can move on our Javascript script. We will create a new index2.js and copy the below code. Comments are following.
Now, let's change this index2.js script to send 1 ETH to our Smart Contract MyContract2.sol.
Here we call our sendEther() methods, using 'send API'. The send method requires several parameters. We will be using the 2 following parameters (more details here in the doc):
From: The address the transaction should be sent from.
Value: The value transferred for the transaction in wei.
Note: 1 WEI = 10^(-18) ETH.
🚨 WARNING, there is a caveat here 🚨
As you can see, the value we are transferring is written as a String type with quite a lot of 0.
If we don’t write the value as above, the value will be in Wei (i.e. 10^-18 ETH). Thus, you should write 1 000 000 000 000 000 000 . The problem is that such big numbers are not managed by Javascript. That’s why the trick is to write it as a String type.
If you have to perform math operations on this number, I recommend using the bn.js library. Here we keep it simple, and use the String version.
This being said, let's run our index2.js . We will see the following results:
Hurra ! It works. We get the receipt of our transaction, as well as the message ‘Ether sent!’. Thus, we successfully sent 1 ETH to our Smart Contract (MyContract2).
Method #2: Send Ether Directly
The second method consists in sending Ether to our Smart Contract, without calling any function.
In our Smart Contract, if you remember, we added what we call a ‘fallback’ method. This method will be fired by default, if a wrong function name is called in our JS file, or if no function name is used. It serves as a fallback…
Here is the updated index2.js. Comments are following.
Instead of calling a method from the Smart Contract, we call directly the sendTransaction method.
Thus we are not referencing a specific method from our Smart Contract. As parameters we are using:
From: same as previous.
To: the contract address we want to send ETH to: contract.options.address.
Value: same as previous.
Our fallback function should be fired. Like the previous test, we are console.logging the receipt, as well as the value of our public message. It should print ‘Fallback function!’.
After running the code, we see the results below:
🎉 🥳 👯♂️ 🎈 Congratulations, it works , hurra ! We successfully sent 1 ETH to our Smart Contract using the fallback method.
Send Ether from an Account to another One
In this additional part, we just want to send some Ethers from an account to another one.
Here, we won't use any Smart Contract.
Let's say, we want to send 1 ETH from Ganache's account #1 to the #2.
Before we start, here are the balances of our Ganache's accounts.
The code is simple, I'm sure you could to do it by yourself :-). Comments are following.
We use the sendTransaction method without mentioning any Smart Contract here, and we give 3 parameters.
From: the account from which the Ether will be withdrew.
To: the credited account.
Value: the amount of WEI we want to transfer. Again, we use the string format otherwise, we will have issue with Javascript (cf. Big Numbers).
Remember, 1 WEI = 1*10^(-18) ETH
Here is our console.log receipt:
And we can check our accounts balances from Ganache. We can see that 1ETH has been transfered from Account #1 to Account #2 !
Hurra! It works! We successfully transferred 1 ETH from Account 1 to Account 2.
You might also like...
May
23
🗄️ Store Sensor's Data to the Blockchain
In this tutorial, we will learn how to store data from a sensor to the Ethereum
Blockchain. At the end,
11 min read
Sep
01
🧰 Web3 [Serie Part 7/10] - Listen to Smart Contract Events
Introduction
What is an Event?
> An event emits data from a Smart Contract, when a transaction occurs.
Ethereum Smart Contracts
7 min read
Sep
01
🧰 Web3 [Serie Part 5/10] - Send Transactions to a Smart Contract (sendTransactions API)
Introduction
In the previous lecture, we read data from our Smart Contract. In this lecture,
we are going to write
3 min read
Sep
01
🧰 Web3 [Serie Part 4/10] - Call A Smart Contract
Introduction
In this chapter, we are going to interact with our Smart Contract.
First, we will use the Call API.
1 min read
Sep
01
🧰 Web3 [Serie Part 3/10] - Create A Contract Instance
Introduction
In this chapter, we will learn how to create an instance of our Smart Contract.
This part is required