import { Response } from "express" ; import * as winston from "winston" ; import { Config } from "./Config" ; const axios = require ( "axios" ) ; param res param status param content export function sendJSONresponse ( res , status , content ) { res . status ( status ) ; res . json ( content ) ; } param { string } numberString returns { string } export function removeScientificNotationFromNumbString ( numberString ) { const numb = + numberString ; const data = String ( numb ) . split ( /[eE]/ ) ; if ( data . length == 1 ) return data [ 0 ] ; let z = "" ; const sign = numb < 0 ? "-" : "" ; const str = data [ 0 ] . replace ( "." , "" ) ; let mag = Number ( data [ 1 ] ) + 1 ; if ( mag < 0 ) { z = sign + "0." ; while ( mag ++ ) z += "0" ; return z + str . replace ( /^\-/ , "" ) ; } mag -= str . length ; while ( mag -- ) z += "0" ; return str + z ; } returns { any } export function loadContractABIs ( ) { const dstList = [ ] ; const path = require ( "path" ) ; const normalizedPath = path . join ( __dirname , "contracts" ) ; require ( "fs" ) . readdirSync ( normalizedPath ) . forEach ( function ( file ) { if ( path . extname ( file ) === ".ts" ) { const abi = require ( path . join ( normalizedPath , file ) ) ; dstList . push ( abi ) ; } } ) ; return dstList ; } param contract export function fetchAbiFromEtherscan ( contract ) { const URL = `https://api.etherscan.io/api?module=contract&action=getabi&address=${ contract }&apikey=YourApiKeyToken` ; return axios . get ( URL ) . then ( ( response ) => { if ( response . data . message === "OK" ) { return response . data . result ; } } ) . catch ( ( err ) => { winston . error ( `Error while fetching contract ${ contract } from etherscan with error: ${ err }` ) ; } ) ; } param { string } address param { string } contractAddress param { number } decimals export function getTokenBalanceForAddress ( address , contractAddress , decimals ) { const abi = require ( "./contracts/Erc20Abi" ) ; const contractInstance = new Config . web3 . eth . Contract ( abi , contractAddress ) ; return contractInstance . methods . balanceOf ( address ) . call ( ) . then ( ( balance ) => { return { contractAddress : contractAddress , balance : balance / 10 ** decimals } ; } ) ; } param { number } t returns { Promise < any > } export function setDelay ( t ) { return new Promise ( ( resolve ) => { setTimeout ( resolve , t ) ; } ) ; } interface Array < T > { flatMap < E > ( callback ) : Array < E > ; } Object . defineProperty ( Array . prototype , "flatMap" , { value : function ( f ) { return this . reduce ( ( ys , x ) => { return ys . concat ( f . call ( this , x ) ) ; } , [ ] ) ; } , enumerable : false , } ) ;