Solidity

我可以在創建的合約中呼叫函式嗎?

  • May 26, 2017

我有一個類似於這個典型工廠的結構。

contract Factory {
 bytes32[] Names;
 address[] newContracts;

 function createContract (bytes32 name) {
     address newContract = new Contract(name);
     newContracts.push(newContract);
 } 

 function getName (uint i) {
     Contract con = Contract(newContracts[i]);
     Names[i] = con.Name();
 }
}

contract Contract {
 bytes32 public Name;
 string public numberString;

 function Contract (bytes32 name) {
     Name = name;
 }

 function setNumberString (string time){
     numberString = time;      
 }
}

我在這個結構中添加了函式setNumberString和變數numberString,我用Web3 js定義了這個:

myContract = web3.eth.contract(ABIArray).at(contractAddress);
myContract.setNumberString(state, function(error, result){
if(!error)
    console.log(result)
else
    console.error(error);
})

創建的“契約契約”的ABIArraycontractAddress是,當我嘗試呼叫該函式時,我在 Dapp 的瀏覽器中獲得此消息:

Uncaught TypeError: myContract.setNumberString is not a function

當我嘗試使用正常契約執行此操作時,我沒有任何問題,我做錯了什麼?可以呼叫這個創建的合約的函式嗎?

這可能是因為setNumberString未在您的 ABI 中定義。

嘗試使用這個

ABIArray=[{"constant":false,"inputs":[{"name":"time","type":"string"}],"name":"setNumberString","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"Name","outputs":[{"name":"","type":"bytes32"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"numberString","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"inputs":[{"name":"name","type":"bytes32"}],"payable":false,"type":"constructor"}];

引用自:https://ethereum.stackexchange.com/questions/16664