Skip to content

xuperchain/xuper-java-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

xuper-java-sdk

The java sdk of xuperunion https://github.com/xuperchain/xuperunion

Usage

Add Dependency

If your're using Maven, just add the following dependency in pom.xml.

<dependency>
  <groupId>com.baidu.xuper</groupId>
  <artifactId>xuper-java-sdk</artifactId>
  <version>0.2.0</version>
</dependency>

Config file

If you use the endorsement feature, set the configuration file like this:

Config.setConfigPath("./conf/sdk.yaml");

The config file is here: src/main/java/com/baidu/xuper/conf/sdk.yaml.

Test net config file is here: src/main/java/com/baidu/xuper/conf/sdk.testnet.yaml.

Create client

XuperClient client = new XuperClient("127.0.0.1:37101");

Import account keys

// Import account from local keys
Account account = Account.create("./keys");

Create account and mnemonic

// Import account from local keys
Account account = Account.create(1, 2);
System.out.println(account.getAddress());
System.out.println(account.getMnemonic());

Create contract account

// The account name is XC1111111111111111@xuper
client.createContractAccount(account, "1111111111111111");

Transfer xuper to contract account

client.transfer(account, "XC1111111111111111@xuper", BigInteger.valueOf(1000000), "1");

Query balance of account

 BigInteger result = client.getBalance("XC1111111111111111@xuper");

Query balance details of account

 XuperClient.BalDetails[] result = client.getBalanceDetails("XC1111111111111111@xuper");

Deploy contract using contract account

// Using a contract account to deploy contract
account.setContractAccount("XC1111111111111111@xuper");
Map<String, byte[]> args = new HashMap<>();
args.put("creator", "icexin".getBytes());
String codePath = "./counter.wasm";
byte[] code = Files.readAllBytes(Paths.get(codePath));
// the runtime is c
client.deployWasmContract(account, code, "counter", "c", args);

Invoke contract

Map<String, byte[]> args = new HashMap<>();
args.put("key", "icexin".getBytes());
Transaction tx = client.invokeContract(account, "wasm", "counter", "increase", args);
System.out.println("txid: " + tx.getTxid());
System.out.println("response: " + tx.getContractResponse().getBodyStr());
System.out.println("gas: " + tx.getGasUsed());

EVM contract

String abi = "[{\"inputs\":[{\"internalType\":\"uint256\"......";
String bin = "6080604......";

Map<String, String> args = new HashMap<>();
args.put("num", "5889");

Transaction t = client.deployEVMContract(account, bin.getBytes(), abi.getBytes(), contractName, args);
System.out.println("txID:" + t.getTxid());

// storagepay is a payable method. Amount param can be NULL if there is no need to transfer to the contract.
Transaction t1 = xuperClient.invokeEVMContract(account, contractName, "storepay", args, BigInteger.ONE);
System.out.println("txID:" + t1.getTxid());
System.out.println("tx gas:" + t1.getGasUsed());

Transaction t2 = xuperClient.queryEVMContract(account, contractName, "retrieve", null);
System.out.println("tx res getMessage:" + t2.getContractResponse().getMessage());
System.out.println("tx res getBodyStr:" + t2.getContractResponse().getBodyStr());