Search in sources :

Example 1 with EthBlockNumber

use of org.bcos.web3j.protocol.core.methods.response.EthBlockNumber in project web3sdk by FISCO-BCOS.

the class Ethereum method main.

public static void main(String[] args) throws Exception {
    // 初始化Service
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Service service = context.getBean(Service.class);
    service.run();
    Thread.sleep(3000);
    System.out.println("开始测试...");
    System.out.println("===================================================================");
    logger.info("初始化AOMP的ChannelEthereumService");
    ChannelEthereumService channelEthereumService = new ChannelEthereumService();
    channelEthereumService.setChannelService(service);
    // 使用AMOP消息信道初始化web3j
    Web3j web3 = Web3j.build(channelEthereumService);
    logger.info("调用web3的getBlockNumber接口");
    EthBlockNumber ethBlockNumber = web3.ethBlockNumber().sendAsync().get();
    logger.info("获取ethBlockNumber:{}", ethBlockNumber);
    // 初始化交易签名私钥
    ECKeyPair keyPair = Keys.createEcKeyPair();
    Credentials credentials = Credentials.create(keyPair);
    // 初始化交易参数
    java.math.BigInteger gasPrice = new BigInteger("30000000");
    java.math.BigInteger gasLimit = new BigInteger("30000000");
    java.math.BigInteger initialWeiValue = new BigInteger("0");
    // 部署合约
    Ok ok = Ok.deploy(web3, credentials, gasPrice, gasLimit, initialWeiValue).get();
    System.out.println("Ok getContractAddress " + ok.getContractAddress());
    // 调用合约接口
    java.math.BigInteger Num = new BigInteger("999");
    Uint256 num = new Uint256(Num);
    TransactionReceipt receipt = ok.trans(num).get();
    System.out.println("receipt transactionHash" + receipt.getTransactionHash());
    // 查询合约数据
    num = ok.get().get();
    System.out.println("ok.get() " + num.getValue());
}
Also used : EthBlockNumber(org.bcos.web3j.protocol.core.methods.response.EthBlockNumber) ECKeyPair(org.bcos.web3j.crypto.ECKeyPair) TransactionReceipt(org.bcos.web3j.protocol.core.methods.response.TransactionReceipt) Service(org.bcos.channel.client.Service) ChannelEthereumService(org.bcos.web3j.protocol.channel.ChannelEthereumService) ChannelEthereumService(org.bcos.web3j.protocol.channel.ChannelEthereumService) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Web3j(org.bcos.web3j.protocol.Web3j) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger) Uint256(org.bcos.web3j.abi.datatypes.generated.Uint256) Credentials(org.bcos.web3j.crypto.Credentials)

Example 2 with EthBlockNumber

use of org.bcos.web3j.protocol.core.methods.response.EthBlockNumber in project web3sdk by FISCO-BCOS.

the class JsonRpc2_0Web3j method getBlockNumberCache.

@Override
public BigInteger getBlockNumberCache() {
    if (getBlockNumber().intValue() == 1) {
        try {
            EthBlockNumber ethBlockNumber = ethBlockNumber().sendAsync().get();
            setBlockNumber(ethBlockNumber.getBlockNumber());
        } catch (Exception e) {
            logger.error("Exception: " + e);
        }
    }
    return getBlockNumber().add(new BigInteger("500"));
}
Also used : EthBlockNumber(org.bcos.web3j.protocol.core.methods.response.EthBlockNumber) BigInteger(java.math.BigInteger)

Aggregations

BigInteger (java.math.BigInteger)2 EthBlockNumber (org.bcos.web3j.protocol.core.methods.response.EthBlockNumber)2 Service (org.bcos.channel.client.Service)1 Uint256 (org.bcos.web3j.abi.datatypes.generated.Uint256)1 Credentials (org.bcos.web3j.crypto.Credentials)1 ECKeyPair (org.bcos.web3j.crypto.ECKeyPair)1 Web3j (org.bcos.web3j.protocol.Web3j)1 ChannelEthereumService (org.bcos.web3j.protocol.channel.ChannelEthereumService)1 TransactionReceipt (org.bcos.web3j.protocol.core.methods.response.TransactionReceipt)1 ApplicationContext (org.springframework.context.ApplicationContext)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1