Skip to content

Commit

Permalink
feat!: onDeploy fuels config supports all Sway program types (#3383)
Browse files Browse the repository at this point in the history
* feat(fuels): onDeploy callback updated to support automation with scripts and predicates additionally to contracts

* Update .changeset/khaki-trees-wink.md

Co-authored-by: Peter Smith <[email protected]>

* Update .changeset/khaki-trees-wink.md

Co-authored-by: Peter Smith <[email protected]>

---------

Co-authored-by: Peter Smith <[email protected]>
Co-authored-by: Nedim Salkić <[email protected]>
  • Loading branch information
3 people authored Nov 14, 2024
1 parent 0585ee4 commit 5dee7ff
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-trees-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fuels": minor
---

feat!: `onDeploy` fuels config supports all Sway program types
6 changes: 3 additions & 3 deletions apps/demo-fuels/fuels.config.full.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */
import { createConfig } from 'fuels';
import type { ContractDeployOptions, DeployedContract, FuelsConfig } from 'fuels';
import type { ContractDeployOptions, DeployedData, FuelsConfig } from 'fuels';

const MY_FIRST_DEPLOYED_CONTRACT_NAME = '';

Expand Down Expand Up @@ -91,9 +91,9 @@ export default createConfig({
// #endregion onBuild

// #region onDeploy
// #import { DeployedContract, FuelsConfig };
// #import { DeployedData, FuelsConfig };

onDeploy: (config: FuelsConfig, data: DeployedContract[]) => {
onDeploy: (config: FuelsConfig, data: DeployedData) => {
console.log('fuels:onDeploy', { config, data });
},
// #endregion onDeploy
Expand Down
13 changes: 9 additions & 4 deletions packages/fuels/src/cli/commands/deploy/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Wallet } from '@fuel-ts/account';

import { fuelsConfig } from '../../../../test/fixtures/fuels.config';
import { launchTestNode } from '../../../test-utils';
import type { DeployedContract } from '../../types';
import type { DeployedData } from '../../types';

import { deploy } from '.';
import * as createWalletMod from './createWallet';
Expand Down Expand Up @@ -32,11 +32,16 @@ describe('deploy', () => {
// TODO: Fix this test
test.skip('should call onDeploy callback', async () => {
const { onDeploy } = await mockAll();
const expectedContracts: DeployedContract[] = [];
const config = { ...fuelsConfig, contracts: [], onDeploy };
const expectedData: DeployedData = {
contracts: [],
scripts: [],
predicates: [],
};

const config = { ...fuelsConfig, contracts: [], scripts: [], predicates: [], onDeploy };

await deploy(config);

expect(onDeploy).toHaveBeenCalledWith(config, expectedContracts);
expect(onDeploy).toHaveBeenCalledWith(config, expectedData);
});
});
18 changes: 13 additions & 5 deletions packages/fuels/src/cli/commands/deploy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ export async function deploy(config: FuelsConfig) {
/**
* Deploy contract and save their IDs to JSON file.
*/
const contractIds = await deployContracts(config);
await saveContractIds(contractIds, config.output);

config.onDeploy?.(config, contractIds);
const contracts = await deployContracts(config);
await saveContractIds(contracts, config.output);

/**
* Deploy scripts and save deployed files to disk.
Expand All @@ -29,11 +27,21 @@ export async function deploy(config: FuelsConfig) {
const predicates = await deployPredicates(config);
savePredicateFiles(predicates, config);

config.onDeploy?.(config, {
contracts,
scripts,
predicates,
});

/**
* After deploying scripts/predicates, we need to
* re-generate factory classe with the loader coee
*/
await generateTypes(config);

return contractIds;
return {
contracts,
scripts,
predicates,
};
}
6 changes: 5 additions & 1 deletion packages/fuels/src/cli/commands/dev/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ describe('dev', () => {
.mockReturnValue(Promise.resolve(fuelsConfig));

const build = vi.spyOn(buildMod, 'build').mockResolvedValue();
const deploy = vi.spyOn(deployMod, 'deploy').mockResolvedValue([]);
const deploy = vi.spyOn(deployMod, 'deploy').mockResolvedValue({
contracts: [],
scripts: [],
predicates: [],
});

return {
autoStartFuelCore,
Expand Down
6 changes: 5 additions & 1 deletion packages/fuels/src/cli/commands/withConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ describe('withConfig', () => {
if (params?.shouldErrorOnDeploy) {
throw new Error('Something happened');
}
return Promise.resolve([]);
return Promise.resolve({
contracts: [],
scripts: [],
predicates: [],
});
});

const { error } = mockLogger();
Expand Down
8 changes: 7 additions & 1 deletion packages/fuels/src/cli/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type CommandEvent =
}
| {
type: Commands.deploy;
data: DeployedContract[];
data: DeployedData;
}
| {
type: Commands.dev;
Expand Down Expand Up @@ -51,6 +51,12 @@ export type DeployedPredicate = DeployedScript & {
predicateRoot: string;
};

export type DeployedData = {
contracts?: DeployedContract[];
scripts?: DeployedScript[];
predicates?: DeployedPredicate[];
};

export type ContractDeployOptions = {
contracts: DeployedContract[];
contractName: string;
Expand Down
6 changes: 5 additions & 1 deletion packages/fuels/test/features/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ describe('build', { timeout: 180000 }, () => {

function mockAll() {
const { autoStartFuelCore, killChildProcess } = mockStartFuelCore();
const deploy = vi.spyOn(deployMod, 'deploy').mockResolvedValue([]);
const deploy = vi.spyOn(deployMod, 'deploy').mockResolvedValue({
contracts: [],
scripts: [],
predicates: [],
});

return { autoStartFuelCore, killChildProcess, deploy };
}
Expand Down
8 changes: 7 additions & 1 deletion packages/fuels/test/features/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ describe('dev', () => {
const { autoStartFuelCore, killChildProcess } = mockStartFuelCore();

const build = vi.spyOn(buildMod, 'build').mockReturnValue(Promise.resolve());
const deploy = vi.spyOn(deployMod, 'deploy').mockReturnValue(Promise.resolve([]));
const deploy = vi.spyOn(deployMod, 'deploy').mockReturnValue(
Promise.resolve({
contracts: [],
scripts: [],
predicates: [],
})
);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const on: any = vi.fn(() => ({ on }));
Expand Down

0 comments on commit 5dee7ff

Please sign in to comment.