Published 1 November 2018

Creating a console application on Java using CREDITS API and Maven. Cash transfer

Requirements

Requirements

JDK 8

Java IDE (optional, JetBrains IntelliJ is considered)

Text editor (for example, Visual Studio Code)

Maven (installation is described in the "Installing Maven on Windows 10" article)

GIT (installation)

A prerequisite for the operation is to run the following Java application from the standard CREDITS delivery (port 9080 should not be busy):

start call java -jar contract-executor.jar

Assembly. Sequence of steps

Assembly. Sequence of steps

This article is the next step for "Creating user tools (applications) on the basis of the CREDITS API and Java".

It is assumed that the application is already created and you want to implement the functionality of coin transfer using the CREDITS API.

We will consider source codes of CREDITS Desktop Wallet a source of information and methods https://github.com/CREDITSCOM/CS-Wallet-desktop

The following methods are wrappers around the CREDITS API:

private static BigDecimal getBalance(String account, ApiClient client)

      throws CreditsCommonException, LevelDbClientException, CreditsNodeException {

  return client.getBalance(Converter.decodeFromBASE58(account), (byte)1);

}

public static void callTransactionFlow(ApiClient client,

                                     PrivateKey privateKey,

                                     long innerId,

                                     String source,

                                     String target,

                                     BigDecimal amount,

                                     BigDecimal balance,

                                     byte currency,

                                     BigDecimal fee) throws LevelDbClientException,

                                     CreditsNodeException, CreditsCommonException {

   TransactionStruct tStruct = new TransactionStruct(innerId, source, target, amount, fee, currency, null);

  ByteBuffer signature= Utils.signTransactionStruct(tStruct, privateKey);

  TransactionFlowData transactionFlowData =

          new TransactionFlowData(innerId,

                                  Converter.decodeFromBASE58(source),

                                  Converter.decodeFromBASE58(target),

                                  amount,

                                  balance,

                                  currency,

                                  signature.array(), fee);

  client.transactionFlow(transactionFlowData,false);

}

private static void Transfer(String account,

                           PrivateKey privateKey,

                           long innerId,

                           ApiClient client,

                           BigDecimal amount,

                           String toAddress) throws CreditsCommonException, LevelDbClientException, CreditsNodeException {

  callTransactionFlow(client,

                      privateKey,

                      innerId,

                      account,

                      toAddress,

                      amount,

                      new BigDecimal(1000000),

                      (byte)1,

                      new BigDecimal(0.1));

}

Considering the problem from the applied point of view, namely, the transfer from wallet to wallet, we are interested in the "Transfer" method with the following input parameters:

String account - public key of the wallet

PrivateKey privateKey - private key of the wallet

long innerId - time variable (transaction date)

ApiClient client - object api client

String toAddress - address of the destination wallet

The following is the "main" method of our console utility. You can see that the parameters for calling the "Transfer" method are "sewn" into the source code, these parameters may be put in the command line arguments and be set when the utility is called through the command line.

public static void main(String[] args) throws CreditsCommonException, LevelDbClientException, CreditsNodeException {

  String account = "H5ptdUUfjJBGiK2X3gN2EzNYxituCUUnXv2tiMdQKP3b";

  String privateKeyString = "3Ki86Y3dy8enEgM1LXL97oQ6zLnhVbjJPpWAdqhgkAh7uFab37ergRWJxyDDsa46ra3UiQXqe2rW7JrJPkekBWMs";

  ApiClient client = ApiClient.getInstance("127.0.0.1", 9090);

  BigDecimal balance = getBalance(account, client);

  System.out.print(balance);

  PrivateKey privateKey = Ed25519.bytesToPrivateKey(Converter.decodeFromBASE64(privateKeyString));

  Transfer(account, privateKey, new Date().getTime(), client, new BigDecimal(1000), "6zxLh3nbbtxt36ED211Y3askbbSY4E1iV98TKcFzjoUk");

}

Result:

0 Useful article

Contents