forked from Scaffold-Stark/scaffold-stark-2
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from Quantum3-Labs/33-split-deploy
Split deploy.js
- Loading branch information
Showing
2 changed files
with
122 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |