元掩码错误:交易错误。合同代码中引发的异常

时间:2019-09-25 01:53:32

标签: solidity smartcontracts metamask

我已经成功编译并部署了智能合约Campaign.sol。合同中的所有功能都可以成功运行,但是当我使用JavaScript文件RequestRow.js调用函数finalizeRequest时,Metamask会弹出错误Transaction error. Exception thrown in contract code 我已经搜索了此错误,并尝试通过thisthis解决我的问题。但是,它们对我的错误没有帮助。 谁能告诉我我的智能合约在哪里做错了。我不知道该怎么解决。请帮忙!

我的Campaign.sol文件

pragma solidity ^0.4.17;

contract CampaignFactory{
    address[] public deployedCampaigns;
    function createCampaign(uint minimum)public{
        address newCampaign = new Campaign(minimum, msg.sender);
        deployedCampaigns.push(newCampaign);
    }
    function getDeployedCampaign() public view returns(address[]){
        return deployedCampaigns;
    }
}

contract Campaign{
    struct Request {
        string description;
        uint value;
        address recipient;
        bool complete;
        uint approvalCount;
        mapping(address => bool) approvals;
    }
    Request[] public requests;
    address public manager;
    uint public minimumContribution;
    mapping (address => bool) public approvers;
    uint public approversCount;
    modifier restricted(){
        require(msg.sender == manager);
        _;
    }
    function Campaign (uint minimum, address creator)public{
        manager = creator;
        minimumContribution = minimum;
    }
    function contribue()public payable{
        require(msg.value > minimumContribution);
        approvers[msg.sender] = true;
        approversCount++;
    }

    function createRequest(string description, uint value, address recipient)public restricted{
        Request memory newRequest = Request({
            description: description,
            value: value,
            recipient: recipient,
            complete: false,
            approvalCount: 0
        });
        requests.push(newRequest); 
    }

    function approveRequest(uint index)public{
        Request storage request = requests[index];

        require(approvers[msg.sender]);
        require(!request.approvals[msg.sender]);

        request.approvals[msg.sender] = true;
        request.approvalCount++;
    }
    function finalizeRequest(uint index)public restricted{
        Request storage request = requests[index];

        require(request.approvalCount > (approversCount/2));
        require(!request.complete);

        request.recipient.transfer(request.value);
        request.complete = true;
    }

    function getSummary() public view returns (
        uint, uint, uint, uint, address
    ){
        return (
            minimumContribution,
            this.balance,
            requests.length,
            approversCount,
            manager
        );
    }

    function getRequestsCount() public view returns (uint){
        return requests.length;
    }
}

我的RequestRow.js文件

import React, {Component} from 'react';
import {Table, Button} from 'semantic-ui-react';
import web3 from '../ethereum/web3';
import Campaign from '../ethereum/campaign';

class RequestRow extends Component{
    onApprove = async() => {
        const campaign = Campaign(this.props.address);
        const accounts = await web3.eth.getAccounts();
        await campaign.methods.approveRequest(this.props.id).send({
            from: accounts[0]
        });
    };
    onFinalize = async() => {
        const campaign = Campaign(this.props.address);
        const accounts = await web3.eth.getAccounts();
        await campaign.methods.finalizeRequest(this.props.id).send({
            from: accounts[0]
        });
    };
    render(){
        const {Row, Cell} = Table;
        const {id, request, approversCount} = this.props;
        return (
            <Row>
                <Cell>{id}</Cell>
                <Cell>{request.description}</Cell>
                <Cell>{web3.utils.fromWei(request.value,'ether')}</Cell>
                <Cell>{request.recipient}</Cell>
                <Cell>{request.approvalCount}/{approversCount}</Cell>
                <Cell>
                    <Button color="green" basic onClick={this.onApprove}>
                        Approve
                    </Button>
                </Cell>
                <Cell>
                    <Button color="teal" basic onClick={this.onFinalize}>
                        Finalize
                    </Button>
                </Cell>
            </Row>
        )
    }
}

export default RequestRow;

在开发人员控制台中收到的完整错误消息

Uncaught (in promise) Error: Transaction has been reverted by the EVM:
{
  "blockHash": "0xcca9f6dac8c99c2ecc1f9314661399bda3a6096bf0f36d1c1724d84dcece42b2",
  "blockNumber": 5145820,
  "contractAddress": null,
  "cumulativeGasUsed": 270372,
  "from": "0x284aea2dca83c9c6247907554cb27120f7ce1892",
  "gasUsed": 55899,
  "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  "status": false,
  "to": "0x6ff70b2a9c30909431714902581539087fe25a3b",
  "transactionHash": "0x24b15e5db8bb673375409327cdde0fba98844126f5909ed7681845ee3bccce84",
  "transactionIndex": 2,
  "events": {}
}
    at index.js?959bf61:366

2 个答案:

答案 0 :(得分:0)

此错误是由于所需的帐户切换非常复杂。

首先,您需要使用一个MetaMask帐户创建一个广告系列, 然后,您需要使用与“经理”相同的帐户创建一个请求。复制并粘贴收件人的地址后,请务必在提交请求之后切换回正确的帐户。

然后,您需要使用另一个MetaMask帐户向广告系列供款,并在确保您仍在使用其供款资金的帐户中批准请求。

通常情况下,切换帐户不会有问题,但是它模拟了多个不同用户与合同的交互。

致谢

标记

答案 1 :(得分:0)

您需要使用松露/ HDwallet并设置提供程序。