Search in sources :

Example 1 with RemoteCall

use of org.fisco.bcos.web3j.protocol.core.RemoteCall in project web3sdk by FISCO-BCOS.

the class MixContractClient method testMixContract.

@SuppressWarnings({ "rawtypes", "unchecked" })
public static void testMixContract(String[] args) throws Exception {
    final Resource contractResource = new ClassPathResource("contract.properties");
    PropertiesConfiguration prop = new PropertiesConfiguration(contractResource.getFile());
    Object addressObj = prop.getProperty("mix_address");
    if (addressObj != null) {
        contractAddress = (String) addressObj;
    } else {
        deploymixContract();
    }
    ContractGasProvider contractGasProvider = new StaticGasProvider(gasPrice, gasLimit);
    MixContract mixContract = MixContract.load(contractAddress, web3j, credentials, contractGasProvider);
    // create table
    if ("create".equals(args[0])) {
        TransactionReceipt receipt = mixContract.create().send();
        List<CreateResultEventResponse> createResultEvents = mixContract.getCreateResultEvents(receipt);
        if (createResultEvents.size() == 0) {
            System.out.println("create t_demo table failed.");
            return;
        }
        CreateResultEventResponse createResultEventResponse = createResultEvents.get(0);
        int createCount = createResultEventResponse.count.intValue();
        switch(createCount) {
            case 255:
                System.out.println("non-authorized to create t_demo table.");
                break;
            case 0:
                System.out.println("t_demo table already exist.");
                break;
            case 1:
                System.out.println("create t_demo table completed.");
                break;
        }
    } else // insert
    if ("insert".equals(args[0])) {
        if (args.length == 4) {
            String name = args[1];
            int item_id = Integer.parseInt(args[2]);
            String item_name = args[3];
            RemoteCall<TransactionReceipt> insert = mixContract.insert(name, BigInteger.valueOf(item_id), item_name);
            TransactionReceipt txReceipt = insert.send();
            List<InsertResultEventResponse> insertResultEvents = mixContract.getInsertResultEvents(txReceipt);
            if (insertResultEvents.size() > 0) {
                for (int i = 0; i < insertResultEvents.size(); i++) {
                    InsertResultEventResponse insertResultEventResponse = insertResultEvents.get(i);
                    logger.info("insertCount = " + insertResultEventResponse.count.intValue());
                    System.out.println("insertCount = " + insertResultEventResponse.count.intValue());
                }
            } else {
                System.out.println("t_demo table does not exist.");
            }
        } else {
            System.out.println("\nPlease enter as follow example:\n 1 insert fruit 1 apple");
        }
    } else // select
    if ("select".equals(args[0])) {
        if (args.length == 2) {
            try {
                String keyName = args[1];
                Tuple3<List<byte[]>, List<BigInteger>, List<byte[]>> lists = mixContract.read(keyName).send();
                List<byte[]> value1 = lists.getValue1();
                List<BigInteger> value2 = lists.getValue2();
                List<byte[]> value3 = lists.getValue3();
                logger.info("record numbers = " + value1.size());
                System.out.println("record numbers = " + value1.size());
                for (int i = 0; i < value1.size(); i++) {
                    String name = new String(value1.get(i));
                    logger.info("name = " + name);
                    System.out.println("name = " + name);
                    int item_id = value2.get(i).intValue();
                    logger.info("item_id = " + item_id);
                    System.out.println("item_id = " + item_id);
                    String item_name = new String(value3.get(i));
                    logger.info("item_name = " + item_name);
                    System.out.println("item_name = " + item_name);
                }
                System.out.println();
                System.out.println("totalKeys = " + mixContract.totalKeys().send());
            } catch (Exception e) {
                logger.info("record numbers = 0");
                System.out.println("record numbers = 0");
            }
        } else {
            System.out.println("\nPlease enter as follow example:\n 1 select fruit");
        }
    } else // update
    if ("update".equals(args[0])) {
        if (args.length == 4) {
            String name = args[1];
            int item_id = Integer.parseInt(args[2]);
            String item_name = args[3];
            RemoteCall<TransactionReceipt> update = mixContract.update(name, BigInteger.valueOf(item_id), item_name);
            TransactionReceipt transactionReceipt = update.send();
            List<UpdateResultEventResponse> updateResultEvents = mixContract.getUpdateResultEvents(transactionReceipt);
            if (updateResultEvents.size() > 0) {
                for (int i = 0; i < updateResultEvents.size(); i++) {
                    UpdateResultEventResponse updateResultEventResponse = updateResultEvents.get(i);
                    System.out.println("updateCount = " + updateResultEventResponse.count.intValue());
                    logger.info("updateCount = " + updateResultEventResponse.count.intValue());
                }
            } else {
                System.out.println("t_demo table does not exist.");
            }
        } else {
            System.out.println("\nPlease enter as follow example:\n 1 update fruit 1 orange");
        }
    } else // remove
    if ("remove".equals(args[0])) {
        if (args.length == 3) {
            String name = args[1];
            int item_id = Integer.parseInt(args[2]);
            RemoteCall<TransactionReceipt> remove = mixContract.remove(name, BigInteger.valueOf(item_id));
            TransactionReceipt transactionReceipt = remove.send();
            List<RemoveResultEventResponse> removeResultEvents = mixContract.getRemoveResultEvents(transactionReceipt);
            if (removeResultEvents.size() > 0) {
                RemoveResultEventResponse reomveResultEventResponse = removeResultEvents.get(0);
                logger.info("removeCount = " + reomveResultEventResponse.count.intValue());
                System.out.println("removeCount = " + reomveResultEventResponse.count.intValue());
            } else {
                System.out.println("t_demo table does not exist.");
            }
        } else {
            System.out.println("\nPlease enter as follow example:\n 1 remove fruit 1");
        }
    } else {
        System.out.println("\nPlease choose follow commands:\n deploy, create, insert, select, update or remove");
    }
}
Also used : StaticGasProvider(org.fisco.bcos.web3j.tx.gas.StaticGasProvider) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) TransactionReceipt(org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt) InsertResultEventResponse(org.fisco.bcos.channel.test.contract.MixContract.InsertResultEventResponse) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) ContractGasProvider(org.fisco.bcos.web3j.tx.gas.ContractGasProvider) ClassPathResource(org.springframework.core.io.ClassPathResource) TransactionException(org.fisco.bcos.web3j.protocol.exceptions.TransactionException) UpdateResultEventResponse(org.fisco.bcos.channel.test.contract.MixContract.UpdateResultEventResponse) CreateResultEventResponse(org.fisco.bcos.channel.test.contract.MixContract.CreateResultEventResponse) BigInteger(java.math.BigInteger) List(java.util.List) RemoteCall(org.fisco.bcos.web3j.protocol.core.RemoteCall) RemoveResultEventResponse(org.fisco.bcos.channel.test.contract.MixContract.RemoveResultEventResponse)

Aggregations

BigInteger (java.math.BigInteger)1 List (java.util.List)1 PropertiesConfiguration (org.apache.commons.configuration.PropertiesConfiguration)1 CreateResultEventResponse (org.fisco.bcos.channel.test.contract.MixContract.CreateResultEventResponse)1 InsertResultEventResponse (org.fisco.bcos.channel.test.contract.MixContract.InsertResultEventResponse)1 RemoveResultEventResponse (org.fisco.bcos.channel.test.contract.MixContract.RemoveResultEventResponse)1 UpdateResultEventResponse (org.fisco.bcos.channel.test.contract.MixContract.UpdateResultEventResponse)1 RemoteCall (org.fisco.bcos.web3j.protocol.core.RemoteCall)1 TransactionReceipt (org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt)1 TransactionException (org.fisco.bcos.web3j.protocol.exceptions.TransactionException)1 ContractGasProvider (org.fisco.bcos.web3j.tx.gas.ContractGasProvider)1 StaticGasProvider (org.fisco.bcos.web3j.tx.gas.StaticGasProvider)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 Resource (org.springframework.core.io.Resource)1