我有以下 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");
}
}
我正在尝试使用 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)
})
})
测试没有最后两行。但是,我试图让代码以美元打印出来,即用户帐户中的金额。出于某种原因,我在 bank.convertToUSD(depositer)
上收到
VM Exception while processing transaction: revert
错误
如果有人能指出我可能做错了什么,那就太好了,因为这是我第一次学习这个。谢谢!