Addresses

在合約中使用地址的問題。錯誤:地址無效

  • December 29, 2021

我有一個私有區塊鏈。

我正在創建一個契約,然後我正在呼叫一個返回主帳戶的函式。呼叫此函式時出錯。

這是合約創建程式碼:

var browser_untitled1_sol_mytokenContract = web3.eth.contract([{"constant":false,"inputs":[],"name":"GetMain","outputs":[{"name":"resultMain","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"GetBalance","outputs":[{"name":"result","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"}]);
var browser_untitled1_sol_mytoken = browser_untitled1_sol_mytokenContract.new(
  {
    from: web3.eth.accounts[0], 
    data: '0x6060604052341561000f57600080fd5b5b33600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506301406f406000819055505b5b6101238061006d6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063266b1c72146047578063f8f8a912146099575b600080fd5b3415605157600080fd5b605760bf565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341560a357600080fd5b60a960ea565b6040518082815260200191505060405180910390f35b6000600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1690505b90565b6000805490508090505b905600a165627a7a72305820d65d41c2db3b45a3505ad0645f63c88098090cacaedf6000af2c10ba2dab8c720029', 
    gas: '4700000'
  }, function (e, contract){
   console.log(e, contract);
   if (typeof contract.address !== 'undefined') {
        console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
   }
})

我呼叫函式browser_untitled1_sol_mytoken.GetMain();來獲取主地址並得到錯誤:

Error: invalid address
   at web3.js:3879:15
   at web3.js:3705:20
   at web3.js:4948:28
   at map (<native code>)
   at web3.js:4947:12
   at web3.js:4973:18
   at web3.js:4998:23
   at web3.js:4061:16
   at apply (<native code>)
   at web3.js:4147:16

我相信,這條線是錯誤的:

}, function (e, contract){

您沒有從contract.new(). 你得到交易雜湊。然後,正如 Badr 所說,您必須等待交易被探勘。

我對這個庫很幸運:https ://gist.github.com/xavierlepretre/88682e871f4ad07be4534ae560692ee6

您只需將程式碼包含在您的 JS 中,然後當您發送交易時,您會跟進(使用與 Truffle 一致的承諾):

.then(function (txnHash) {
 return web3.eth.getTransactionReceiptMined(txnHash);
})
.then

它可讀且可靠。

希望能幫助到你。

嘗試設置預設賬戶,然後執行:

web3.eth.defaultAccount = web3.eth.accounts[0]

希望這可以幫助…!

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