交易:消耗所有气体后,以错误(状态0)退出

发布于 2025-01-23 01:42:08 字数 4611 浏览 0 评论 0原文

嗨,我正在创建一个钱包,我可以通过3个授权地址的批准转让ETH。我的第二个测试条件失败了。我是牢固性的新手,请提供帮助。

wallet.js

const Wallet = artifacts.require('Wallet');

contract('Wallet',(accounts) => {
    let wallet;
    beforeEach(async () =>{
        wallet = await Wallet.new([accounts[0],accounts[1],accounts[2]],2);
        await web3.eth.sendTransaction({from:accounts[0], to:wallet.address, value: 1000});
});

    it('should heve correct approvers and quorum',async()=> {
        const approvers = await wallet.getApprovers();
        const quorum = await wallet.quorum();
        assert(approvers.length === 3);
        assert(approvers[0]===accounts[0]);
        assert(approvers[1]===accounts[1]);
        assert(approvers[2]===accounts[2]);
        assert(quorum.toNumber() === 2);

});
    it('should create transfers',async () => {
      await wallet.createTransfer(100, accounts[5], {from: accounts[0]});
      const transfers = await wallet.getTransfers();
      assert(transfers.length === 1);
      assert(transfers[0].id === '0'); 
      assert(transfers[0].amount ==='100');
      assert(transfers[0].to === accounts[5]);
      assert(transfers[0].approval === '0');
      assert(transfers[0].sent === false);
});
});

wallet.sol

//SPDX-License-Identifier:MIT
pragma solidity ^0.6.0;
pragma experimental ABIEncoderV2;

contract Wallet{
    //address array stores address of the approvers.
    //quorum is the number of approvers.
    address[] public approvers;
    uint public quorum;
    struct Transfer{
        uint id;
        uint amount;
        address payable to;
        uint approval;
        bool sent;
    }
    Transfer[] public transfers;
   // mapping(uint => Transfer) public transfers;
    mapping(address => mapping(uint => bool)) public approvals;
    
    
    constructor(address[] memory _approvers, uint _quorum)public{
        approvers = _approvers;
        quorum = _quorum;
    }

    function getApprovers() external view returns(address[] memory){
        return approvers;
    }
    function getTransfers() external view returns(Transfer[] memory){
        return transfers;
    }
    function createTransfer(uint amount, address payable to) external onlyApprovers(){
        transfers.push(Transfer(
           transfers.length,
            amount,
            to,
            0,
            false
        ));
    }
    function approveTransfer(uint id)external onlyApprovers(){
        require(transfers[id].sent == false,'transfer has already been sent');
        require(approvals[msg.sender][id]== false,'cannot approve transfer twice');

        approvals[msg.sender][id]=true;
        transfers[id].approval++;

        if(transfers[id].approval >= quorum){
            transfers[id].sent = true;
            address payable to = transfers[id].to;
            uint amount = transfers[id].amount;
            to.transfer(amount);
        }

    }
    receive() external payable{}

    modifier onlyApprovers(){
        bool allowed =false;
        for(uint i=0;i<= approvers.length;i++){
            if(approvers[i] == msg.sender){
                allowed =true;
            }
    }
            require(allowed=true,'only approvers can approve');
            _;
        }
    }

我在测试时会遇到此错误。

Compiling your contracts...
===========================
> Compiling ./contracts/Migrations.sol
> Compiling ./contracts/Wallet.sol
> Artifacts written to /var/folders/kx/64r1zrfn28g8v6_sy50p98480000gn/T/test--50180-yxWHgIg50Wuu
> Compiled successfully using:
   - solc: 0.6.0+commit.26b70077.Emscripten.clang


  Contract: Wallet
    ✔ should heve correct approvers and quorum (64ms)
    1) should create transfers
    > No events were emitted


  1 passing (6s)
  1 failing

  1) Contract: Wallet
       should create transfers:
     Transaction: 0xd41ddeb4f3801cd59e065679a8a26180d92aa31778704e16ccfde77e01b9d830 exited with an error (status 0) after consuming all gas.
     Please check that the transaction:
     - satisfies all conditions set by Solidity `assert` statements.
     - has enough gas to execute the full transaction.
     - does not trigger an invalid opcode by other means (ex: accessing an array out of bounds).
  StatusError: Transaction: 0xd41ddeb4f3801cd59e065679a8a26180d92aa31778704e16ccfde77e01b9d830 exited with an error (status 0) after consuming all gas.
      at Context.<anonymous> (test/Wallet.js:21:17)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)

