-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(common): add viem utils (#1245)
- Loading branch information
Showing
10 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export type { MUDChain } from "./types"; | ||
export { latticeTestnet } from "./latticeTestnet"; | ||
export { mudFoundry } from "./mudFoundry"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; | ||
}, | ||
}), | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import createDebug from "debug"; | ||
|
||
export const debug = createDebug("mud:common"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from "./createBurnerAccount"; | ||
export * from "./mudTransportObserver"; | ||
export * from "./TableId"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.