Skip to content

Interacting from the server (node.js process)

Course: Client side ETH - chapter 7

We’ll be using the ethers library - https://docs.ethers.org/v6/

  • Initialise an empty ts project
npm init -y
npm install typescript
npx tsc --init
  • Change rootDir and outDir
"rootDir": "./src",
"outDir": "./dist"
  • Create abi.ts
$1b
  • Create a provider instance
import { JsonRpcProvider, id } from "ethers";
const provider = new JsonRpcProvider("https://eth-mainnet.g.alchemy.com/v2/nnY0qPUQLYsUvb5BKJM5bh81sI6O0PQG");
  • Create a function called pollBlock that polls a specific block and gets all the USDT transfers that have happened
import { JsonRpcProvider, id } from "ethers";
const provider = new JsonRpcProvider("https://eth-mainnet.g.alchemy.com/v2/nnY0qPUQLYsUvb5BKJM5bh81sI6O0PQG");
async function pollBlock(blockNumber: number) {
const logs = await provider.getLogs({
address: "0xdac17f958d2ee523a2206206994597c13d831ec7",
fromBlock: blockNumber,
toBlock: blockNumber + 2,
topics: [id("Transfer(address,address,uint256)")]
});
console.log(logs);
}
pollBlock(21493826)

Can you write a process that polls through the complete blockchain to see what USDT balance 0x587EFaEe4f308aB2795ca35A27Dff8c1dfAF9e3f has ?