Install: https://github.com/makerdao/multicall.js
yarn add @makerdao/multicall
Install: https://github.com/makerdao/multicall.js
yarn add @makerdao/multicall
// node scripts_local/multicall/multicall.js | |
require("dotenv").config(); | |
const { aggregate, createWatcher } = require("@makerdao/multicall"); | |
// https://github.com/makerdao/multicall | |
let config = { | |
rpcUrl: "https://bsc-dataseed1.binance.org:443", | |
multicallAddress: "0x41263cba59eb80dc200f3e2544eda4ed6a90e76c", | |
interval: 1000 | |
}; | |
// Custom config. Default is BSC mainnet | |
const initConfig = (rpcUrl, multicallAddress, interval = config.interval) => { | |
config = { rpcUrl, multicallAddress, interval }; | |
}; | |
// Init watcher with your calls | |
// return watcher | |
const initWatcher = (cb) => { | |
const watcher = createWatcher( | |
[ | |
{ | |
call: [ | |
"getEthBalance(address)(uint256)", | |
"0x000000000000000000000000000000000000dead" | |
], | |
returns: [["BNB_BALANCE", val => val / 10 ** 18]] | |
}, | |
{ | |
target: "0x55d398326f99059ff775485246999027b3197955", | |
call: ["balanceOf(address)(uint256)", "0x000000000000000000000000000000000000dead"], | |
returns: [["BALANCE_OF_USDT_DEAD", val => val / 10 ** 18]] | |
}, | |
// https://docs.chain.link/docs/binance-smart-chain-addresses/ | |
{ | |
target: "0x0567F2323251f0Aab15c8dFb1967E4e8A7D42aeE", | |
call: ["latestAnswer()(uint256)"], | |
returns: [ | |
["PRICE_FEED_BNB_USD", val => val / 10 ** 8] | |
] | |
} | |
], | |
config | |
); | |
watcher.subscribe(update => { | |
// console.log(`Update: ${update.type} = ${update.value}`); | |
if (cb) cb(update.type, update.value); | |
}); | |
return watcher; | |
}; | |
// Start polling | |
const start = async (watcher, initialFetchedCb) => { | |
watcher.start(); | |
await watcher.awaitInitialFetch(); | |
if (initialFetchedCb) initialFetchedCb(); | |
}; | |
// module.exports = { | |
// initConfig, | |
// initWatcher, | |
// start | |
// }; | |
// Example | |
const example = async () => { | |
const watcher = initWatcher((type, value) => { | |
console.log(`Update: ${type} = ${value}`); | |
}); | |
await start(watcher, () => { | |
console.log("Initial fetch completed"); | |
}); | |
// Get initial data manually | |
watcher.batch().subscribe(updates => { | |
console.log("Initial data:", updates); | |
}).unsub(); | |
}; | |
example().then(); | |