-
Notifications
You must be signed in to change notification settings - Fork 196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: upgrade viem to v2 #2284
feat: upgrade viem to v2 #2284
Conversation
🦋 Changeset detectedLatest commit: 39af8bb The changes in this PR will be included in the next version bump. This PR includes changesets to release 30 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
warning when executing
|
I think we could bump zod here too! At least to the minimum range expected by viem/abitype. I wouldn't worry about the warnings for React-related packages, can solve that separately. |
@holic The Correction: viem uses |
Also, I might be able to pass the typescript test without upgrading the mud's abitype, but also it's better to upgrade it alongside viem. |
agree on aligning our abitype version with the one that viem's version uses |
@@ -9,6 +9,7 @@ | |||
"isolatedModules": true, | |||
"esModuleInterop": true, | |||
"forceConsistentCasingInFileNames": true, | |||
"skipLibCheck": true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what are the consequences of this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By leaving skipLibCheck
at default false, I encounter the following error:
cd packages/block-logs-stream && pnpm tsc --noEmit
../../node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/viem/_types/utils/formatters/transaction.d.ts(51,217): error TS2536: Type 'K_1' cannot be used to index type '{ gas: bigint; value: bigint; blockHash: `0x${string}` | null; blockNumber: bigint | null; from: `0x${string}`; hash: `0x${string}`; input: `0x${string}`; nonce: number; r: `0x${string}`; s: `0x${string}`; to: `0x${string}` | null; ... 11 more ...; type: "legacy"; }'.
../../node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/viem/_types/utils/formatters/transaction.d.ts(75,82): error TS2536: Type 'K' cannot be used to index type '{ [K_1 in keyof { gas: bigint; value: bigint; blockHash: `0x${string}` | null; blockNumber: bigint | null; from: `0x${string}`; hash: `0x${string}`; input: `0x${string}`; nonce: number; r: `0x${string}`; s: `0x${string}`; to: `0x${string}` | null; ... 11 more ...; type: "legacy"; } as K_1 extends keyof TOverrideRetu...'.
../../node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/viem/_types/utils/formatters/transaction.d.ts(99,217): error TS2536: Type 'K_1' cannot be used to index type '{ gas: bigint; value: bigint; blockHash: `0x${string}` | null; blockNumber: bigint | null; from: `0x${string}`; hash: `0x${string}`; input: `0x${string}`; nonce: number; r: `0x${string}`; s: `0x${string}`; to: `0x${string}` | null; ... 11 more ...; type: "legacy"; }'.
../../node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/viem/_types/utils/formatters/transaction.d.ts(147,217): error TS2536: Type 'K_3' cannot be used to index type '{ blockHash: `0x${string}` | null; blockNumber: bigint | null; from: `0x${string}`; gas: bigint; hash: `0x${string}`; input: `0x${string}`; nonce: number; r: `0x${string}`; s: `0x${string}`; to: `0x${string}` | null; transactionIndex: number | null; ... 11 more ...; type: "eip2930"; }'.
../../node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/viem/_types/utils/formatters/transaction.d.ts(171,88): error TS2536: Type 'K_2' cannot be used to index type '{ [K_3 in keyof { blockHash: `0x${string}` | null; blockNumber: bigint | null; from: `0x${string}`; gas: bigint; hash: `0x${string}`; input: `0x${string}`; nonce: number; r: `0x${string}`; s: `0x${string}`; to: `0x${string}` | null; transactionIndex: number | null; ... 11 more ...; type: "eip2930"; } as K_3 extends ...'.
../../node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/viem/_types/utils/formatters/transaction.d.ts(195,217): error TS2536: Type 'K_3' cannot be used to index type '{ blockHash: `0x${string}` | null; blockNumber: bigint | null; from: `0x${string}`; gas: bigint; hash: `0x${string}`; input: `0x${string}`; nonce: number; r: `0x${string}`; s: `0x${string}`; to: `0x${string}` | null; transactionIndex: number | null; ... 11 more ...; type: "eip2930"; }'.
../../node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/viem/_types/utils/formatters/transaction.d.ts(243,217): error TS2536: Type 'K_5' cannot be used to index type '{ blockHash: `0x${string}` | null; blockNumber: bigint | null; from: `0x${string}`; gas: bigint; hash: `0x${string}`; input: `0x${string}`; nonce: number; r: `0x${string}`; s: `0x${string}`; to: `0x${string}` | null; transactionIndex: number | null; ... 11 more ...; type: "eip1559"; }'.
../../node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/viem/_types/utils/formatters/transaction.d.ts(267,88): error TS2536: Type 'K_4' cannot be used to index type '{ [K_5 in keyof { blockHash: `0x${string}` | null; blockNumber: bigint | null; from: `0x${string}`; gas: bigint; hash: `0x${string}`; input: `0x${string}`; nonce: number; r: `0x${string}`; s: `0x${string}`; to: `0x${string}` | null; transactionIndex: number | null; ... 11 more ...; type: "eip1559"; } as K_5 extends ...'.
../../node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/viem/_types/utils/formatters/transaction.d.ts(291,217): error TS2536: Type 'K_5' cannot be used to index type '{ blockHash: `0x${string}` | null; blockNumber: bigint | null; from: `0x${string}`; gas: bigint; hash: `0x${string}`; input: `0x${string}`; nonce: number; r: `0x${string}`; s: `0x${string}`; to: `0x${string}` | null; transactionIndex: number | null; ... 11 more ...; type: "eip1559"; }'.
../../node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/viem/_types/utils/formatters/transaction.d.ts(339,219): error TS2536: Type 'K_7' cannot be used to index type '{ blockHash: `0x${string}` | null; blockNumber: bigint | null; from: `0x${string}`; gas: bigint; hash: `0x${string}`; input: `0x${string}`; nonce: number; r: `0x${string}`; s: `0x${string}`; to: `0x${string}` | null; transactionIndex: number | null; ... 11 more ...; type: "eip4844"; }'.
../../node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/viem/_types/utils/formatters/transaction.d.ts(363,88): error TS2536: Type 'K_6' cannot be used to index type '{ [K_7 in keyof { blockHash: `0x${string}` | null; blockNumber: bigint | null; from: `0x${string}`; gas: bigint; hash: `0x${string}`; input: `0x${string}`; nonce: number; r: `0x${string}`; s: `0x${string}`; to: `0x${string}` | null; transactionIndex: number | null; ... 11 more ...; type: "eip4844"; } as K_7 extends ...'.
../../node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/viem/_types/utils/formatters/transaction.d.ts(387,219): error TS2536: Type 'K_7' cannot be used to index type '{ blockHash: `0x${string}` | null; blockNumber: bigint | null; from: `0x${string}`; gas: bigint; hash: `0x${string}`; input: `0x${string}`; nonce: number; r: `0x${string}`; s: `0x${string}`; to: `0x${string}` | null; transactionIndex: number | null; ... 11 more ...; type: "eip4844"; }'.
../../node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/viem/_types/utils/formatters/transactionRequest.d.ts(21,598): error TS2536: Type 'K' cannot be used to index type '{ [K_1 in keyof TransactionRequestLegacy<`0x${string}`, `0x${string}`, "0x0"> as K_1 extends keyof TOverrideReturnType ? TOverrideReturnType[K_1] extends void ? never : K_1 : K_1]: K_1 extends keyof TOverrideReturnType ? TOverrideReturnType[K_1] : TransactionRequestLegacy<...>[K_1]; } & TOverrideReturnType'.
../../node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/viem/_types/utils/formatters/transactionRequest.d.ts(21,1558): error TS2536: Type 'K_2' cannot be used to index type '{ [K_3 in keyof TransactionRequestEIP2930<`0x${string}`, `0x${string}`, "0x1"> as K_3 extends keyof TOverrideReturnType ? TOverrideReturnType[K_3] extends void ? never : K_3 : K_3]: K_3 extends keyof TOverrideReturnType ? TOverrideReturnType[K_3] : TransactionRequestEIP2930<...>[K_3]; } & TOverrideReturnType'.
../../node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/viem/_types/utils/formatters/transactionRequest.d.ts(21,2522): error TS2536: Type 'K_4' cannot be used to index type '{ [K_5 in keyof TransactionRequestEIP1559<`0x${string}`, `0x${string}`, "0x2"> as K_5 extends keyof TOverrideReturnType ? TOverrideReturnType[K_5] extends void ? never : K_5 : K_5]: K_5 extends keyof TOverrideReturnType ? TOverrideReturnType[K_5] : TransactionRequestEIP1559<...>[K_5]; } & TOverrideReturnType'.
This is the same error mentioned in wevm/viem#1828 (comment), where it's suggested to enable the flag. It appears that viem and its examples have this flag turned on.
By setting "skipLibCheck": true
, our tsc skips type checking of declaration files. I'll look into this to see if there's a better solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"skipLibCheck": true
is necessary for using viem, and even in the viem repository, the same error occurs when removing the "skipLibCheck": true
. I believe there isn't a good way to maintain "skipLibCheck": false
, although I don't fully understand the exact cause of the error.
Skipping the check should be fine here because the declaration files are not utilized in the packages where I enabled the flag, making it relevant only for library files.
@@ -17,7 +17,7 @@ export const accessManagementSystemDeployedBytecodeSize = size( | |||
); | |||
export const accessManagementSystemBytecode = encodeDeployData({ | |||
bytecode: accessManagementSystemBuild.bytecode.object as Hex, | |||
abi: [], | |||
abi: [] as Abi, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we have to cast this? I would expect TS to be able to infer this in the call to encodeDeployData
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
abi: []
is now not allowed for encodeDeployData
. I think []
is not the abi expected here. I'll look into this.
src/deploy/ensureWorldFactory.ts:18:64 - error TS2345: Argument of type '{ bytecode: `0x${string}`; abi: never[]; }' is not assignable to parameter of type 'never'.
18 export const accessManagementSystemBytecode = encodeDeployData({
~
19 bytecode: accessManagementSystemBuild.bytecode.object as Hex,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 abi: [],
~~~~~~~~~~
21 });
~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The encodeDeployData function does the task only if the contract has constructor args. It appears to have started rejecting abis with no effects at the type level? The logic of the function hasn't changed, so its behavior remains the same as before.
Alternatively, we can bypass this function:
diff --git a/packages/cli/src/deploy/ensureWorldFactory.ts b/packages/cli/src/deploy/ensureWorldFactory.ts
index 1f2913a8..f45cbed8 100644
--- a/packages/cli/src/deploy/ensureWorldFactory.ts
+++ b/packages/cli/src/deploy/ensureWorldFactory.ts
@@ -15,10 +15,7 @@ import { Contract } from "./ensureContract";
export const accessManagementSystemDeployedBytecodeSize = size(
accessManagementSystemBuild.deployedBytecode.object as Hex
);
-export const accessManagementSystemBytecode = encodeDeployData({
- bytecode: accessManagementSystemBuild.bytecode.object as Hex,
- abi: [] as Abi,
-});
+export const accessManagementSystemBytecode = accessManagementSystemBuild.bytecode.object as Hex;
export const accessManagementSystem = getCreate2Address({
from: deployer,
bytecode: accessManagementSystemBytecode,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems fine to leave the empty ABI approach! feels clearer to me
@@ -3,7 +3,6 @@ import type { MUDChain } from "./types"; | |||
export const latticeTestnet = { | |||
name: "Lattice Testnet", | |||
id: 4242, | |||
network: "lattice-testnet", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for anyone reading: the network
key got removed from Chain
in latest viem (I think this was a ethers carry-over)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The network field was removed in this commit. It suggests using the id field instead when necessary.
export function encodeSystemCall<abi extends Abi, functionName extends string = string>({ | ||
export function encodeSystemCall<abi extends Abi, functionName extends ContractFunctionName<abi>>({ | ||
abi, | ||
systemId, | ||
functionName, | ||
args, | ||
}: SystemCall<abi, functionName>): GetFunctionArgs<typeof IWorldCallAbi, "call">["args"] { | ||
}: SystemCall<abi, functionName>): AbiParametersToPrimitiveTypes< | ||
ExtractAbiFunction<typeof IWorldCallAbi, "call">["inputs"] | ||
> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These encode call functions have been manually tested like this:
await worldContract.write.call(
encodeSystemCall({
abi: worldContract.abi,
functionName: "addTask",
args: [label],
systemId: resourceToHex({ type: "system", namespace: "", name: "TasksSystem" }),
})
);
await worldContract.write.batchCall([
encodeSystemCalls(worldContract.abi, [
{
functionName: "addTask",
args: [label],
systemId: resourceToHex({ type: "system", namespace: "", name: "TasksSystem" }),
},
]).map(([systemId, callData]) => ({ systemId, callData })),
]);
await worldContract.write.callFrom(
encodeSystemCallFrom({
from: walletClient.account.address,
abi: worldContract.abi,
functionName: "addTask",
args: [label],
systemId: resourceToHex({ type: "system", namespace: "", name: "TasksSystem" }),
})
);
await worldContract.write.batchCallFrom([
encodeSystemCallsFrom(worldContract.abi, walletClient.account.address, [
{
functionName: "addTask",
args: [label],
systemId: resourceToHex({ type: "system", namespace: "", name: "TasksSystem" }),
},
]).map(([from, systemId, callData]) => ({ from, systemId, callData })),
]);
wallet: walletClient, | ||
}, | ||
}) as unknown as GetContractReturnType<TAbi, { public: TPublicClient; wallet: TWalletClient }, TAddress> & { | ||
write: unknown; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what does this bit do? do we need it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This corresponds to this viem code. At the type level, the properties attached to the contract object depend on the clients and the abi types. Since the actual abi type isn't known here, I needed to specify it to have the 'write' property. (To fix Property 'write' does not exist on type '...
typescript error)
There might be a better way to handle this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While calling this MUD's getContract, it is typed the same as Viem's since it uses the same arguments and return type.
packages/common/src/getContract.ts
Outdated
@@ -98,11 +108,11 @@ export function getContract< | |||
args, | |||
...options, | |||
onWrite, | |||
} as unknown as WriteContractParameters<TAbi, typeof functionName, TChain, TAccount>; | |||
} as unknown as WriteContractParameters; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are we losing type specificity/safety here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This loses some type specificity. I'll fix it.
return [ | ||
systemId, | ||
encodeFunctionData({ | ||
abi, | ||
functionName, | ||
args, | ||
} as unknown as EncodeFunctionDataParameters<abi, functionName>), | ||
} as unknown as EncodeFunctionDataParameters), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above, are we losing any type specificity/safety here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is looking great!
just a few comments and I went ahead and added a changeset
@holic Thank you so much for the review and the changeset! I've addressed the comments and resolved the conflict. |
closes #2267