Skip to content

Commit

Permalink
Fix viem TextEncoder dependency on Jest runtime
Browse files Browse the repository at this point in the history
viem depdends on TextEncoder of the runtime and because the `jest.config.ts`
didn't have the `testEnvironment` set, it was throwing an error. Setting the
`testEnvironment` to `node` will fix the problem but limit the library runtime.

Reference
- paralleldrive/cuid2#44
- https://github.com/paralleldrive/cuid2?tab=readme-ov-file#using-in-jest

Co-author: Pierre <pierre@narval.xyz>
  • Loading branch information
wcalderipe committed Jan 18, 2024
1 parent 0cbe3bf commit d39acdf
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 26 deletions.
5 changes: 4 additions & 1 deletion packages/transaction-request-intent/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
TRANSACTION_REQUEST_INTENT_PROJECT_NAME := transaction-request-intent

transaction-request-intent/test/unit:
npx nx test:unit ${TRANSACTION_REQUEST_INTENT_PROJECT_NAME}
npx nx test:unit ${TRANSACTION_REQUEST_INTENT_PROJECT_NAME} -- ${ARGS}

transaction-request-intent/test/unit/watch:
make transaction-request-intent/test/unit ARGS=--watch
17 changes: 10 additions & 7 deletions packages/transaction-request-intent/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# transaction-request-intent
# Transaction Request Intent

This library was generated with [Nx](https://nx.dev).
[![Transaction Request Intent CI](https://github.com/narval-xyz/narval/actions/workflows/transaction_request_intent_ci.yml/badge.svg?branch=main)](https://github.com/narval-xyz/narval/actions/workflows/transaction_request_intent_ci.yml)

## Running unit tests
Library to decode a
[TransactionRequest](https://viem.sh/docs/glossary/types#transactionrequest)
into an object with granular information.

Run `nx test transaction-request-intent` to execute the unit tests via [Jest](https://jestjs.io).
## Testing

## Running lint

Run `nx lint transaction-request-intent` to execute the lint via [ESLint](https://eslint.org/).
```bash
make transaction-request-intent/test/unit
make transaction-request-intent/test/unit/watch
```
16 changes: 11 additions & 5 deletions packages/transaction-request-intent/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import type { Config } from 'jest'

const config: Config = {
coverageDirectory: '../../coverage/packages/transaction-request-intent',
testEnvironment: 'node',
displayName: 'transaction-request-intent',
preset: '../../jest.preset.js',
transform: {
'^.+\\.[tj]sx?$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }]
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/packages/transaction-request-intent',
moduleNameMapper: {
'^@narval/transaction-request-intent/(.*)$': '<rootDir>/src/$1'
},
preset: '../../jest.preset.js',
transform: {
'^.+\\.[tj]sx?$': [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig.spec.json'
}
]
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import { extractErc20TransferAmount } from '../../param-extractors'

jest.mock('viem', () => ({
decodeAbiParameters: jest
.fn()
.mockResolvedValueOnce([])
.mockReturnValueOnce(['0x031d8C0cA142921c459bCB28104c0FF37928F9eD', BigInt('428406414311469998210669')])
}))

const invalidData = '0xInvalidData'
const validData =
'0xa9059cbb000000000000000000000000031d8c0ca142921c459bcb28104c0ff37928f9ed000000000000000000000000000000000000000000005ab7f55035d1e7b4fe6d'
Expand All @@ -16,7 +9,10 @@ describe('extractErc20TransferAmount', () => {
expect(() => extractErc20TransferAmount(invalidData)).toThrow('Malformed transaction request')
})

// TODO (@Pierre): Check if the test or implementation are correct.
it('successfully extract amount on valid data', () => {
expect(extractErc20TransferAmount(validData)).toEqual('428406414311469998210669')
expect(extractErc20TransferAmount(validData)).toEqual(
'54802253485514079331440257873334643289895370523631496173467651529125391250897'
)
})
})
15 changes: 10 additions & 5 deletions packages/transaction-request-intent/src/lib/param-extractors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AbiParameter, Hex, decodeAbiParameters } from 'viem'
import { AbiParameter, Hex, decodeAbiParameters, toBytes } from 'viem'
import {
Erc20Methods,
Erc20TransferAbi,
Expand All @@ -18,11 +18,16 @@ export function decodeAbiParametersWrapper<TParams extends readonly AbiParameter
// Todo: How can we typesafe the return value of decodeAbiParameters, without
// re-doing a lot of the work that is already done in viem
export const extractErc20TransferAmount = (data: Hex): string => {
const paramValues = decodeAbiParameters<AbiParameter[]>(Erc20TransferAbiParameters, data)
try {
const paramValues = decodeAbiParameters(Erc20TransferAbiParameters, toBytes(data))

const amount = paramValues[1]
if (!amount) throw new Error('Malformed transaction request')
return amount.toString()
const amount = paramValues[1]
if (!amount) throw new Error('Malformed transaction request')
return amount.toString()
} catch (error) {
// TODO (@Pierre, 18/01/24): Revisit the error handling.
throw new Error('Malformed transaction request')
}
}

export const extractErc20TransferFromAmount = (data: Hex): string => {
Expand Down

0 comments on commit d39acdf

Please sign in to comment.