UNPKG

@phiresky/eth-scan

Version:

An efficient Ether and token balance scanner

34 lines 2.41 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.retryCalls=exports.callSingle=exports.toNestedBalanceMap=exports.toBalanceMap=void 0;var _abi=require("@findeth/abi"),_constants=require("./constants"),_providers=require("./providers"),_utils=require("./utils");const isResult=a=>Array.isArray(a)&&2===a.length,toBalanceMap=(a,b)=>{const c={};for(const[d,e]of b.entries()){const b=isResult(e)?(0,_abi.toNumber)(e[1].slice(0,32)):e;c[a[d]]=b}return c};/** * Get a balance map from an array of addresses and an array of balances. * * @param {string[]} addresses * @param {bigint[]} results * @return {BalanceMap} */exports.toBalanceMap=toBalanceMap;/** * Get a nested balance map from an array of addresses, token addresses, and results. * * @param {string[]} addresses * @param {bigint[]} tokenAddresses * @param {BalanceMap<BalanceMap>} results */const toNestedBalanceMap=(a,b,c)=>{const d={};for(const[e,f]of c.entries())d[a[e]]=toBalanceMap(b,f);return d};/** * Low level API function to send a contract call that returns a single Result array. It will automatically retry any * failed calls. * * @param provider * @param batchAddresses The addresses to batch by * @param addresses The address(es) to use when retrying failed calls * @param contractAddresses The contract address(es) to use when retrying failed calls * @param encodeData * @param encodeSingle * @param options */exports.toNestedBalanceMap=toNestedBalanceMap;const callSingle=async(a,b,c,d,e,f,g)=>{var h,i;const j=null!==(h=null===g||void 0===g?void 0:g.contractAddress)&&void 0!==h?h:_constants.CONTRACT_ADDRESS,k=null!==(i=null===g||void 0===g?void 0:g.batchSize)&&void 0!==i?i:_constants.BATCH_SIZE,l=await(0,_utils.batch)(async b=>{const c=e(b),d=await(0,_providers.call)(a,j,c);return(0,_abi.decode)(["(bool,bytes)[]"],d)[0]},k,b);return retryCalls(a,c,d,l,f)};/** * Retry calls to the contract directly, if a contract call in the eth-scan contract failed. * * @param provider * @param addresses * @param contracts * @param results * @param encodeData */exports.callSingle=callSingle;const retryCalls=async(a,b,c,d,e)=>Promise.all(d.map(async(d,f)=>{if(d[0])return d;const g="string"==typeof b?b:b[f],h="string"==typeof c?c:c[f],i=e(g);try{const b=await(0,_providers.call)(a,h,i);return[!0,b]}catch{// noop }return d}));exports.retryCalls=retryCalls; //# sourceMappingURL=api.js.map