Web3js
您可以在松露遷移期間將數組參數傳遞給合約函式嗎?
在 TestRPC 上進行測試,在嘗試將數組參數傳遞給合約函式時
truffle migrate --reset
我收到以下錯誤:
Unhandled promise rejection (rejection id: 2): Error: Invalid number of arguments to Solidity function
為了追捕罪魁禍首,我做了一個玩具範例,通過刪除任何 ConvertLib 引用並添加一個接受 uint 的初始化函式來修改原始 /contracts/MetaCoin.sol 範例
$$ $$範圍:
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]; } }
以上已成功編譯
truffle compile
為簡單起見,我還刪除了遷移契約,並在 /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); } }); };
然而,即使在這個最簡單的範例中,無論我如何聲明測試數組或格式化欄位,錯誤仍然存在。
繼續我的搜尋,我在 github 上遇到了這個相關問題: https ://github.com/trufflesuite/truffle/issues/526 他們認為這是 web3 v0.19 的問題
版本資訊 -
MacOS Sierra v10.12.6
松露 v3.4.9
堅固性 v0.4.15
EthereumJS TestRPC v4.1.1
Web3js v0.20.1
節點 v6.11.2
任何解決方法或解決方案將不勝感激!
我用
truffle init
. 粘貼您的元幣文件和遷移文件,它工作正常。stack1$ truffle deploy Compiling ./contracts/ConvertLib.sol... Compiling ./contracts/MetaCoin.sol... Compiling ./contracts/Migrations.sol... Writing artifacts to ./build/contracts Using network 'development'. Running migration: 1_initial_migration.js Deploying Migrations... Migrations: 0x8d2fc79377f2e736bfe606ab5cf1a4d6a9b98d3f Saving successful migration to network... Saving artifacts... Running migration: 2_deploy_contracts.js Saving successful migration to network... Deploying new MetaCoin contract Saving artifacts... Deployed new MetaCoin contract at 0x39a59797ea58d98055cd719470285554cb164580 ["3","3","3"]
- 你有程式碼試圖從區塊鏈中獲取合約,你確定你正在使用你的測試 RPC 嗎?也許您正在從測試或實時網路呼叫舊版本的元幣?嘗試首先部署合約並刪除/註釋掉試圖獲取它的程式碼。
- 您是否嘗試在測試而不是遷移中呼叫該函式?
- 也許通過建構子而不是“初始化”函式傳遞參數會更好(我知道這只是一個例子,所以這一點可能不准確)。例如
deployer.deploy(MetaCoint, myArray);
return exiM
在這裡就足夠了return Promise.resolve(exiM);
test_array : [web3.toBigNumber(3).valueOf(), web3.toBigNumber(3).valueOf(), web3.toBigNumber(3).valueOf()]
嘗試將大數字作為字元串傳遞。