合同方法无效的参数数量

时间:2018-05-30 11:59:25

标签: unit-testing solidity truffle

我刚刚开始玩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)

1 个答案:

答案 0 :(得分:0)

将行更改为

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

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