-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
31_SendEther.sol
48 lines (38 loc) · 1.51 KB
/
31_SendEther.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/* Note:
We can send Ether to other contracts by
1. transfer (uses 2300 gas stipend, throws error)
2. send (uses 2300 gas stipend, returns bool status)
3. call (forward all gas or set gas, returns bool status)
*/
contract SendEther{
// To enable the contract to store ether while deploying, we have to make the constructor payable.
constructor() payable{}
function transferExample(address payable _receiver) external payable{
// This will transfer the amount: number of wei sent with the message
_receiver.transfer(msg.value);
}
function sendExample(address payable _receiver) external payable{
// This will transfer the amount: number of wei sent with the message
bool sentStatus = _receiver.send(msg.value);
require(sentStatus,"Fund Transfer Failed");
}
// This is the recommended method (more to be discussed later)
function callExample(address payable _receiver) external payable{
// This will transfer the amount: number of wei sent with the message
(bool sentStatus,) = _receiver.call{value: msg.value}("");
require(sentStatus,"Fund Transfer Failed");
}
}
contract ReceiveEther{
event Log(address sender,uint amount,uint gas);
receive() external payable{
// gasleft() is a function which exists in global namespace.
// It returns the remaining gas.
emit Log(msg.sender,msg.value,gasleft());
}
function getBalance() external view returns(uint){
return address(this).balance;
}
}