Created
July 18, 2024 17:00
-
-
Save Oluwatobilobaoke/919515d48cfce445b763573d9a9b108d to your computer and use it in GitHub Desktop.
Mortgage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: Unlicensed | |
pragma solidity 0.8.19; | |
import "forge-std/Test.sol"; | |
import "../src/Mortgage.sol"; // Adjust the import path according to your directory structure | |
contract MortgageTest is Test { | |
Mortgage mortgage; | |
MustCall mustCall; | |
address mortgagee = address(0xa); | |
address mortgagor = address(0xb); | |
function setUp() public { | |
mustCall = new MustCall(); | |
mortgage = new Mortgage(address(mortgagee), address(mustCall)); | |
// Fund the contract for testing purposes | |
fundUserContract(address(mortgage)); | |
} | |
function testMortgageDeposit() public { | |
switchSigner(mortgagor); | |
fundUserEth(mortgagor); | |
// check the mortgagor balance | |
uint256 bal = address(mortgagor).balance; | |
console.log("balance of mortgagor=>>>", bal); | |
//check the contract balance | |
uint256 contractbal2 = mortgage.contractBalance(); | |
console.log("contract balance=>>>", contractbal2); | |
mortgage.mortgagorDeposit{value: 2 ether}(); | |
assertEq(mortgage.contractBalance(), 12 ether); | |
assertEq(mortgage.balances(mortgagor), 2 ether); | |
} | |
function testDepositAndWithdraw() public { | |
switchSigner(mortgagee); | |
//check the contract balance | |
uint256 contractbal2 = mortgage.contractBalance(); | |
console.log("contract balance=>>>", contractbal2); | |
mortgage.withdrawal(1 ether); | |
} | |
function switchSigner(address _newSigner) public { | |
address foundrySigner = 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38; | |
if (msg.sender == foundrySigner) { | |
vm.startPrank(_newSigner); | |
} else { | |
vm.stopPrank(); | |
vm.startPrank(_newSigner); | |
} | |
} | |
function fundUserEth(address userAdress) public { | |
vm.deal(address(userAdress), 5 ether); | |
} | |
function fundUserContract(address userAdress) public { | |
vm.deal(address(userAdress), 10 ether); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: Unlicensed | |
pragma solidity 0.8.19; | |
contract MustCall { | |
function communicate(uint256 message) external {} | |
} | |
contract Mortgage is MustCall { | |
address public _mortgagee; | |
address public _mortgagor; | |
MustCall private mustCall; | |
mapping(address => uint256) public balances; | |
event Withdrawn(address indexed mortgagee, uint256 amount); | |
constructor(address mortgageel, address _mustCall) { | |
_mortgagee = mortgageel; | |
mustCall = MustCall(_mustCall); | |
} | |
function mortgagorDeposit() public payable { | |
require(msg.value >= 1 ether, "you cannot deposit less than 1 ether"); | |
balances[msg.sender] += msg.value; | |
} | |
function withdrawal(uint256 _amount) external mortgageeOnly { | |
payable(msg.sender).transfer(_amount); | |
emit Withdrawn(msg.sender, _amount); | |
mustCall.communicate(_amount); | |
} | |
function contractBalance() public view returns (uint) { | |
return address(this).balance; | |
} | |
modifier mortgageeOnly() { | |
require(msg.sender == _mortgagee); | |
_; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment