4

我想通过移动设备(此处为 Android)与以太坊中的智能合约进行交互。智能合约与以太坊的 Javascript API 配合良好。所以我想使用 Go Ethereum Mobile Client (1.6.0)(包含在 gradle 中)。我可以连接到区块链,获取节点计数,读取余额,但与合约交互是我的问题。

使用命令行工具“abigen”生成 Java-Class 很好。我只需要把“bool”改成“boolean”,就可以上课了。每当我尝试交互时,Go Ethereum 都会在以下位置引发异常:this.Contract.call()

所以我只想做以下事情:

Contract1 electionContract = new Contract1(contractAddress,node.getEthereumClient());

//Action 1 
TransactOpts transactOpts = new TransactOpts();
transactOpts.setFrom(keyStore.getAccounts().get(0).getAddress());
                    transactOpts.setContext(gethcontext);    
Transaction transaction = contract1.sendString(transactOpts,new BigInt(0),"String");

//Action 2
contract1.Results contract1=electionContract.getString(null,new BigInt(0),new BigInt(0));

动作 1 声明(生成的类)

protected Transaction sendString(TransactOpts opts, BigInt identifier, String teststring) throws Exception {

Interfaces args = Geth.newInterfaces(2);
args.set(0, Geth.newInterface());
args.get(0).setBigInt(identifier);
args.set(1, Geth.newInterface());
args.get(1).setString(teststring);


return this.Contract.transact(opts, "sendString", args); //at this point the error is thrown

尝试发送数据时(参见上面的操作 1)

go.Universe$proxyerror: argument count mismatch: 1 for 2

行动 2 宣言

protected Results getString(CallOpts opts, BigInt randomID, BigInt randomID2) throws Exception {
        Interfaces args = Geth.newInterfaces(2);
        args.set(0, Geth.newInterface());
        args.get(0).setBigInt(randomID);
        args.set(1, Geth.newInterface());
        args.get(1).setBigInt(randomID2);


        Interfaces results = Geth.newInterfaces(2);
        Interface result0 = Geth.newInterface();
        result0.setDefaultString();
        results.set(0, result0);
        Interface result1 = Geth.newInterface();
        result1.setDefaultBigInt();
        results.set(1, result1);


        if (opts == null) {
            opts = Geth.newCallOpts();
        }
        this.Contract.call(opts, results, "getString", args); //at this point the error is thrown

        Results result = new Results();
        result.String = results.get(0).getString();
        result.Count = results.get(1).getBigInt();

        return result;

    }

试图从一个常量函数中获取数据(所以应该是免费的)

go.Universe$proxyerror: `getString` abi: cannot use invalid as type ptr as argument //see action 2 below

ABI 字符串提取:

[ {
    "constant": false,
    "inputs": [{
        "name": "randomID",
        "type": "uint256"
    }, {
        "name": "randomString",
        "type": "string"
    }],
    "name": "sendString",
    "outputs": [],
    "payable": false,
    "type": "function"
}, {
    "constant": true,
    "inputs": [{
        "name": "randomID",
        "type": "uint256"
    }, {
        "name": "randomID2",
        "type": "uint256"
    }],
    "name": "getString",
    "outputs": [{
        "name": "randomString",
        "type": "string"
    }, {
        "name": "randomID",
        "type": "uint256"
    }],
    "payable": false,
    "type": "function"
}]

这是相关的 github 问题:https ://github.com/ethereum/go-ethereum/issues/14363

4

0 回答 0