【问题标题】:Contract method invalid number of arguments合同方法无效的参数数量
【发布时间】:2018-05-30 11:59:25
【问题描述】:

我刚开始玩 Truffle 和 Solidity,并写了我的第一份基本合同。我也写了一个测试,但它一直失败给我以下消息:

Error: Invalid number of arguments to Solidity function

现在,这个问题似乎很直接,我没有在争论中提出正确的数量...除了据我所知。

这是我的相关合同代码:

pragma solidity ^0.4.18;

contract FundEth {

    mapping (uint => Project) _projects;

    struct Project {
        uint id;
        uint targetWei;
        uint targetBlock;
        uint balanceWei;
        string name;
        string description;
        bool payedOut;
    }

    function fund(uint projectId) public payable
    {
        _projects[projectId].balanceWei += msg.value;
    }

    function create(uint targetWei, uint blocks, string name, string description)
        public
        returns (uint)
    {
        Project memory p = Project({
            id: ++_indexCounter,
            targetWei: targetWei,
            targetBlock: block.number + blocks,
            balanceWei: 0,
            name: name,
            description: description,
            payedOut: false
        });

        _projects[p.id] = p;

        return p.id;
    }

    function getProjectName(uint projectId)
        public
        view
        returns (string)
    {
        return "FOO";
    }

    function getProjectBalance(uint projectId)
        public
        view
        returns (uint)
    {
        return 10000000;
    }

    ...
}

这是我的测试代码:

const FundEth = artifacts.require("./FundEth.sol");

contract('FundEth', accounts => {
    var _id;
    var _fundEth;

    it("should create a project", () => {
        return FundEth.deployed()
            .then(fundEth => {
                _fundEth = fundEth;
                return fundEth.create(1000000000000000000 /* 1 Eth */ , 5, "FOO", "We want to fund this for testing.")
            }).then(id => {
                _id = id;
                return _fundEth.getProjectName.call(_id)
            }).then(name => {
                assert.equal(name, "FOO", "Has not created a valid project.");
            });
    });

    it("should fund a project", () => {

        return FundEth.deployed()
            .then(fundEth => {
                assert.notEqual(_id, 0);
                _fundEth = fundEth;
                _fundEth.fund.sendTransaction(_id, { from: accounts[0], value: 10000000 }); << SEEMS TO FAIL HERE.
            }).then(() => {
                return _fundEth.getProjectBalance.call(_id);;
            }).then(balance => {
                assert.equal(balance, 10000000, "Balance of test project was not 1 ether.");
            });
    });
});

我知道合同现在不是很有用,但我不明白为什么它会失败。完整的错误:

1) Contract: FundEth
   should fund a project:
 Uncaught Error: Invalid number of arguments to Solidity function
  at Object.InvalidNumberOfSolidityArgs (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/errors.js:25:1)
  at SolidityFunction.validateArgs (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/function.js:74:1)
  at SolidityFunction.toPayload (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/function.js:90:1)
  at SolidityFunction.sendTransaction (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/function.js:163:1)
  at /usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-contract/contract.js:135:1
  at new Promise (<anonymous>)
  at /usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-contract/contract.js:126:1
  at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:118:7)

【问题讨论】:

    标签: unit-testing solidity truffle


    【解决方案1】:

    换行

    return _fundEth.fund(_id, { from: accounts[0], value: 10000000 });
    

    似乎解决了这个问题。但是,我还需要在调用之前删除断言以便进行工作测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-10
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 2020-03-02
      • 2011-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多