- vừa được xem lúc

Creating an Account and Sending Payment Transactions using Algorand JavaScript SDK

0 0 7

Người đăng: Cường Phạm Vũ

Theo Viblo Asia

In this tutorial, we will learn how to create an Algorand account and send payment transactions using the Algorand JavaScript SDK. The Algorand SDK provides various methods and wrappers for connecting to the Algorand network. Here, we will be using JavaScript, but there are also SDKs available for Python, Go, and Java. Additionally, there are community-driven SDKs such as the C#/.NET SDK and Unity SDK, offering multiple options to develop on the Algorand blockchain.

Step 1: Cloning the Repository

First, clone the repository https://github.com/cuongpo/algo-bootcamp to get the necessary code for our tutorial.

git clone https://github.com/cuongpo/algo-bootcamp

Step 2: Installing Dependencies

Next, navigate to the cloned repository and install the required dependencies using yarn:

cd algo-bootcamp yarn install

Step 3: Importing Important Packages

Open the index.ts file in your preferred code editor. The first thing we need to do is import the algosdk package, which provides various methods and wrappers for interacting with the Algorand network.

import algosdk from 'algosdk';

The algosdk package allows us to connect to the Algorand network and interact with it. Along with JavaScript, there are SDKs available for Python, Go, and Java. Although the official SDKs are the recommended ones, there are also community-driven SDKs that offer similar functionalities. For example, there is a C#/.NET SDK and a Unity SDK.

Additionally, we need to import information about the smart contract. In this case, we can import it from the contract.json file:

import * as abi from './contract/contract.json';

Step 4: Creating an Account

To interact with the Algorand blockchain, we need to ensure that we have an account. There are several ways to obtain an account. You can use a wallet like Pera, a local key management daemon (KMD), or create an account using the SDK.

To create an account using the SDK, we can use the generateAccount() method:

const account = algosdk.generateAccount();

After creating the account, we can print the secret key and address of the account:

console.log('Mnemonic:', algosdk.secretKeyToMnemonic(account.sk));
console.log('Address:', account.addr);

The generateAccount() method generates a new Algorand account and returns an object containing the secret key (account.sk) and the address (account.addr). The secret key is used to sign transactions and should be kept secure.

Step 5: Check account balance

Now that we have created our first account, let's proceed to learn how to check the balance of the account. We need to connect to the Algorand network using the algodClient, which provides a connection to the Algorand node's HTTP endpoints.

const algodToken = '';
const algodServer = 'https://testnet-api.algonode.cloud';
const algodPort = undefined;
const algodClient = new algosdk.Algodv2(algodToken, algodServer, algodPort);

