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();
}
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);
}
}
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");
}
}
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);
}
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");
}
}
Aggregations