-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Predicate's bin evaluation in Abi Typegen (#968)
- Loading branch information
Showing
4 changed files
with
115 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
packages/abi-typegen/src/utils/collectBinFilePaths.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |