Skip to content

Commit

Permalink
feat: refactor .fromAbi params
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Sep 26, 2024
1 parent 98b2974 commit 536ee56
Show file tree
Hide file tree
Showing 30 changed files with 449 additions and 450 deletions.
2 changes: 2 additions & 0 deletions src/Errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ test('exports', () => {
"TypedData_BytesSizeMismatchError",
"TypedData_InvalidPrimaryTypeError",
"Value_InvalidDecimalNumberError",
"WebAuthnP256_CredentialCreationFailedError",
"WebAuthnP256_CredentialRequestFailedError",
]
`)
})
5 changes: 5 additions & 0 deletions src/Errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,8 @@ export {
} from './internal/TypedData/errors.js'

export { Value_InvalidDecimalNumberError } from './internal/Value/errors.js'

export {
WebAuthnP256_CredentialCreationFailedError,
WebAuthnP256_CredentialRequestFailedError,
} from './internal/WebAuthnP256/errors.js'
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export * as AbiConstructor from './AbiConstructor.js'
* 'function bar(string a) returns (uint256 x)',
* ])
*
* const item = AbiError.fromAbi(abi, { name: 'BadSignatureV' }) // [!code focus]
* const item = AbiError.fromAbi(abi, 'BadSignatureV') // [!code focus]
* // ^?
*
*
Expand Down Expand Up @@ -253,7 +253,7 @@ export * as AbiConstructor from './AbiConstructor.js'
* import { Abi, AbiError } from 'ox'
*
* const abi = Abi.from([...])
* const error = AbiError.fromAbi(abi, { name: 'InvalidSignature' })
* const error = AbiError.fromAbi(abi, 'InvalidSignature')
*
* const value = AbiError.decode(error, '0xecde634900000000000000000000000000000000000000000000000000000000000001a400000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001') // [!code focus]
* // @log: [420n, 69n, 1]
Expand Down Expand Up @@ -282,7 +282,7 @@ export * as AbiError from './AbiError.js'
* 'function bar(string a) returns (uint256 x)',
* ])
*
* const item = AbiEvent.fromAbi(abi, { name: 'Transfer' }) // [!code focus]
* const item = AbiEvent.fromAbi(abi, 'Transfer') // [!code focus]
* // ^?
*
*
Expand Down Expand Up @@ -397,7 +397,7 @@ export * as AbiEvent from './AbiEvent.js'
* 'function bar(string a) returns (uint256 x)',
* ])
*
* const item = AbiFunction.fromAbi(abi, { name: 'bar' }) // [!code focus]
* const item = AbiFunction.fromAbi(abi, 'bar') // [!code focus]
* // ^?
*
*
Expand Down Expand Up @@ -495,7 +495,7 @@ export * as AbiFunction from './AbiFunction.js'
* 'function bar(string a) returns (uint256 x)',
* ])
*
* const item = AbiItem.fromAbi(abi, { name: 'Transfer' }) // [!code focus]
* const item = AbiItem.fromAbi(abi, 'Transfer') // [!code focus]
* // ^?
*
*
Expand Down
16 changes: 8 additions & 8 deletions src/internal/AbiError/decode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ describe('behavior: network', async () => {
params: [
{
data: AbiFunction.encodeInput(
AbiFunction.fromAbi(abi, { name: 'revertRead' }),
AbiFunction.fromAbi(abi, 'revertRead'),
),
to: contractAddress,
},
],
})
.catch((error) => {
const errorItem = AbiError.fromAbi(abi, { name: error.data })
const errorItem = AbiError.fromAbi(abi, error.data)
const value = AbiError.decode(errorItem, error.data)
return [errorItem, value]
})
Expand Down Expand Up @@ -137,14 +137,14 @@ describe('behavior: network', async () => {
params: [
{
data: AbiFunction.encodeInput(
AbiFunction.fromAbi(abi, { name: 'assertRead' }),
AbiFunction.fromAbi(abi, 'assertRead'),
),
to: contractAddress,
},
],
})
.catch((error) => {
const errorItem = AbiError.fromAbi(abi, { name: error.data })
const errorItem = AbiError.fromAbi(abi, error.data)
const value = AbiError.decode(errorItem, error.data)
if (typeof value !== 'number') return
return [errorItem, value, AbiError.panicReasons[value]]
Expand Down Expand Up @@ -176,14 +176,14 @@ describe('behavior: network', async () => {
params: [
{
data: AbiFunction.encodeInput(
AbiFunction.fromAbi(abi, { name: 'overflowRead' }),
AbiFunction.fromAbi(abi, 'overflowRead'),
),
to: contractAddress,
},
],
})
.catch((error) => {
const errorItem = AbiError.fromAbi(abi, { name: error.data })
const errorItem = AbiError.fromAbi(abi, error.data)
const value = AbiError.decode(errorItem, error.data)
if (typeof value !== 'number') return
return [errorItem, value, AbiError.panicReasons[value]]
Expand Down Expand Up @@ -215,14 +215,14 @@ describe('behavior: network', async () => {
params: [
{
data: AbiFunction.encodeInput(
AbiFunction.fromAbi(abi, { name: 'complexCustomWrite' }),
AbiFunction.fromAbi(abi, 'complexCustomWrite'),
),
to: contractAddress,
},
],
})
.catch((error) => {
const errorItem = AbiError.fromAbi(abi, { name: error.data })
const errorItem = AbiError.fromAbi(abi, error.data)
const value = AbiError.decode(errorItem, error.data)
return [errorItem, value]
})
Expand Down
8 changes: 4 additions & 4 deletions src/internal/AbiError/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import type { AbiError } from './types.js'
* import { Abi, AbiError } from 'ox'
*
* const abi = Abi.from([...]) // [!code hl]
* const error = AbiError.fromAbi(abi, { name: 'InvalidSignature' }) // [!code hl]
* const error = AbiError.fromAbi(abi, 'InvalidSignature') // [!code hl]
*
* const value = AbiError.decode(error, '0xecde634900000000000000000000000000000000000000000000000000000000000001a400000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001')
* // @log: [420n, 69n, 1]
Expand All @@ -53,7 +53,7 @@ import type { AbiError } from './types.js'
* const data = '0xecde634900000000000000000000000000000000000000000000000000000000000001a400000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001'
*
* const abi = Abi.from([...])
* const error = AbiError.fromAbi(abi, { name: data }) // [!code hl]
* const error = AbiError.fromAbi(abi, data) // [!code hl]
*
* const value = AbiError.decode(error, data)
* // @log: [420n, 69n, 1]
Expand Down Expand Up @@ -84,7 +84,7 @@ import type { AbiError } from './types.js'
* },
* // ...
* ])
* const approve = AbiFunction.fromAbi(abi, { name: 'approve' })
* const approve = AbiFunction.fromAbi(abi, 'approve')
*
* // 2. Encode the Function Input.
* const data = AbiFunction.encodeInput(
Expand All @@ -105,7 +105,7 @@ import type { AbiError } from './types.js'
* })
* } catch (e) { // [!code focus]
* // 4. Extract and decode the Error. // [!code focus]
* const error = AbiError.fromAbi(abi, { name: e.data }) // [!code focus]
* const error = AbiError.fromAbi(abi, e.data) // [!code focus]
* const value = AbiError.decode(error, e.data) // [!code focus]
* console.error(`${error.name}(${value})`) // [!code focus]
* // @error: Error(ERC721: approve caller is not owner nor approved for all)
Expand Down
2 changes: 1 addition & 1 deletion src/internal/AbiError/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import type { AbiError } from './types.js'
* }, // [!code hl]
* // ... // [!code hl]
* ]) // [!code hl]
* const error = AbiError.fromAbi(abi, { name: 'InvalidSignature' }) // [!code hl]
* const error = AbiError.fromAbi(abi, 'InvalidSignature') // [!code hl]
*
* const data = AbiError.encode(
* error,
Expand Down
50 changes: 17 additions & 33 deletions src/internal/AbiError/fromAbi.snap-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import { test } from 'vitest'
import { seaportContractConfig } from '../../../test/constants/abis.js'

test('default', () => {
const item = AbiError.fromAbi(seaportContractConfig.abi, {
name: 'BadSignatureV',
})
const item = AbiError.fromAbi(seaportContractConfig.abi, 'BadSignatureV')
attest(item).type.toString.snap(`{
readonly inputs: readonly [
{ readonly name: "v"; readonly type: "uint8" }
Expand All @@ -20,28 +18,21 @@ test('default', () => {
test('behavior: unknown abi', () => {
const item = AbiError.fromAbi(
seaportContractConfig.abi as readonly unknown[],
{
name: 'BadSignatureV',
},
'BadSignatureV',
)
attest(item).type.toString.snap('AbiError')
})

test('behavior: data', () => {
const item = AbiError.fromAbi(seaportContractConfig.abi, {
name: 'BadSignatureV',
})
const item = AbiError.fromAbi(seaportContractConfig.abi, 'BadSignatureV')
const selector = AbiItem.getSelector(item)
const item_2 = AbiError.fromAbi(seaportContractConfig.abi, {
name: selector,
})
const item_2 = AbiError.fromAbi(seaportContractConfig.abi, selector)
attest(item_2.name).type.toString.snap(` | "Error"
| "Panic"
| "InvalidSignature"
| "BadSignatureV"
| "BadContractSignature"
| "BadFraction"
| "BadReturnValueFromERC20OnTransfer"
| "BadSignatureV"
| "ConsiderationCriteriaResolverOutOfRange"
| "ConsiderationNotMet"
| "CriteriaNotEnabledForItem"
Expand All @@ -60,6 +51,7 @@ test('behavior: data', () => {
| "InvalidNativeOfferItem"
| "InvalidProof"
| "InvalidRestrictedOrder"
| "InvalidSignature"
| "InvalidSigner"
| "InvalidTime"
| "MismatchedFulfillmentOfferAndConsiderationComponents"
Expand All @@ -84,9 +76,7 @@ test('behavior: data', () => {

test('behavior: able to extract solidity Error', () => {
const abi = Abi.from(['error Bar()', 'error Foo()', 'error Foo(uint256)'])
const item = AbiError.fromAbi(abi, {
name: 'Error',
})
const item = AbiError.fromAbi(abi, 'Error')
attest(item).type.toString.snap(`{
readonly inputs: readonly [
{ readonly name: "message"; readonly type: "string" }
Expand All @@ -98,9 +88,7 @@ test('behavior: able to extract solidity Error', () => {

test('behavior: able to extract solidity Panic', () => {
const abi = Abi.from(['error Bar()', 'error Foo()', 'error Foo(uint256)'])
const item = AbiError.fromAbi(abi, {
name: 'Panic',
})
const item = AbiError.fromAbi(abi, 'Panic')
attest(item).type.toString.snap(`{
readonly inputs: readonly [
{ readonly name: "reason"; readonly type: "uint8" }
Expand All @@ -112,9 +100,7 @@ test('behavior: able to extract solidity Panic', () => {

test('behavior: overloads', () => {
const abi = Abi.from(['error Bar()', 'error Foo()', 'error Foo(uint256)'])
const item = AbiError.fromAbi(abi, {
name: 'Foo',
})
const item = AbiError.fromAbi(abi, 'Foo')
attest(item).type.toString.snap(`{
readonly name: "Foo"
readonly type: "error"
Expand All @@ -124,8 +110,7 @@ test('behavior: overloads', () => {

test('behavior: overloads with args', () => {
const abi = Abi.from(['error Bar()', 'error Foo()', 'error Foo(uint256)'])
const item = AbiError.fromAbi(abi, {
name: 'Foo',
const item = AbiError.fromAbi(abi, 'Foo', {
args: [1n],
})
attest(item).type.toString.snap(`{
Expand All @@ -141,9 +126,7 @@ test('behavior: overloads: no inputs or args', () => {
'error Foo(bytes)',
'error Foo(uint256)',
])
const item = AbiError.fromAbi(abi, {
name: 'Foo',
})
const item = AbiError.fromAbi(abi, 'Foo')
attest(item).type.toString.snap(`{
readonly name: "Foo"
readonly type: "error"
Expand All @@ -162,16 +145,16 @@ test('behavior: overloads: no inputs or args', () => {

test('behavior: widened name', () => {
const abi = Abi.from(seaportContractConfig.abi)
const abiItem = AbiError.fromAbi(abi, {
name: 'BadContractSignature' as AbiError.Name<typeof abi>,
})
const abiItem = AbiError.fromAbi(
abi,
'BadContractSignature' as AbiError.Name<typeof abi>,
)
attest(abiItem.name).type.toString.snap(` | "Error"
| "Panic"
| "InvalidSignature"
| "BadSignatureV"
| "BadContractSignature"
| "BadFraction"
| "BadReturnValueFromERC20OnTransfer"
| "BadSignatureV"
| "ConsiderationCriteriaResolverOutOfRange"
| "ConsiderationNotMet"
| "CriteriaNotEnabledForItem"
Expand All @@ -190,6 +173,7 @@ test('behavior: widened name', () => {
| "InvalidNativeOfferItem"
| "InvalidProof"
| "InvalidRestrictedOrder"
| "InvalidSignature"
| "InvalidSigner"
| "InvalidTime"
| "MismatchedFulfillmentOfferAndConsiderationComponents"
Expand Down
Loading

0 comments on commit 536ee56

Please sign in to comment.