Go-Ethereum
Nethereum“WebException:底層連接已關閉:連接意外關閉。”
我正在嘗試使用公共節點從 asp.net 後端呼叫契約函式。
var abi = @"[{""constant"":false,""inputs"": [{""name"":""addr"",""type"":""address""}, {""name"":""amount"",""type"":""uint256""}],""name"":""test1"",""outputs"": [{""name"":"""",""type"":""bool""}], ""payable"":false,""stateMutability"":""nonpayable"",""type"":""function""}, {""constant"":false,""inputs"":[{""name"":""addr"",""type"":""address""}, {""name"":""amount"",""type"":""uint256""}],""name"":""test2"", ""outputs"":[{""name"":"""",""type"":""bool""}], ""payable"":false,""stateMutability"":""nonpayable"",""type"":""function""}, {""anonymous"":false,""inputs"": [{""indexed"":false,""name"":"""",""type"":""address""}, {""indexed"":false,""name"":"""",""type"":""uint256""}], ""name"":""Test1"",""type"":""event""}, {""anonymous"":false,""inputs"": [{""indexed"":false,""name"":"""",""type"":""address""}, {""indexed"":false,""name"":"""",""type"":""uint256""}], ""name"":""Test2"",""type"":""event""}]"; var account = new Nethereum.Web3.Accounts.Account("private-key-here"); var web3 = new Nethereum.Geth.Web3Geth(acc, "http://104.236.182.85:30303"); var contract = web3.Eth .GetContract(abi, "0xcff863b8f953caf4418c50d56f2cc5afc839be01"); var myFunction = contract.GetFunction("test1"); var result = await myFunction .CallAsync<bool>("d56F18bD24557E75E1b0082B66d6B9f5134Ed1da", 2);
我總是得到這個 WebException。即使我只是要求我的帳戶餘額:
var balance = await web3.Eth.GetBalance .SendRequestAsync("d56F18bD24557E75E1b0082B66d6B9f5134Ed1da");
還有其他方法可以執行合約功能嗎?我必須執行自己的節點而不是公共節點嗎?
節點的埠被打開。
問題出在 geth 客戶端初始化中。它應該從這些參數開始(rpc 是最重要的):
geth --rpc --rpcaddr <ip> --rpcport <portnumber>
可能我嘗試使用的公共節點沒有啟用此功能。如果您不需要自己的節點,您可以使用 Infura。它不再像我之前想像的那樣只是一個 REST API 服務。
var web3 = new Nethereum.Geth.Web3Geth(acc, url: "https://mainnet.infura.io");
正確初始化geth並允許訪問特定的api
geth --rpc <ip> --rpcapi eth,web3 --rpccorsdomain "http://localhost"
您還有其他 API,因此請確保您已啟動 geth,並啟用了您希望通過 Nethereum 訪問的正確 API。
以下是可用 API 的範例
{ "id":1, "jsonrpc":"2.0", "result":{ "admin":"1.0", "db":"1.0", "debug":"1.0", "eth":"1.0", "miner":"1.0", "net":"1.0", "personal":"1.0", "shh":"1.0", "txpool":"1.0", "web3":"1.0" }
如果從瀏覽器訪問 geth 節點,此參數很有用,您可以輸入“*”,但我不推薦它,因為它允許任何人連接和管理節點。如果我在本地執行 geth 並在本地與之互動,我將使用 localhost 或 127.0.0.1
--rpccorsdomain "http://localhost"