forked from near/near-api-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
get-tx-detail.js
81 lines (69 loc) · 2.78 KB
/
get-tx-detail.js
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
const { connect, keyStores } = require("near-api-js");
const path = require("path");
const homedir = require("os").homedir();
const CREDENTIALS_DIR = ".near-credentials";
// block hash of query start (oldest block)
const START_BLOCK_HASH = "GZ8vKdcgsavkEndkDWHCjuhyqSR2TGnp9VDZbTzd6ufG";
// block hash of query end (newest block)
const END_BLOCK_HASH = "8aEcKhF7N1Jyw84e6vHW6Hzp3Ep7mSXJ6Rvnsy5qGJPF";
// contract ID or account ID you want to find transactions details for
const CONTRACT_ID = "relayer.ropsten.testnet";
const credentialsPath = path.join(homedir, CREDENTIALS_DIR);
const keyStore = new keyStores.UnencryptedFileSystemKeyStore(credentialsPath);
// NOTE: we're using the archival rpc to look back in time for a specific set
// of transactions. For a full list of what nodes are available, visit:
// https://docs.near.org/docs/develop/node/intro/types-of-node
const config = {
keyStore,
networkId: "testnet",
nodeUrl: "https://archival-rpc.testnet.near.org",
};
getTransactions(START_BLOCK_HASH, END_BLOCK_HASH, CONTRACT_ID);
async function getTransactions(startBlock, endBlock, accountId) {
const near = await connect(config);
// creates an array of block hashes for given range
const blockArr = [];
let blockHash = endBlock;
do {
const currentBlock = await getBlockByID(blockHash);
blockArr.push(currentBlock.header.hash);
blockHash = currentBlock.header.prev_hash;
console.log("working...", blockHash);
} while (blockHash !== startBlock);
// returns block details based on hashes in array
const blockDetails = await Promise.all(
blockArr.map((blockId) =>
near.connection.provider.block({
blockId,
})
)
);
// returns an array of chunk hashes from block details
const chunkHashArr = blockDetails.flatMap((block) =>
block.chunks.map(({ chunk_hash }) => chunk_hash)
);
//returns chunk details based from the array of hashes
const chunkDetails = await Promise.all(
chunkHashArr.map(chunk => near.connection.provider.chunk(chunk))
);
// checks chunk details for transactions
// if there are transactions in the chunk we
// find ones associated with passed accountId
const transactions = chunkDetails.flatMap((chunk) =>
(chunk.transactions || []).filter((tx) => tx.signer_id === accountId)
);
//creates transaction links from matchingTxs
const txsLinks = transactions.map((txs) => ({
method: txs.actions[0].FunctionCall.method_name,
link: `https://explorer.testnet.near.org/transactions/${txs.hash}`,
}));
console.log("MATCHING TRANSACTIONS: ", transactions);
console.log("TRANSACTION LINKS: ", txsLinks);
}
async function getBlockByID(blockID) {
const near = await connect(config);
const blockInfoByHeight = await near.connection.provider.block({
blockId: blockID,
});
return blockInfoByHeight;
}