Instadapp Utils
Install package:
# npm
npm install @instadapp/utils
# yarn
yarn add @instadapp/utils
# pnpm
pnpm install @instadapp/utilsImport:
// ESMimport{}from"@instadapp/utils";// CommonJSconst{}=require("@instadapp/utils");basic setup :
import{AbiFetcher}from"@instadapp/utils";constabiFetcher=newAbiFetcher();awaitabiFetcher.get("0x0000000000000000000000000000000000001010","polygon");with caching:
import{AbiFetcher}from"@instadapp/utils";importNodeCachefrom"node-cache";constabiCache=newNodeCache();constabiFetcher=newAbiFetcher({cache: {get(key){returnabiCache.get(key);},set(key,value){returnabiCache.set(key,value,10);},},});awaitabiFetcher.get("0x0000000000000000000000000000000000001010","polygon");with etherscan api keys (recommended)
import{AbiFetcher}from"@instadapp/utils";constabiFetcher=newAbiFetcher({etherscanApiKey: {mainnet: "9D13ZE7XSBTJ94N9BNJ2MA33VMAY2YPIRB",},});awaitabiFetcher.get("0xB8c77482e45F1F44dE1745F52C74426C631bDD52","mainnet");basic setup :
import{CastDecoder}from"@instadapp/utils";constcastDecoder=newCastDecoder();// using cast dataawaitcastDecoder.getSpells("0x9304c934000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000009414156452d56322d410000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000084ce88b439000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee00000000000000000000000000000000000000000000000000071afd498d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","mainnet",);// using encodedSpellsawaitcastDecoder.getSpells({targets: ["AAVE-V2-A"],spells: ["0xce88b439000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee00000000000000000000000000000000000000000000000000071afd498d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",],},"mainnet",);// decode one encoded spellawaitcastDecoder.getSpell("AAVE-V2-A","0xce88b439000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee00000000000000000000000000000000000000000000000000071afd498d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","mainnet",);import{retry}from"@instadapp/utils";retry(()=>asyncCall(),{timeouts: [5_000,10_000,15_000],// timeouts for each retry attempt in msdelay: 300,// delay between retries in ms});import{wait}from"@instadapp/utils";awaitwait(300);import{promiseTimeout}from"@instadapp/utils";awaitpromiseTimeout(promiseFn,10_000);import{JsonRpcRetryProvider}from"@instadapp/utils";constprovider=newJsonRpcRetryProvider("https://rpc.ankr.io/eth");constproviderWithCustomOptions=newJsonRpcRetryProvider("https://rpc.ankr.io/eth",{timeouts: [5_000,10_000,15_000],// timeouts for each retry attempt in msdelay: 300,// delay between retries in ms},);import{JsonRpcRetryProvider}from"@instadapp/utils";constprovider=newJsonRpcRetryProvider(["https://rpc.ankr.com/invalid","https://rpc.ankr.com/invalid-2","https://1rpc.io/eth","https://eth.llamarpc.com",]);import{JsonRpcRetryBatchProvider}from"@instadapp/utils";constprovider=newJsonRpcRetryBatchProvider("https://rpc.ankr.io/eth");constproviderWithCustomOptions=newJsonRpcRetryBatchProvider("https://rpc.ankr.io/eth",{timeouts: [5_000,10_000,15_000],// timeouts for each retry attempt in msdelay: 300,// delay between retries in ms},);import{StaticJsonRpcRetryProvider}from"@instadapp/utils";constprovider=newStaticJsonRpcRetryProvider("https://rpc.ankr.io/eth");constproviderWithCustomOptions=newStaticJsonRpcRetryProvider("https://rpc.ankr.io/eth",{timeouts: [5_000,10_000,15_000],// timeouts for each retry attempt in msdelay: 300,// delay between retries in ms},);import{Cache,ICacheDriver}from"@instadapp/utils";Cache.extend("mongodb",{asyncget(key: string){},asyncset(key: string,value: any,seconds?: number){},asyncforget(key: string){},asyncflush(){},// optional, and experimentallock(key: string,seconds: number){return{asyncacquire(){returntrue;},asyncrelease(){},};},});// Note: you should set this once per life cycleCache.setDefault("mongodb");// default is `memory` https://github.com/Instadapp/utils/tree/master/src/cache/drivers/index.tsawaitCache.get("key1");awaitCache.get("key1","default");awaitCache.get("key1",async()=>"default");awaitCache.put("key2");awaitCache.put("key2","default");awaitCache.put("key2",async()=>"default");constseconds=42;awaitCache.put("key2","default");awaitCache.put("key2","default",seconds);awaitCache.put("key2",async()=>"default",seconds);awaitCache.pull("key3");// get and forgetawaitCache.forget("key1");awaitCache.flush();constusers=awaitCache.remember("users",seconds,async()=>{returnawaitUser.find();});awaitCache.store("memory").get("users:1");// safe way to switch driver for a moment// experimentalconstisLocked=awaitCache.lock("key").get(async()=>{console.log("do something");});// orconstisAcquired=awaitCache.getLock("key",async()=>{console.log("do something");});// orconstlock=Cache.lock("key");if(awaitlock.get()){console.log("do something");awaitlock.release();}// orconstlock=Cache.lock("key");try{awaitlock.block(5);// Lock acquired after waiting a maximum of 5 seconds...}catch{// Unable to acquire lock...}finally{awaitlock.release();}// orawaitCache.block("key",5,async()=>{// Lock acquired after waiting a maximum of 5 seconds...});import{toJsonRpcProvider}from"@instadapp/utils";import{ethers}from"ethers";letprovider=toJsonRpcProvider(window.ethereum);// Metamask, Rabby, ...letprovider=toJsonRpcProvider(web3);// Web3.js instance, `web3.currentProvider` will be used internallyletprovider=toJsonRpcProvider(web3.currentProvider);// Web3.js provider, ex: Metamask, Rabby, WalletConnect, ...letprovider=toJsonRpcProvider("https://1rpc.io/eth");// Http RPC URLletprovider=toJsonRpcProvider(newethers.providers.JsonRpcProvider("https://1rpc.io/eth"),);// ethers JsonRpcProvider instanceimport{Blockscan,Chain}from"@instadapp/utils";constetherscan=newBlockscan(Chain.Mainnet);awaitetherscan.getTransactions("0x6975be450864c02b4613023c2152ee0743572325");awaitetherscan.getInternalTransactions("0x6975be450864c02b4613023c2152ee0743572325",);awaitetherscan.getErc20TokenTransferEvents("0x6975be450864c02b4613023c2152ee0743572325",);awaitetherscan.getErc721TokenTransferEvents("0x6975be450864c02b4613023c2152ee0743572325",);awaitetherscan.getErc1155TokenTransferEvents("0x6975be450864c02b4613023c2152ee0743572325",);constbasescan=Blockscan.custom("https://basescan.org","https://api.basescan.org/api",);awaitbasescan.contractSourceCode("0x833589fcd6edb6e08f4c7c32d4f71b54bda02913");- Clone this repository
- Install dependencies using
yarn install - Run interactive tests using
yarn dev
Made with 💛
Published under MIT License.