Skip to content

Commit

Permalink
Merge pull request #45 from Quantum3-Labs/33-split-deploy
Browse files Browse the repository at this point in the history
Split deploy.js
  • Loading branch information
0xquantum3labs authored Apr 16, 2024
2 parents 4046b77 + 849edab commit 4f7fbb5
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 116 deletions.
117 changes: 1 addition & 116 deletions packages/snfoundry/scripts_js/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,122 +7,7 @@ const { TransactionStatus } = require("starknet");
const { hash } = require("starknet");

const { CallData } = require("starknet-dev");

const networkName = argv.network;

const { provider, deployer } = networks[networkName];
const deployContract = async (
constructorArgs,
contractName,
exportContractName,
) => {
const compiledContractCasm = JSON.parse(
fs
.readFileSync(
path.resolve(
__dirname,
`../contracts/target/dev/contracts_${contractName}.compiled_contract_class.json`,
),
)
.toString("ascii"),
);

const compiledContractSierra = JSON.parse(
fs
.readFileSync(
path.resolve(
__dirname,
`../contracts/target/dev/contracts_${contractName}.contract_class.json`,
),
)
.toString("ascii"),
);

let classHash;
let existingClass;
let contractAddress;

const precomputedClassHash = hash.computeSierraContractClassHash(
compiledContractSierra,
);
const contractCalldata = new CallData(compiledContractSierra.abi);
const constructorCalldata = constructorArgs
? contractCalldata.compile("constructor", constructorArgs)
: [];
console.log("Deploying Contract ", contractName);

let totalFee = 0n;

try {
const { suggestedMaxFee: estimatedFeeDeclare } =
await deployer.estimateDeclareFee(
{
contract: compiledContractSierra,
casm: compiledContractCasm,
},
{},
);
totalFee = estimatedFeeDeclare * 2n;
} catch (e) {
const { suggestedMaxFee: estimatedFeeDeploy } =
await deployer.estimateDeployFee({
classHash: precomputedClassHash,
constructorCalldata,
});
totalFee = estimatedFeeDeploy * 2n;
}

try {
const tryDeclareAndDeploy = await deployer.declareAndDeploy(
{
contract: compiledContractSierra,
casm: compiledContractCasm,
constructorCalldata,
},
{
maxFee: totalFee * 20n, // this optional max fee serves when error AccountValidation Failed or small fee on public networks , try 5n , 10n, 20n, 50n, 100n
},
);
const debug = await provider.waitForTransaction(
tryDeclareAndDeploy.deploy.transaction_hash,
{
successStates: [TransactionStatus.ACCEPTED_ON_L2],
// retryInterval: 10000, // we can retry in 10 seconds
},
);
classHash = tryDeclareAndDeploy.declare.class_hash;
existingClass = await provider.getClassByHash(classHash);
contractAddress = tryDeclareAndDeploy.deploy.address;
contractAddress = "0x" + contractAddress.slice(2).padStart(64, "0");
} catch (e) {
console.log("Error", e);
}
console.log("Deployed contract ", contractName, " at: ", contractAddress);
const chainIdPath = path.resolve(
__dirname,
`../deployments/${networkName}.json`,
);
let deployments = {};
if (fs.existsSync(chainIdPath)) {
deployments = JSON.parse(fs.readFileSync(chainIdPath).toString());
}

let finalContractName = exportContractName || contractName;

deployments[finalContractName] = {
classHash: classHash,
address: contractAddress,
contract: contractName,
};

fs.writeFileSync(chainIdPath, JSON.stringify(deployments, null, 2));

return {
classHash: classHash,
abi: JSON.stringify(existingClass.abi),
address: contractAddress,
};
};
const deployContract = require("./deploy_contract");

const deployScript = async () => {
// const {
Expand Down
121 changes: 121 additions & 0 deletions packages/snfoundry/scripts_js/deploy_contract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
const fs = require("fs");
const path = require("path");
const networks = require("./helpers/networks");
const {hash, CallData, TransactionStatus} = require("starknet");
const networkName = argv.network;

const {provider, deployer} = networks[networkName];
const deployContract = async (
constructorArgs,
contractName,
exportContractName
) => {
const compiledContractCasm = JSON.parse(
fs
.readFileSync(
path.resolve(
__dirname,
`../contracts/target/dev/contracts_${contractName}.compiled_contract_class.json`
)
)
.toString("ascii")
);

const compiledContractSierra = JSON.parse(
fs
.readFileSync(
path.resolve(
__dirname,
`../contracts/target/dev/contracts_${contractName}.contract_class.json`
)
)
.toString("ascii")
);

let classHash;
let existingClass;
let contractAddress;

const precomputedClassHash = hash.computeSierraContractClassHash(
compiledContractSierra
);
const contractCalldata = new CallData(compiledContractSierra.abi);
const constructorCalldata = constructorArgs
? contractCalldata.compile("constructor", constructorArgs)
: [];
console.log("Deploying Contract ", contractName);

let totalFee = 0n;

try {
const {suggestedMaxFee: estimatedFeeDeclare} =
await deployer.estimateDeclareFee(
{
contract: compiledContractSierra,
casm: compiledContractCasm,
},
{}
);
totalFee = estimatedFeeDeclare * 2n;
} catch (e) {
const {suggestedMaxFee: estimatedFeeDeploy} =
await deployer.estimateDeployFee({
classHash: precomputedClassHash,
constructorCalldata,
});
totalFee = estimatedFeeDeploy * 2n;
}

try {
const tryDeclareAndDeploy = await deployer.declareAndDeploy(
{
contract: compiledContractSierra,
casm: compiledContractCasm,
constructorCalldata,
},
{
maxFee: totalFee * 20n, // this optional max fee serves when error AccountValidation Failed or small fee on public networks , try 5n , 10n, 20n, 50n, 100n
}
);
const debug = await provider.waitForTransaction(
tryDeclareAndDeploy.deploy.transaction_hash,
{
successStates: [TransactionStatus.ACCEPTED_ON_L2],
// retryInterval: 10000, // we can retry in 10 seconds
}
);
classHash = tryDeclareAndDeploy.declare.class_hash;
existingClass = await provider.getClassByHash(classHash);
contractAddress = tryDeclareAndDeploy.deploy.address;
contractAddress = "0x" + contractAddress.slice(2).padStart(64, "0");
} catch (e) {
console.log("Error", e);
}
console.log("Deployed contract ", contractName, " at: ", contractAddress);
const chainIdPath = path.resolve(
__dirname,
`../deployments/${networkName}.json`
);
let deployments = {};
if (fs.existsSync(chainIdPath)) {
deployments = JSON.parse(fs.readFileSync(chainIdPath).toString());
}

let finalContractName = exportContractName || contractName;

deployments[finalContractName] = {
classHash: classHash,
address: contractAddress,
contract: contractName,
};

fs.writeFileSync(chainIdPath, JSON.stringify(deployments, null, 2));

return {
classHash: classHash,
abi: JSON.stringify(existingClass.abi),
address: contractAddress,
};
};

export default deployContract

0 comments on commit 4f7fbb5

Please sign in to comment.