我是固体的新手,请帮助。我尝试了直接解决我的交易的天然气和加斯普雷斯,但问题尚未解决。

Hi i am creating a wallet with which i can transfer ETH on approval from 3 authorised address. My second test condition fails. I am fairly new to solidity please help.

wallet.js

const Wallet = artifacts.require('Wallet');

contract('Wallet',(accounts) => {
    let wallet;
    beforeEach(async () =>{
        wallet = await Wallet.new([accounts[0],accounts[1],accounts[2]],2);
        await web3.eth.sendTransaction({from:accounts[0], to:wallet.address, value: 1000});
});

    it('should heve correct approvers and quorum',async()=> {
        const approvers = await wallet.getApprovers();
        const quorum = await wallet.quorum();
        assert(approvers.length === 3);
        assert(approvers[0]===accounts[0]);
        assert(approvers[1]===accounts[1]);
        assert(approvers[2]===accounts[2]);
        assert(quorum.toNumber() === 2);

});
    it('should create transfers',async () => {
      await wallet.createTransfer(100, accounts[5], {from: accounts[0]});
      const transfers = await wallet.getTransfers();
      assert(transfers.length === 1);
      assert(transfers[0].id === '0'); 
      assert(transfers[0].amount ==='100');
      assert(transfers[0].to === accounts[5]);
      assert(transfers[0].approval === '0');
      assert(transfers[0].sent === false);
});
});

wallet.sol

//SPDX-License-Identifier:MIT
pragma solidity ^0.6.0;
pragma experimental ABIEncoderV2;

contract Wallet{
    //address array stores address of the approvers.
    //quorum is the number of approvers.
    address[] public approvers;
    uint public quorum;
    struct Transfer{
        uint id;
        uint amount;
        address payable to;
        uint approval;
        bool sent;
    }
    Transfer[] public transfers;
   // mapping(uint => Transfer) public transfers;
    mapping(address => mapping(uint => bool)) public approvals;
    
    
    constructor(address[] memory _approvers, uint _quorum)public{
        approvers = _approvers;
        quorum = _quorum;
    }

    function getApprovers() external view returns(address[] memory){
        return approvers;
    }
    function getTransfers() external view returns(Transfer[] memory){
        return transfers;
    }
    function createTransfer(uint amount, address payable to) external onlyApprovers(){
        transfers.push(Transfer(
           transfers.length,
            amount,
            to,
            0,
            false
        ));
    }
    function approveTransfer(uint id)external onlyApprovers(){
        require(transfers[id].sent == false,'transfer has already been sent');
        require(approvals[msg.sender][id]== false,'cannot approve transfer twice');

        approvals[msg.sender][id]=true;
        transfers[id].approval++;

        if(transfers[id].approval >= quorum){
            transfers[id].sent = true;
            address payable to = transfers[id].to;
            uint amount = transfers[id].amount;
            to.transfer(amount);
        }

    }
    receive() external payable{}

    modifier onlyApprovers(){
        bool allowed =false;
        for(uint i=0;i<= approvers.length;i++){
            if(approvers[i] == msg.sender){
                allowed =true;
            }
    }
            require(allowed=true,'only approvers can approve');
            _;
        }
    }

I am getting this error when testing.

Compiling your contracts...
===========================
> Compiling ./contracts/Migrations.sol
> Compiling ./contracts/Wallet.sol
> Artifacts written to /var/folders/kx/64r1zrfn28g8v6_sy50p98480000gn/T/test--50180-yxWHgIg50Wuu
> Compiled successfully using:
   - solc: 0.6.0+commit.26b70077.Emscripten.clang


  Contract: Wallet
    ✔ should heve correct approvers and quorum (64ms)
    1) should create transfers
    > No events were emitted


  1 passing (6s)
  1 failing

  1) Contract: Wallet
       should create transfers:
     Transaction: 0xd41ddeb4f3801cd59e065679a8a26180d92aa31778704e16ccfde77e01b9d830 exited with an error (status 0) after consuming all gas.
     Please check that the transaction:
     - satisfies all conditions set by Solidity `assert` statements.
     - has enough gas to execute the full transaction.
     - does not trigger an invalid opcode by other means (ex: accessing an array out of bounds).
  StatusError: Transaction: 0xd41ddeb4f3801cd59e065679a8a26180d92aa31778704e16ccfde77e01b9d830 exited with an error (status 0) after consuming all gas.
      at Context.<anonymous> (test/Wallet.js:21:17)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)

I am new to solidity please help.I tried including gas and gasPrice directly to my transaction still the issue was not solved.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文