-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoint_savings.sol
75 lines (61 loc) · 2.93 KB
/
joint_savings.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
63
64
65
66
67
68
69
70
71
72
73
74
75
pragma solidity ^0.5.0;
// Define a new contract named `JointSavings`
contract JointSavings {
/*
Define the following variables in the new contract:
- Two variables of type `address payable` named `accountOne` and `accountTwo`
- A variable of type `address public` named `lastToWithdraw`
- Two variables of type `uint public` named `lastWithdrawAmount` and `contractBalance`
*/
address payable accountOne;
address payable accountTwo;
address public lastToWithdraw;
uint public lastWithdrawAmount;
uint public contractBalance;
/*
Define a function named `withdraw` that accepts two arguments:
- An `amount` of type `uint`
- A `recipient` of type `payable address`
*/
function withdraw(uint amount, address payable recipient) public {
/*
Define a `require` statement that checks if `recipient` is equal to either `accountOne` or `accountTwo`. If it isn't, the `require` statement returns the "You don't own this account!" text
*/
require(recipient == accountOne || recipient == accountTwo, "You don't own this account!");
/*
Define a `require` statement that checks if `balance` is sufficient for accomplishing the withdrawal operation. If there are insufficient funds, it returns the "Insufficient funds!" text
*/
require(amount <= contractBalance, "Insufficient funds!");
/*
Add an `if` statement to check if `lastToWithdraw` is not equal to (`!=`) to `recipient`. If it's not equal, set it to the current value of `recipient`
*/
if (lastToWithdraw != recipient) {
lastToWithdraw = recipient;
}
// Call the `transfer` function of the `recipient` and pass it the `amount` to transfer as an argument
recipient.transfer(amount);
// Set `lastWithdrawAmount` equal to `amount`
lastWithdrawAmount = amount;
// Set the `contractBalance` variable equal to the balance of the contract by using `address(this).balance` to reflect the new balance of the contract
contractBalance = address(this).balance;
}
// Define a `public payable` function named `deposit`
function deposit() public payable {
/*
Set the `contractBalance` variable equal to the balance of the contract by using `address(this).balance`
*/
contractBalance = address(this).balance;
}
/*
Define a `public` function named `setAccounts` that receive two `address payable` arguments named `account1` and `account2`.
*/
function setAccounts(address payable account1, address payable account2) public{
// Set the values of `accountOne` and `accountTwo` to `account1` and `account2`, respectively
accountOne = account1;
accountTwo = account2;
}
/*
Add a fallback function so that your contract can store ether that's sent from outside the deposit function
*/
function() external payable {}
}