区块链技术与金融白话区块链区块链研习社

Android与以太坊(ETH)智能合约交互

2018-10-17  本文已影响38人  唠嗑008

所谓与合约交互,就是部署好合约之后,客户端去连接以太坊节点,然后调用合约的方法。

部署智能合约的步骤

一般来说,部署智能合约的步骤为:

2,3步的话,Remix可以直接完成。

案例解析

1、编写一个简单的合约文件SimpleStorage.sol

pragma solidity ^0.4.0;

contract SimpleStorage {
    uint storedData;
    function set(uint num){
        storedData = num;
    }
    
    function get() constant returns (string retVal){
        return "first depoly  call contract";
    }
}

2、通过solc、web3j命令行工具把sol合约文件编译成Java类SimpleStorage.class

如果这一步,你还不熟悉,请参考以太坊Web3j命令行生成Java版本的智能合约

3、测试节点是否链接成功
这里我连接的是infura的Rinkeby测试网络

String rinkebyUrl = "https://rinkeby.infura.io/v3/YOURE-API-KEY";
Web3j web3j = Web3jFactory.build(new HttpService(rinkebyUrl));
    /**
     * 获取版本信息
     */
    public void getWeb3ClientVersion() {
        try {
            Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().sendAsync().get();
            Log.i(TAG, "getWeb3ClientVersion: " + web3ClientVersion.getWeb3ClientVersion());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

如果能够返回版本号,就是连接成功了。

4、部署合约
注意:合约你可以使用remix+MetaMask或者Mist钱包部署,也可以使用代码部署

 //加载账户人信息,转账人私钥
    Credentials    credentials = Credentials.create(privateKey);
   /**
     * 部署合约
     */
    public void deploy() {
        //部署智能合约
        try {
        SimpleStorage  simpleStorage = SimpleStorage.deploy(web3j, credentials, Contract.GAS_PRICE, Contract.GAS_LIMIT).sendAsync().get();
            //部署合约后的地址
            String deployContractAddress = simpleStorage.getContractAddress();
            Log.i(TAG, "deployContractAddress : " + deployContractAddress);
            load(deployContractAddress);
        } catch (Exception e) {
            e.printStackTrace();
            Log.i(TAG, "deploy: " + e.getMessage());
        }
    }

SimpleStorageSimpleStorage.sol合约文件编译生成的java类

5、加载合约
加载一个已经成功部署到ETH链上的合约

    /**
     * 加载一个已经部署的合约
     * @param contractAddress 合约地址
     */
    public void load(String contractAddress) {
        simpleStorage = SimpleStorage.load(contractAddress, web3j, credentials, Contract.GAS_PRICE, Contract.GAS_LIMIT);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    boolean isValid = simpleStorage.isValid();
                    Log.i(TAG, "contract isValid : " + isValid);
                    if (isValid) {
                        get();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.i(TAG, "load: " + e.getMessage());
                }
            }
        }).start();
    }

simpleStorage.isValid()是验证合约是否可用,如果合约地址没有问题,接下来就可以调用合约的方法进行交互了。

6、合约交互
注意:这里是调用合约中的一个get()方法


    /**
     * 调用合约的方法
     */
    public void get() {
        RemoteCall<String> remoteCall = simpleStorage.get();
        try {
            String result = remoteCall.sendAsync().get();
            Log.i(TAG, "get result : " + result);
        } catch (InterruptedException e) {
            e.printStackTrace();
            Log.i(TAG, "get: " + e.getMessage());
        } catch (ExecutionException e) {
            e.printStackTrace();
            Log.i(TAG, "get: " + e.getMessage());
        }
    }

这里的get在java类中是这样的

public RemoteCall<String> get() {
        final Function function = new Function(FUNC_GET, 
                Arrays.<Type>asList(), 
                Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {}));
        return executeRemoteCallSingleValueReturn(function, String.class);
    }

调用合约get()方法的结果

至此,android与ETH合约交互就成功了。

上一篇 下一篇

猜你喜欢

热点阅读