All Products
Search
Document Center

Quick Start Guide

Last Updated: Jan 10, 2020

This topic describes how to execute applications and provides an example execution procedure.

Execute an application

To execute an application, perform the following steps:

  1. Prepare the environment.
  2. Write the application.
  3. Compile the application.
  4. Execute the application.

Prepare an environment

  • Prepare files for establishing an SSL connection.
    To establish an SSL connection to the BaaS platform, you must prepare the following four certificate files: CA root certificate file (trustCa), certificate file of the client (client.crt), private key file of the client (client.key), and private key file of the user’s account (user.key). The following table describes the four files.

    File Description Source
    client.crt Certificate file of the client Use the BaaS-provided key generation tool to generate the certificate request file client.csr. Then, submit this file to the BaaS platform to apply for the certificate, and download the .crt file after the application is approved.
    client.key Private key file of the client Use the BaaS-provided key generation tool to generate the file.
    trustCa TrustStore to store the CA certificate Download the file from the BaaS platform. The password of the file is ‘mychain’.
    user.key Private key file of the account Use the BaaS-provided key generation tool to generate the file.
  • Obtain the IP address and port number of the node on the blockchain.
    This information is used for interactions with the blockchain. To obtain the information, view details of the node on the BaaS platform.

Write an application

  1. Use IntelliJ IDEA to create a Maven file-based demo project. After the project is created, the directory structure is as follows:
    1545295559042-521770ec-412b-49ed-935e-a6b644bb0949.png

  2. In the java directory, customize a package, for example, com.example.demo. Then, copy content in DemoSample.java to the new package, and store the client.crt, client.key, trustCa, and user.key files in resources. You can click DemoSample.java to download the file.
    1

    Note: For the demo project, you must use the bytecode generated after the source code of the contract is compiled. For more information about the source code, see Solidity contract development. For more information about the tool used to compile Solidity contracts, see Solidity contract compilation tool.

  3. Add dependencies to the pom.xml file. Add the SDK and slf4j-log4j12 to pom.xml and the configuration file of log4j to resources.

    Note: In this step, you must use the latest package on which the SDK depends.

    pom

    The following example shows a pom file with dependencies for slf4j-log4j12.

    1. <dependencies>
    2. <dependency>
    3. <groupId>com.alipay.mychainx</groupId>
    4. <artifactId>mychainx-sdk</artifactId>
    5. <!--Use the latest SDK -->
    6. <version>0.10.2.6</version>
    7. </dependency>
    8. <dependency>
    9. <groupId>org.slf4j</groupId>
    10. <artifactId>slf4j-log4j12</artifactId>
    11. <version>1.8.0-alpha0</version>
    12. </dependency>
    13. </dependencies>
    14. <build>
    15. <extensions>
    16. <extension>
    17. <groupId>kr.motd.maven</groupId>
    18. <artifactId>os-maven-plugin</artifactId>
    19. <version>1.6.1</version>
    20. </extension>
    21. </extensions>
    22. </build>

    log4j.properties file content is as follows:

    1. log4j.rootLogger=INFO, R
    2. # Log outputs are in the console.
    3. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    4. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    5. log4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%n
    6. # Log outputs are in a file.
    7. log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
    8. log4j.appender.R.File=./sdk.log
    9. log4j.appender.R.layout=org.apache.log4j.PatternLayout
    10. log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n

Compile an application

Run mvn clean compile in the root directory of the project to compile the project.

Execute the application

Execute the project in DemoSample.java. The generated log is stored in root directory ./sdk.log. If the log contains the “Hand shake success” response, the project is connected to the BaaS platform.

The expected output is as follows:

  1. create testAccount1 success.
  2. create testAccount2 success.
  3. deploy contract success.
  4. issue success.
  5. transfer success.
  6. check account balance success.

Error message

If the execution result of the transaction indicates that it fails to be executed, you can use the error code to locate the failure.

  • ErrorCode: The errorCode field in MychainBaseResult indicates the reason for this failure.

If the request is for a transaction, you can obtain details about the failure based on the returned values. Take contract deployment for example:

  1. MychainBaseResult<ReplyTransactionReceipt> result = sdk.getContractService()
  2. .deployContract(
  3. DeployContractRequest.build(adminAccount.getIdentity(),
  4. Utils.getIdentityByName(testContractId,env), contractCode, VMTypeEnum.EVM,
  5. contractParameters, BigInteger.ZERO, params));
  6. assertTrue(result.isSuccess());
  7. assertEquals(0, result.getData().getTransactionReceipt().getResult());
  • If result.getData().getTransactionReceipt().getResult() is 0, the transaction execution succeeds. You can use the value of result to find the reason for a failure from MychainErrorCodeEnum.
  • result.isSuccess() indicates whether the transaction is sent. If the transaction fails to be sent, you can use result.getErrorCode() to obtain the error code.

Example execution procedure

  1. Initialize the environment.

    1. //step 1: init logger.
    2. initLogger();
    3. //step 2:init mychain env.
    4. env = initMychainEnv();
  2. Create a transaction.

    1. //step 4: init account that will be created.
    2. initAccount();
    3. //step 5: init private key list which will be used during transaction.
    4. initPrivateKeyList();
    5. //step 6: execute create two accounts.
    6. createAccount();
  3. Deploy a smart contract.

    1. //step 7 : deploy a contract using testAccount1.
    2. deployContract();
  4. Purchase points.

    1. //step 8:issue 100 credits to testAccount2.
    2. issue();
  5. Conduct an account transfer.

    1. //step 9 : transfer 50 credits from testAccount2 to testAccount1
    2. transfer();
  6. Query the account balance.

    1. //step 10 : query testAccount2 whose balance should be 50.
    2. BigInteger balance = query(test2PrivateKeyArrayList,testAccount2);
    3. //step 11 : compare to expect balance.
    4. expect(balance,BigInteger.valueOf(50));
  7. Disconnect SDK.

    1. //step 12 : sdk shut down
    2. sdk.shutDown();