Your First Transaction
This section is a quick start guide for interacting with Algorand network using Java. This guide will help to install 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.
Sandbox Install¶
Prerequisites
- Docker Compose (install guide)
- Git (install guide)
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 testnet
This will install a Sandbox node connected to the Algorand TestNet. To read more about Algorand networks see Algorand Networks.
Warning
The sandbox installation may take a few minutes to startup in order to catch up to the current block round. To learn more about fast catchup, see Sync Node Network using Fast Catchup
.
Install SDK For Runtime¶
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.
Requirements: 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 on Algorand¶
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());
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¶
The code above prompts to fund the newly created account. Before sending transactions to the Algorand network, the account must be funded to cover the minimal transaction fees that exist on Algorand. To fund the account use the Algorand TestNet 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¶
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 algodHost
corresponds to http://localhost:4001
.
Build First Transaction¶
Communication with the Algorand network is performed using transactions. To create a payment transaction use the following code, which also includes some utility functions, connectToNetwork
, and PrintBalance
. Add this code to the GettingStarted class above.
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);
Info
Algorand supports many transaction types. To see what types are supported see Transactions.
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);
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();
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);
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);
Complete Example¶
The complete example below illustrates how to quickly submit your first transaction.
TODO::¶
Warning
In order for this transaction to be successful, the account must be funded.
Viewing the Transaction¶
To view the transaction, open the Algorand Blockchain Explorer or Goal Seeker and paste the transaction ID into the search bar or simply click on the funded transaction link on the dispenser page.