Search in sources :

Example 1 with ContractGasProvider

use of org.fisco.bcos.web3j.tx.gas.ContractGasProvider in project web3sdk by FISCO-BCOS.

the class OkClient method testOk.

@SuppressWarnings({ "rawtypes", "unchecked" })
public static void testOk(String[] args) throws Exception {
    final Resource contractResource = new ClassPathResource("contract.properties");
    PropertiesConfiguration prop = new PropertiesConfiguration(contractResource.getFile());
    Object addressObj = prop.getProperty("ok_address");
    if (addressObj != null) {
        contractAddress = (String) addressObj;
    } else {
        deployOk();
    }
    ContractGasProvider contractGasProvider = new StaticGasProvider(gasPrice, gasLimit);
    ;
    Ok ok = Ok.load(contractAddress, web3j, credentials, contractGasProvider);
    // trans
    if ("trans".equals(args[0])) {
        if (args.length == 2) {
            String num = args[1];
            RemoteCall<TransactionReceipt> insert = ok.trans(new BigInteger(num));
            TransactionReceipt txReceipt = insert.send();
            System.out.println(txReceipt.getTransactionHash());
        } else {
            System.out.println("\nPlease enter as follow example:\n 1 trans 5");
        }
    } else // get
    if ("get".equals(args[0])) {
        BigInteger num = ok.get().send();
        System.out.println("num = " + num);
    } else {
        System.out.println("\nPlease choose follow commands:\n deploy, trans or get");
    }
}
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) BigInteger(java.math.BigInteger) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) ContractGasProvider(org.fisco.bcos.web3j.tx.gas.ContractGasProvider) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 2 with ContractGasProvider

use of org.fisco.bcos.web3j.tx.gas.ContractGasProvider 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)

Example 3 with ContractGasProvider

use of org.fisco.bcos.web3j.tx.gas.ContractGasProvider in project web3sdk by FISCO-BCOS.

the class TableTestClient method testTableTest.

@SuppressWarnings({ "rawtypes", "unchecked" })
public static void testTableTest(String[] args) throws Exception {
    final Resource contractResource = new ClassPathResource("contract.properties");
    PropertiesConfiguration prop = new PropertiesConfiguration(contractResource.getFile());
    Object addressObj = prop.getProperty("crud_address");
    if (addressObj != null) {
        contractAddress = (String) addressObj;
    } else {
        deployTableTest();
    }
    ContractGasProvider contractGasProvider = new StaticGasProvider(gasPrice, gasLimit);
    TableTest tabletest = TableTest.load(contractAddress, web3j, credentials, contractGasProvider);
    // create table
    if ("create".equals(args[0])) {
        create(tabletest);
    } else // insert
    if ("insert".equals(args[0])) {
        insert(args, tabletest);
    } else // select
    if ("select".equals(args[0])) {
        select(args, tabletest);
    } else // update
    if ("update".equals(args[0])) {
        update(args, tabletest);
    } else // remove
    if ("remove".equals(args[0])) {
        remove(args, tabletest);
    } 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) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) ContractGasProvider(org.fisco.bcos.web3j.tx.gas.ContractGasProvider) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 4 with ContractGasProvider

use of org.fisco.bcos.web3j.tx.gas.ContractGasProvider in project web3sdk by FISCO-BCOS.

the class GMOkTransaction method main.

