Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

29 Commits

Repository files navigation

og-sdk-java

Java SDK for OG. It makes it easy to send transactions, query basic information such as balance and receipts, process with Solidity based smart contract through Annchain.OG.

Requirement

Only support Java version 8 or higher.

Web3j is required for Solidity related use cases. Add web3j into pom.xml to use it.

<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>4.2.0</version>
</dependency>

Usage

Initialize account

// create Account object by a random private key.AccountaccountRandom = newAccount();
System.out.println(accountRandom.GetAddress());
// create Account object by a specific private key.AccountaccountPriv = newAccount("0000000000000000000000000000000000000000000000000000000000000001");
System.out.println(accountPriv.GetAddress());

Initialize a OG io.annchain.og.server

StringOG_URL = "http://localhost:8000";
OGog = newOG(OG_URL);

All the usages will relay on this og object.

Query balance

// query default token balance.QueryBalanceRespbalanceResp = og.QueryBalance(account.GetAddress(), 0);
if (balanceResp.getErr().equals("")) {
System.out.println("the balance is: " + balanceResp.getData().getBalance())
}

Send transaction

Accountaccount = newAccount(myPrivateKey);
Stringto = "receiver address";
BigIntegervalue = newBigInteger("100");
SendTransactionRespresp = og.SendTransaction(account, to, value);
StringtxHash = resp.getHash();
System.out.println("tx hash is: " + txHash);

Send async transaction with callback

Accountaccount = newAccount(myPrivateKey);
Stringto = "receiver address";
BigIntegervalue = newBigInteger("100");
// you should implement your own confirm callback.ConfirmCallbackconfirmCallback = newConfirmCallback();
newThread(() -> {
try {
og.SendTransactionAsync(account, to, value, confirmCallback);
} catch (IOException | InterruptedExceptione) {
// do something
}
}).start();

Confirm callback interface

interfaceIConfirmCallback {
/** * Return the frequency time to check if a tx is confirmed. * Time unit: Millisecond. * */IntegerIntervalTime();
/** * Return the longest confirm time to wait * */IntegerTimeout();
voidConfirmEvent(ConfirmCallbackRespresult);
}

Deploy contract

Solidity compiler is not supported for this SDK, so you should compile the source code and get the bytecode by yourself. remix is really recommended to be your Solidity editor and compiler.

Assume you have got the bytecode:

StringcontractBytecode = "60806040523480156100105760008...";
BigIntegervalue = newBigInteger("0");
SendTransactionRespdeployResp = og.DeployContract(account, value, contractBytecode, null);

Deploy contract with constructor parameter

importorg.web3j.abi.FunctionEncoder;
importorg.web3j.abi.datatypes.*;
StringcontractBytecode = "60806040523480156100105760008...";
BigIntegervalue = newBigInteger("0");
// add constructor parametersList<Type> constructorParameters = newArrayList<>();
constructorParameters.add(newUint64(100L));
constructorParameters.add(newAddress("addressInHex"));
contractBytecode += FunctionEncoder.encodeConstructor(constructorParameters);
SendTransactionRespdeployResp = og.DeployContract(account, value, contractBytecode, null);

Call contract

Call contract will create a new transaction which is contract related and send this transaction to the main chain.

Assume you want to call a solidity function called issueToken:

function issueToken(address toUser, bytes memory data) public returns (bytes32 hash){
...
}

You can call this function like this:

StringcontractAddress = "0xContractAddressInHex"StringfuncName = "issueToken";
AddresstoUser = newAddress("0xAddressInHex");
byte[] b = {0x11, 0x22, 0x33};
DynamicBytesdata = newDynamicBytes(b);
List<Type> inputs = newArrayList<Type>();
inputs.add(toUser);
inputs.add(data);
List<TypeReference<?>> outputParams = newArrayList<>();
outputParams.add(TypeReference.create(Bytes32.class));
SendTransactionRespcallContractResp = og.CallContract(issuer, contractAddress, value, funcName, inputs, outputParams);
if (!callContractResp.getErr().equals("")) {
System.out.println("call contract err: " + callContractResp.getErr());
}

Query contract

Query contract is used when you only want to query the data of the contract without any changes.

Assume you want to call a static solidity function named getWallet:

function getWallet(address user) public view returns (bytes32[] memory hashes){
...
}

You can query this function by code:

StringcontractAddress = "0xContractAddressInHex"StringfuncName = "getWallet";
Addressuser = newAddress("0xUserAddressInHex");
List<Type> inputs = newArrayList<Type>();
inputs.add(user);
List<TypeReference<?>> outputParams = newArrayList<>();
outputParams.add(newTypeReference<DynamicArray<Bytes32>>() {});
QueryContractRespqueryContractResp = og.QueryContract(contractAddress, funcName, inputs, outputParams);
if (!queryContractResp.getErr().equals("")) {
System.out.println("contract error: " + queryContractResp.getErr());
return;
}
// initialize outputsList<Type> respOutputs = queryContractResp.getOutputs();
DynamicArray<Bytes32> dynamicArray = (DynamicArray<Bytes32>) respOutputs.get(0);
List<Bytes32> bts = dynamicArray.getValue();
// do something with bts

For more details, you can check the examples.

About

Java SDK for OG DLT

Resources

Stars

4 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages