-
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.
feat: auto-loading storage slots based on ABI filepath (#1346)
- Loading branch information
Showing
13 changed files
with
166 additions
and
23 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 | ||
--- | ||
|
||
Auto-loading `*-storage_slots.json` based on `*-abi.json` filepaths |
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
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
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
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
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
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
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 |
---|---|---|
@@ -1,29 +1,43 @@ | ||
{{header}} | ||
|
||
import { Interface, Contract, ContractFactory } from "fuels"; | ||
import type { Provider, Account, AbstractAddress, BytesLike, DeployContractOptions } from "fuels"; | ||
import type { Provider, Account, AbstractAddress, BytesLike, DeployContractOptions, StorageSlot } from "fuels"; | ||
import type { {{capitalizedName}}, {{capitalizedName}}Interface } from "../{{capitalizedName}}"; | ||
|
||
const _abi = {{abiJsonString}} | ||
const _abi = {{abiJsonString}}; | ||
|
||
const _storageSlots: StorageSlot[] = {{storageSlotsJsonString}}; | ||
|
||
export class {{capitalizedName}}__factory { | ||
static readonly abi = _abi | ||
static readonly abi = _abi; | ||
|
||
static readonly storageSlots = _storageSlots; | ||
|
||
static createInterface(): {{capitalizedName}}Interface { | ||
return new Interface(_abi) as unknown as {{capitalizedName}}Interface | ||
} | ||
|
||
static connect( | ||
id: string | AbstractAddress, | ||
accountOrProvider: Account | Provider | ||
): {{capitalizedName}} { | ||
return new Contract(id, _abi, accountOrProvider) as unknown as {{capitalizedName}} | ||
} | ||
|
||
static async deployContract( | ||
bytecode: BytesLike, | ||
wallet: Account, | ||
options: DeployContractOptions = {} | ||
): Promise<{{capitalizedName}}> { | ||
const factory = new ContractFactory(bytecode, _abi, wallet); | ||
const contract = await factory.deployContract(options); | ||
|
||
const { storageSlots } = {{capitalizedName}}__factory; | ||
|
||
const contract = await factory.deployContract({ | ||
storageSlots, | ||
...options, | ||
}); | ||
|
||
return contract as unknown as {{capitalizedName}}; | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
packages/abi-typegen/src/utils/collectStorageSlotsFilePaths.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,33 @@ | ||
import { getProjectResources, ForcProjectsEnum } from '../../test/fixtures/forc-projects'; | ||
import { ProgramTypeEnum } from '../types/enums/ProgramTypeEnum'; | ||
|
||
import { collectStorageSlotsFilepaths } from './collectStorageSlotsFilePaths'; | ||
|
||
describe('collectStorageSlotsFilePaths.ts', () => { | ||
const script = getProjectResources(ForcProjectsEnum.SCRIPT); | ||
const predicate = getProjectResources(ForcProjectsEnum.PREDICATE); | ||
const contract = getProjectResources(ForcProjectsEnum.MINIMAL); | ||
|
||
afterEach(jest.restoreAllMocks); | ||
|
||
test('should collect storage slot files', () => { | ||
const contractStorageSlots = collectStorageSlotsFilepaths({ | ||
filepaths: [contract.abiPath], | ||
programType: ProgramTypeEnum.CONTRACT, | ||
}); | ||
|
||
const predicateStorageSlots = collectStorageSlotsFilepaths({ | ||
filepaths: [predicate.abiPath], | ||
programType: ProgramTypeEnum.PREDICATE, | ||
}); | ||
|
||
const scriptStorageSlots = collectStorageSlotsFilepaths({ | ||
filepaths: [script.abiPath], | ||
programType: ProgramTypeEnum.SCRIPT, | ||
}); | ||
|
||
expect(contractStorageSlots.length).toEqual(1); | ||
expect(predicateStorageSlots.length).toEqual(0); | ||
expect(scriptStorageSlots.length).toEqual(0); | ||
}); | ||
}); |
35 changes: 35 additions & 0 deletions
35
packages/abi-typegen/src/utils/collectStorageSlotsFilePaths.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,35 @@ | ||
import { existsSync, readFileSync } from 'fs'; | ||
|
||
import { ProgramTypeEnum } from '../types/enums/ProgramTypeEnum'; | ||
import type { IFile } from '../types/interfaces/IFile'; | ||
|
||
export const collectStorageSlotsFilepaths = (params: { | ||
filepaths: string[]; | ||
programType: ProgramTypeEnum; | ||
}) => { | ||
const { filepaths, programType } = params; | ||
|
||
// collect filepaths for storage slots JSON files | ||
const storageSlotsFiles: IFile[] = []; | ||
|
||
// abort unless we're dealing with contract types | ||
if (programType !== ProgramTypeEnum.CONTRACT) { | ||
return storageSlotsFiles; | ||
} | ||
|
||
filepaths.forEach((abiFilepath) => { | ||
const storageSlotsFilepath = abiFilepath.replace('-abi.json', '-storage_slots.json'); | ||
const storageSlotsExists = existsSync(storageSlotsFilepath); | ||
|
||
if (storageSlotsExists) { | ||
const storageSlots: IFile = { | ||
path: storageSlotsFilepath, | ||
contents: readFileSync(storageSlotsFilepath, 'utf-8'), | ||
}; | ||
|
||
storageSlotsFiles.push(storageSlots); | ||
} | ||
}); | ||
|
||
return storageSlotsFiles; | ||
}; |
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
Oops, something went wrong.