Testing on TestRPC, when trying to pass an array parameter to a contract function during
truffle migrate --reset
I've been getting the following error:
Unhandled promise rejection (rejection id: 2): Error: Invalid number of arguments to Solidity function
In an attempt to hunt down the culprit I have made a toy example, amending the original /contracts/MetaCoin.sol example by removing any ConvertLib reference and adding an initialize function which accepts a uint[] parameter:
contract MetaCoin {
mapping (address => uint) balances;
uint testArraySum;
event Transfer(address indexed _from, address indexed _to, uint256 _value);
function MetaCoin() {
balances[tx.origin] = 10000;
}
function initialize(uint[] _test_array) returns(bool){
for(uint i = 0; i < _test_array.length; i++) {
testArraySum += _test_array[i];
}
return true;
}
function sendCoin(address receiver, uint amount) returns(bool sufficient) {
if (balances[msg.sender] < amount) return false;
balances[msg.sender] -= amount;
balances[receiver] += amount;
Transfer(msg.sender, receiver, amount);
return true;
}
function getBalance(address addr) returns(uint) {
return balances[addr];
}
}
The above has been successfully compiled with
truffle compile
For simplicity, I have also removed the migrations contract and written the following deployment script in /migrations/2_deploy_contracts.js:
var MetaCoin = artifacts.require("./MetaCoin.sol");
module.exports = function(deployer, network) {
var metaCoin;
if (network == 'development') {
metaCoin = {
address : '0xa8e055d89579a74b0110728a740e18e80c44e211',
test_array : [web3.toBigNumber(3).valueOf(), web3.toBigNumber(3).valueOf(), web3.toBigNumber(3).valueOf()]
};
} else if (network == "ropsten") { // ropsten testnet
} else if (network == "rinkeby") { // rinkeby testnet
} else if (network == "live") {
throw "error: deployment for live network is not yet supported";
}
var mPromise = MetaCoin.at(metaCoin.address).then(function (exiM) {
console.log('Found existing MetaCoin contract at ' + exiM.address);
return Promise.resolve(exiM);
});
mPromise.then(function (m) {
console.log('MetaCoin contract at ' + m.address);
return m.initialize(metaCoin.test_array);
}).catch(function (err) {
if (err.message && err.message.includes('Cannot create instance of')) {
console.log('Deploying new MetaCoin contract');
MetaCoin.new().then(function (newM) {
console.log('Deployed new MetaCoin contract at ' + newM.address);
console.log(JSON.stringify(metaCoin.test_array));
return newM.initialize(metaCoin.test_array);
});
} else {
console.error(err);
return Promise.resolve(null);
}
});
};
Yet, even in this simplest of examples the error still persists no matter how I declare the test array or format the fields.
Continuing my search I ran across this related issue on github: https://github.com/trufflesuite/truffle/issues/526 Where they conclude that this is an issue of web3 as of v0.19
Version Information -
MacOS Sierra v10.12.6
Truffle v3.4.9
Solidity v0.4.15
EthereumJS TestRPC v4.1.1
Web3js v0.20.1
Node v6.11.2
Any workaround or solution would be greatly appreciated!
copy-pasted
code with lot's of unneeded lines. My suggestion is to make it as small as possible, it will be easier to find an error.