安全帽测试错误“错误:处理交易时虚拟机异常:已恢复,原因字符串‘存款更多’”

问题描述 投票:0回答:1

在 Hardhat Test.js 文件上出现

Error: VM Exception while processing transaction: reverted with reason string 'deposit more'
错误

测试.js ->

    it("should abe to withdraw if no one apply", async function() {
    const salary = "10" + "000000000000000000";
    const jobDesc = "https://example.com";
    const startBalance = await addrs[0].getBalance();
    const duration = 30 * 86400;
    
    //await addrs[0].sendTransaction({ to: tasksV1.address, value: salary });
    
    //taskid should be 3
    await tasksV1.connect(addrs[0])
    .createTask(duration, jobDesc, { value: salary });
    const taskId = 3;
    
    await tasksV1.connect(addrs[0]).cancelTask(taskId);
    await tasksV1.connect(addrs[0]).withdrawDeposit(taskId);
    const newBalance = await addrs[0].getBalance();
    
    expect(startBalance.sub(newBalance).div("1000000000000000000")).equal(0);
    });

Solidity 函数 ->


    function withdrawDeposit(uint256 taskId) external {
    require(tasks[taskId].creator == msg.sender, "Not creator");
    require(tasks[taskId].deposit > 0, "deposit more");
    require(
    tasks[taskId].status == TaskStatus.failed
    ||
    (tasks[taskId].status == TaskStatus.cancelled && applicants[taskId].length == 0)
    ||
    (tasks[taskId].status == TaskStatus.cancelled && block.timestamp > tasks[taskId].createdTime + unlockTime)
    , "Cannot withdraw");
    
    uint256 valueToWithdraw = tasks[taskId].deposit;
    tasks[taskId].deposit = 0;
    payable(msg.sender).transfer(valueToWithdraw);
    
    emit TaskUpdate(taskId, msg.sender, TaskActions.withdraw);
    }

我发现这个错误来自于Solidity函数

require(tasks[taskId].deposit > 0, "deposit more");

所以,我什至尝试过

const salary = ethers.utils.parseEther("10"); // 10 Ether in Wei
这个

但仍然遇到同样的错误

这里有完整的代码,以便更好地理解 -> https://github.com/akashpanda122/gig_board

被困在这个问题很久了!

javascript blockchain solidity web3js hardhat
1个回答
0
投票

感谢您提供 GitHub 链接。这里的问题是,在 createTask() 函数中,有三个参数。但在您的测试中,您只通过了两个,即 durationjobDesc。所以我用第三个参数 refId 运行了你的测试。现在一切正常。请查看随附的屏幕截图。

© www.soinside.com 2019 - 2024. All rights reserved.