-1

I have the following code for Bank.sol

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

import "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";

contract Bank is Initializable, AccessControlUpgradeable {
    // Code to handle two different roles: Manager and User
    bytes32 public constant MANAGER_ROLE = keccak256("MANAGER_ROLE");
    bytes32 public constant USER_ROLE = keccak256("USER_ROLE");
    mapping(address => uint256) private _balances;


    // Code for initializer
    function initialize() public initializer {

    }

    function deposit() public payable {
        _balances[msg.sender] += msg.value;

    }

    function convertToUSD(address account) public view returns (uint256) {
        uint256 ethAmount = _balances[account];
        int256 price = getLatestPrice();
        uint256 usdAmount = uint256(price) * ethAmount / 1e18;
        return usdAmount;
    }

    function getLatestPrice() public view returns (int256) {
        AggregatorV3Interface priceFeed = AggregatorV3Interface(0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e);
        (, int256 price, , , ) = priceFeed.latestRoundData();
        return price;
    }

    function balanceOf(address account) public view returns (uint256) {
        return _balances[account];
    }

    function withdraw(uint _amount) public {
        require (_balances[msg.sender] >= _amount, "Insufficient balance");
        _balances[msg.sender] -= _amount;
        (bool sent,) = msg.sender.call{value: _amount}("sent");
        require(sent, "Failed to Complete");
    }
}

And I am trying to run a test for it using Truffle

const Bank = artifacts.require('Bank.sol')

contract("Bank", async accounts => {
    it("User should be able to deposit funds", async () => {
        const bank = await Bank.new()
        const depositer = accounts[1]
        const amount = web3.utils.toWei('10', 'ether')
        await bank.deposit({from: depositer, value: amount})
        let balance = await bank.balanceOf(depositer)
        balance = parseInt(web3.utils.fromWei(balance, 'ether'))
        assert.equal(balance, 10)
        let usd = await bank.convertToUSD(depositer)
        console.log(usd)
    })
})

The test works without the last 2 lines. However, I am trying to get the code to print out in USD, the amount a user has in his account. For some reason, I am getting an error:

VM Exception while processing transaction: revert

on bank.convertToUSD(depositer)

TylerH
  • 20,799
  • 66
  • 75
  • 101

1 Answers1

0

When using chainlink, you need to specifically choose what address from chainlink you want to do your conversion with, and when you do normal testing, it is done locally where such an address doesn't exist.

The problem is fixed when you use the correct address and test it with the contract being deployed on the correct testnet.

TylerH
  • 20,799
  • 66
  • 75
  • 101