Skip to content

Commit

Permalink
feat: bump viem to 1.6.0 (#1308)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Aug 15, 2023
1 parent 5294a7d commit b8a6158
Show file tree
Hide file tree
Showing 40 changed files with 184 additions and 287 deletions.
6 changes: 6 additions & 0 deletions .changeset/lazy-ladybugs-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@latticexyz/common": minor
---

- adds `defaultPriorityFee` to `mudFoundry` for better support with MUD's default anvil config and removes workaround in `createContract`
- improves nonce error detection using viem's custom errors
14 changes: 14 additions & 0 deletions .changeset/red-turkeys-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"@latticexyz/block-logs-stream": patch
"@latticexyz/common": patch
"@latticexyz/dev-tools": patch
"@latticexyz/network": patch
"@latticexyz/protocol-parser": patch
"@latticexyz/schema-type": patch
"@latticexyz/std-client": patch
"@latticexyz/store-indexer": patch
"@latticexyz/store-sync": patch
"create-mud": patch
---

bump viem to 1.6.0
7 changes: 7 additions & 0 deletions .changeset/sharp-worms-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@latticexyz/block-logs-stream": major
---

- removes our own `getLogs` function now that viem's `getLogs` supports using multiple `events` per RPC call.
- removes `isNonPendingBlock` and `isNonPendingLog` helpers now that viem narrows `Block` and `Log` types based on inputs
- simplifies `groupLogsByBlockNumber` types and tests
5 changes: 5 additions & 0 deletions .changeset/spotty-cups-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/store-sync": patch
---

remove usages of `isNonPendingBlock` and `isNonPendingLog` (fixed with more specific viem types)
2 changes: 1 addition & 1 deletion e2e/packages/client-vanilla/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"react": "^18.2.0",
"rxjs": "7.5.5",
"threads": "^1.7.0",
"viem": "1.3.1"
"viem": "1.6.0"
},
"devDependencies": {
"rimraf": "^3.0.2",
Expand Down
2 changes: 1 addition & 1 deletion e2e/packages/sync-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"execa": "^7.1.1",
"jsdom": "^22.0.0",
"typescript": "5.1.6",
"viem": "1.3.1",
"viem": "1.6.0",
"vite": "^4.2.1",
"vitest": "^0.31.0"
}
Expand Down
29 changes: 22 additions & 7 deletions e2e/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/minimal/packages/client-phaser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"styled-components": "^5.3.10",
"threads": "^1.7.0",
"use-resize-observer": "^9.1.0",
"viem": "1.3.1",
"viem": "1.6.0",
"vite": "^4.2.1",
"zustand": "^4.3.8"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal/packages/client-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"react-dom": "^18.2.0",
"rxjs": "7.5.5",
"threads": "^1.7.0",
"viem": "1.3.1"
"viem": "1.6.0"
},
"devDependencies": {
"@types/react": "^18.2.6",
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal/packages/client-vanilla/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"react": "^18.2.0",
"rxjs": "7.5.5",
"threads": "^1.7.0",
"viem": "1.3.1"
"viem": "1.6.0"
},
"devDependencies": {
"vite": "^4.2.1",
Expand Down
36 changes: 27 additions & 9 deletions examples/minimal/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 2 additions & 11 deletions packages/block-logs-stream/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,14 @@ A set of utilities for efficiently retrieving blockchain event logs. Built on to
```ts
import { filter, map, mergeMap } from "rxjs";
import { createPublicClient, parseAbi } from "viem";
import {
createBlockStream,
isNonPendingBlock,
groupLogsByBlockNumber,
blockRangeToLogs,
} from "@latticexyz/block-logs-stream";
import { createBlockStream, groupLogsByBlockNumber, blockRangeToLogs } from "@latticexyz/block-logs-stream";

const publicClient = createPublicClient({
// your viem public client config here
});

const latestBlock$ = await createBlockStream({ publicClient, blockTag: "latest" });

const latestBlockNumber$ = latestBlock$.pipe(
filter(isNonPendingBlock),
map((block) => block.number)
);
const latestBlockNumber$ = latestBlock$.pipe(map((block) => block.number));

latestBlockNumber$
.pipe(
Expand Down
2 changes: 1 addition & 1 deletion packages/block-logs-stream/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"abitype": "0.9.3",
"debug": "^4.3.4",
"rxjs": "7.5.5",
"viem": "1.3.1"
"viem": "1.6.0"
},
"devDependencies": {
"@types/debug": "^4.1.7",
Expand Down
4 changes: 2 additions & 2 deletions packages/block-logs-stream/src/blockRangeToLogs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EMPTY, OperatorFunction, concatMap, from, pipe, tap } from "rxjs";
import { FetchLogsResult, fetchLogs } from "./fetchLogs";
import { AbiEvent, Address } from "abitype";
import { BlockNumber, PublicClient } from "viem";
import { AbiEvent } from "abitype";
import { Address, BlockNumber, PublicClient } from "viem";

export type BlockRangeToLogsOptions<TAbiEvents extends readonly AbiEvent[]> = {
/**
Expand Down
11 changes: 7 additions & 4 deletions packages/block-logs-stream/src/createBlockStream.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { Observable } from "rxjs";
import type { Block, BlockTag, PublicClient } from "viem";

export type CreateBlockStreamOptions = {
export type CreateBlockStreamOptions<TBlockTag extends BlockTag> = {
publicClient: PublicClient;
blockTag: BlockTag;
blockTag: TBlockTag;
};

export type CreateBlockStreamResult = Observable<Block>;
export type CreateBlockStreamResult<TBlockTag extends BlockTag> = Observable<Block<bigint, false, TBlockTag>>;

export function createBlockStream({ publicClient, blockTag }: CreateBlockStreamOptions): CreateBlockStreamResult {
export function createBlockStream<TBlockTag extends BlockTag>({
publicClient,
blockTag,
}: CreateBlockStreamOptions<TBlockTag>): CreateBlockStreamResult<TBlockTag> {
return new Observable(function subscribe(subscriber) {
return publicClient.watchBlocks({
blockTag,
Expand Down
12 changes: 7 additions & 5 deletions packages/block-logs-stream/src/fetchLogs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { AbiEvent, Address } from "abitype";
import { PublicClient, BlockNumber } from "viem";
import { AbiEvent } from "abitype";
import { Address, PublicClient, BlockNumber, GetLogsReturnType } from "viem";
import { bigIntMin, wait } from "@latticexyz/common/utils";
import { GetLogsResult, getLogs } from "./getLogs";
import { debug } from "./debug";

export type FetchLogsOptions<TAbiEvents extends readonly AbiEvent[]> = {
Expand Down Expand Up @@ -40,7 +39,7 @@ export type FetchLogsOptions<TAbiEvents extends readonly AbiEvent[]> = {
export type FetchLogsResult<TAbiEvents extends readonly AbiEvent[]> = {
fromBlock: BlockNumber;
toBlock: BlockNumber;
logs: GetLogsResult<TAbiEvents>;
logs: GetLogsReturnType<undefined, TAbiEvents, true, BlockNumber, BlockNumber>;
};

/**
Expand All @@ -61,6 +60,7 @@ export type FetchLogsResult<TAbiEvents extends readonly AbiEvent[]> = {
export async function* fetchLogs<TAbiEvents extends readonly AbiEvent[]>({
maxBlockRange = 1000n,
maxRetryCount = 3,
publicClient,
...getLogsOpts
}: FetchLogsOptions<TAbiEvents>): AsyncGenerator<FetchLogsResult<TAbiEvents>> {
let fromBlock = getLogsOpts.fromBlock;
Expand All @@ -70,13 +70,14 @@ export async function* fetchLogs<TAbiEvents extends readonly AbiEvent[]>({
while (fromBlock <= getLogsOpts.toBlock) {
try {
const toBlock = fromBlock + blockRange;
const logs = await getLogs({ ...getLogsOpts, fromBlock, toBlock });
const logs = await publicClient.getLogs({ ...getLogsOpts, fromBlock, toBlock, strict: true });
yield { fromBlock, toBlock, logs };
fromBlock = toBlock + 1n;
blockRange = bigIntMin(maxBlockRange, getLogsOpts.toBlock - fromBlock);
} catch (error: unknown) {
if (!(error instanceof Error)) throw error;

// TODO: figure out actual rate limit message for RPCs
if (error.message.includes("rate limit exceeded") && retryCount < maxRetryCount) {
const seconds = 2 * retryCount;
debug(`too many requests, retrying in ${seconds}s`, error);
Expand All @@ -85,6 +86,7 @@ export async function* fetchLogs<TAbiEvents extends readonly AbiEvent[]>({
continue;
}

// TODO: figure out actual block range exceeded message for RPCs
if (error.message.includes("block range exceeded")) {
blockRange /= 2n;
if (blockRange <= 0n) {
Expand Down
Loading

0 comments on commit b8a6158

Please sign in to comment.