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

Add tests for ProxyFactoryDecoder #1139

Merged
merged 3 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Hex } from 'viem';
import { faker } from '@faker-js/faker';
import { ProxyFactoryDecoder } from '@/domain/relay/contracts/proxy-factory-decoder.helper';
import { createProxyWithNonceEncoder } from '@/domain/relay/contracts/__tests__/proxy-factory-encoder.builder';

describe('ProxyFactoryDecoder', () => {
let target: ProxyFactoryDecoder;

beforeEach(() => {
jest.clearAllMocks();
fmrsabino marked this conversation as resolved.
Show resolved Hide resolved
target = new ProxyFactoryDecoder();
});

it('decodes a createProxyWithNonce function call correctly', () => {
const createProxyWithNonce = createProxyWithNonceEncoder();
const args = createProxyWithNonce.build();
fmrsabino marked this conversation as resolved.
Show resolved Hide resolved
const data = createProxyWithNonce.encode();
fmrsabino marked this conversation as resolved.
Show resolved Hide resolved

expect(target.decodeFunctionData({ data })).toEqual({
functionName: 'createProxyWithNonce',
args: [args.singleton, args.initializer, args.saltNonce],
});
});

it('throws if the function call cannot be decoded', () => {
const data = faker.string.hexadecimal({ length: 138 }) as Hex;

expect(() => target.decodeFunctionData({ data })).toThrow();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { faker } from '@faker-js/faker';
import { encodeFunctionData, getAddress, Hex, parseAbi } from 'viem';

import { IEncoder } from '@/__tests__/encoder-builder';
import { Builder } from '@/__tests__/builder';
import { setupEncoder } from '@/domain/contracts/contracts/__tests__/safe-encoder.builder';

// createProxyWithNonce

type CreateProxyWithNonceArgs = {
singleton: Hex;
initializer: Hex;
saltNonce: bigint;
};

class SetupEncoder<T extends CreateProxyWithNonceArgs>
extends Builder<T>
implements IEncoder
{
static readonly FUNCTION_SIGNATURE =
'function createProxyWithNonce(address _singleton, bytes memory initializer, uint256 saltNonce)';

encode(): Hex {
const abi = parseAbi([SetupEncoder.FUNCTION_SIGNATURE]);

const args = this.build();

return encodeFunctionData({
abi,
functionName: 'createProxyWithNonce',
args: [args.singleton, args.initializer, args.saltNonce],
});
}
}

export function createProxyWithNonceEncoder(): SetupEncoder<CreateProxyWithNonceArgs> {
const initializer = setupEncoder().encode();
return new SetupEncoder()
.with('singleton', getAddress(faker.finance.ethereumAddress()))
.with('initializer', initializer)
.with('saltNonce', faker.number.bigInt());
}