Skip to content

Commit

Permalink
extract deployments, refactor tests, fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
mmv08 committed Jul 10, 2024
1 parent a97b531 commit 94cf3a1
Show file tree
Hide file tree
Showing 11 changed files with 245 additions and 206 deletions.
11 changes: 4 additions & 7 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import eslint from "@eslint/js";
import tseslint from "typescript-eslint";
import prettier from "eslint-plugin-prettier/recommended";
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import prettier from 'eslint-plugin-prettier/recommended';

export default [
eslint.configs.recommended,
...tseslint.configs.recommended,
{
rules: {
"@typescript-eslint/no-unused-vars": [
"error",
{ ignoreRestSiblings: true },
],
'@typescript-eslint/no-unused-vars': ['error', { ignoreRestSiblings: true }],
},
},
prettier,
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/assets.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fs from 'fs';
import path from 'path';
import { SingletonDeploymentJSON } from '../types';
import { SingletonDeploymentJSON, AddressType } from '../types';

const KNOWN_ADDRESS_TYPES = ['canonical', 'eip155', 'zksync'];
const KNOWN_ADDRESS_TYPES: AddressType[] = ['canonical', 'eip155', 'zksync'];

function assetPath(...paths: string[]) {
return path.join(__dirname, '..', 'assets', ...paths);
Expand Down Expand Up @@ -45,7 +45,7 @@ describe('assets/', () => {
expect(keys).toEqual(sorted);
});

it('networks should only contain canonical address types', async () => {
it('networks should only contain known address types', async () => {
const deploymentJson = await readAssetJSON(version, file);
if (!deploymentJson) {
throw new Error(`Failed to read asset ${version}/${file}`);
Expand Down
120 changes: 72 additions & 48 deletions src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import GnosisSafe120 from './assets/v1/v1.2.0/gnosis_safe.json';
import GnosisSafe111 from './assets/v1/v1.1.1/gnosis_safe.json';
import GnosisSafe100 from './assets/v1/v1.0.0/gnosis_safe.json';
import { findDeployment } from '../utils';
import { _safeDeployments, _safeL2Deployments } from '../safes';
import { _SAFE_DEPLOYMENTS, _SAFE_L2_DEPLOYMENTS } from '../deployments';
import { SingletonDeployment, SingletonDeploymentJSON, DeploymentFormats, SingletonDeploymentV2 } from '../types';

const _safeDeploymentsReverse = [..._safeDeployments].reverse();
const _safeDeploymentsReverse = [..._SAFE_DEPLOYMENTS].reverse();

describe('utils.ts', () => {
describe('findDeployment', () => {
Expand All @@ -19,32 +19,38 @@ describe('utils.ts', () => {
const testUnreleasedDeploymentJson: SingletonDeploymentJSON = {
version: '',
abi: [],
addresses: {
canonical: '0xbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef',
deployments: {
canonical: {
address: '0xbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef',
codeHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
},
},
networkAddresses: { '1': 'canonical' },
contractName: '',
codeHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
released: false,
};
const testReleasedDeploymentJson: SingletonDeploymentJSON = {
version: '',
abi: [],
addresses: {
canonical: '0xbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef',
deployments: {
canonical: {
address: '0xbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef',
codeHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
},
},
networkAddresses: { '1': 'canonical' },
contractName: '',
codeHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
released: true, // Default filter value
};
const testReleasedDeployment: SingletonDeployment = {
defaultAddress: '0xbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef',
addresses: {
canonical: '0xbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef',
deployments: {
canonical: {
address: '0xbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef',
codeHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
},
},
networkAddresses: { '1': '0xbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef' },
codeHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
released: true,
abi: [],
version: '',
Expand All @@ -65,17 +71,17 @@ describe('utils.ts', () => {

it('should return the correct deployment (filtered by version)', () => {
// Chronological deployments
expect(findDeployment({ version: '1.3.0' }, _safeDeployments)).toMatchObject(GnosisSafe130);
expect(findDeployment({ version: '1.2.0' }, _safeDeployments)).toMatchObject(GnosisSafe120);
expect(findDeployment({ version: '1.1.1' }, _safeDeployments)).toMatchObject(GnosisSafe111);
expect(findDeployment({ version: '1.0.0' }, _safeDeployments)).toMatchObject(GnosisSafe100);
expect(findDeployment({ version: '1.3.0' }, _SAFE_DEPLOYMENTS)).toMatchObject(GnosisSafe130);
expect(findDeployment({ version: '1.2.0' }, _SAFE_DEPLOYMENTS)).toMatchObject(GnosisSafe120);
expect(findDeployment({ version: '1.1.1' }, _SAFE_DEPLOYMENTS)).toMatchObject(GnosisSafe111);
expect(findDeployment({ version: '1.0.0' }, _SAFE_DEPLOYMENTS)).toMatchObject(GnosisSafe100);
// Incorrect filter:
expect(findDeployment({ version: '2.0.0' }, _safeDeployments)).toBeUndefined();
expect(findDeployment({ version: '2.0.0' }, _SAFE_DEPLOYMENTS)).toBeUndefined();

// L2 deployments
expect(findDeployment({ version: '1.3.0+L2' }, _safeL2Deployments)).toMatchObject(GnosisSafeL2130);
expect(findDeployment({ version: '1.3.0+L2' }, _SAFE_L2_DEPLOYMENTS)).toMatchObject(GnosisSafeL2130);
// Incorrect filter:
expect(findDeployment({ version: '2.0.0+L2' }, _safeL2Deployments)).toBeUndefined();
expect(findDeployment({ version: '2.0.0+L2' }, _SAFE_L2_DEPLOYMENTS)).toBeUndefined();
});

it('should return the correct deployment (filtered by released flag)', () => {
Expand Down Expand Up @@ -123,15 +129,15 @@ describe('utils.ts', () => {
const testDeployments = [testUnreleasedDeploymentJson, testReleasedDeploymentJson];

// Chronological deployments
expect(findDeployment({ released: true }, _safeDeployments)).toMatchObject(Safe141);
expect(findDeployment({ released: true }, _SAFE_DEPLOYMENTS)).toMatchObject(Safe141);

// Reverse chronological deployments
expect(findDeployment({ released: true }, _safeDeploymentsReverse)).toMatchObject(GnosisSafe100);
// Released flag set to false:
expect(findDeployment({ released: false }, testDeployments)).toMatchObject(testUnreleasedDeployment);

// L2 deployments
expect(findDeployment({ released: true }, _safeL2Deployments)).toMatchObject(SafeL2141);
expect(findDeployment({ released: true }, _SAFE_L2_DEPLOYMENTS)).toMatchObject(SafeL2141);
});

it('should return the correct deployment (filtered by network)', () => {
Expand All @@ -143,18 +149,18 @@ describe('utils.ts', () => {
expect(findDeployment({ network: '0' }, _safeDeploymentsReverse)).toBeUndefined();

// L2 deployments
expect(findDeployment({ network: '100' }, _safeL2Deployments)).toMatchObject(SafeL2141);
expect(findDeployment({ network: '100' }, _SAFE_L2_DEPLOYMENTS)).toMatchObject(SafeL2141);
// Incorrect filter:
expect(findDeployment({ network: '0' }, _safeL2Deployments)).toBeUndefined();
expect(findDeployment({ network: '0' }, _SAFE_L2_DEPLOYMENTS)).toBeUndefined();
});
it('should return the correct deployment (filtered by version and released)', () => {
// Chronological deployments
expect(findDeployment({ version: '1.3.0', released: true }, _safeDeployments)).toMatchObject(GnosisSafe130);
expect(findDeployment({ version: '1.2.0', released: true }, _safeDeployments)).toMatchObject(GnosisSafe120);
expect(findDeployment({ version: '1.1.1', released: true }, _safeDeployments)).toMatchObject(GnosisSafe111);
expect(findDeployment({ version: '1.0.0', released: true }, _safeDeployments)).toMatchObject(GnosisSafe100);
expect(findDeployment({ version: '1.3.0', released: true }, _SAFE_DEPLOYMENTS)).toMatchObject(GnosisSafe130);
expect(findDeployment({ version: '1.2.0', released: true }, _SAFE_DEPLOYMENTS)).toMatchObject(GnosisSafe120);
expect(findDeployment({ version: '1.1.1', released: true }, _SAFE_DEPLOYMENTS)).toMatchObject(GnosisSafe111);
expect(findDeployment({ version: '1.0.0', released: true }, _SAFE_DEPLOYMENTS)).toMatchObject(GnosisSafe100);
// Incorrect filter:
expect(findDeployment({ version: '1.0.0', released: false }, _safeDeployments)).toBeUndefined();
expect(findDeployment({ version: '1.0.0', released: false }, _SAFE_DEPLOYMENTS)).toBeUndefined();

// L2 deployments
expect(
Expand All @@ -163,14 +169,14 @@ describe('utils.ts', () => {
version: '1.3.0',
released: true,
},
_safeL2Deployments,
_SAFE_L2_DEPLOYMENTS,
),
).toMatchObject(GnosisSafeL2130);
expect(findDeployment({ version: '1.3.0+L2', released: true }, _safeL2Deployments)).toMatchObject(
expect(findDeployment({ version: '1.3.0+L2', released: true }, _SAFE_L2_DEPLOYMENTS)).toMatchObject(
GnosisSafeL2130,
);
// Incorrect filter:
expect(findDeployment({ version: '1.3.0+L2', released: false }, _safeL2Deployments)).toBeUndefined();
expect(findDeployment({ version: '1.3.0+L2', released: false }, _SAFE_L2_DEPLOYMENTS)).toBeUndefined();
});
it('should return the correct deployment (filtered by version and network)', () => {
// Reverse chronological deployments
Expand Down Expand Up @@ -220,14 +226,14 @@ describe('utils.ts', () => {
version: '1.3.0',
network: '100',
},
_safeL2Deployments,
_SAFE_L2_DEPLOYMENTS,
),
).toMatchObject(GnosisSafeL2130);
expect(findDeployment({ version: '1.3.0+L2', network: '100' }, _safeL2Deployments)).toMatchObject(
expect(findDeployment({ version: '1.3.0+L2', network: '100' }, _SAFE_L2_DEPLOYMENTS)).toMatchObject(
GnosisSafeL2130,
);
// Incorrect filter:
expect(findDeployment({ version: '1.3.0+L2', network: '0' }, _safeL2Deployments)).toBeUndefined();
expect(findDeployment({ version: '1.3.0+L2', network: '0' }, _SAFE_L2_DEPLOYMENTS)).toBeUndefined();
});
it('should return the correct deployment (filtered by released and network)', () => {
const testUnreleasedDeploymentJson: SingletonDeploymentJSON = {
Expand Down Expand Up @@ -300,9 +306,9 @@ describe('utils.ts', () => {
);

// L2 deployments
expect(findDeployment({ released: true, network: '100' }, _safeL2Deployments)).toMatchObject(SafeL2141);
expect(findDeployment({ released: true, network: '100' }, _SAFE_L2_DEPLOYMENTS)).toMatchObject(SafeL2141);
// Incorrect filter:
expect(findDeployment({ released: true, network: '0' }, _safeL2Deployments)).toBeUndefined();
expect(findDeployment({ released: true, network: '0' }, _SAFE_L2_DEPLOYMENTS)).toBeUndefined();
expect(findDeployment({ released: false, network: '100' }, testDeployments)).toBeUndefined();
});
it('should return the correct deployment (filtered by version, released and network)', () => {
Expand Down Expand Up @@ -338,21 +344,21 @@ describe('utils.ts', () => {
released: true,
network: '100',
},
_safeL2Deployments,
_SAFE_L2_DEPLOYMENTS,
),
).toMatchObject(GnosisSafeL2130);
expect(
findDeployment({ version: '1.3.0+L2', released: true, network: '100' }, _safeL2Deployments),
findDeployment({ version: '1.3.0+L2', released: true, network: '100' }, _SAFE_L2_DEPLOYMENTS),
).toMatchObject(GnosisSafeL2130);
// Incorrect filter:
expect(
findDeployment({ version: '1.3.0+L2', released: false, network: '100' }, _safeL2Deployments),
findDeployment({ version: '1.3.0+L2', released: false, network: '100' }, _SAFE_L2_DEPLOYMENTS),
).toBeUndefined();
expect(
findDeployment({ version: '1.3.0+L2', released: true, network: '0' }, _safeL2Deployments),
findDeployment({ version: '1.3.0+L2', released: true, network: '0' }, _SAFE_L2_DEPLOYMENTS),
).toBeUndefined();
expect(
findDeployment({ version: '2.0.0+L2', released: true, network: '100' }, _safeL2Deployments),
findDeployment({ version: '2.0.0+L2', released: true, network: '100' }, _SAFE_L2_DEPLOYMENTS),
).toBeUndefined();
});
});
Expand All @@ -362,26 +368,44 @@ describe('utils.ts', () => {
const testReleasedDeploymentJson: SingletonDeploymentJSON = {
version: '',
abi: [],
addresses: {
canonical: '0xbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef',
eip155: '0xc0ffeec0ffeec0ffeec0ffeec0ffeec0ffeebabe',
zksync: '0xbabebabebabebabebabebabebabebabebabebabe',
deployments: {
canonical: {
address: '0xbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef',
codeHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
},
eip155: {
address: '0xc0ffeec0ffeec0ffeec0ffeec0ffeec0ffeebabe',
codeHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
},
zksync: {
address: '0xbabebabebabebabebabebabebabebabebabebabe',
codeHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
},
},
networkAddresses: { '1': 'canonical', '100': 'zksync', '101': ['canonical', 'eip155'] },
contractName: '',
codeHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
released: true, // Default filter value
};
const expectedDeployment: SingletonDeploymentV2 = {
addresses: {
canonical: '0xbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef',
deployments: {
canonical: {
address: '0xbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef',
codeHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
},
eip155: {
address: '0xc0ffeec0ffeec0ffeec0ffeec0ffeec0ffeebabe',
codeHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
},
zksync: {
address: '0xbabebabebabebabebabebabebabebabebabebabe',
codeHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
},
},
networkAddresses: {
'1': '0xbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef',
'100': '0xbabebabebabebabebabebabebabebabebabebabe',
'101': ['0xbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef', '0xc0ffeec0ffeec0ffeec0ffeec0ffeec0ffeebabe'],
},
codeHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
released: true,
abi: [],
version: '',
Expand Down
22 changes: 4 additions & 18 deletions src/accessors.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
import SimulateTxAccessor130 from './assets/v1.3.0/simulate_tx_accessor.json';
import SimulateTxAccessor141 from './assets/v1.4.1/simulate_tx_accessor.json';

import {
DeploymentFilter,
SingletonDeploymentJSON,
SingletonDeployment,
DeploymentFormats,
SingletonDeploymentV2,
} from './types';
import { DeploymentFilter, SingletonDeployment, DeploymentFormats, SingletonDeploymentV2 } from './types';
import { findDeployment } from './utils';

// This is a sorted array (newest to oldest)
const accessorDeployments: SingletonDeploymentJSON[] = [
SimulateTxAccessor141 as SingletonDeploymentJSON,
SimulateTxAccessor130 as SingletonDeploymentJSON,
];
import { _ACCESSOR_DEPLOYMENTS } from './deployments';

/**
* Retrieves a single simulate transaction accessor deployment based on the provided filter.
Expand All @@ -23,7 +9,7 @@ const accessorDeployments: SingletonDeploymentJSON[] = [
* @returns {SingletonDeployment | undefined} - The found deployment or undefined if no deployment matches the filter.
*/
export const getSimulateTxAccessorDeployment = (filter?: DeploymentFilter): SingletonDeployment | undefined => {
return findDeployment(filter, accessorDeployments);
return findDeployment(filter, _ACCESSOR_DEPLOYMENTS);
};

/**
Expand All @@ -33,5 +19,5 @@ export const getSimulateTxAccessorDeployment = (filter?: DeploymentFilter): Sing
* @returns {SingletonDeploymentV2 | undefined} - The found deployments in the specified format or undefined if no deployments match the filter.
*/
export const getSimulateTxAccessorDeployments = (filter?: DeploymentFilter): SingletonDeploymentV2 | undefined => {
return findDeployment(filter, accessorDeployments, DeploymentFormats.MULTIPLE);
return findDeployment(filter, _ACCESSOR_DEPLOYMENTS, DeploymentFormats.MULTIPLE);
};
Loading

0 comments on commit 94cf3a1

Please sign in to comment.