@massalabs/massa-sc-utils
Version:
Massa smart contract utils
94 lines • 4.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.deploySmartContract = void 0;
const tslib_1 = require("tslib");
const massa_web3_1 = require("@massalabs/massa-web3");
const SmartContractUtils_1 = require("./SmartContractUtils");
const chalk = require("chalk");
const deploySmartContract = (wasmFile, contractData, web3Client, awaitFinalization, deployerAccount) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const header = "=".repeat(process.stdout.columns - 1);
console.log(header);
console.log(`Deploying smart contract...`);
console.log(header);
let deploymentOperationId;
try {
// do checks
if (!deployerAccount) {
deployerAccount = web3Client.wallet().getBaseAccount();
}
// set default values in case of missing ones
contractData.maxGas = contractData.maxGas || 200000;
contractData.fee = contractData.fee || 0;
contractData.datastore = new Map();
// construct a sc utils helper
const utils = new SmartContractUtils_1.SmartContractUtils();
// compile sc from wasm file ready for deployment
console.log(`Running ${chalk.green("compilation")} of smart contract on path ${chalk.yellow(wasmFile)}....`);
let compiledSc;
try {
compiledSc = yield utils.compileSmartContractFromWasmFile(wasmFile);
console.log(`Smart Contract ${chalk.green("successfully")} compiled to deployable bytecode under ${wasmFile}`);
}
catch (ex) {
const msg = chalk.red(`Error compiling smart contract on path ${chalk.yellow(wasmFile)}`);
console.error(msg);
throw new Error(ex);
}
if (!compiledSc.base64) {
const msg = chalk.red(`No bytecode to deploy for wasm file ${chalk.yellow(wasmFile)}. Check AS compiler`);
console.error(msg);
throw new Error(msg);
}
contractData.contractDataBinary = compiledSc.binary;
contractData.contractDataText = compiledSc.text;
const key1 = Uint8Array.from([0, 1, 2, 3, 4]);
contractData.datastore = new Map();
contractData.datastore.set(key1, compiledSc.binary);
// deploy smart contract
console.log(`Running ${chalk.green("deployment")} of smart contract....`);
let deployTxId;
try {
deployTxId = yield web3Client.smartContracts().deploySmartContract(contractData, deployerAccount);
if (deployTxId.length === 0) {
throw new Error(`No transaction ids were produced by the deployment. Something went wrong. Please check your contract and network`);
}
deploymentOperationId = deployTxId[0];
console.log(`Smart Contract ${chalk.green("successfully")} deployed to Massa Network. Operation ID ${chalk.yellow(deploymentOperationId)}`);
}
catch (ex) {
const msg = chalk.red(`Error deploying smart contract ${chalk.yellow(wasmFile)} to Massa Network`);
console.error(msg);
throw new Error(ex);
}
// await final state
if (awaitFinalization) {
console.log(`Awaiting ${chalk.green("FINAL")} transaction status....`);
let status;
try {
status = yield web3Client.smartContracts().awaitRequiredOperationStatus(deploymentOperationId, massa_web3_1.EOperationStatus.FINAL);
console.log(`Transaction with Operation ID ${chalk.yellow(deploymentOperationId)} has reached finality!`);
}
catch (ex) {
const msg = chalk.red(`Error getting finality of transaction ${chalk.yellow(deploymentOperationId)}`);
console.error(msg);
throw new Error(ex);
}
if (status !== massa_web3_1.EOperationStatus.FINAL) {
const msg = chalk.red(`Transaction ${chalk.yellow(deploymentOperationId)} did not reach finality after considerable amount of time. Try redeploying anew`);
console.error(msg);
throw new Error(msg);
}
}
}
catch (ex) {
const msg = chalk.red(`Error deploying smart contract ${chalk.yellow(wasmFile)} to Massa Network`);
console.error(msg);
throw new Error(ex);
}
console.log(header);
console.log(`Smart Contract Deployment finished!`);
console.log(header);
return deploymentOperationId;
});
exports.deploySmartContract = deploySmartContract;
//# sourceMappingURL=SmartContractDeployer.js.map