Skip to content

Commit

Permalink
fix: Predicate's bin evaluation in Abi Typegen (#968)
Browse files Browse the repository at this point in the history
  • Loading branch information
arboleya authored May 3, 2023
1 parent 2e64857 commit e51a26d
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/fair-meals-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/abi-typegen": patch
---

Fixing Predicate's bin evaluation in Abi Typegen
25 changes: 4 additions & 21 deletions packages/abi-typegen/src/runTypegen.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { hexlify } from '@ethersproject/bytes';
import { existsSync, readFileSync, writeFileSync } from 'fs';
import { readFileSync, writeFileSync } from 'fs';
import { sync as globSync } from 'glob';
import mkdirp from 'mkdirp';
import { basename } from 'path';
import rimraf from 'rimraf';

import { AbiTypeGen } from './AbiTypeGen';
import { ProgramTypeEnum } from './types/enums/ProgramTypeEnum';
import type { ProgramTypeEnum } from './types/enums/ProgramTypeEnum';
import type { IFile } from './types/interfaces/IFile';
import { validateBinFile } from './utils/validateBinFile';
import { collectBinFilepaths } from './utils/collectBinFilePaths';

export interface IGenerateFilesParams {
cwd: string;
Expand Down Expand Up @@ -53,23 +52,7 @@ export function runTypegen(params: IGenerateFilesParams) {
return abi;
});

const isScript = programType === ProgramTypeEnum.SCRIPT;

const binFiles = !isScript
? []
: filepaths.map((abiFilepath) => {
const binFilepath = abiFilepath.replace('-abi.json', '.bin');
const binExists = existsSync(binFilepath);

validateBinFile({ abiFilepath, binFilepath, binExists, programType });

const bin: IFile = {
path: binFilepath,
contents: hexlify(readFileSync(binFilepath)),
};

return bin;
});
const binFiles = collectBinFilepaths({ filepaths, programType });

/*
Starting the engine
Expand Down
70 changes: 70 additions & 0 deletions packages/abi-typegen/src/utils/collectBinFilePaths.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { hexlify } from '@ethersproject/bytes';
import { readFileSync } from 'fs';

import { getProjectResources, ForcProjectsEnum } from '../../test/fixtures/forc-projects';
import { ProgramTypeEnum } from '../types/enums/ProgramTypeEnum';

import { collectBinFilepaths } from './collectBinFilePaths';
import * as validateBinFileMod from './validateBinFile';

describe('collectBinFilePaths.ts', () => {
const script = getProjectResources(ForcProjectsEnum.SCRIPT);
const predicate = getProjectResources(ForcProjectsEnum.PREDICATE);
const contract = getProjectResources(ForcProjectsEnum.MINIMAL);

function mockDeps() {
const validateBinFile = jest.spyOn(validateBinFileMod, 'validateBinFile').mockImplementation();

return { validateBinFile };
}

afterEach(jest.restoreAllMocks);

test('should collect bin files for Scripts', () => {
const { validateBinFile } = mockDeps();

const params = {
filepaths: [script.abiPath],
programType: ProgramTypeEnum.SCRIPT,
};

const binFilepaths = collectBinFilepaths(params);

const path = script.abiPath.replace('-abi.json', '.bin');
const contents = hexlify(readFileSync(path));

expect(binFilepaths).toStrictEqual([{ contents, path }]);
expect(validateBinFile).toHaveBeenCalledTimes(1);
});

test('should collect bin files for Predicates', () => {
const { validateBinFile } = mockDeps();

const params = {
filepaths: [predicate.abiPath],
programType: ProgramTypeEnum.PREDICATE,
};

const path = predicate.abiPath.replace('-abi.json', '.bin');
const contents = hexlify(readFileSync(path));

const binFilepaths = collectBinFilepaths(params);

expect(binFilepaths).toStrictEqual([{ contents, path }]);
expect(validateBinFile).toHaveBeenCalledTimes(1);
});

test('should not collect bin files for Contracts', () => {
const { validateBinFile } = mockDeps();

const params = {
filepaths: [contract.abiPath],
programType: ProgramTypeEnum.CONTRACT,
};

const binFilepaths = collectBinFilepaths(params);

expect(binFilepaths).toStrictEqual([]); // empty array
expect(validateBinFile).toHaveBeenCalledTimes(0); // zero calls this time
});
});
36 changes: 36 additions & 0 deletions packages/abi-typegen/src/utils/collectBinFilePaths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { hexlify } from '@ethersproject/bytes';
import { existsSync, readFileSync } from 'fs';

import { ProgramTypeEnum } from '../types/enums/ProgramTypeEnum';
import type { IFile } from '../types/interfaces/IFile';

import { validateBinFile } from './validateBinFile';

export const collectBinFilepaths = (params: {
filepaths: string[];
programType: ProgramTypeEnum;
}) => {
const { filepaths, programType } = params;

const isContract = programType === ProgramTypeEnum.CONTRACT;
if (isContract) {
return []; // bin files not needed
}

// validate and collect bin filepaths for Scripts and/or Predicates
const binFiles = filepaths.map((abiFilepath) => {
const binFilepath = abiFilepath.replace('-abi.json', '.bin');
const binExists = existsSync(binFilepath);

validateBinFile({ abiFilepath, binFilepath, binExists, programType });

const bin: IFile = {
path: binFilepath,
contents: hexlify(readFileSync(binFilepath)),
};

return bin;
});

return binFiles;
};

0 comments on commit e51a26d

Please sign in to comment.