We create an instance of algosdk.Algodv2 with the algodToken (your Algod API token) and the algodServer (the Algorand node's URL). If necessary, you can specify the algodPort as well.

To check the balance of the account via algodClient, we can use the following code:

let accountInfo = await algodClient.accountInformation(account.addr).do(); console.log('accountInfo:', accountInfo); await waitForInput();

The accountInformation() method retrieves the account information from the Algorand node. The account information includes the balance, pending rewards, and other relevant data. The await keyword is used to wait for the asynchronous call to complete.

Additionally, we create an async function called waitForBalance() to wait until the account is funded:

// Check balance of account via algod
const waitForBalance = async () => { accountInfo = await algodClient.accountInformation(account.addr).do(); const balance = accountInfo.amount; if (balance === 0) { await waitForBalance(); }
}; await waitForBalance(); console.log(`${account.addr} funded!`); await waitForInput();

The waitForBalance() function retrieves the account information repeatedly until the account balance is greater than zero. This ensures that the account is funded before proceeding with the next steps. The waitForInput() function is used to pause the execution and wait for user input.

Full code index.ts file

import algosdk from 'algosdk';
import readline from 'readline';
import process from 'process';
import fs from 'fs';
import * as abi from './contract/contract.json'; const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const skipPrompts = process.argv.includes('--skip-prompts'); function waitForInput() { if (skipPrompts === true) return new Promise((resolve) => { resolve(true); }); return new Promise((resolve) => { rl.question('Press enter to continue...', resolve); });
} async function main() { // Account creation const account = algosdk.generateAccount(); console.log('Mnemonic:', algosdk.secretKeyToMnemonic(account.sk)); console.log('Address:', account.addr); await waitForInput(); // Create connection to network via public algod API const algodToken = ''; const algodServer = 'https://testnet-api.algonode.cloud'; const algodPort = undefined; const algodClient = new algosdk.Algodv2(algodToken, algodServer, algodPort); // get accountInfo let accountInfo = await algodClient.accountInformation(account.addr).do(); console.log('accountInfo:', accountInfo); await waitForInput(); console.log('Dispsene ALGO at https://testnet.algoexplorer.io/dispenser. Script will continue once ALGO is received...'); // Check balance of account via algod const waitForBalance = async () => { accountInfo = await algodClient.accountInformation(account.addr).do(); const balance = accountInfo.amount; if (balance === 0) { await waitForBalance(); } }; await waitForBalance(); console.log(`${account.addr} funded!`); await waitForInput(); // Get basic information needed for every transcation const suggestedParams = await algodClient.getTransactionParams().do(); console.log('suggestedParams:', suggestedParams); await waitForInput(); }

Let's run the code by using

npx tsx index.ts

The account was generated successfully

Now, to fund your Algorand account, you can visit one of the following faucet websites:

After funding your account, you can proceed with the next steps in the script.

Congratulations! You have successfully created an Algorand account and checked its balance. In the next steps, you can learn how to send payment transactions using the Algorand JavaScript SDK.

Bình luận

Bài viết tương tự

- vừa được xem lúc

[Blockchain] Road to Bitcoin

. Chắc mọi người hẳn đã không còn xa lạ gì với anh chàng tỷ phú đã ném vỡ cửa kính ô tô nhà mình cùng với siêu năng lực điều khiển vật giá chỉ bằng lời nói, người đã đẩy định giá Bitcoin trên thị trường vượt ngưỡng 50K dolar/coin với những bài twitter để đời . .

0 0 44

- vừa được xem lúc

Khi Ethereum có chi phí giao dịch quá đắt đỏ - Tương lai cho layer2 ?

Với sự phát triển như vũ bão của Blockchain, ETH dường như đang quá tải và hệ quả là chi phí Gas đã lên đến 1000Gwei, phí để tạo những transaction phức tạp đã xấp xỉ 500$ . Và một giải pháp cứu cánh cho các sản phẩm Defi trên ETH chính là Layer2, và trong nhiệm vụ lần này Matic đang thể hiện khả năn

0 0 73

- vừa được xem lúc

Blockchain với Java - Tại sao không?

Cuộc cách mạng công nghiệp 4.0 ra đời kéo theo nhiều sự thay đổi và xu hướng mới được hình thành. Riêng đối với lĩnh vực CNTT cũng không nằm ngoài vùng ảnh hưởng mạnh mẽ. Chính làn sóng 4.

0 0 79

- vừa được xem lúc

Phân loại và tầm quan trọng của các node trong mạng blockchain

Trước khi đi vào phân loại và nêu rõ được tầm quan trọng của các node trọng mạng blockchain thì mình xin được trích dẫn khái niệm về blockchain từ Wikipedia như sau:. .

0 1 46

- vừa được xem lúc

Code Smart Contract bằng Assembly ?

Introduction. Hồi còn học trong ghế nhà trường bộ môn lập trình tốn nhiều não nhất của mình là code assembly. Nôm na thì bất cứ ngôn ngữ bậc cao nào như C , Go, Java,... được sinh ra để người dễ hiểu và dễ code , tuy nhiên chúng đều sẽ được compiled down xuống assembly một ngôn ngữ bậc thấp để máy h

0 0 43

- vừa được xem lúc

Dextool - Công cụ phân tích Decentralized Exchange tuyệt vời

. Trend Defi mặc dù đã bớt nhiệt nhưng những sản phẩm nổi bật của làn sóng này mang lại thì vẫn rất được người dùng ưa chuộng. Đặc biệt là các nền tảng Decentralized Exchange, tiêu biểu là Uniswap, SushiSwap, 1inch Exchange, FalconSwap,... Nhưng khi đã sử dụng các nền tảng DEx này mà không biết đến

0 0 92