From 3fb9ce2839271a0dcfe97f86394195f7a6f70f50 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Fri, 4 Aug 2023 07:51:22 -0700 Subject: [PATCH] feat(common): add viem utils (#1245) --- .changeset/fifty-squids-eat.md | 23 +++++++++++++++++++++ packages/common/package.json | 2 ++ packages/common/src/chains/index.ts | 1 + packages/common/src/chains/mudFoundry.ts | 18 ++++++++++++++++ packages/common/src/createBurnerAccount.ts | 22 ++++++++++++++++++++ packages/common/src/debug.ts | 3 +++ packages/common/src/index.ts | 2 ++ packages/common/src/mudTransportObserver.ts | 23 +++++++++++++++++++++ packages/recs/tsconfig.json | 2 +- pnpm-lock.yaml | 6 ++++++ 10 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 .changeset/fifty-squids-eat.md create mode 100644 packages/common/src/chains/mudFoundry.ts create mode 100644 packages/common/src/createBurnerAccount.ts create mode 100644 packages/common/src/debug.ts create mode 100644 packages/common/src/mudTransportObserver.ts diff --git a/.changeset/fifty-squids-eat.md b/.changeset/fifty-squids-eat.md new file mode 100644 index 0000000000..e16c747cd6 --- /dev/null +++ b/.changeset/fifty-squids-eat.md @@ -0,0 +1,23 @@ +--- +"@latticexyz/common": minor +--- + +Add utils for using viem with MUD + +- `mudFoundry` chain with a transaction request formatter that temporarily removes max fees to work better with anvil `--base-fee 0` +- `createBurnerAccount` that also temporarily removes max fees during transaction signing to work better with anvil `--base-fee 0` +- `mudTransportObserver` that will soon let MUD Dev Tools observe transactions + +You can use them like: + +```ts +import { createBurnerAccount, mudTransportObserver } from "@latticexyz/common"; +import { mudFoundry } from "@latticexyz/common/chains"; + +createWalletClient({ + account: createBurnerAccount(privateKey), + chain: mudFoundry, + transport: mudTransportObserver(http()), + pollingInterval: 1000, +}); +``` diff --git a/packages/common/package.json b/packages/common/package.json index 913504d352..efe6f9432f 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -55,12 +55,14 @@ "@latticexyz/schema-type": "workspace:*", "@solidity-parser/parser": "^0.16.0", "chalk": "^5.2.0", + "debug": "^4.3.4", "execa": "^7.0.0", "prettier": "^2.8.4", "prettier-plugin-solidity": "^1.1.2", "viem": "1.3.1" }, "devDependencies": { + "@types/debug": "^4.1.7", "@types/node": "^18.15.11", "tsup": "^6.7.0", "vitest": "0.31.4" diff --git a/packages/common/src/chains/index.ts b/packages/common/src/chains/index.ts index 6595447e39..e968c6fad0 100644 --- a/packages/common/src/chains/index.ts +++ b/packages/common/src/chains/index.ts @@ -1,2 +1,3 @@ export type { MUDChain } from "./types"; export { latticeTestnet } from "./latticeTestnet"; +export { mudFoundry } from "./mudFoundry"; diff --git a/packages/common/src/chains/mudFoundry.ts b/packages/common/src/chains/mudFoundry.ts new file mode 100644 index 0000000000..e885b4dd1e --- /dev/null +++ b/packages/common/src/chains/mudFoundry.ts @@ -0,0 +1,18 @@ +import { defineTransactionRequest } from "viem"; +import { foundry } from "viem/chains"; + +export const mudFoundry = { + ...foundry, + formatters: { + transactionRequest: defineTransactionRequest({ + format() { + // Temporarily override base fee for MUD's anvil config + // TODO: remove once https://github.com/wagmi-dev/viem/pull/963 is fixed + return { + maxFeePerGas: 0n, + maxPriorityFeePerGas: 0n, + }; + }, + }), + }, +}; diff --git a/packages/common/src/createBurnerAccount.ts b/packages/common/src/createBurnerAccount.ts new file mode 100644 index 0000000000..08bf3c763b --- /dev/null +++ b/packages/common/src/createBurnerAccount.ts @@ -0,0 +1,22 @@ +import { Account, Hex } from "viem"; +import { privateKeyToAccount } from "viem/accounts"; + +export function createBurnerAccount(privateKey: Hex): Account { + const account = privateKeyToAccount(privateKey); + + // Temporarily override base fee for MUD's anvil config + // TODO: remove once https://github.com/wagmi-dev/viem/pull/963 is fixed + const signTransaction: typeof account.signTransaction = async (transaction, ...args) => { + // TODO: refine check for mud anvil (0 base fee) + if (transaction.chainId === 31337) { + transaction.maxFeePerGas = 0n; + transaction.maxPriorityFeePerGas = 0n; + } + return account.signTransaction(transaction, ...args); + }; + + return { + ...account, + signTransaction, + }; +} diff --git a/packages/common/src/debug.ts b/packages/common/src/debug.ts new file mode 100644 index 0000000000..265b1c4086 --- /dev/null +++ b/packages/common/src/debug.ts @@ -0,0 +1,3 @@ +import createDebug from "debug"; + +export const debug = createDebug("mud:common"); diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index 8a8f194442..ceec57f366 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -1 +1,3 @@ +export * from "./createBurnerAccount"; +export * from "./mudTransportObserver"; export * from "./TableId"; diff --git a/packages/common/src/mudTransportObserver.ts b/packages/common/src/mudTransportObserver.ts new file mode 100644 index 0000000000..74fbf6b188 --- /dev/null +++ b/packages/common/src/mudTransportObserver.ts @@ -0,0 +1,23 @@ +import { Hex, Transport, keccak256 } from "viem"; +import { debug as parentDebug } from "./debug"; + +const debug = parentDebug.extend("mudTransportObserver"); + +export function mudTransportObserver(transport: TTransport): TTransport { + return ((opts) => { + const result = transport(opts); + const request: typeof result.request = async (req) => { + if (req.method === "eth_sendRawTransaction" && req.params instanceof Array) { + const txs = req.params.map((data: Hex) => keccak256(data)); + debug("saw txs", txs); + // TODO: pass these tx hashes into dev tools + } + // TODO: add support for `eth_sendTransaction` + return result.request(req); + }; + return { + ...result, + request, + }; + }) as TTransport; +} diff --git a/packages/recs/tsconfig.json b/packages/recs/tsconfig.json index 19db6a5548..e89755d995 100644 --- a/packages/recs/tsconfig.json +++ b/packages/recs/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "target": "es2019", + "target": "es2020", "module": "esnext", "moduleResolution": "node", "types": ["jest"], diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 23bfea35aa..9cb8b4bb47 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -223,6 +223,9 @@ importers: chalk: specifier: ^5.2.0 version: 5.2.0 + debug: + specifier: ^4.3.4 + version: 4.3.4(supports-color@8.1.1) execa: specifier: ^7.0.0 version: 7.0.0 @@ -236,6 +239,9 @@ importers: specifier: 1.3.1 version: 1.3.1(typescript@5.1.6)(zod@3.21.4) devDependencies: + '@types/debug': + specifier: ^4.1.7 + version: 4.1.7 '@types/node': specifier: ^18.15.11 version: 18.15.11