Skip to content

Commit

Permalink
feat: implement predicates/scripts CLI deploy (#3254)
Browse files Browse the repository at this point in the history
* implement script deploy pseudocode

* fix

* add comment

* refactor

* save script files to disk
  • Loading branch information
Dhaiwat10 authored Oct 5, 2024
1 parent 42d3480 commit ce44041
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 42 deletions.
3 changes: 2 additions & 1 deletion nodemon.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"**/out/release/**",
"apps/demo-typegen/src/contract-types/**",
"apps/demo-typegen/src/predicate-types/**",
"apps/demo-typegen/src/script-types/**"
"apps/demo-typegen/src/script-types/**",
"packages/fuels/src/cli/commands/deploy/proxy/types/**"
]
}
18 changes: 14 additions & 4 deletions packages/contract/src/contract-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,16 @@ export default class ContractFactory {
transactionResult: TransactionResult<TransactionType.Blob>;
loaderBytecode: string;
}>;
blobId: string;
loaderBytecode: Uint8Array;
loaderBytecodeHexlified: string;
}> {
const account = this.getAccount();
if (configurableConstants) {
this.setConfigurableConstants(configurableConstants);
}

// TODO: We think that we should not be setting configurable constants here, but rather when we try to call the script.
// if (configurableConstants) {
// this.setConfigurableConstants(configurableConstants);
// }

// Generate the associated create tx for the loader contract
const blobId = hash(this.bytecode);
Expand Down Expand Up @@ -444,7 +449,12 @@ export default class ContractFactory {
return { transactionResult: result, loaderBytecode: hexlify(loaderBytecode) };
};

return { waitForResult };
return {
waitForResult,
blobId,
loaderBytecode,
loaderBytecodeHexlified: hexlify(loaderBytecode),
};
}

/**
Expand Down
55 changes: 20 additions & 35 deletions packages/fuels/src/cli/commands/deploy/deployScripts.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import type { WalletUnlocked } from '@fuel-ts/account';
import type { DeployContractOptions } from '@fuel-ts/contract';
import { ContractFactory } from '@fuel-ts/contract';
import { debug, log } from 'console';
import { readFileSync } from 'fs';

import type { ForcToml } from '../../config/forcUtils';
import {
getClosestForcTomlDir,
getBinaryPath,
getABIPath,
getStorageSlotsPath,
getContractName,
readForcToml,
} from '../../config/forcUtils';
import { getBinaryPath, getABIPath, getContractName } from '../../config/forcUtils';
import type { FuelsConfig, DeployedScript } from '../../types';

import { createWallet } from './createWallet';
Expand All @@ -21,24 +16,23 @@ export async function deployScript(
wallet: WalletUnlocked,
binaryPath: string,
abiPath: string,
storageSlotsPath: string,
scriptPath: string,
tomlContents: ForcToml
configurableConstants?: DeployContractOptions['configurableConstants']
) {
debug(`Deploying script for ABI: ${abiPath}`);

// Implement script deploy
await Promise.resolve({
wallet,
binaryPath,
abiPath,
storageSlotsPath,
scriptPath,
tomlContents,
});
const bytecode = readFileSync(binaryPath);
const abi = JSON.parse(readFileSync(abiPath, 'utf-8'));
const factory = new ContractFactory(bytecode, abi, wallet);

// TODO: implement me
return 'deployed-blob-id';
const { waitForResult, blobId, loaderBytecode, loaderBytecodeHexlified } =
await factory.deployAsBlobTxForScript(configurableConstants);
await waitForResult();

return {
blobId,
loaderBytecode,
loaderBytecodeHexlified,
};
}

/**
Expand All @@ -55,32 +49,23 @@ export async function deployScripts(config: FuelsConfig) {

for (let i = 0; i < scriptsLen; i++) {
const scriptPath = config.scripts[i];
const forcTomlPath = getClosestForcTomlDir(scriptPath);
const binaryPath = getBinaryPath(scriptPath, config);
const abiPath = getABIPath(scriptPath, config);
const storageSlotsPath = getStorageSlotsPath(scriptPath, config);
const projectName = getContractName(scriptPath);
// const scriptName = getContractCamelCase(scriptPath);
const tomlContents = readForcToml(forcTomlPath);

const blobId = await deployScript(
const { blobId, loaderBytecode, loaderBytecodeHexlified } = await deployScript(
wallet,
binaryPath,
abiPath,
storageSlotsPath,
scriptPath,
tomlContents
abiPath
);

// TODO: implement me
const loaderBytecode = `0x${blobId}`;

debug(`Script deployed: ${projectName} - ${blobId}`);

scripts.push({
path: scriptPath,
blobId,
loaderBytecode,
loaderBytecodeHexlified,
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/fuels/src/cli/commands/deploy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function deploy(config: FuelsConfig) {
* After saving the script files, we need to
* re-generate types for them.
*
* This time, the script will have to binaries:
* This time, the script will have two binaries:
* - the original one
* - the loader one (after deploy)
*
Expand Down
12 changes: 12 additions & 0 deletions packages/fuels/src/cli/commands/deploy/saveScriptFiles.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { writeFileSync } from 'fs';

import { getScriptName } from '../../config/forcUtils';
import type { DeployedScript, FuelsConfig } from '../../types';

export async function saveScriptFiles(scripts: DeployedScript[], _config: FuelsConfig) {
Expand All @@ -9,6 +12,15 @@ export async function saveScriptFiles(scripts: DeployedScript[], _config: FuelsC
* - Hash file for scripts
* - Root file for predicates
*/
const scriptName = getScriptName(path);
const buildMode = _config.buildMode;

const scriptBlobIdPath = `${path}/out/${buildMode}/${scriptName}-deployed-bin-hash`;
writeFileSync(scriptBlobIdPath, blobId);

const loaderBytecodePath = `${path}/out/${buildMode}/${scriptName}-deployed.bin`;
writeFileSync(loaderBytecodePath, loaderBytecode);

await Promise.resolve({ path, blobId, loaderBytecode });
}
}
5 changes: 5 additions & 0 deletions packages/fuels/src/cli/config/forcUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ export function getContractName(contractPath: string) {
return project.name;
}

export function getScriptName(scriptPath: string) {
const { project } = readForcToml(scriptPath);
return project.name;
}

export function getContractCamelCase(contractPath: string) {
const projectName = getContractName(contractPath);
return camelCase(projectName);
Expand Down
3 changes: 2 additions & 1 deletion packages/fuels/src/cli/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export type DeployedContract = {
export type DeployedScript = {
path: string;
blobId: string;
loaderBytecode: string;
loaderBytecode: Uint8Array;
loaderBytecodeHexlified: string;
};

export type ContractDeployOptions = {
Expand Down

0 comments on commit ce44041

Please sign in to comment.