public static void main(String[] args) throws Exception {
    EncryptType encryptType = new EncryptType(1);
    String groupId = "1";
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Service service = context.getBean(Service.class);
    service.run();
    System.out.println("===================================================================");
    ChannelEthereumService channelEthereumService = new ChannelEthereumService();
    channelEthereumService.setChannelService(service);
    channelEthereumService.setTimeout(10000);
    Web3j web3 = Web3j.build(channelEthereumService, Integer.parseInt(groupId));
    BigInteger gasPrice = new BigInteger("300000000");
    BigInteger gasLimit = new BigInteger("3000000000");
    Credentials credentials1 = GenCredential.create("a392604efc2fad9c0b3da43b5f698a2e3f270f170d859912be0d54742275c5f6");
    ContractGasProvider contractGasProvider = new StaticGasProvider(gasPrice, gasLimit);
    final Ok okDemo = Ok.deploy(web3, credentials1, contractGasProvider).send();
    for (int i = 0; i < 1; i++) {
        System.out.println("####contract address is: " + okDemo.getContractAddress());
        TransactionReceipt receipt = okDemo.trans(new BigInteger("4")).send();
        System.out.println(" balance = " + okDemo.get().send().intValue());
    }
    System.exit(0);
}
Also used : StaticGasProvider(org.fisco.bcos.web3j.tx.gas.StaticGasProvider) TransactionReceipt(org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt) ChannelEthereumService(org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService) Service(org.fisco.bcos.channel.client.Service) ChannelEthereumService(org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService) ContractGasProvider(org.fisco.bcos.web3j.tx.gas.ContractGasProvider) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) Web3j(org.fisco.bcos.web3j.protocol.Web3j) EncryptType(org.fisco.bcos.web3j.crypto.EncryptType) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) BigInteger(java.math.BigInteger) Credentials(org.fisco.bcos.web3j.crypto.Credentials)

Example 5 with ContractGasProvider

use of org.fisco.bcos.web3j.tx.gas.ContractGasProvider in project web3sdk by FISCO-BCOS.

the class GMErc20Transaction method main.

public static void main(String[] args) throws Exception {
    EncryptType encryptType = new EncryptType(1);
    System.out.println(encryptType.getEncryptType());
    String groupId = "1";
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Service service = context.getBean(Service.class);
    service.run();
    System.out.println("===================================================================");
    ChannelEthereumService channelEthereumService = new ChannelEthereumService();
    channelEthereumService.setChannelService(service);
    channelEthereumService.setTimeout(10000);
    Web3j web3 = Web3j.build(channelEthereumService, Integer.parseInt(groupId));
    BigInteger gasPrice = new BigInteger("300000000");
    BigInteger gasLimit = new BigInteger("300000000");
    Credentials credentials1 = GenCredential.create("a392604efc2fad9c0b3da43b5f698a2e3f270f170d859912be0d54742275c5f6");
    ContractGasProvider contractGasProvider = new StaticGasProvider(gasPrice, gasLimit);
    NewSolTest erc20 = NewSolTest.deploy(web3, credentials1, contractGasProvider).send();
    for (int i = 0; i < 1; i++) {
        System.out.println("####contract address is: " + erc20.getContractAddress());
        erc20.transfer("0x0f49a17d17f82da2a7d92ecf19268274150eaf5e", new BigInteger("100")).send();
        BigInteger oldBalance = erc20.balanceOf("0x0f49a17d17f82da2a7d92ecf19268274150eaf5e").send();
        System.out.println("0x0f49a17d17f82da2a7d92ecf19268274150eaf5e balance" + oldBalance.intValue());
    }
    System.exit(0);
}
Also used : StaticGasProvider(org.fisco.bcos.web3j.tx.gas.StaticGasProvider) ChannelEthereumService(org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService) Service(org.fisco.bcos.channel.client.Service) ChannelEthereumService(org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService) ContractGasProvider(org.fisco.bcos.web3j.tx.gas.ContractGasProvider) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) Web3j(org.fisco.bcos.web3j.protocol.Web3j) EncryptType(org.fisco.bcos.web3j.crypto.EncryptType) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) BigInteger(java.math.BigInteger) Credentials(org.fisco.bcos.web3j.crypto.Credentials)

Aggregations

ContractGasProvider (org.fisco.bcos.web3j.tx.gas.ContractGasProvider)6 StaticGasProvider (org.fisco.bcos.web3j.tx.gas.StaticGasProvider)6 BigInteger (java.math.BigInteger)4 PropertiesConfiguration (org.apache.commons.configuration.PropertiesConfiguration)4 ClassPathResource (org.springframework.core.io.ClassPathResource)4 Resource (org.springframework.core.io.Resource)4 TransactionReceipt (org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt)3 Service (org.fisco.bcos.channel.client.Service)2 Credentials (org.fisco.bcos.web3j.crypto.Credentials)2 EncryptType (org.fisco.bcos.web3j.crypto.EncryptType)2 Web3j (org.fisco.bcos.web3j.protocol.Web3j)2 ChannelEthereumService (org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService)2 ApplicationContext (org.springframework.context.ApplicationContext)2 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)2 List (java.util.List)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