Skip to content

Instantly share code, notes, and snippets.

@mertimus
Created August 17, 2022 21:14
Show Gist options
  • Save mertimus/bf88d27e05f44711e47cd1c7ddb71793 to your computer and use it in GitHub Desktop.
Save mertimus/bf88d27e05f44711e47cd1c7ddb71793 to your computer and use it in GitHub Desktop.
const axios = require('axios');
const apiURL = 'https://api.helius.xyz/v0/addresses/';
const resource = '/transactions';
const options = '?api-key=d63a869e-bb73-4792-ae33-a06f4c72f226&before=';
let lastTxn = '';
const SOL_PER_LAMPORT = 0.000000001;
const getAllTxns = async (address) => {
const allTxns = [];
while(true) {
console.log("fetching transactions...")
const { data } = await axios.get(apiURL + address + resource + options + lastTxn)
if(!data.length) {
console.log("got all transactions")
break
}
lastTxn = data[data.length - 1].signature
allTxns.push(...data);
}
return allTxns
}
const getBalances = async (address) => {
try {
const { data } = await axios.get(apiURL + address + "/balances?api-key=d63a869e-bb73-4792-ae33-a06f4c72f226")
return data
} catch(e) {
console.error("error: ", e)
}
}
const reconcile = async (address) => {
const allTxns = await getAllTxns(address);
const balances =await getBalances(address)
let balanceSum = 0;
for(let i = 0; i < allTxns.length; i++) {
const txn = allTxns[i];
const acct = txn.accountData.find(x => x.account == address)
balanceSum += acct.nativeBalanceChange
}
console.log(`balance difference is ${Math.abs(balanceSum - balances.nativeBalance)*SOL_PER_LAMPORT} SOL`)
}
const address = '3imfKr7yLb1vLwfJQHxyZfc3XvhsjvaWojGzd6jrnWHv';
reconcile(address);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment