Skip to content
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

Change viem usage for EAS to get typed data #131

Conversation

datadanne
Copy link
Collaborator

@datadanne datadanne commented Dec 14, 2023

Viem helped catch a bug here! I made a typo in one of the function names, multiRevokeOffchain. I had written multiRevokeOffChain with a capital C, and tsc knew that switch (decoded.functionName) could never match on that name with a cpaital C because the data is typed :)

Comment on lines +31 to +33
let decoded: ReturnType<typeof decodeTransactionInputViem<typeof ABIs.EAS>>;
try {
decoded = decodeTransactionInputViem(
transaction.input as Hex,
ABIs.EAS as Abi,
);
decoded = decodeTransactionInputViem(transaction.input as Hex, ABIs.EAS);
Copy link
Collaborator Author

@datadanne datadanne Dec 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be simplified so that someone that implements a detect function doesn't have to define the return type like this. A suggestion:

const decoded = tryDecodeTransactionInput(transaction.input, ABIs.EAS);
if (!decoded) return false;

} catch (err) {
return false;
}

if (!decoded || !decoded.functionName) return false;
return [
const handledFunctions: ExtractAbiFunctionNames<typeof ABIs.EAS>[] = [
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be simplified into a helper function, maybe something like

return pickABIFunctionNames([
  'attest', 
  'attestByDelegation',
  '...',
], ABIs.EAS).includes(decoded.functionName);

Copy link
Contributor

@ponyjackal ponyjackal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome

@@ -1,4 +1,4 @@
[
const abi = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we leave this abi file as json? and handle types in constants?

Copy link
Collaborator Author

@datadanne datadanne Dec 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately not :( microsoft/TypeScript#32063

It needs to be a const type and typescript doesn't support that for json files

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, maybe we can leave abis as json, and then import as const in constants.ts ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No that doesn't work unfortunately, typescript cannot infer a const type for a json file regardless of where/how it is imported

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay,

@ponyjackal ponyjackal merged commit 8b9096c into benguyen0214/ou-1358-switch-from-ethers-to-viem-in-context-repo Dec 14, 2023
@ponyjackal ponyjackal deleted the datadanne/viem-type-fixes branch December 14, 2023 20:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants