A fintech startup company has recently hired you. This company is disrupting the finance industry with its own cross-border, Ethereum-compatible blockchain that connects financial institutions. Currently, the team is building smart contracts to automate many of the institutions’ financial processes and features, such as hosting joint savings accounts.
To automate the creation of joint savings accounts, you’ll create a Solidity smart contract that accepts two user addresses. These addresses will be able to control a joint savings account. Your smart contract will use ether management functions to implement a financial institution’s requirements for providing the features of the joint savings account. These features will consist of the ability to deposit and withdraw funds from the account.
-
The completed Solidity
JointSavings
smart contract. -
A folder named
Execution_Results
that contains at least eight images. These images should confirm that the deposit and withdrawal transactions, which are designed to test theJointSavings
functionality in the JavaScript VM, worked as expected.
The steps for this homework are divided into the following sections:
-
Create a Joint Savings Account Contract in Solidity
-
Compile and Deploy Your Contract in the JavaScript VM
-
Interact with Your Deployed Smart Contract
-
From the provided starter code, open the Solidity file named
joint_savings.sol
in the Remix IDE. -
Define a new contract named
JointSavings
. -
Define the following variables in the new contract:
-
Two variables of type
address payable
namedaccountOne
andaccountTwo
-
A variable of type
address public
namedlastToWithdraw
-
Two variables of type
uint public
namedlastWithdrawAmount
andcontractBalance
-
-
Define a function named
withdraw
that accepts two arguments:amount
of typeuint
andrecipient
of typepayable address
. In this function, code the following:-
Define a
require
statement that checks ifrecipient
is equal to eitheraccountOne
oraccountTwo
. If it isn’t, therequire
statement returns the “You don't own this account!” text. -
Define a
require
statement that checks ifbalance
is sufficient for accomplishing the withdrawal operation. If there are insufficient funds, it returns the “Insufficient funds!” text. -
Add an
if
statement to check iflastToWithdraw
is not equal (!=
) torecipient
. If it’s not equal, set it to the current value ofrecipient
. -
Call the
transfer
function of therecipient
, and pass it theamount
to transfer as an argument. -
Set
lastWithdrawAmount
equal toamount
. -
Set the
contractBalance
variable equal to the balance of the contract by usingaddress(this).balance
to reflect the new balance of the contract.
-
-
Define a
public payable
function nameddeposit
. In this function, code the following:- Set the
contractBalance
variable equal to the balance of the contract by usingaddress(this).balance
.
- Set the
-
Define a
public
function namedsetAccounts
that takes twoaddress payable
arguments, namedaccount1
andaccount2
. In the body of the function, set the values ofaccountOne
andaccountTwo
toaccount1
andaccount2
, respectively. -
Add a fallback function so that your contract can store ether that’s sent from outside the deposit function.
-
Compile your smart contract. If an error occurs, review your code, and make the necessary changes for a successful compilation.
-
In the Remix IDE, navigate to the “Deploy & Run Transactions” pane, and then make sure that “JavaScript VM” is selected as the environment.
-
Click the Deploy button to deploy your smart contract, and then confirm that it successfully deployed.
Now that your contract is deployed, it’s time to test its functionality! After each step, capture a screenshot of the execution, and then save it in a folder named Execution_Results
. You’ll share this folder with your final submission.
To interact with your deployed smart contract, complete the following steps:
-
Use the
setAccounts
function to define the authorized Ethereum address that will be able to withdraw funds from your contract.Note You can either use the following Ethereum addresses or create new, dummy addresses on the Vanity-ETH website, which includes an Ethereum vanity address generator.
Dummy account1 address: 0x0c0669Cd5e60a6F4b8ce437E4a4A007093D368Cb Dummy account2 address: 0x7A1f3dFAa0a4a19844B606CD6e91d693083B12c0
-
Test the deposit functionality of your smart contract by sending the following amounts of ether. After each transaction, use the
contractBalance
function to verify that the funds were added to your contract:-
Transaction 1: Send 1 ether as wei.
-
Transaction 2: Send 10 ether as wei.
-
Transaction 3: Send 5 ether.
Note Remembering how to convert ether to wei and vice versa can be challenging. So, you can use a website like Ethereum Unit Converter to ease doing the conversion.
-
-
Once you’ve successfully deposited funds into your contract, test the contract’s withdrawal functionality by withdrawing 5 ether into
accountOne
and 10 ether intoaccountTwo
. After each transaction, use thecontractBalance
function to verify that the funds were withdrawn from your contract. Also, use thelastToWithdraw
andlastWithdrawAmount
functions to verify that the address and amount were correct.