Ошибка: недопустимый ответ JSON RPC: «» при использовании expectRevert из открытых помощников по тестированию zeppelin.
Столкнулся с проблемой при тестировании
withdraw
метод, я ожидаю, что транзакция должна быть отменена, но ошибка
Error: Invalid JSON RPC response: ""
. Ниже мой контракт и тестовый файл:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract Timelock is Ownable {
using SafeMath for uint256;
// map that keep user balances
mapping(address => uint256) private balances;
// map that keep user locked time
mapping(address => uint256) private lockTime;
uint256 private constant TIME_LOCK = 1 days;
modifier notLocked(address _sender) {
require(block.timestamp > lockTime[_sender], "locked");
_;
}
// allow user to deposit, fund will be locked
function deposit() external payable {
balances[msg.sender] += msg.value;
lockTime[msg.sender] = block.timestamp + TIME_LOCK;
}
// user can withdraw after being unlocked
function withdraw() external notLocked(msg.sender) {
// check that the sender has deposited
require(balances[msg.sender] > 0, "not deposited");
uint256 amount = balances[msg.sender];
balances[msg.sender] = 0;
(bool success, ) = msg.sender.call{value: amount}("");
require(success, "failed to sent ether");
}
function getDeposit(address _from) public view returns (uint256) {
return balances[_from];
}
}
Тестовый файл
const { expect } = require('chai');
const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
describe("Timelock", () => {
beforeEach(async () => {
const Timelock = await ethers.getContractFactory("Timelock");
const timelock = await Timelock.deploy();
await timelock.deployed();
this.timelock = timelock;
});
it("should deposit successfully", async () => {
const [_, addr1] = await ethers.getSigners();
const instance = this.timelock.connect(addr1);
const amount = ethers.utils.parseUnits('1', 'ether')
await instance.deposit({ value: amount });
const balance = await instance.getDeposit(addr1.address);
expect(balance, "should be 1 ether").to.equal(amount);
});
it("should init zero balance", async () => {
const [_, addr1] = await ethers.getSigners();
const instance = this.timelock.connect(addr1);
const balance = await instance.getDeposit(addr1.address);
expect(balance, "should be 1 ether").to.equal(ethers.BigNumber.from('0'));
});
it("should withdraw error", async () => {
const [_, addr1] = await ethers.getSigners();
const instance = this.timelock.connect(addr1);
const amount = ethers.utils.parseUnits('1', 'ether')
await instance.deposit({ value: amount });
await expectRevert(
instance.withdraw(),
"locked"
)
});
})