所以我在iOS中玩以太坊,尝试与部署到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 iOS无法解码结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52614947/

10-17 01:30