Go-Ethereum iOS 无法解组结果
Go-Ethereum iOS cannot unmarshal results
所以我在 iOS 玩 go-ethereum,我在尝试与部署到 Rinkeby testnet 的合约进行交互时遇到了很多麻烦,我对整个过程都很陌生区块链技术,所以任何帮助表示赞赏。
我想做的就是访问已部署的合同并获取字符串的值,但我遇到的问题是当我尝试调用绑定合同时出现此错误:
Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=go Code=1 "abi: cannot unmarshal string in to []interface {}" UserInfo={NSLocalizedDescription=abi: cannot unmarshal string in to []interface {}}
这是我用来拨打电话的代码。
// Declare the error variables
var clientError: NSErrorPointer;
var addressError: NSErrorPointer;
var contractError: NSErrorPointer;
// Get the bindContract from Rinkeby test network.
let client = GethNewEthereumClient("https://rinkeby.infura.io/v3/398ed56d211646faaf010ca183de11f2", clientError);
let contractAddress = GethNewAddressFromHex("0x7259667715d671Ee370d7788647f95Fe7C3B532d", addressError);
guard let contractABI = ReadJsonResourceAsString(fileName: "InboxContractInterface", fileType: "json") else {
print("[ViewController] failed to read the abi json as string.")
return;
}
let boundContract = GethBindContract(contractAddress, contractABI, client, contractError);
// Prepare the callOpts
let callOpts = GethNewCallOpts();
callOpts?.setGasLimit(300000);
callOpts?.setContext(GethNewContext());
// Prepare the results & params interfaces
let results = GethNewInterfaces(1);
let params = GethNewInterfaces(0);
let stringResult = GethNewInterface();
stringResult?.setDefaultString();
try! results?.set(0, object: stringResult);
// Make the call
let methodName = "message";
try! boundContract?.call(callOpts, out_: results, method: methodName, args: params);
// Show results.
print("[ViewController] message call result: " + (stringResult?.getString())!);
这是我的合约代码:
pragma solidity ^0.4.17;
contract Inbox {
string public message;
function Inbox (string initialMessage) public {
message = initialMessage;
}
function setMessage (string newMessage) public {
message = newMessage;
}
}
对于在进一步挖掘后可能会发现相同问题的任何人,我在 android 中发现了这个问题:https://github.com/ethereum/go-ethereum/issues/14832
幸运的是这已经被修复了,所以完全是我没有使用最新版本的错。
我使用的是 Geth v1.5.9,所以在更新到 v1.8.2 后终于可以使用了,但不确定中间的哪个版本得到了修复。
所以我在 iOS 玩 go-ethereum,我在尝试与部署到 Rinkeby testnet 的合约进行交互时遇到了很多麻烦,我对整个过程都很陌生区块链技术,所以任何帮助表示赞赏。
我想做的就是访问已部署的合同并获取字符串的值,但我遇到的问题是当我尝试调用绑定合同时出现此错误:
Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=go Code=1 "abi: cannot unmarshal string in to []interface {}" UserInfo={NSLocalizedDescription=abi: cannot unmarshal string in to []interface {}}
这是我用来拨打电话的代码。
// Declare the error variables
var clientError: NSErrorPointer;
var addressError: NSErrorPointer;
var contractError: NSErrorPointer;
// Get the bindContract from Rinkeby test network.
let client = GethNewEthereumClient("https://rinkeby.infura.io/v3/398ed56d211646faaf010ca183de11f2", clientError);
let contractAddress = GethNewAddressFromHex("0x7259667715d671Ee370d7788647f95Fe7C3B532d", addressError);
guard let contractABI = ReadJsonResourceAsString(fileName: "InboxContractInterface", fileType: "json") else {
print("[ViewController] failed to read the abi json as string.")
return;
}
let boundContract = GethBindContract(contractAddress, contractABI, client, contractError);
// Prepare the callOpts
let callOpts = GethNewCallOpts();
callOpts?.setGasLimit(300000);
callOpts?.setContext(GethNewContext());
// Prepare the results & params interfaces
let results = GethNewInterfaces(1);
let params = GethNewInterfaces(0);
let stringResult = GethNewInterface();
stringResult?.setDefaultString();
try! results?.set(0, object: stringResult);
// Make the call
let methodName = "message";
try! boundContract?.call(callOpts, out_: results, method: methodName, args: params);
// Show results.
print("[ViewController] message call result: " + (stringResult?.getString())!);
这是我的合约代码:
pragma solidity ^0.4.17;
contract Inbox {
string public message;
function Inbox (string initialMessage) public {
message = initialMessage;
}
function setMessage (string newMessage) public {
message = newMessage;
}
}
对于在进一步挖掘后可能会发现相同问题的任何人,我在 android 中发现了这个问题:https://github.com/ethereum/go-ethereum/issues/14832
幸运的是这已经被修复了,所以完全是我没有使用最新版本的错。 我使用的是 Geth v1.5.9,所以在更新到 v1.8.2 后终于可以使用了,但不确定中间的哪个版本得到了修复。