Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.8 kB
34
Indexable
Never
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.7;

contract Bank {
    uint256 addressCount;
    mapping(uint256 => address) addressIndex;

    mapping(address => uint256) balanceLedger;

    modifier balanceCheck(uint256 amt) {
        require(amt <= balanceLedger[msg.sender], "Insufficient Balance");
        _;
    }

    function deposit() public payable {
        if (balanceLedger[msg.sender] == 0) {
            addressIndex[addressCount++] = msg.sender;
        }
        balanceLedger[msg.sender] = balanceLedger[msg.sender] + msg.value;
    }

    function withdraw(uint256 amt) public balanceCheck(amt) {
        //require(balanceLedger[msg.sender] >= amt, "insufficient balance");
        balanceLedger[msg.sender] = balanceLedger[msg.sender] - amt;
        payable(msg.sender).transfer(amt);
        //casting msg.sender as payable allowing it to receive ether
    }

    function transferToAnother(address recipient, uint256 amt)
        public
        balanceCheck(amt)
    {
        //require(balanceLedger[msg.sender] >= amt, "insufficient balance");

        balanceLedger[msg.sender] = balanceLedger[msg.sender] - amt;
        payable(recipient).transfer(amt);
    }

    function getMaxvalues() public view returns (address, uint256) {
        
        address maxAddress;
        uint256 maxBalance;
        address indexAddress;
        for (uint256 i = 0; i < addressCount; i++) {
            indexAddress = addressIndex[i];
            if (maxBalance < balanceLedger[indexAddress]) {
                maxBalance = balanceLedger[indexAddress];
                maxAddress = indexAddress;
            }
        }

        return (maxAddress, maxBalance);
    }

    function getMyBalance() public view returns (uint256) {
        return balanceLedger[msg.sender];
    }
}
Leave a Comment