Skip to content

Commit

Permalink
feat(common): add viem utils (#1245)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Aug 4, 2023
1 parent 081f57b commit 3fb9ce2
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 1 deletion.
23 changes: 23 additions & 0 deletions .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,
});
```
2 changes: 2 additions & 0 deletions packages/common/package.json
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/chains/index.ts
@@ -1,2 +1,3 @@
export type { MUDChain } from "./types";
export { latticeTestnet } from "./latticeTestnet";
export { mudFoundry } from "./mudFoundry";
18 changes: 18 additions & 0 deletions 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,
};
},
}),
},
};
22 changes: 22 additions & 0 deletions 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,
};
}
3 changes: 3 additions & 0 deletions packages/common/src/debug.ts
@@ -0,0 +1,3 @@
import createDebug from "debug";

export const debug = createDebug("mud:common");
2 changes: 2 additions & 0 deletions packages/common/src/index.ts
@@ -1 +1,3 @@
export * from "./createBurnerAccount";
export * from "./mudTransportObserver";
export * from "./TableId";
23 changes: 23 additions & 0 deletions 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<TTransport extends Transport>(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;
}
2 changes: 1 addition & 1 deletion packages/recs/tsconfig.json
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es2019",
"target": "es2020",
"module": "esnext",
"moduleResolution": "node",
"types": ["jest"],
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 3fb9ce2

Please sign in to comment.