java调用以太坊合约HelloWorld

公众号回复“1”,拉你进区块链技术讨论微信群

作者:志明

来源:区块链兄弟

著权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

合约代码HelloWorld

pragma solidity ^0.4.17;

contract HelloWorld {

function sayHello() return (string){

return ("hello world");

}

}

-----------------------------------------------------------------------

import java.math.BigInteger;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

import java.util.List;

import org.web3j.abi.EventEncoder;

import org.web3j.abi.EventValues;

import org.web3j.abi.FunctionEncoder;

import org.web3j.abi.TypeReference;

import org.web3j.abi.datatypes.Event;

import org.web3j.abi.datatypes.Function;

import org.web3j.abi.datatypes.Type;

import org.web3j.abi.datatypes.Utf8String;

import org.web3j.crypto.Credentials;

import org.web3j.protocol.Web3j;

import org.web3j.protocol.core.DefaultBlockParameter;

import org.web3j.protocol.core.RemoteCall;

import org.web3j.protocol.core.methods.request.EthFilter;

import org.web3j.protocol.core.methods.response.Log;

import org.web3j.protocol.core.methods.response.TransactionReceipt;

import org.web3j.tx.Contract;

import org.web3j.tx.TransactionManager;

import rx.Observable;

import rx.functions.Func1;

/**

*

*/

public class HelloWorld extends Contract{

//合约bytecode

private static final String BINARY = "0x6060604052341561000f57600080fd5b6101578061001e6000396000f300606060405260043610610041576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063ef5fb05b14610046575b600080fd5b341561005157600080fd5b6100596100d4565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561009957808201518184015260208101905061007e565b50505050905090810190601f1680156100c65780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6100dc610117565b6040805190810160405280600b81526020017f68656c6c6f20776f726c64000000000000000000000000000000000000000000815250905090565b6020604051908101604052806000815250905600a165627a7a72305820046da66a94c77b53794c67d2fcab03df58940472d851fb7fc8b8fe6eecd0d2550029";

public HelloWorld(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {

super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);

}

public RemoteCall sayHello() {

Function function = new Function("sayHello",

Arrays.asList(),

Arrays.>asList(new TypeReference() {}));

return executeRemoteCallSingleValueReturn(function, String.class);

}

}

-----------------------------------------------------------------

import org.web3j.crypto.Credentials;

import org.web3j.protocol.Web3j;

import org.web3j.protocol.core.RemoteCall;

import org.web3j.protocol.core.methods.response.Web3ClientVersion;

import org.web3j.protocol.http.HttpService;

import org.web3j.tx.Contract;

import java.math.BigInteger;

import java.sql.SQLException;

import static org.web3j.tx.Contract.GAS_LIMIT;

import static org.web3j.tx.ManagedTransaction.GAS_PRICE;

/**

* Created by

*/

public class EthHello {

public static void main( String[] args ) throws Exception

{

Web3j web3 = Web3j.build(new HttpService()); // defaults to http://localhost:8545/

//Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().send();

//String clientVersion = web3ClientVersion.getWeb3ClientVersion();

//System.out.println("clientVersion"+clientVersion);

try {

String account = web3.ethAccounts().send().getAccounts().get(0);

Credentials credentials = Credentials.create(account);

//合约地址

String address="0x4558d789249923896905dce1408c2eabd15f9e1c";

HelloWorld g=new HelloWorld(address,web3,credentials, Contract.GAS_PRICE,Contract.GAS_PRICE);

RemoteCall s= g.sayHello();

System.out.println("b"+g.getContractBinary());

System.out.println("返回结果:"+s.send());

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

文章发布只为分享区块链技术内容,版权归原作者所有,观点仅代表作者本人,绝不代表区块链兄弟赞同其观点或证实其描述。

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180413G1C98V00?refer=cp_1026
  • 腾讯「云+社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。

扫码关注云+社区

领取腾讯云代金券