-
Notifications
You must be signed in to change notification settings - Fork 30
/
7_Giver.sol
62 lines (51 loc) · 2.07 KB
/
7_Giver.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
pragma tvm-solidity >= 0.72.0;
pragma AbiHeader expire;
import "7_CrashContract.sol";
interface AbstractContract {
function receiveTransfer(uint64 number) pure external;
}
//This contract allows to perform different kinds of currency transactions and control the result using the fallback function.
contract Giver {
// State variable storing the number of times receive/fallback/onBounce function was called.
uint public counter = 0;
constructor() {
// check that contract's public key is set
require(tvm.pubkey() != 0, 101);
// Check that message has signature (msg.pubkey() is not zero) and message is signed with the owner's private key
require(msg.pubkey() == tvm.pubkey(), 102);
tvm.accept();
}
modifier checkOwnerAndAccept {
// Check that message was signed with contracts key.
require(msg.pubkey() == tvm.pubkey(), 102);
tvm.accept();
_;
}
onBounce(TvmSlice /*slice*/) external {
++counter;
}
// This function can transfer currency to an existing contract with fallback
// function.
function transferToAddress(address destination, coins value) external view checkOwnerAndAccept {
destination.transfer(value);
}
// This function calls an AbstractContract which would case a crash and call of onBounce function.
function transferToAbstractContract(address destination, coins amount) external view checkOwnerAndAccept {
AbstractContract(destination).receiveTransfer{value: amount}(123);
}
// This function call a CrashContract's function which would cause a crash during transaction
// and call of onBounce function.
function transferToCrashContract(address destination, coins amount) external view checkOwnerAndAccept {
CrashContract(destination).doCrash{value: amount}();
}
// Function which allows to make a transfer to an arbitrary address.
function transferToAddress2(address destination, coins value, bool bounce, uint16 flag)
view
public
checkOwnerAndAccept
{
// Runtime function that allows to make a transfer with arbitrary settings
// and can be used to send tons to non-existing address.
destination.transfer(value, bounce, flag);
}
}