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 the Algorand network using JavaScript. This guide will help to install the Algorand sandbox, which provides a node for testing and development. This guide will also help to install the JavaScript SDK, create an account and submit your first transaction using different JavaScript Runtimes. ​

Sandbox Install

Prerequisites

Algorand provides a Docker instance for setting up a node or private network, which can be used to get started developing. You can find more information about setting up a development environment in this section. To install and use this instance, follow these instructions. ​

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

More Information about the sandbox and how to use it. ​

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 JavaScript. The instructions for installing the SDK will depend on what runtime you plan on using.

Prerequisites

  • Install Node.js
    # initialize project
    npm init
    # install Algorand sdk
    npm install algosdk
    # list the version
    npm list algosdk
    
    # This package provides TypeScript types, but you will need TypeScript version 4.2 or higher to use them properly.
    

Watch Video

More Information

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

Info

Using a Web Runtime requires the AlgoSigner extension or other web-based private key management software. For more information see community wallets.

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. The account object contains an address (addr) and private key (sk). You can also export the mnemonic so you can later import the account in key management software like AlgoSigner.

const generatedAccount = algosdk.generateAccount();
const passphrase = algosdk.secretKeyToMnemonic(generatedAccount.sk);
console.log(`My address: ${generatedAccount.addr}`);
console.log(`My passphrase: ${passphrase}`);
Snippet Source

More Information

Tip

Make sure to save your account's address and passphrase at a separate place, as they will be used later on. ​

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 below 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 minimum balance see Account Overview

Watch Video

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 (64 "a"s) with server address http://localhost and port 4001.

const algodToken = 'a'.repeat(64);
const algodServer = 'http://localhost';
const algodPort = 4001;

const algodClient = new algosdk.Algodv2(algodToken, algodServer, algodPort);
Snippet Source

Info

The example code connects to the sandbox Algod client. If you want to connect to a other clients, see Purestake or AlgoExplorer Developer API.

Check Your Balance

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

const acctInfo = await algodClient.accountInformation(acct.addr).do();
console.log(`Account balance: ${acctInfo.amount} microAlgos`);
Snippet Source

Build First Transaction

To interact with the Algorand blockchain, you can send different types of transactions. The following code shows how to create a payment transaction to transfer Algo tokens to a different address. To construct a transaction, you need to retrieve the parameters about the Algorand network first. You can choose to set a fee yourself, however, by default the fee is set to 1000 microAlgos (0.001 Algo). Optionally, you can add a message to the transaction using the note field (up to 1000 bytes). You can find more information about transaction fields in the documentation. ​

const suggestedParams = await algodClient.getTransactionParams().do();
const ptxn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
  from: acct.addr,
  suggestedParams,
  to: acct2.addr,
  amount: 10000,
  note: new Uint8Array(Buffer.from('hello world')),
});
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. Now, you can extract the transaction ID. Actually, you can even extract the transaction ID before signing the transaction. You'll use the txId to look up the status of the transaction in the following sections of this guide. ​

const signedTxn = ptxn.signTxn(acct.privateKey);
Snippet Source

Info

Algorand provides many ways to sign transactions. To see other ways see Authorization.

Submit the Transaction

The signed transaction can now be submitted to the network.waitForConfirmation is called after the transaction is submitted to wait until the transaction is broadcast to the Algorand blockchain and is confirmed. The below snippet also shows how you can decode the data in the node field again to make it readable.

const { txId } = await algodClient.sendRawTransaction(signedTxn).do();
const result = await algosdk.waitForConfirmation(algodClient, txId, 4);
console.log(result);
console.log(`Transaction Information: ${result.txn}`);
console.log(`Decoded Note: ${Buffer.from(result.txn.txn.note).toString()}`);
Snippet Source

Warning

In order for this transaction to be successful, the generated account must be funded.

View the Transaction

To view the transaction, open AlgoExplorer or Goal Seeker and paste the transaction ID into the search bar.