-
Notifications
You must be signed in to change notification settings - Fork 194
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 actions that work the same as the current wrap…
…pers (#2347) Co-authored-by: Kevin Ingersoll <[email protected]>
- Loading branch information
Showing
14 changed files
with
193 additions
and
50 deletions.
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,25 @@ | ||
--- | ||
"@latticexyz/common": minor | ||
"create-mud": minor | ||
--- | ||
|
||
Added viem custom client actions that work the same as MUD's now-deprecated `getContract`, `writeContract`, and `sendTransaction` wrappers. Templates have been updated to reflect the new patterns. | ||
|
||
You can migrate your own code like this: | ||
|
||
```diff | ||
-import { createWalletClient } from "viem"; | ||
-import { getContract, writeContract, sendTransaction } from "@latticexyz/common"; | ||
+import { createWalletClient, getContract } from "viem"; | ||
+import { transactionQueue, writeObserver } from "@latticexyz/common/actions"; | ||
|
||
-const walletClient = createWalletClient(...); | ||
+const walletClient = createWalletClient(...) | ||
+ .extend(transactionQueue()) | ||
+ .extend(writeObserver({ onWrite }); | ||
|
||
const worldContract = getContract({ | ||
client: { publicClient, walletClient }, | ||
- onWrite, | ||
}); | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./transactionQueue"; | ||
export * from "./writeObserver"; |
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,14 @@ | ||
import type { Transport, Chain, Account, WalletActions, WalletClient } from "viem"; | ||
import { writeContract as mud_writeContract } from "../writeContract"; | ||
import { sendTransaction as mud_sendTransaction } from "../sendTransaction"; | ||
|
||
export function transactionQueue<TChain extends Chain, TAccount extends Account>(): ( | ||
client: WalletClient<Transport, TChain, TAccount>, | ||
) => Pick<WalletActions<TChain, TAccount>, "writeContract" | "sendTransaction"> { | ||
return (client) => ({ | ||
// Applies to: `client.writeContract`, `getContract(client, ...).write` | ||
writeContract: (args) => mud_writeContract(client, args), | ||
// Applies to: `client.sendTransaction` | ||
sendTransaction: (args) => mud_sendTransaction(client, args), | ||
}); | ||
} |
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,34 @@ | ||
import type { | ||
WriteContractParameters, | ||
Transport, | ||
Chain, | ||
Account, | ||
WalletActions, | ||
WalletClient, | ||
WriteContractReturnType, | ||
} from "viem"; | ||
import { getAction } from "viem/utils"; | ||
import { writeContract } from "viem/actions"; | ||
import { type ContractWrite } from "../getContract"; | ||
|
||
type WriteObserverParameters = { onWrite: (write: ContractWrite) => void }; | ||
|
||
export function writeObserver<TChain extends Chain, TAccount extends Account>({ | ||
onWrite, | ||
}: WriteObserverParameters): ( | ||
client: WalletClient<Transport, TChain, TAccount>, | ||
) => Pick<WalletActions<TChain, TAccount>, "writeContract"> { | ||
let nextWriteId = 0; | ||
|
||
return (client) => ({ | ||
// Applies to: `client.writeContract`, `getContract(client, ...).write` | ||
writeContract: (args): Promise<WriteContractReturnType> => { | ||
const result = getAction(client, writeContract, "writeContract")(args); | ||
|
||
const id = `${client.chain.id}:${client.account.address}:${nextWriteId++}`; | ||
onWrite({ id, request: args as WriteContractParameters, result }); | ||
|
||
return result; | ||
}, | ||
}); | ||
} |
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
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
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
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
Oops, something went wrong.