Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
boudra committed Oct 16, 2023
1 parent e424d83 commit 5d30967
Showing 1 changed file with 46 additions and 20 deletions.
66 changes: 46 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,78 @@

Chainsauce is a general-purpose Ethereum indexer that sources contract events to build easily queryable data.

## How to use?

Install the package:
## Installation

```bash
$ npm install boudra/chainsauce#main
```

Example:
## Basic usage

```ts
import { createIndexer} from "chainsauce";
import { erc20ABI } from "./erc20ABI";
Create an indexer:

```ts
const MyContracts = {
ERC20: erc20ABI,
};

const indexer = createIndexer({
chain: {
name: "mainnet",
id: 1,
rpc: {
rpcClient: createHttpRpcClient({
url: "https://mainnet.infura.io/v3/...",
},
}),
},
contracts: MyContracts,
});
```

indexer.on("ERC20:Transfer", async ({ event }) => {
console.log("Transfer event:", event.params);
});
Subscribe to deployed contracts:

// Subscribe to deployed contracts
```ts
indexer.subscribeToContract({
contract: "ERC20",
address: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",

// optional
fromBlock: 18363594n,
toBlock: "latest"
});
```

// this will index to latest and return
await indexer.indexToBlock("latest");
// or index to a specific block number
// await indexer.indexToBlock(16000000n);
// or, this will index to latest and watch for new blocks
// await indexer.watch();
Subscribe to events:

```ts
// subscribe to a specific event
indexer.on("ERC20:Approval", async ({ event }) => {
console.log("Approval event:", event.params);
});

// subscribe to all events
indexer.on("events", async ({ event }) => {
console.log("Approval event:", event.params);
});
```

Type an event handler:

```ts
import { Indexer as ChainsauceIndexer } from "chainsauce";

type MyContext = {
db: DatabaseConnection
};

type Indexer = ChainsauceIndexer<typeof MyContracts, MyContext>;

async function handleTransfer({
event, context: { db }
}: EventHandlerArgs<Indexer, "ERC20", "Transfer">) {
// db is a DatabaseConnection
console.log("Transfer event:", event.params);
}

indexer.on("ERC20:Transfer", handleTransfer);
```

## Complete examples
Expand Down

0 comments on commit 5d30967

Please sign in to comment.