CityCoins
Search…
Get Account Transactions
Getting and manipulating transactions from the Stacks blockchain.

Getting Account Transactions

The Stacks API provides an endpoint for getting all transactions related to a principal, which can be a Stacks address or a contract identifier.
Contract identifiers are formatted deployer_address.contract-name
e.g. SP2H8PY27SEZ03MWRKS5XABZYQN17ETGQS3527SA5.newyorkcitycoin-core-v1
The limit parameter can fetch a maximum of 50 transactions at a time, and the offset parameter allows you to set the index for the first transaction to fetch.
Using the total value from the initial result, a simple do...while loop can help iterate over and collect all transactions for an address.

Node.js

Requires node-fetchin package.json
1
import fetch from "node-fetch";
2
3
// returns a Promise that resolves after 'ms' milliseconds
4
const timer = (ms) => new Promise((res) => setTimeout(res, ms));
5
6
// fetches all account transactions for a given address or contract identifier
7
async function getAccountTxs(address) {
8
let counter = 0;
9
let total = 0;
10
let limit = 50;
11
let url = "";
12
let txResults = [];
13
14
// bonus points if you use your own node!
15
let stxApi = "https://stacks-node-api.mainnet.stacks.co";
16
17
console.log(`checking address: ${address}`);
18
19
// obtain all account transactions 50 at a time
20
do {
21
url = `${stxApi}/extended/v1/address/${address}/transactions?limit=${limit}&offset=${counter}`;
22
const response = await fetch(url);
23
if (response.status === 200) {
24
// success
25
const responseJson = await response.json();
26
// get total number of tx
27
if (total === 0) {
28
total = responseJson.total;
29
console.log(`Total Txs: ${total}`);
30
}
31
// add all transactions to main array
32
responseJson.results.map((tx) => {
33
txResults.push(tx);
34
counter++;
35
});
36
// output counter
37
console.log(`Processed ${counter} of ${total}`);
38
} else {
39
// error
40
throw new Error(
41
`getAccountTxs response err: ${response.status} ${response.statusText}`
42
);
43
}
44
// pause for 1sec, avoid rate limiting
45
await timer(1000);
46
} while (counter < total);
47
48
// view the output
49
//console.log(JSON.stringify(txResults));
50
51
return txResults;
52
}
53
54
getAccountTxs("SP3CK642B6119EVC6CT550PW5EZZ1AJW661ZMQTYD");
55
Copied!

Filtering Transactions

With the full set of transactions for an address, the results can then easily be filtered into a new array with useful data.

MIA Mining

Filters for all MIA mining transactions for the specified address.
1
// get all MIA mining tx for address
2
let address = 'SP3CK642B6119EVC6CT550PW5EZZ1AJW661ZMQTYD';
3
let miningTxs = [];
4
let txs = getAccountTxs(address);
5
txs.map(tx => {
6
if (tx.tx_type === 'contract_call') {
7
if (tx.contract_call.contract_id === `SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-core-v1` && tx.tx_status === 'success') {
8
if (tx.contract_call.function_name === 'mine-tokens' || tx.contract_call.function_name === 'mine-many') {
9
miningTxs.push(tx);
10
}
11
}
12
}
13
});
14
15
console.log(`total MIA mining txs: ${miningTxs.length}`);
Copied!

NYC Mining Claims

Filters for all NYC mining claim transactions for the specified address.
1
// get all NYC mining claim tx for address
2
let miningClaimTxs = [];
3
let address = 'SP3CK642B6119EVC6CT550PW5EZZ1AJW661ZMQTYD';
4
let txs = getAccountTxs(address);
5
txs.map(tx => {
6
if (tx.tx_type === 'contract_call') {
7
if (tx.contract_call.contract_id === `SP2H8PY27SEZ03MWRKS5XABZYQN17ETGQS3527SA5.newyorkcitycoin-core-v1` && tx.tx_status === 'success') {
8
if (tx.contract_call.function_name === 'claim-mining-reward') {
9
miningClaimTxs.push(tx);
10
}
11
}
12
}
13
});
14
console.log(`total NYC mining claim txs: ${miningClaimTxs.length}`);
Copied!

MIA Stacking

1
// get all MIA stacking tx for address
2
let stackingTxs = [];
3
let address = 'SP3CK642B6119EVC6CT550PW5EZZ1AJW661ZMQTYD';
4
let txs = getAccountTxs(address);
5
txs.map(tx => {
6
if (tx.tx_type === 'contract_call') {
7
if (tx.contract_call.contract_id === `SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-core-v1` && tx.tx_status === 'success') {
8
if (tx.contract_call.function_name === 'stack-tokens') {
9
stackingTxs.push(tx);
10
}
11
}
12
}
13
});
14
console.log(`total MIA stacking txs: ${stackingTxs.length}`);
Copied!

NYC Stacking Claims

1
// get all NYC stacking claim tx for address
2
let stackingClaimTxs = [];
3
let address = 'SP3CK642B6119EVC6CT550PW5EZZ1AJW661ZMQTYD';
4
let txs = getAccountTxs(address);
5
txs.map(tx => {
6
if (tx.tx_type === 'contract_call') {
7
if (tx.contract_call.contract_id === `SP2H8PY27SEZ03MWRKS5XABZYQN17ETGQS3527SA5.newyorkcitycoin-core-v1` && tx.tx_status === 'success') {
8
if (tx.contract_call.function_name === 'claim-stacking-reward') {
9
stackingClaimTxs.push(tx);
10
}
11
}
12
}
13
});
14
console.log(`total NYC stacking claim txs: ${stackingClaimTxs.length}`);
Copied!