-
Notifications
You must be signed in to change notification settings - Fork 0
/
joint_savings.sol
67 lines (52 loc) · 2.97 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
pragma solidity ^0.5.0;
// Define a new contract named `JointSavings`
contract JointSavings{
/*
Inside the new contract define the following variables:
- 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 will accept two arguments.
- A `uint` variable named `amount`
- A `payable address` named `recipient`
*/
function withdraw(uint amount, address payable recipient) public {
//Define a `require` statement that checks if the `recipient` is equal to either `accountOne` or `accountTwo`.
//The `requiere` statement returns the text `"You don't own this account!"` if it does not.
require(recipient == accountOne || recipient == accountTwo, "You don't own this account!");
//Define a `require` statement that checks if the `balance` is sufficient to accomplish the withdraw operation.
// If there are insufficient funds, the text `Insufficient funds!` is returned.
require(address(this).balance >= amount, "Insufficient funds!");
//Add and `if` statement to check if the `lastToWithdraw` is not equal to (`!=`) to `recipient`
// If `lastToWithdraw` is not equal, then 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;
// Call the `contractBalance` variable and set it 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 {
//Call the `contractBalance` variable and set it 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;
}
//Finally, add the **default fallback function** so that your contract can store Ether sent from outside the deposit function.
function () external payable{}
}