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

feat: support Panic & custom contract errors #57

Merged
merged 7 commits into from
Feb 9, 2023
Merged

Conversation

jxom
Copy link
Member

@jxom jxom commented Feb 8, 2023

  • Support Panic & custom contract errors thrown from contracts
  • Added @wagmi/cli so we can build contract bytecode, generate abis, and then deploy them in tests

@changeset-bot
Copy link

changeset-bot bot commented Feb 8, 2023

🦋 Changeset detected

Latest commit: bf0ee4d

The changes in this PR will be included in the next version bump.

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

@vercel
Copy link

vercel bot commented Feb 8, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
viem-benchmark ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Feb 9, 2023 at 0:26AM (UTC)
viem-playground ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Feb 9, 2023 at 0:26AM (UTC)
viem-site ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Feb 9, 2023 at 0:26AM (UTC)

@socket-security
Copy link

Socket Security Pull Request Report

Dependency issues detected. If you merge this pull request, you will not be alerted to the instances of these issues again.

📜 Install scripts

Install scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.

Packages should not be running non-essential scripts during install and there are often solutions to problems people solve with install scripts that can be run at publish time instead.

Package Script field Source
[email protected] (upgraded) postinstall package.json via @wagmi/[email protected], [email protected], site/package.json via [email protected]
Pull request report summary
Issue Status
Install scripts ⚠️ 1 issue
Native code ✅ 0 issues
Bin script confusion ✅ 0 issues
Bin script shell injection ✅ 0 issues
Unresolved require ✅ 0 issues
Invalid package.json ✅ 0 issues
HTTP dependency ✅ 0 issues
Git dependency ✅ 0 issues
Potential typo squat ✅ 0 issues
Known Malware ✅ 0 issues
Telemetry ✅ 0 issues
Protestware/Troll package ✅ 0 issues
Bot Commands

To ignore an alert, reply with a comment starting with @SocketSecurity ignore followed by a space separated list of package-name@version specifiers. e.g. @SocketSecurity ignore [email protected] [email protected]

Powered by socket.dev

@@ -0,0 +1,50 @@
// SPDX-License-Identifier: Unlicense
Copy link
Member Author

Choose a reason for hiding this comment

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

This contract is to test contract errors.

})
}

export async function deployErrorExample() {
Copy link
Member Author

Choose a reason for hiding this comment

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

We can deploy the bytecode generated from @wagmi/cli to use in our tests.

Comment on lines -39 to -51
const abiItem = getAbiItem({ abi, args, name: functionName })
const formattedArgs = abiItem
? formatAbiItemWithArgs({
abiItem,
args,
includeFunctionName: false,
includeName: false,
})
: undefined
const functionWithParams = abiItem
? formatAbiItem(abiItem, { includeName: true })
: undefined

Copy link
Member Author

@jxom jxom Feb 8, 2023

Choose a reason for hiding this comment

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

Just moved this logic into ContractFunctionRevertedError.

const { abiItem, errorName, args: errorArgs } = decodedData
if (errorName === 'Error') {
reason = (errorArgs as [string])[0]
} else if (errorName === 'Panic') {
Copy link
Member Author

Choose a reason for hiding this comment

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

Support Panic & retrieve corresponding reason.

} else if (errorName === 'Panic') {
const [firstArg] = errorArgs as [number]
reason = panicReasons[firstArg as keyof typeof panicReasons]
} else if (errorArgs) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Support custom errors.

@@ -156,6 +152,159 @@ describe('bayc', () => {
})
})

describe('contract errors', () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

E2E examples of contract errors can be seen here!

@@ -0,0 +1,52 @@
name: 'Setup'
Copy link
Member Author

Choose a reason for hiding this comment

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

Feel like we use these steps in a lot of actions. Might make sense to create a composite action for them.

Copy link
Member

Choose a reason for hiding this comment

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

Good idea

Comment on lines 31 to 33
run: pnpm i
env:
SKIP_WAGMI_CLI: true
Copy link
Member

Choose a reason for hiding this comment

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

could we do pnpm i --ignore-scripts instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

good idea lol

Comment on lines 51 to 83
"panicReasons": {
"1": "An \`assert\` condition failed.",
"17": "Arithmic operation resulted in underflow or overflow.",
"18": "Division or modulo by zero (e.g. \`5 / 0\` or \`23 % 0\`).",
"33": "Attempted to convert to an invalid type.",
"34": "Attempted to access a storage byte array that is incorrectly encoded.",
"49": "Performed \`.pop()\` on an empty array",
"50": "Array index is out of bounds.",
"65": "Allocated too much memory or created an array which is too large.",
"81": "Attempted to call a zero-initialized variable of internal function type.",
},
"solidityError": {
"inputs": [
{
"internalType": "string",
"name": "message",
"type": "string",
},
],
"name": "Error",
"type": "error",
},
"solidityPanic": {
"inputs": [
{
"internalType": "uint256",
"name": "reason",
"type": "uint256",
},
],
"name": "Panic",
"type": "error",
},
Copy link
Member

Choose a reason for hiding this comment

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

some duplication here from other tests. in my mind, index.test.ts tests are for locking down exports. could switch to expect(Object.keys(exports)).toMatchInlineSnapshot() instead.

import { AbiError } from 'abitype'

// https://docs.soliditylang.org/en/v0.8.16/control-structures.html#panic-via-assert-and-error-via-require
export const panicReasons = {
Copy link
Member

Choose a reason for hiding this comment

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

🔥🔥🔥

export const solidityPanic: AbiError = {
inputs: [
{
internalType: 'uint256',
Copy link
Member

Choose a reason for hiding this comment

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

could drop internalType from here (and other ABIs that are around in the codebase).

Copy link
Member

@tmm tmm left a comment

Choose a reason for hiding this comment

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

cool use of cli

Base automatically changed from jxom/multicall to main February 8, 2023 23:57
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