Search in sources :

Example 6 with StaticGasProvider

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

the class TestBase method setUpBeforeClass.

@BeforeClass
public static void setUpBeforeClass() throws Exception {
    context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Service service = context.getBean(Service.class);
    service.run();
    ChannelEthereumService channelEthereumService = new ChannelEthereumService();
    channelEthereumService.setChannelService(service);
    System.out.println("EncryptType =>  " + EncryptType.getEncryptType());
    web3j = Web3j.build(channelEthereumService, service.getGroupId());
    credentials = GenCredential.create();
    Ok ok = Ok.deploy(web3j, credentials, new StaticGasProvider(gasPrice, gasLimit)).send();
    blockNumber = ok.getTransactionReceipt().get().getBlockNumber();
    blockHash = ok.getTransactionReceipt().get().getBlockHash();
    txHash = ok.getTransactionReceipt().get().getTransactionHash();
}
Also used : StaticGasProvider(org.fisco.bcos.web3j.tx.gas.StaticGasProvider) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ChannelEthereumService(org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService) Service(org.fisco.bcos.channel.client.Service) ChannelEthereumService(org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService) Ok(org.fisco.bcos.channel.test.guomi.Ok) BeforeClass(org.junit.BeforeClass)

Example 7 with StaticGasProvider

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

the class OkTest method testOkContract.

@Test
public void testOkContract() throws Exception {
    Ok ok = Ok.deploy(web3j, credentials, new StaticGasProvider(gasPrice, gasLimit)).send();
    if (ok != null) {
        TransactionReceipt receipt = ok.trans(new BigInteger("4")).send();
        assertTrue(receipt.getBlockNumber().intValue() > 0);
        assertTrue(receipt.getTransactionIndex().intValue() >= 0);
        assertTrue(receipt.getGasUsed().intValue() > 0);
        BigInteger oldBalance = ok.get().sendAsync().get(60000, TimeUnit.MILLISECONDS);
        ok.trans(new BigInteger("4")).sendAsync().get(60000, TimeUnit.MILLISECONDS);
        BigInteger newBalance = ok.get().sendAsync().get(60000, TimeUnit.MILLISECONDS);
        assertTrue(newBalance.intValue() == oldBalance.intValue() + 4);
    }
}
Also used : StaticGasProvider(org.fisco.bcos.web3j.tx.gas.StaticGasProvider) TransactionReceipt(org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt) BigInteger(java.math.BigInteger) Test(org.junit.Test)

Example 8 with StaticGasProvider

use of org.fisco.bcos.web3j.tx.gas.StaticGasProvider 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 9 with StaticGasProvider

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

Example 10 with StaticGasProvider

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

the class GMTableTestClient method deployTableTest.

/* deploy the contract,get address from blockchain */
@SuppressWarnings("deprecation")
public static void deployTableTest() {
    RemoteCall<TableTest> deploy = TableTest.deploy(web3j, credentials, new StaticGasProvider(gasPrice, gasLimit));
    TableTest tabletest;
    try {
        tabletest = deploy.send();
        contractAddress = tabletest.getContractAddress();
        System.out.println("deploy contract address: " + contractAddress);
        logger.info("deploy contract address: " + contractAddress);
        final Resource contractResource = new ClassPathResource("contract.properties");
        PropertiesConfiguration prop = new PropertiesConfiguration(contractResource.getFile());
        prop.setProperty("crud_address", contractAddress);
        prop.save();
        System.out.println("deploy contract successful!");
    } catch (TransactionException e) {
        if ("0x19".equals(e.getStatus())) {
            System.out.println("non-authorized to deploy contracts!");
        } else {
            System.out.println("deploy transaction is abnormal, please check the environment msg:" + e.getMessage());
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("deploy transaction is abnormal, please check the environment");
    }
}
Also used : StaticGasProvider(org.fisco.bcos.web3j.tx.gas.StaticGasProvider) TransactionException(org.fisco.bcos.web3j.protocol.exceptions.TransactionException) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) ClassPathResource(org.springframework.core.io.ClassPathResource) TransactionException(org.fisco.bcos.web3j.protocol.exceptions.TransactionException)

Aggregations

StaticGasProvider (org.fisco.bcos.web3j.tx.gas.StaticGasProvider)16 BigInteger (java.math.BigInteger)10 PropertiesConfiguration (org.apache.commons.configuration.PropertiesConfiguration)6 ContractGasProvider (org.fisco.bcos.web3j.tx.gas.ContractGasProvider)6 ClassPathResource (org.springframework.core.io.ClassPathResource)6 Resource (org.springframework.core.io.Resource)6 Service (org.fisco.bcos.channel.client.Service)5 Credentials (org.fisco.bcos.web3j.crypto.Credentials)5 ChannelEthereumService (org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService)5 TransactionReceipt (org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt)5 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)5 Web3j (org.fisco.bcos.web3j.protocol.Web3j)4 ApplicationContext (org.springframework.context.ApplicationContext)4 Random (java.util.Random)3 TransactionException (org.fisco.bcos.web3j.protocol.exceptions.TransactionException)3 ThreadPoolTaskExecutor (org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)3 RateLimiter (com.google.common.util.concurrent.RateLimiter)2 Date (java.util.Date)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2