- Hands-On Blockchain for Python Developers
- Arjuna Sky Kok
- 260字
- 2021-07-02 13:13:08
Sending ether to smart contracts
Now, let's send some ether to the smart contract. Let's use the second account for this. The second account wants to donate 5 ether using a smart contract as follows:
truffle(development)> Donation.deployed().then(function(instance) { return instance.donate({ from: "0x6d3eBC3000d112B70aaCA8F770B06f961C852014", value: 5000000000000000000 }); });
You would get the following output:

As well as the from field, you need to add a value field. In this value field, you input how much money you want to send to the smart contract. You may wonder why this number has so many zeros. When you transfer money in Ethereum blockchain, you must use the lowest currency unit (similarly to converting from dollars to cents), which is called wei. 1 ether is 1,000,000,000,000,000,000 wei (18 zeros). You want to send 5 ether, making it 5,000,000,000,000,000,000. Now, if you look at your Ganache screen, you will notice that the balance drops to 95 ether. So 5 ether is now held in the smart contract, as demonstrated in the following screenshot:

Let's withdraw this using the first account:
truffle(development)> Donation.deployed().then(function(instance) { return instance.receive_donation({ from: "0xb105F01Ce341Ef9282dc2201BDfdA2c26903da77" }); });
You will get the following output:

This execution has the same syntax as before, minus the value field. Now take a look at the Ganache screen. The first account has between 104 and 105 ether (such as 104.8 or 104.9 ether). It is not 105 ether because we already outlaid some money launching the smart contract using the first account, and for paying the transaction fee when executing some functions in the smart contract:

- 尖叫的數(shù)學(xué):令人驚嘆的數(shù)學(xué)之美
- 有限自動機理論
- 我的第一本趣味數(shù)學(xué)書2
- 粗糙集的論域擴展理論及在專家系統(tǒng)中的應(yīng)用
- 現(xiàn)代數(shù)值計算(第2版)
- 魔方的思維世界
- Abaqus有限元分析從入門到精通(2022版)
- 數(shù)學(xué)的力量
- 數(shù)字、代數(shù)和圖象(全彩版)
- Hyperledger Cookbook
- 丈量世界:時間、空間與數(shù)學(xué)(萬物皆數(shù)學(xué))
- 美妙的數(shù)學(xué)(插圖珍藏版)
- 計算進化史:改變數(shù)學(xué)的命運
- 黎曼猜想漫談:一場攀登數(shù)學(xué)高峰的天才盛宴
- ANSYS 15.0有限元分析從入門到精通 (清華社視頻大講堂大系)