-
Notifications
You must be signed in to change notification settings - Fork 210
/
Copy pathindexer.ts
101 lines (86 loc) · 2.66 KB
/
indexer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* eslint-disable import/extensions */
/* eslint-disable import/no-unresolved */
/* eslint-disable no-promise-executor-return */
/* eslint-disable no-console */
import { Buffer } from 'buffer';
import {
getLocalIndexerClient,
getLocalAccounts,
getLocalAlgodClient,
indexerWaitForRound,
} from './utils';
import algosdk from '../src';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function getIndexerClient(): void {
// example: INDEXER_CREATE_CLIENT
const indexerToken = 'a'.repeat(64);
const indexerServer = 'http://localhost';
const indexerPort = 8980;
const indexerClient = new algosdk.Indexer(
indexerToken,
indexerServer,
indexerPort
);
// example: INDEXER_CREATE_CLIENT
console.log(indexerClient);
}
async function main() {
const indexerClient = getLocalIndexerClient();
// example: INDEXER_SEARCH_MIN_AMOUNT
const transactionInfo = await indexerClient
.searchForTransactions()
.currencyGreaterThan(100)
.do();
console.log(transactionInfo.transactions.map((t) => t.id));
// example: INDEXER_SEARCH_MIN_AMOUNT
// example: INDEXER_PAGINATE_RESULTS
let nextToken = '';
// nextToken will be undefined if we reached the last page
while (nextToken !== undefined) {
// eslint-disable-next-line no-await-in-loop
const response = await indexerClient
.searchForTransactions()
.limit(5)
.currencyGreaterThan(10)
.nextToken(nextToken)
.do();
nextToken = response['next-token'];
const txns = response.transactions;
if (txns.length > 0)
console.log(`Transaction IDs: ${response.transactions.map((t) => t.id)}`);
}
// example: INDEXER_PAGINATE_RESULTS
const client = getLocalAlgodClient();
const accounts = await getLocalAccounts();
const suggestedParams = await client.getTransactionParams().do();
const sender = accounts[0];
const txn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
from: sender.addr,
to: sender.addr,
amount: 1e6,
note: new Uint8Array(Buffer.from('Hello World!')),
suggestedParams,
});
await client.sendRawTransaction(txn.signTxn(sender.privateKey)).do();
const result = await algosdk.waitForConfirmation(
client,
txn.txID().toString(),
3
);
// ensure indexer is caught up
await indexerWaitForRound(indexerClient, result['confirmed-round'], 30);
// example: INDEXER_PREFIX_SEARCH
const txnsWithNotePrefix = await indexerClient
.searchForTransactions()
.notePrefix(Buffer.from('Hello'))
.do();
console.log(
`Transactions with note prefix "Hello" ${JSON.stringify(
txnsWithNotePrefix,
undefined,
2
)}`
);
// example: INDEXER_PREFIX_SEARCH
}
main();