Skip to content

Commit 24a3046

Browse files
fionnachanaldur
authored andcommitted
Document Algodv2 methods (#486)
* Document Algodv2 methods * Link to relevant methods on Algodv2 doc * Replace algoexplorer api with sandbox api * Clean up pendingTxns examples * Show setIntEncoding method on docs
1 parent 267f6ac commit 24a3046

File tree

2 files changed

+257
-7
lines changed

2 files changed

+257
-7
lines changed

src/client/v2/algod/algod.ts

Lines changed: 255 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,37 @@ import {
2424
CustomTokenHeader,
2525
} from '../../urlTokenBaseHTTPClient';
2626

27+
/**
28+
* Algod client connects an application to the Algorand blockchain. The algod client requires a valid algod REST endpoint IP address and algod token from an Algorand node that is connected to the network you plan to interact with.
29+
*
30+
* Algod is the main Algorand process for handling the blockchain. Messages between nodes are processed, the protocol steps are executed, and the blocks are written to disk. The algod process also exposes a REST API server that developers can use to communicate with the node and the network. Algod uses the data directory for storage and configuration information.
31+
*
32+
* #### Relevant Information
33+
* [How do I obtain an algod address and token?](https://developer.algorand.org/docs/archive/build-apps/setup/?from_query=algod#how-do-i-obtain-an-algod-address-and-token)
34+
*
35+
* [Run Algod in Postman OAS3](https://developer.algorand.org/docs/rest-apis/restendpoints/?from_query=algod#algod-indexer-and-kmd-rest-endpoints)
36+
*/
2737
export default class AlgodClient extends ServiceClient {
2838
/**
2939
* Create an AlgodClient from
3040
* * either a token, baseServer, port, and optional headers
3141
* * or a base client server for interoperability with external dApp wallets
42+
*
43+
* #### Example
44+
* ```typescript
45+
* const token = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
46+
* const server = "http://localhost";
47+
* const port = 4001;
48+
* const algodClient = new algosdk.Algodv2(token, server, port);
49+
* ```
50+
* @remarks
51+
* The above configuration is for a sandbox private network.
52+
* For applications on production, you are encouraged to run your own node, or use an Algorand REST API provider with a dedicated API key.
53+
*
54+
* @param tokenOrBaseClient - The algod token from the Algorand node you are interacting with
55+
* @param baseServer - REST endpoint
56+
* @param port - Port number if specifically configured by the server
57+
* @param headers - Optional headers
3258
*/
3359
constructor(
3460
tokenOrBaseClient:
@@ -43,123 +69,345 @@ export default class AlgodClient extends ServiceClient {
4369
super('X-Algo-API-Token', tokenOrBaseClient, baseServer, port, headers);
4470
}
4571

72+
/**
73+
* Returns OK if healthy.
74+
*
75+
* #### Example
76+
* ```typescript
77+
* const health = await algodClient.healthCheck().do();
78+
* ```
79+
*
80+
* [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/v2/#get-health)
81+
* @category GET
82+
*/
4683
healthCheck() {
4784
return new HealthCheck(this.c);
4885
}
4986

87+
/**
88+
* Retrieves the supported API versions, binary build versions, and genesis information.
89+
*
90+
* #### Example
91+
* ```typescript
92+
* const versionsDetails = await algodClient.versionsCheck().do();
93+
* ```
94+
*
95+
* [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/v2/#get-versions)
96+
* @category GET
97+
*/
5098
versionsCheck() {
5199
return new Versions(this.c);
52100
}
53101

102+
/**
103+
* Broadcasts a raw transaction to the network.
104+
*
105+
* #### Example
106+
* ```typescript
107+
* const { txId } = await algodClient.sendRawTransaction(signedTxns).do();
108+
* const result = await waitForConfirmation(algodClient, txid, 3);
109+
* ```
110+
*
111+
* [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/v2/#post-v2transactions)
112+
*
113+
* @remarks
114+
* Often used with {@linkcode waitForConfirmation}
115+
* @param stxOrStxs - Signed transactions
116+
* @category POST
117+
*/
54118
sendRawTransaction(stxOrStxs: Uint8Array | Uint8Array[]) {
55119
return new SendRawTransaction(this.c, stxOrStxs);
56120
}
57121

58122
/**
59-
* Returns the given account's information.
123+
* Returns the given account's status, balance and spendable amounts.
124+
*
125+
* #### Example
126+
* ```typescript
127+
* const address = "XBYLS2E6YI6XXL5BWCAMOA4GTWHXWENZMX5UHXMRNWWUQ7BXCY5WC5TEPA";
128+
* const accountInfo = await algodClient.accountInformation(address).do();
129+
* ```
130+
*
131+
* [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/v2/#get-v2accountsaddress)
60132
* @param account - The address of the account to look up.
133+
* @category GET
61134
*/
62135
accountInformation(account: string) {
63136
return new AccountInformation(this.c, this.intDecoding, account);
64137
}
65138

66139
/**
67140
* Gets the block info for the given round.
141+
*
142+
* #### Example
143+
* ```typescript
144+
* const roundNumber = 18038133;
145+
* const block = await algodClient.block(roundNumber).do();
146+
* ```
147+
*
148+
* [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/v2/#get-v2blocksround)
68149
* @param roundNumber - The round number of the block to get.
150+
* @category GET
69151
*/
70152
block(roundNumber: number) {
71153
return new Block(this.c, roundNumber);
72154
}
73155

74156
/**
75157
* Returns the transaction information for a specific pending transaction.
158+
*
159+
* #### Example
160+
* ```typescript
161+
* const txId = "DRJS6R745A7GFVMXEXWP4TGVDGKW7VILFTA7HC2BR2GRLHNY5CTA";
162+
* const pending = await algodClient.pendingTransactionInformation(txId).do();
163+
* ```
164+
*
165+
* [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/v2/#get-v2transactionspendingtxid)
166+
*
167+
* @remarks
168+
* <br><br>
169+
* There are several cases when this might succeed:
170+
* - transaction committed (committed round > 0)
171+
* - transaction still in the pool (committed round = 0, pool error = "")
172+
* - transaction removed from pool due to error (committed round = 0, pool error != "")
173+
*
174+
* Or the transaction may have happened sufficiently long ago that the node no longer remembers it, and this will return an error.
175+
*
76176
* @param txid - The TxID string of the pending transaction to look up.
177+
* @category GET
77178
*/
78179
pendingTransactionInformation(txid: string) {
79180
return new PendingTransactionInformation(this.c, txid);
80181
}
81182

82183
/**
83-
* Returns transactions that are pending in the pool.
184+
* Returns the list of pending transactions in the pool, sorted by priority, in decreasing order, truncated at the end at MAX.
185+
* If MAX = 0, returns all pending transactions.
186+
*
187+
* #### Example 1
188+
* ```typescript
189+
* const pendingTxns = await algodClient.pendingTransactionsInformation().do();
190+
* ```
191+
*
192+
* #### Example 2
193+
* ```typescript
194+
* const maxTxns = 5;
195+
* const pendingTxns = await algodClient
196+
* .pendingTransactionsInformation()
197+
* .max(maxTxns)
198+
* .do();
199+
* ```
200+
*
201+
* [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/v2/#get-v2transactionspending)
202+
* @category GET
84203
*/
85204
pendingTransactionsInformation() {
86205
return new PendingTransactions(this.c);
87206
}
88207

89208
/**
90-
* Returns transactions that are pending in the pool sent by a specific sender.
209+
* Returns the list of pending transactions sent by the address, sorted by priority, in decreasing order, truncated at the end at MAX.
210+
* If MAX = 0, returns all pending transactions.
211+
*
212+
* #### Example 1
213+
* ```typescript
214+
* const address = "XBYLS2E6YI6XXL5BWCAMOA4GTWHXWENZMX5UHXMRNWWUQ7BXCY5WC5TEPA";
215+
* const pendingTxnsByAddr = await algodClient.pendingTransactionByAddress(address).do();
216+
* ```
217+
*
218+
* #### Example 2
219+
* ```typescript
220+
* const maxTxns = 5;
221+
* const address = "XBYLS2E6YI6XXL5BWCAMOA4GTWHXWENZMX5UHXMRNWWUQ7BXCY5WC5TEPA";
222+
* const pendingTxns = await algodClient
223+
* .pendingTransactionByAddress(address)
224+
* .max(maxTxns)
225+
* .do();
226+
* ```
227+
*
228+
* [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/v2/#get-v2accountsaddresstransactionspending)
91229
* @param address - The address of the sender.
230+
* @category GET
92231
*/
93232
pendingTransactionByAddress(address: string) {
94233
return new PendingTransactionsByAddress(this.c, address);
95234
}
96235

97236
/**
98237
* Retrieves the StatusResponse from the running node.
238+
*
239+
* #### Example
240+
* ```typescript
241+
* const status = await algodClient.status().do();
242+
* ```
243+
*
244+
* [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/v2/#get-v2status)
245+
* @category GET
99246
*/
100247
status() {
101248
return new Status(this.c, this.intDecoding);
102249
}
103250

104251
/**
105-
* Waits for a specific round to occur then returns the StatusResponse for that round.
252+
* Waits for a specific round to occur then returns the `StatusResponse` for that round.
253+
*
254+
* #### Example
255+
* ```typescript
256+
* const round = 18038133;
257+
* const statusAfterBlock = await algodClient.statusAfterBlock(round).do();
258+
* ```
259+
*
260+
* [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/v2/#get-v2statuswait-for-block-afterround)
106261
* @param round - The number of the round to wait for.
262+
* @category GET
107263
*/
108264
statusAfterBlock(round: number) {
109265
return new StatusAfterBlock(this.c, this.intDecoding, round);
110266
}
111267

112268
/**
113269
* Returns the common needed parameters for a new transaction.
270+
*
271+
* #### Example
272+
* ```typescript
273+
* const suggestedParams = await algodClient.getTransactionParams().do();
274+
* const amountInMicroAlgos = algosdk.algosToMicroalgos(2); // 2 Algos
275+
* const unsignedTxn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
276+
* from: senderAddress,
277+
* to: receiverAddress,
278+
* amount: amountInMicroAlgos,
279+
* suggestedParams: suggestedParams,
280+
* });
281+
* ```
282+
*
283+
* [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/v2/#get-v2transactionsparams)
284+
*
285+
* @remarks
286+
* Often used with
287+
* {@linkcode makePaymentTxnWithSuggestedParamsFromObject}, {@linkcode algosToMicroalgos}
288+
* @category GET
114289
*/
115290
getTransactionParams() {
116291
return new SuggestedParams(this.c);
117292
}
118293

119294
/**
120-
* Gets the supply details for the specified node's ledger.
295+
* Returns the supply details for the specified node's ledger.
296+
*
297+
* #### Example
298+
* ```typescript
299+
* const supplyDetails = await algodClient.supply().do();
300+
* ```
301+
*
302+
* [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/v2/#get-v2ledgersupply)
303+
* @category GET
121304
*/
122305
supply() {
123306
return new Supply(this.c, this.intDecoding);
124307
}
125308

309+
/**
310+
* Compiles TEAL source code to binary, returns base64 encoded program bytes and base32 SHA512_256 hash of program bytes (Address style).
311+
*
312+
* #### Example
313+
* ```typescript
314+
* const source = "TEAL SOURCE CODE";
315+
* const compiledSmartContract = await algodClient.compile(source).do();
316+
* ```
317+
*
318+
* [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/v2/#post-v2tealcompile)
319+
* @remarks
320+
* This endpoint is only enabled when a node's configuration file sets `EnableDeveloperAPI` to true.
321+
* @param source
322+
* @category POST
323+
*/
126324
compile(source: string | Uint8Array) {
127325
return new Compile(this.c, source);
128326
}
129327

328+
/**
329+
* Provides debugging information for a transaction (or group).
330+
*
331+
* Executes TEAL program(s) in context and returns debugging information about the execution. This endpoint is only enabled when a node's configureation file sets `EnableDeveloperAPI` to true.
332+
*
333+
* #### Example
334+
* ```typescript
335+
* const dryRunResult = await algodClient.dryrun(dr).do();
336+
* ```
337+
*
338+
* [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/v2/#post-v2tealdryrun)
339+
* @param dr
340+
* @category POST
341+
*/
130342
dryrun(dr: modelsv2.DryrunRequest) {
131343
return new Dryrun(this.c, dr);
132344
}
133345

134346
/**
135347
* Given an asset ID, return asset information including creator, name, total supply and
136348
* special addresses.
349+
*
350+
* #### Example
351+
* ```typescript
352+
* const asset_id = 163650;
353+
* const asset = await algodClient.getAssetByID(asset_id).do();
354+
* ```
355+
*
356+
* [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/v2/#get-v2assetsasset-id)
137357
* @param index - The asset ID to look up.
358+
* @category GET
138359
*/
139360
getAssetByID(index: number) {
140361
return new GetAssetByID(this.c, this.intDecoding, index);
141362
}
142363

143364
/**
144-
* Given an application ID, it returns application information including creator, approval
365+
* Given an application ID, return the application information including creator, approval
145366
* and clear programs, global and local schemas, and global state.
367+
*
368+
* #### Example
369+
* ```typescript
370+
* const index = 60553466;
371+
* const app = await algodClient.getApplicationByID(index).do();
372+
* ```
373+
*
374+
* [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/v2/#get-v2applicationsapplication-id)
146375
* @param index - The application ID to look up.
376+
* @category GET
147377
*/
148378
getApplicationByID(index: number) {
149379
return new GetApplicationByID(this.c, this.intDecoding, index);
150380
}
151381

152382
/**
153383
* Returns the entire genesis file.
384+
*
385+
* #### Example
386+
* ```typescript
387+
* const genesis = await algodClient.genesis().do();
388+
* ```
389+
*
390+
* [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/v2/#get-genesis)
391+
* @category GET
154392
*/
155393
genesis() {
156394
return new Genesis(this.c, this.intDecoding);
157395
}
158396

159397
/**
160-
* Get the proof for a given transaction in a round.
398+
* Returns a Merkle proof for a given transaction in a block.
399+
*
400+
* #### Example
401+
* ```typescript
402+
* const round = 18038133;
403+
* const txId = "MEUOC4RQJB23CQZRFRKYEI6WBO73VTTPST5A7B3S5OKBUY6LFUDA";
404+
* const proof = await algodClient.getProof(round, txId).do();
405+
* ```
406+
*
407+
* [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/v2/#get-v2blocksroundtransactionstxidproof)
161408
* @param round - The round in which the transaction appears.
162409
* @param txID - The transaction ID for which to generate a proof.
410+
* @category GET
163411
*/
164412
getProof(round: number, txID: string) {
165413
return new Proof(this.c, this.intDecoding, round, txID);

0 commit comments

Comments
 (0)