From 9e89e3619faa1f60caba2ea26f69d38463cafad6 Mon Sep 17 00:00:00 2001 From: Ikem Date: Sat, 3 Aug 2024 09:40:41 +0100 Subject: [PATCH 1/6] optimized deploy-contract --- .../snfoundry/scripts-ts/deploy-contract.ts | 150 +++++++++++------- packages/snfoundry/scripts-ts/deploy.ts | 49 +++++- packages/snfoundry/scripts-ts/types.ts | 9 +- 3 files changed, 146 insertions(+), 62 deletions(-) diff --git a/packages/snfoundry/scripts-ts/deploy-contract.ts b/packages/snfoundry/scripts-ts/deploy-contract.ts index a332ee8a3..0282d1d67 100644 --- a/packages/snfoundry/scripts-ts/deploy-contract.ts +++ b/packages/snfoundry/scripts-ts/deploy-contract.ts @@ -3,40 +3,40 @@ import path from "path"; import { networks } from "./helpers/networks"; import yargs from "yargs"; import { - BlockIdentifier, CallData, - hash, stark, RawArgs, - constants, - ec, - validateAndParseAddress, transaction, -} from "starknet"; -import { Network } from "./types"; -import { - LegacyContractClass, - CompiledSierra, extractContractHashes, + DeclareContractPayload, + UniversalDetails, } from "starknet"; +import { DeployContractParams, Network } from "./types"; const argv = yargs(process.argv.slice(2)).argv; const networkName: string = argv["network"]; let deployments = {}; - let deployCalls = []; +let deploymentOptions: UniversalDetails; const { provider, deployer }: Network = networks[networkName]; -const declareIfNot_NotWait = async (payload: any) => { +const declareIfNot_NotWait = async ( + payload: DeclareContractPayload, +) => { const declareContractPayload = extractContractHashes(payload); try { await provider.getClassByHash(declareContractPayload.classHash); } catch (error) { - let { transaction_hash } = await deployer.declare(payload); - if (networkName == "sepolia" || networkName == "mainnet") { - await provider.waitForTransaction(transaction_hash); + try { + const { transaction_hash } = await deployer.declare(payload, deploymentOptions); + if (networkName === "sepolia" || networkName === "mainnet") { + await provider.waitForTransaction(transaction_hash); + } + } catch (e) { + console.error("Error declaring contract:", e); + throw e; } } return { @@ -49,38 +49,65 @@ const deployContract_NotWait = async (payload: { classHash: string; constructorCalldata: RawArgs; }) => { - let { calls, addresses } = transaction.buildUDCCall( - payload, - deployer.address - ); - deployCalls.push(...calls); - return { - contractAddress: addresses[0], - }; + try { + const { calls, addresses } = transaction.buildUDCCall( + payload, + deployer.address + ); + deployCalls.push(...calls); + return { + contractAddress: addresses[0], + }; + } catch (error) { + console.error("Error building UDC call:", error); + throw error; + } }; +/** + * Deploy a contract using the specified parameters. + * + * @param {DeployContractParams} params - The parameters for deploying the contract. + * @param {string} params.contract - The name of the contract to deploy. + * @param {string} [params.contractName] - The name to export the contract as (optional). + * @param {RawArgs} [params.constructorArgs] - The constructor arguments for the contract (optional). + * @param {UniversalDetails} [params.options] - Additional deployment options (optional). + * + * @returns {Promise<{ classHash: string; address: string }>} The deployed contract's class hash and address. + * + * @example + * ///Example usage of deployContract function + * await deployContract({ + * contract: "YourContract", + * contractName: "YourContractExportName", + * constructorArgs: { owner: deployer.address }, + * options: { maxFee: BigInt(1000000000000) } + * }); + */ const deployContract = async ( - constructorArgs: RawArgs, - contractName: string, - exportContractName?: string, - options?: { - maxFee: bigint; - } + params: DeployContractParams ): Promise<{ classHash: string; address: string; }> => { + const { contract, constructorArgs, contractName, options } = params; + deploymentOptions = options; + try { await deployer.getContractVersion(deployer.address); } catch (e) { if (e.toString().includes("Contract not found")) { - throw new Error( - `The wallet you're using to deploy the contract is not deployed in ${networkName} network` - ); + const errorMessage = `The wallet you're using to deploy the contract is not deployed in the ${networkName} network.`; + console.error(errorMessage); + throw new Error(errorMessage); + } else { + console.error("Error getting contract version:", e); + throw e; } } let compiledContractCasm; + let compiledContractSierra; try { compiledContractCasm = JSON.parse( @@ -88,7 +115,7 @@ const deployContract = async ( .readFileSync( path.resolve( __dirname, - `../contracts/target/dev/contracts_${contractName}.compiled_contract_class.json` + `../contracts/target/dev/contracts_${contract}.compiled_contract_class.json` ) ) .toString("ascii") @@ -102,12 +129,12 @@ const deployContract = async ( const match = error.message.match( /\/dev\/(.+?)\.compiled_contract_class/ ); - const contractName = match ? match[1].split("_").pop() : "Unknown"; + const missingContract = match ? match[1].split("_").pop() : "Unknown"; console.error( - `The contract "${contractName}" doesn't exist or is not compiled` + `The contract "${missingContract}" doesn't exist or is not compiled` ); } else { - console.error(error); + console.error("Error reading compiled contract class file:", error); } return { classHash: "", @@ -115,27 +142,37 @@ const deployContract = async ( }; } - const compiledContractSierra = JSON.parse( - fs - .readFileSync( - path.resolve( - __dirname, - `../contracts/target/dev/contracts_${contractName}.contract_class.json` + try { + compiledContractSierra = JSON.parse( + fs + .readFileSync( + path.resolve( + __dirname, + `../contracts/target/dev/contracts_${contract}.contract_class.json` + ) ) - ) - .toString("ascii") - ); + .toString("ascii") + ); + } catch (error) { + console.error("Error reading contract class file:", error); + return { + classHash: "", + address: "", + }; + } const contractCalldata = new CallData(compiledContractSierra.abi); const constructorCalldata = constructorArgs ? contractCalldata.compile("constructor", constructorArgs) : []; - console.log("Deploying Contract ", contractName); + console.log("Deploying Contract ", contract); - let { classHash } = await declareIfNot_NotWait({ - contract: compiledContractSierra, - casm: compiledContractCasm, - }); + let { classHash } = await declareIfNot_NotWait( + { + contract: compiledContractSierra, + casm: compiledContractCasm, + } + ); let randomSalt = stark.randomAddress(); @@ -147,12 +184,12 @@ const deployContract = async ( console.log("Contract Deployed at ", contractAddress); - let finalContractName = exportContractName || contractName; + let finalContractName = contract || contractName; deployments[finalContractName] = { classHash: classHash, address: contractAddress, - contract: contractName, + contract: contract, }; return { @@ -163,17 +200,18 @@ const deployContract = async ( const executeDeployCalls = async () => { try { - let { transaction_hash } = await deployer.execute(deployCalls); + let { transaction_hash } = await deployer.execute(deployCalls, deploymentOptions); console.log("Deploy Calls Executed at ", transaction_hash); - if (networkName == "sepolia" || networkName == "mainnet") { + if (networkName === "sepolia" || networkName === "mainnet") { await provider.waitForTransaction(transaction_hash); } } catch (error) { + console.error("Error executing deploy calls:", error); // split the calls in half and try again recursively if (deployCalls.length > 1) { - let half = deployCalls.length / 2; + let half = Math.ceil(deployCalls.length / 2); let firstHalf = deployCalls.slice(0, half); - let secondHalf = deployCalls.slice(half, deployCalls.length); + let secondHalf = deployCalls.slice(half); deployCalls = firstHalf; await executeDeployCalls(); deployCalls = secondHalf; diff --git a/packages/snfoundry/scripts-ts/deploy.ts b/packages/snfoundry/scripts-ts/deploy.ts index 4489299b2..c23019fd1 100644 --- a/packages/snfoundry/scripts-ts/deploy.ts +++ b/packages/snfoundry/scripts-ts/deploy.ts @@ -5,13 +5,52 @@ import { deployer, } from "./deploy-contract"; +/** + * Deploy a contract using the specified parameters. + * + * @example (deploy contract with contructorArgs) + * const deployScript = async (): Promise => { + * await deployContract( + * { + * contract: "YourContract", + * contractName: "YourContractExportName", + * constructorArgs: { + * owner: deployer.address, + * }, + * options: { + * maxFee: BigInt(1000000000000) + * } + * } + * ); + * }; + * + * @example (deploy contract without contructorArgs) + * const deployScript = async (): Promise => { + * await deployContract( + * { + * contract: "YourContract", + * contractName: "YourContractExportName", + * options: { + * maxFee: BigInt(1000000000000) + * } + * } + * ); + * }; + * + * + * @returns {Promise} + */ const deployScript = async (): Promise => { - await deployContract( - { - owner: deployer.address, // the deployer address is the owner of the contract + await deployContract({ + contract: "YourContract", + contractName: "YourContractExportName", + constructorArgs: { + owner: deployer.address, }, - "YourContract" - ); + // options: { + // maxFee: BigInt(1000000000000), + // }, + }); }; deployScript() diff --git a/packages/snfoundry/scripts-ts/types.ts b/packages/snfoundry/scripts-ts/types.ts index a5965a61b..eabc5c9f4 100644 --- a/packages/snfoundry/scripts-ts/types.ts +++ b/packages/snfoundry/scripts-ts/types.ts @@ -1,4 +1,4 @@ -import { Account, RpcProvider } from "starknet"; +import { Account, RawArgs, RpcProvider, UniversalDetails } from "starknet"; export type Networks = Record<"devnet" | "sepolia" | "mainnet", Network>; @@ -6,3 +6,10 @@ export type Network = { provider: RpcProvider; deployer: Account; }; + +export type DeployContractParams = { + contract: string; + contractName?: string; + constructorArgs?: RawArgs; + options?: UniversalDetails; +}; From d37ae58f19fc33b8c23e6f9de5155ae38b2809a0 Mon Sep 17 00:00:00 2001 From: Ikem Date: Sat, 3 Aug 2024 09:52:46 +0100 Subject: [PATCH 2/6] added Chalk to log. and yarn format --- packages/snfoundry/package.json | 1 + .../snfoundry/scripts-ts/deploy-contract.ts | 52 +++++++++++-------- packages/snfoundry/scripts-ts/deploy.ts | 3 +- yarn.lock | 8 +++ 4 files changed, 41 insertions(+), 23 deletions(-) diff --git a/packages/snfoundry/package.json b/packages/snfoundry/package.json index 17456ef37..52c300531 100644 --- a/packages/snfoundry/package.json +++ b/packages/snfoundry/package.json @@ -23,6 +23,7 @@ "yargs": "^17.7.2" }, "dependencies": { + "chalk": "^5.3.0", "dotenv": "^16.3.1", "envfile": "^6.18.0", "prettier": "^2.8.8", diff --git a/packages/snfoundry/scripts-ts/deploy-contract.ts b/packages/snfoundry/scripts-ts/deploy-contract.ts index 0282d1d67..0802060d2 100644 --- a/packages/snfoundry/scripts-ts/deploy-contract.ts +++ b/packages/snfoundry/scripts-ts/deploy-contract.ts @@ -11,6 +11,7 @@ import { DeclareContractPayload, UniversalDetails, } from "starknet"; +import chalk from "chalk"; import { DeployContractParams, Network } from "./types"; const argv = yargs(process.argv.slice(2)).argv; @@ -22,20 +23,21 @@ let deploymentOptions: UniversalDetails; const { provider, deployer }: Network = networks[networkName]; -const declareIfNot_NotWait = async ( - payload: DeclareContractPayload, -) => { +const declareIfNot_NotWait = async (payload: DeclareContractPayload) => { const declareContractPayload = extractContractHashes(payload); try { await provider.getClassByHash(declareContractPayload.classHash); } catch (error) { try { - const { transaction_hash } = await deployer.declare(payload, deploymentOptions); + const { transaction_hash } = await deployer.declare( + payload, + deploymentOptions + ); if (networkName === "sepolia" || networkName === "mainnet") { await provider.waitForTransaction(transaction_hash); } } catch (e) { - console.error("Error declaring contract:", e); + console.error(chalk.red("Error declaring contract:"), e); throw e; } } @@ -59,7 +61,7 @@ const deployContract_NotWait = async (payload: { contractAddress: addresses[0], }; } catch (error) { - console.error("Error building UDC call:", error); + console.error(chalk.red("Error building UDC call:"), error); throw error; } }; @@ -98,10 +100,10 @@ const deployContract = async ( } catch (e) { if (e.toString().includes("Contract not found")) { const errorMessage = `The wallet you're using to deploy the contract is not deployed in the ${networkName} network.`; - console.error(errorMessage); + console.error(chalk.red(errorMessage)); throw new Error(errorMessage); } else { - console.error("Error getting contract version:", e); + console.error(chalk.red("Error getting contract version:"), e); throw e; } } @@ -131,10 +133,15 @@ const deployContract = async ( ); const missingContract = match ? match[1].split("_").pop() : "Unknown"; console.error( - `The contract "${missingContract}" doesn't exist or is not compiled` + chalk.red( + `The contract "${missingContract}" doesn't exist or is not compiled` + ) ); } else { - console.error("Error reading compiled contract class file:", error); + console.error( + chalk.red("Error reading compiled contract class file:"), + error + ); } return { classHash: "", @@ -154,7 +161,7 @@ const deployContract = async ( .toString("ascii") ); } catch (error) { - console.error("Error reading contract class file:", error); + console.error(chalk.red("Error reading contract class file:"), error); return { classHash: "", address: "", @@ -165,14 +172,12 @@ const deployContract = async ( const constructorCalldata = constructorArgs ? contractCalldata.compile("constructor", constructorArgs) : []; - console.log("Deploying Contract ", contract); + console.log(chalk.yellow("Deploying Contract "), contract); - let { classHash } = await declareIfNot_NotWait( - { - contract: compiledContractSierra, - casm: compiledContractCasm, - } - ); + let { classHash } = await declareIfNot_NotWait({ + contract: compiledContractSierra, + casm: compiledContractCasm, + }); let randomSalt = stark.randomAddress(); @@ -182,7 +187,7 @@ const deployContract = async ( constructorCalldata, }); - console.log("Contract Deployed at ", contractAddress); + console.log(chalk.green("Contract Deployed at "), contractAddress); let finalContractName = contract || contractName; @@ -200,13 +205,16 @@ const deployContract = async ( const executeDeployCalls = async () => { try { - let { transaction_hash } = await deployer.execute(deployCalls, deploymentOptions); - console.log("Deploy Calls Executed at ", transaction_hash); + let { transaction_hash } = await deployer.execute( + deployCalls, + deploymentOptions + ); + console.log(chalk.green("Deploy Calls Executed at "), transaction_hash); if (networkName === "sepolia" || networkName === "mainnet") { await provider.waitForTransaction(transaction_hash); } } catch (error) { - console.error("Error executing deploy calls:", error); + console.error(chalk.red("Error executing deploy calls:"), error); // split the calls in half and try again recursively if (deployCalls.length > 1) { let half = Math.ceil(deployCalls.length / 2); diff --git a/packages/snfoundry/scripts-ts/deploy.ts b/packages/snfoundry/scripts-ts/deploy.ts index c23019fd1..34ec1f26c 100644 --- a/packages/snfoundry/scripts-ts/deploy.ts +++ b/packages/snfoundry/scripts-ts/deploy.ts @@ -1,3 +1,4 @@ +import chalk from "chalk"; import { deployContract, executeDeployCalls, @@ -58,6 +59,6 @@ deployScript() executeDeployCalls().then(() => { exportDeployments(); }); - console.log("All Setup Done"); + console.log(chalk.bgGreen("All Setup Done")); }) .catch(console.error); diff --git a/yarn.lock b/yarn.lock index 48990de1e..eacdf2c65 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1710,6 +1710,7 @@ __metadata: "@types/node": ^20 "@types/prettier": ^2 "@types/yargs": ^17.0.32 + chalk: ^5.3.0 dotenv: ^16.3.1 envfile: ^6.18.0 globals: ^15.8.0 @@ -2929,6 +2930,13 @@ __metadata: languageName: node linkType: hard +"chalk@npm:^5.3.0": + version: 5.3.0 + resolution: "chalk@npm:5.3.0" + checksum: 623922e077b7d1e9dedaea6f8b9e9352921f8ae3afe739132e0e00c275971bdd331268183b2628cf4ab1727c45ea1f28d7e24ac23ce1db1eb653c414ca8a5a80 + languageName: node + linkType: hard + "chokidar@npm:3.3.1": version: 3.3.1 resolution: "chokidar@npm:3.3.1" From 1f9431887f05b4d0ec42b54a01c668ab5152266a Mon Sep 17 00:00:00 2001 From: Ikem Date: Sat, 3 Aug 2024 10:17:40 +0100 Subject: [PATCH 3/6] Revert "added Chalk to log. and yarn format" This reverts commit d37ae58f19fc33b8c23e6f9de5155ae38b2809a0. --- packages/snfoundry/package.json | 1 - .../snfoundry/scripts-ts/deploy-contract.ts | 52 ++++++++----------- packages/snfoundry/scripts-ts/deploy.ts | 3 +- yarn.lock | 8 --- 4 files changed, 23 insertions(+), 41 deletions(-) diff --git a/packages/snfoundry/package.json b/packages/snfoundry/package.json index 52c300531..17456ef37 100644 --- a/packages/snfoundry/package.json +++ b/packages/snfoundry/package.json @@ -23,7 +23,6 @@ "yargs": "^17.7.2" }, "dependencies": { - "chalk": "^5.3.0", "dotenv": "^16.3.1", "envfile": "^6.18.0", "prettier": "^2.8.8", diff --git a/packages/snfoundry/scripts-ts/deploy-contract.ts b/packages/snfoundry/scripts-ts/deploy-contract.ts index 0802060d2..0282d1d67 100644 --- a/packages/snfoundry/scripts-ts/deploy-contract.ts +++ b/packages/snfoundry/scripts-ts/deploy-contract.ts @@ -11,7 +11,6 @@ import { DeclareContractPayload, UniversalDetails, } from "starknet"; -import chalk from "chalk"; import { DeployContractParams, Network } from "./types"; const argv = yargs(process.argv.slice(2)).argv; @@ -23,21 +22,20 @@ let deploymentOptions: UniversalDetails; const { provider, deployer }: Network = networks[networkName]; -const declareIfNot_NotWait = async (payload: DeclareContractPayload) => { +const declareIfNot_NotWait = async ( + payload: DeclareContractPayload, +) => { const declareContractPayload = extractContractHashes(payload); try { await provider.getClassByHash(declareContractPayload.classHash); } catch (error) { try { - const { transaction_hash } = await deployer.declare( - payload, - deploymentOptions - ); + const { transaction_hash } = await deployer.declare(payload, deploymentOptions); if (networkName === "sepolia" || networkName === "mainnet") { await provider.waitForTransaction(transaction_hash); } } catch (e) { - console.error(chalk.red("Error declaring contract:"), e); + console.error("Error declaring contract:", e); throw e; } } @@ -61,7 +59,7 @@ const deployContract_NotWait = async (payload: { contractAddress: addresses[0], }; } catch (error) { - console.error(chalk.red("Error building UDC call:"), error); + console.error("Error building UDC call:", error); throw error; } }; @@ -100,10 +98,10 @@ const deployContract = async ( } catch (e) { if (e.toString().includes("Contract not found")) { const errorMessage = `The wallet you're using to deploy the contract is not deployed in the ${networkName} network.`; - console.error(chalk.red(errorMessage)); + console.error(errorMessage); throw new Error(errorMessage); } else { - console.error(chalk.red("Error getting contract version:"), e); + console.error("Error getting contract version:", e); throw e; } } @@ -133,15 +131,10 @@ const deployContract = async ( ); const missingContract = match ? match[1].split("_").pop() : "Unknown"; console.error( - chalk.red( - `The contract "${missingContract}" doesn't exist or is not compiled` - ) + `The contract "${missingContract}" doesn't exist or is not compiled` ); } else { - console.error( - chalk.red("Error reading compiled contract class file:"), - error - ); + console.error("Error reading compiled contract class file:", error); } return { classHash: "", @@ -161,7 +154,7 @@ const deployContract = async ( .toString("ascii") ); } catch (error) { - console.error(chalk.red("Error reading contract class file:"), error); + console.error("Error reading contract class file:", error); return { classHash: "", address: "", @@ -172,12 +165,14 @@ const deployContract = async ( const constructorCalldata = constructorArgs ? contractCalldata.compile("constructor", constructorArgs) : []; - console.log(chalk.yellow("Deploying Contract "), contract); + console.log("Deploying Contract ", contract); - let { classHash } = await declareIfNot_NotWait({ - contract: compiledContractSierra, - casm: compiledContractCasm, - }); + let { classHash } = await declareIfNot_NotWait( + { + contract: compiledContractSierra, + casm: compiledContractCasm, + } + ); let randomSalt = stark.randomAddress(); @@ -187,7 +182,7 @@ const deployContract = async ( constructorCalldata, }); - console.log(chalk.green("Contract Deployed at "), contractAddress); + console.log("Contract Deployed at ", contractAddress); let finalContractName = contract || contractName; @@ -205,16 +200,13 @@ const deployContract = async ( const executeDeployCalls = async () => { try { - let { transaction_hash } = await deployer.execute( - deployCalls, - deploymentOptions - ); - console.log(chalk.green("Deploy Calls Executed at "), transaction_hash); + let { transaction_hash } = await deployer.execute(deployCalls, deploymentOptions); + console.log("Deploy Calls Executed at ", transaction_hash); if (networkName === "sepolia" || networkName === "mainnet") { await provider.waitForTransaction(transaction_hash); } } catch (error) { - console.error(chalk.red("Error executing deploy calls:"), error); + console.error("Error executing deploy calls:", error); // split the calls in half and try again recursively if (deployCalls.length > 1) { let half = Math.ceil(deployCalls.length / 2); diff --git a/packages/snfoundry/scripts-ts/deploy.ts b/packages/snfoundry/scripts-ts/deploy.ts index 34ec1f26c..c23019fd1 100644 --- a/packages/snfoundry/scripts-ts/deploy.ts +++ b/packages/snfoundry/scripts-ts/deploy.ts @@ -1,4 +1,3 @@ -import chalk from "chalk"; import { deployContract, executeDeployCalls, @@ -59,6 +58,6 @@ deployScript() executeDeployCalls().then(() => { exportDeployments(); }); - console.log(chalk.bgGreen("All Setup Done")); + console.log("All Setup Done"); }) .catch(console.error); diff --git a/yarn.lock b/yarn.lock index eacdf2c65..48990de1e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1710,7 +1710,6 @@ __metadata: "@types/node": ^20 "@types/prettier": ^2 "@types/yargs": ^17.0.32 - chalk: ^5.3.0 dotenv: ^16.3.1 envfile: ^6.18.0 globals: ^15.8.0 @@ -2930,13 +2929,6 @@ __metadata: languageName: node linkType: hard -"chalk@npm:^5.3.0": - version: 5.3.0 - resolution: "chalk@npm:5.3.0" - checksum: 623922e077b7d1e9dedaea6f8b9e9352921f8ae3afe739132e0e00c275971bdd331268183b2628cf4ab1727c45ea1f28d7e24ac23ce1db1eb653c414ca8a5a80 - languageName: node - linkType: hard - "chokidar@npm:3.3.1": version: 3.3.1 resolution: "chokidar@npm:3.3.1" From 1506a23f7ef3b0f76128b6ff8cea411c01d99c39 Mon Sep 17 00:00:00 2001 From: Ikem Date: Sat, 3 Aug 2024 10:31:22 +0100 Subject: [PATCH 4/6] used custom color for log colorizing --- .../snfoundry/scripts-ts/deploy-contract.ts | 49 ++++++++++--------- packages/snfoundry/scripts-ts/deploy.ts | 12 +++-- .../scripts-ts/helpers/colorize-log.ts | 16 ++++++ 3 files changed, 51 insertions(+), 26 deletions(-) create mode 100644 packages/snfoundry/scripts-ts/helpers/colorize-log.ts diff --git a/packages/snfoundry/scripts-ts/deploy-contract.ts b/packages/snfoundry/scripts-ts/deploy-contract.ts index 0282d1d67..da037b497 100644 --- a/packages/snfoundry/scripts-ts/deploy-contract.ts +++ b/packages/snfoundry/scripts-ts/deploy-contract.ts @@ -12,6 +12,7 @@ import { UniversalDetails, } from "starknet"; import { DeployContractParams, Network } from "./types"; +import { green, red, yellow } from "./helpers/colorize-log"; const argv = yargs(process.argv.slice(2)).argv; const networkName: string = argv["network"]; @@ -22,20 +23,21 @@ let deploymentOptions: UniversalDetails; const { provider, deployer }: Network = networks[networkName]; -const declareIfNot_NotWait = async ( - payload: DeclareContractPayload, -) => { +const declareIfNot_NotWait = async (payload: DeclareContractPayload) => { const declareContractPayload = extractContractHashes(payload); try { await provider.getClassByHash(declareContractPayload.classHash); } catch (error) { try { - const { transaction_hash } = await deployer.declare(payload, deploymentOptions); + const { transaction_hash } = await deployer.declare( + payload, + deploymentOptions + ); if (networkName === "sepolia" || networkName === "mainnet") { await provider.waitForTransaction(transaction_hash); } } catch (e) { - console.error("Error declaring contract:", e); + console.error(red("Error declaring contract:"), e); throw e; } } @@ -59,7 +61,7 @@ const deployContract_NotWait = async (payload: { contractAddress: addresses[0], }; } catch (error) { - console.error("Error building UDC call:", error); + console.error(red("Error building UDC call:"), error); throw error; } }; @@ -98,10 +100,10 @@ const deployContract = async ( } catch (e) { if (e.toString().includes("Contract not found")) { const errorMessage = `The wallet you're using to deploy the contract is not deployed in the ${networkName} network.`; - console.error(errorMessage); + console.error(red(errorMessage)); throw new Error(errorMessage); } else { - console.error("Error getting contract version:", e); + console.error(red("Error getting contract version: "), e); throw e; } } @@ -131,10 +133,12 @@ const deployContract = async ( ); const missingContract = match ? match[1].split("_").pop() : "Unknown"; console.error( - `The contract "${missingContract}" doesn't exist or is not compiled` + red( + `The contract "${missingContract}" doesn't exist or is not compiled` + ) ); } else { - console.error("Error reading compiled contract class file:", error); + console.error(red("Error reading compiled contract class file: "), error); } return { classHash: "", @@ -154,7 +158,7 @@ const deployContract = async ( .toString("ascii") ); } catch (error) { - console.error("Error reading contract class file:", error); + console.error(red("Error reading contract class file: "), error); return { classHash: "", address: "", @@ -165,14 +169,12 @@ const deployContract = async ( const constructorCalldata = constructorArgs ? contractCalldata.compile("constructor", constructorArgs) : []; - console.log("Deploying Contract ", contract); + console.log(yellow("Deploying Contract "), contract); - let { classHash } = await declareIfNot_NotWait( - { - contract: compiledContractSierra, - casm: compiledContractCasm, - } - ); + let { classHash } = await declareIfNot_NotWait({ + contract: compiledContractSierra, + casm: compiledContractCasm, + }); let randomSalt = stark.randomAddress(); @@ -182,7 +184,7 @@ const deployContract = async ( constructorCalldata, }); - console.log("Contract Deployed at ", contractAddress); + console.log(green("Contract Deployed at "), contractAddress); let finalContractName = contract || contractName; @@ -200,13 +202,16 @@ const deployContract = async ( const executeDeployCalls = async () => { try { - let { transaction_hash } = await deployer.execute(deployCalls, deploymentOptions); - console.log("Deploy Calls Executed at ", transaction_hash); + let { transaction_hash } = await deployer.execute( + deployCalls, + deploymentOptions + ); + console.log(green("Deploy Calls Executed at "), transaction_hash); if (networkName === "sepolia" || networkName === "mainnet") { await provider.waitForTransaction(transaction_hash); } } catch (error) { - console.error("Error executing deploy calls:", error); + console.error(red("Error executing deploy calls: "), error); // split the calls in half and try again recursively if (deployCalls.length > 1) { let half = Math.ceil(deployCalls.length / 2); diff --git a/packages/snfoundry/scripts-ts/deploy.ts b/packages/snfoundry/scripts-ts/deploy.ts index c23019fd1..255a8e8b3 100644 --- a/packages/snfoundry/scripts-ts/deploy.ts +++ b/packages/snfoundry/scripts-ts/deploy.ts @@ -4,6 +4,7 @@ import { exportDeployments, deployer, } from "./deploy-contract"; +import { green } from "./helpers/colorize-log"; /** * Deploy a contract using the specified parameters. @@ -55,9 +56,12 @@ const deployScript = async (): Promise => { deployScript() .then(() => { - executeDeployCalls().then(() => { - exportDeployments(); - }); - console.log("All Setup Done"); + executeDeployCalls() + .then(() => { + exportDeployments(); + }) + .finally(() => { + console.log(green("All Setup Done")); + }); }) .catch(console.error); diff --git a/packages/snfoundry/scripts-ts/helpers/colorize-log.ts b/packages/snfoundry/scripts-ts/helpers/colorize-log.ts new file mode 100644 index 000000000..1e37792c2 --- /dev/null +++ b/packages/snfoundry/scripts-ts/helpers/colorize-log.ts @@ -0,0 +1,16 @@ +const colors = { + reset: "\x1b[0m", + red: "\x1b[31m", + green: "\x1b[32m", + yellow: "\x1b[33m", +}; + +const colorize = (color: string, message: string): string => { + return `${color}${message}${colors.reset}`; +}; + +export const red = (message: string): string => colorize(colors.red, message); +export const green = (message: string): string => + colorize(colors.green, message); +export const yellow = (message: string): string => + colorize(colors.yellow, message); From 8b90b4467e1d956345450c4a86dbebf398a4ff02 Mon Sep 17 00:00:00 2001 From: Ikem Date: Sat, 3 Aug 2024 10:56:58 +0100 Subject: [PATCH 5/6] resolve comments --- packages/snfoundry/scripts-ts/deploy-contract.ts | 2 +- packages/snfoundry/scripts-ts/deploy.ts | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/snfoundry/scripts-ts/deploy-contract.ts b/packages/snfoundry/scripts-ts/deploy-contract.ts index da037b497..6915cae6c 100644 --- a/packages/snfoundry/scripts-ts/deploy-contract.ts +++ b/packages/snfoundry/scripts-ts/deploy-contract.ts @@ -186,7 +186,7 @@ const deployContract = async ( console.log(green("Contract Deployed at "), contractAddress); - let finalContractName = contract || contractName; + let finalContractName = contractName || contract; deployments[finalContractName] = { classHash: classHash, diff --git a/packages/snfoundry/scripts-ts/deploy.ts b/packages/snfoundry/scripts-ts/deploy.ts index 255a8e8b3..80c67ade0 100644 --- a/packages/snfoundry/scripts-ts/deploy.ts +++ b/packages/snfoundry/scripts-ts/deploy.ts @@ -44,13 +44,9 @@ import { green } from "./helpers/colorize-log"; const deployScript = async (): Promise => { await deployContract({ contract: "YourContract", - contractName: "YourContractExportName", constructorArgs: { owner: deployer.address, }, - // options: { - // maxFee: BigInt(1000000000000), - // }, }); }; From 8e18d22a4a536f0f8e8778cd677a2ae77ce98165 Mon Sep 17 00:00:00 2001 From: Ikem Date: Sat, 3 Aug 2024 11:42:54 +0100 Subject: [PATCH 6/6] added error boundary for `executeDeployCalls` and resolved comments --- .../snfoundry/scripts-ts/deploy-contract.ts | 40 ++++++++++--------- packages/snfoundry/scripts-ts/deploy.ts | 13 +++--- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/packages/snfoundry/scripts-ts/deploy-contract.ts b/packages/snfoundry/scripts-ts/deploy-contract.ts index 6915cae6c..cbfbe58df 100644 --- a/packages/snfoundry/scripts-ts/deploy-contract.ts +++ b/packages/snfoundry/scripts-ts/deploy-contract.ts @@ -19,20 +19,19 @@ const networkName: string = argv["network"]; let deployments = {}; let deployCalls = []; -let deploymentOptions: UniversalDetails; const { provider, deployer }: Network = networks[networkName]; -const declareIfNot_NotWait = async (payload: DeclareContractPayload) => { +const declareIfNot_NotWait = async ( + payload: DeclareContractPayload, + options?: UniversalDetails +) => { const declareContractPayload = extractContractHashes(payload); try { await provider.getClassByHash(declareContractPayload.classHash); } catch (error) { try { - const { transaction_hash } = await deployer.declare( - payload, - deploymentOptions - ); + const { transaction_hash } = await deployer.declare(payload, options); if (networkName === "sepolia" || networkName === "mainnet") { await provider.waitForTransaction(transaction_hash); } @@ -93,7 +92,6 @@ const deployContract = async ( address: string; }> => { const { contract, constructorArgs, contractName, options } = params; - deploymentOptions = options; try { await deployer.getContractVersion(deployer.address); @@ -171,10 +169,13 @@ const deployContract = async ( : []; console.log(yellow("Deploying Contract "), contract); - let { classHash } = await declareIfNot_NotWait({ - contract: compiledContractSierra, - casm: compiledContractCasm, - }); + let { classHash } = await declareIfNot_NotWait( + { + contract: compiledContractSierra, + casm: compiledContractCasm, + }, + options + ); let randomSalt = stark.randomAddress(); @@ -200,12 +201,15 @@ const deployContract = async ( }; }; -const executeDeployCalls = async () => { - try { - let { transaction_hash } = await deployer.execute( - deployCalls, - deploymentOptions +const executeDeployCalls = async (options?: UniversalDetails) => { + if (deployCalls.length < 1) { + throw new Error( + red("Aborted: No contract to deploy. Please prepare the contracts with `deployContract`") ); + } + + try { + let { transaction_hash } = await deployer.execute(deployCalls, options); console.log(green("Deploy Calls Executed at "), transaction_hash); if (networkName === "sepolia" || networkName === "mainnet") { await provider.waitForTransaction(transaction_hash); @@ -218,9 +222,9 @@ const executeDeployCalls = async () => { let firstHalf = deployCalls.slice(0, half); let secondHalf = deployCalls.slice(half); deployCalls = firstHalf; - await executeDeployCalls(); + await executeDeployCalls(options); deployCalls = secondHalf; - await executeDeployCalls(); + await executeDeployCalls(options); } } }; diff --git a/packages/snfoundry/scripts-ts/deploy.ts b/packages/snfoundry/scripts-ts/deploy.ts index 80c67ade0..101aa7cfe 100644 --- a/packages/snfoundry/scripts-ts/deploy.ts +++ b/packages/snfoundry/scripts-ts/deploy.ts @@ -51,13 +51,10 @@ const deployScript = async (): Promise => { }; deployScript() - .then(() => { - executeDeployCalls() - .then(() => { - exportDeployments(); - }) - .finally(() => { - console.log(green("All Setup Done")); - }); + .then(async () => { + await executeDeployCalls(); + exportDeployments(); + + console.log(green("All Setup Done")); }) .catch(console.error);