Skip to content

创建文章

We are looking for publications that demonstrate building dApps or smart contracts!
See the full list of Gitcoin bounties that are eligible for rewards.

Your First Transaction

This section is a quick start guide for interacting with Algorand network using Java. This guide will help to install Algorand sandbox, which provides a node for testing and development. This guide will also help to install the Java SDK, create an account and submit your first transaction.

Install Sandbox

Info

This step is only required if you are not using AlgoKit. If you are using AlgoKit, you can spin up a sandbox using the LocalNet, see AlgoKit getting started guide for more information.

Prerequisites

Algorand provides a docker instance for setting up a node, which can be used to get started developing. To install and use this instance, follow these instructions.

git clone https://github.com/algorand/sandbox.git
cd sandbox
./sandbox up dev 

This will install and start private network. To read more about Algorand networks see Algorand Networks.

More Information about the sandbox and how to use it.

Install Java SDK

Algorand provides an SDK for Java. The instructions for installing the SDK are as follows. The Java SDK is available in the MVN repository and can be used in your Maven project by including the following dependency.

Prerequisites

Java SDK requires Java 7+ and Android minSdkVersion 16+. Check for the latest version of the Java SDK here.

<dependency>
    <groupId>com.algorand</groupId>
    <artifactId>algosdk</artifactId>
    <version>2.0.0</version>
</dependency>

The GitHub repository contains additional documentation and examples.

See the Java SDK reference documentation for more information on packages and methods.

The SDK is installed and can now interact with the Sandbox created earlier.

Create an Account

In order to interact with the Algorand blockchain, you must have a funded account. To quickly create a test account use the following code.

Account acct = new Account();
System.out.println("Address: " + acct.getAddress());
System.out.println("Passphrase: " + acct.toMnemonic());
Snippet Source

More Information

Warning

Never share Mnemonic private keys. Production environments require stringent private key management. For more information on key management in community Wallets, click here. For the Algorand open source wallet, click here.

Fund the Account

Before sending transactions to the Algorand network, the account must be funded to cover the minimal transaction fees that exist on Algorand. In this example, we'll be using prefunded accounts available in the Sandbox. To fund an account on Testnet account use the Algorand faucet.

Info

All Algorand accounts require a minimum balance to be registered in the ledger. To read more about Algorand minimums see this link.

Connect Your Client

An Algod client must be instantiated prior to making calls to the API endpoints. You must provide values for <algod-address> and <algod-token>. The CLI tools implement the client natively. By default, the algodToken for each sandbox is set to its aaa... value and the algodAddress corresponds to http://localhost:4001.

String algodHost = "http://localhost";
int algodPort = 4001;
String algodToken = "a".repeat(64);
AlgodClient algodClient = new AlgodClient(algodHost, algodPort, algodToken);

// OR if the API provider requires a specific header key for the token
String tokenHeader = "X-API-Key";
AlgodClient otherAlgodClient = new AlgodClient(algodHost, algodPort, algodToken, tokenHeader);
Snippet Source

Info

The example code connects to the sandbox Algod client. If you want to connect to a public API client, change the host, port, and token parameters to match the API service. See some service available here

Info

If you are connecting to the Testnet, a dispenser is available here

Check Your Balance

Before moving on to the next step, make sure your account has been funded by the faucet.

Response<com.algorand.algosdk.v2.client.model.Account> acctInfoResp = algodClient
        .AccountInformation(acct.getAddress()).execute();
com.algorand.algosdk.v2.client.model.Account acctInfo = acctInfoResp.body();
// print one of the fields in the account info response
System.out.printf("Current balance: %d", acctInfo.amount);
Snippet Source

Build First Transaction

Transactions are used to interact with the Algorand network. To create a payment transaction use the following code. ​

Response<TransactionParametersResponse> suggestedParams = algodClient.TransactionParams().execute();
Integer amount = 1000000; // 1 Algo
Transaction ptxn = Transaction.PaymentTransactionBuilder()
        .sender(acct.getAddress())
        .amount(amount)
        .receiver(acct2.getAddress())
        .suggestedParams(suggestedParams.body()).build();
Snippet Source

Info

Algorand supports many transaction types. To see what types are supported see Transactions.

Sign First Transaction

Before the transaction is considered valid, it must be signed by a private key. Use the following code to sign the transaction.

SignedTransaction sptxn = acct.signTransaction(ptxn);
Snippet Source

Info

Algorand provides additional ways for transactions to be signed, other than by a standalone account. For more information see Authorization.

Submit the Transaction

The signed transaction can now be submitted to the network. The SDK waitForConfirmation utility function is called after the transaction is submitted to wait until the transaction is broadcast to the Algorand blockchain and is confirmed.

// encode the transaction
byte[] encodedTxBytes = Encoder.encodeToMsgPack(sptxn);
// submit the transaction to the algod server
Response<PostTransactionsResponse> resp = algodClient.RawTransaction().rawtxn(encodedTxBytes).execute();
// wait for the transaction to be confirmed
String txid = resp.body().txId;
PendingTransactionResponse result = Utils.waitForConfirmation(algodClient, txid, 4);
System.out.printf("Transaction %s confirmed in round %d\n", txid, result.confirmedRound);
Snippet Source

Viewing the Transaction

To view the transaction we submitted to the sandbox Algod, open DappFlow and choose Sandbox configuration option, then search for the transaction ID.

To view a transaction submitted to public network like testnet, open Pera Explorer or Bitquery and paste the transaction ID into the search bar or simply click on the funded transaction link on the dispenser page.