Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: onDeploy fuels config supports all Sway program types #3383

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