UNPKG

hardhat-scilla-plugin

Version:
229 lines 9.26 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.deployLibrary = exports.initZilliqa = exports.setup = void 0; exports.updateSetup = updateSetup; exports.setAccount = setAccount; exports.deploy = deploy; exports.deployFromFile = deployFromFile; exports.compressContract = compressContract; // This is necessary so that tsc can resolve some of the indirect types for // sc_call, otherwise it errors out - richard@zilliqa.com 2023-03-09 const account_1 = require("@zilliqa-js/account"); const util_1 = require("@zilliqa-js/util"); const zilliqa_1 = require("@zilliqa-js/zilliqa"); const fs_1 = __importDefault(require("fs")); const plugins_1 = require("hardhat/plugins"); const ScillaContractProxy = __importStar(require("../parser/ScillaContractProxy")); const ScillaParser_1 = require("../parser/ScillaParser"); exports.setup = null; // The optional params are listed in popularity order. const initZilliqa = (zilliqaNetworkUrl, chainId, privateKeys, attempts = 10, timeoutMs = 1000, gasPriceQa = 2000, gasLimit = 50000) => { const zilliqaObject = new zilliqa_1.Zilliqa(zilliqaNetworkUrl); const accounts = []; privateKeys.forEach((pk) => { zilliqaObject.wallet.addByPrivateKey(pk); accounts.push(new account_1.Account(pk)); }); exports.setup = { zilliqa: zilliqaObject, version: util_1.bytes.pack(chainId, 1), gasPrice: util_1.units.toQa(gasPriceQa.toString(), util_1.units.Units.Li), gasLimit: util_1.Long.fromNumber(gasLimit), attempts, timeout: timeoutMs, accounts, }; return exports.setup; }; exports.initZilliqa = initZilliqa; function read(f) { const t = fs_1.default.readFileSync(f, "utf8"); return t; } /// Allows you to change setup parameters. Available params: /// gasPrice, gasLimit, attempts, timeout. function updateSetup(args) { if (exports.setup === null) { throw new plugins_1.HardhatPluginError("hardhat-scilla-plugin", "Please call the initZilliqa function."); } const overrides = {}; if (args.gasPrice) { overrides.gasPrice = util_1.units.toQa(args.gasPrice.toString(), util_1.units.Units.Li); } if (args.gasLimit) { overrides.gasLimit = util_1.Long.fromNumber(args.gasLimit); } if (args.timeout) { overrides.timeout = args.timeout; } if (args.attempts) { overrides.attempts = args.attempts; } const newSetup = { ...exports.setup, ...overrides }; exports.setup = newSetup; } function setAccount(account) { if (exports.setup === null) { throw new plugins_1.HardhatPluginError("hardhat-scilla-plugin", "Please call initZilliqa function."); } if (account instanceof account_1.Account) { exports.setup.zilliqa.wallet.defaultAccount = account; } else { exports.setup.zilliqa.wallet.defaultAccount = exports.setup.accounts[account]; } } async function deploy(hre, contractName, compress, userDefinedLibraries, ...args) { var _a; const contractInfo = hre.scillaContracts[contractName]; if (contractInfo === undefined) { throw new Error(`Scilla contract ${contractName} doesn't exist.`); } let txParamsForContractDeployment = {}; const constructorParamsLength = ((_a = contractInfo.parsedContract.constructorParams) === null || _a === void 0 ? void 0 : _a.length) || 0; if (args.length === constructorParamsLength + 1) { // The last param is Tx info such as amount, nonce, gasPrice txParamsForContractDeployment = args.pop(); } const init = fillInit(contractName, userDefinedLibraries, contractInfo.parsedContract.constructorParams, ...args); const [tx, sc] = await deployFromFile(contractInfo.path, init, compress, txParamsForContractDeployment); sc.deployed_by = tx; ScillaContractProxy.injectProxies(exports.setup, contractInfo, sc); return sc; } const deployLibrary = async (hre, libraryName, compress) => { const contractInfo = hre.scillaContracts[libraryName]; if (contractInfo === undefined) { throw new Error(`Scilla contract ${libraryName} doesn't exist.`); } const init = fillLibraryInit(); const [tx, sc] = await deployFromFile(contractInfo.path, init, compress, {}); // FIXME: In #45 sc.deployed_by = tx; return sc; }; exports.deployLibrary = deployLibrary; const fillLibraryInit = () => { const init = [ { vname: "_scilla_version", type: "Uint32", value: "0", }, { vname: "_library", type: "Bool", value: { constructor: "True", argtypes: [], arguments: [], }, }, ]; return init; }; const fillInit = (contractName, userDefinedLibraries, contractParams, ...userSpecifiedArgs) => { const init = [{ vname: "_scilla_version", type: "Uint32", value: "0" }]; if (userDefinedLibraries) { // Underlying zilliqa-js doesn't support push such an object to Init init.push({ vname: "_extlibs", type: "List(Pair String ByStr20)", value: userDefinedLibraries.map((lib) => ({ constructor: "Pair", argtypes: ["String", "ByStr20"], arguments: [lib.name, lib.address], })), }); } if (contractParams) { if (userSpecifiedArgs.length !== contractParams.length) { throw new Error(`Expected to receive ${contractParams.length} parameters for ${contractName} deployment but got ${userSpecifiedArgs.length}`); } contractParams.forEach((param, index) => { if ((0, ScillaParser_1.isNumeric)(param.type)) { init.push({ vname: param.name, type: param.type, value: userSpecifiedArgs[index].toString(), }); } else { // It's an ADT or string init.push({ vname: param.name, type: param.type, value: userSpecifiedArgs[index], }); } }); } else { if (userSpecifiedArgs.length > 0) { throw new Error(`Expected to receive 0 parameters for ${contractName} deployment but got ${userSpecifiedArgs.length}`); } } return init; }; // deploy a smart contract whose code is in a file with given init arguments async function deployFromFile(path, init, compress, txParamsForContractDeployment) { var _a; if (exports.setup === null) { throw new plugins_1.HardhatPluginError("hardhat-scilla-plugin", "Please call initZilliqa function."); } const deployer = (_a = exports.setup.zilliqa.wallet.defaultAccount) !== null && _a !== void 0 ? _a : exports.setup.accounts[0]; let code = read(path); if (compress) { code = compressContract(code); } const contract = exports.setup.zilliqa.contracts.new(code, init); const [tx, sc] = await contract.deploy({ ...exports.setup, pubKey: deployer.publicKey, ...txParamsForContractDeployment }, exports.setup.attempts, exports.setup.timeout, false); // Let's add this function for further signer/executer changes. ScillaContractProxy.injectConnectors(exports.setup, sc); sc.connect(deployer); return [tx, sc]; } function compressContract(code) { // Remove comments code = code.replace(/(\(\*.*?\*\))/gms, ""); // Remove empty lines code = code.replace(/(^[ \t]*\n)/gm, ""); // Remove extra whitespace at the end of the lines return code.replace(/[ \t]+$/gm, ""); } //# sourceMappingURL=ScillaContractDeployer.js.map