0% found this document useful (0 votes)
18 views2 pages

Assessment - Sol File

The document outlines a Solidity smart contract named 'Assessment' that allows the contract owner to deposit and withdraw funds while maintaining a balance. It includes functions to check the balance, return the owner's address, and verify ownership. Custom error handling is implemented for insufficient balance during withdrawals.

Uploaded by

Pradeep Sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

Assessment - Sol File

The document outlines a Solidity smart contract named 'Assessment' that allows the contract owner to deposit and withdraw funds while maintaining a balance. It includes functions to check the balance, return the owner's address, and verify ownership. Custom error handling is implemented for insufficient balance during withdrawals.

Uploaded by

Pradeep Sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

// SPDX-License-Identifier: UNLICENSED

pragma solidity ^0.8.9;

//import "hardhat/console.sol";

contract Assessment {

address payable public owner;


uint256 public balance;

event Deposit(uint256 amount);


event Withdraw(uint256 amount);

constructor(uint initBalance) payable {


owner = payable(msg.sender);
balance = initBalance;
}

function getBalance() public view returns(uint256){


return balance;
}

function deposit(uint256 _amount) public payable {


uint _previousBalance = balance;

// make sure this is the owner


require(msg.sender == owner, "You are not the owner of this account");

// perform transaction
balance += _amount;

// assert transaction completed successfully


assert(balance == _previousBalance + _amount);

// emit the event


emit Deposit(_amount);
}

// custom error
error InsufficientBalance(uint256 balance, uint256 withdrawAmount);

function withdraw(uint256 _withdrawAmount) public {


require(msg.sender == owner, "You are not the owner of this account");
uint _previousBalance = balance;
if (balance < _withdrawAmount) {
revert InsufficientBalance({
balance: balance,
withdrawAmount: _withdrawAmount
});
}

// withdraw the given amount


balance -= _withdrawAmount;

// assert the balance is correct


assert(balance == (_previousBalance - _withdrawAmount));

// emit the event


emit Withdraw(_withdrawAmount);
}

// New function 1: Return the owner address


function getOwner() public view returns (address) {
return owner;
}

// New function 2: Return the contract's current ether balance


function getContractBalance() public view returns (uint256) {
return
address(this).balance;
}

// New function 3: Return if the caller is the owner


function isOwner() public view returns (bool) {
return msg.sender == owner;
}
}

You might also like