UNPKG

hardhat-scilla-plugin

Version:
177 lines 7.64 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; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.injectConnectors = injectConnectors; exports.injectProxies = injectProxies; exports.sc_call = sc_call; const util_1 = require("@zilliqa-js/util"); const plugins_1 = require("hardhat/plugins"); const ScillaContractDeployer = __importStar(require("../deployer/ScillaContractDeployer")); const ScillaParser_1 = require("./ScillaParser"); function handleParam(param, arg) { if (typeof param.typeJSON === "undefined") { throw new plugins_1.HardhatPluginError("hardhat-scilla-plugin", "Parameters were incorrectly parsed. Try clearing your scilla.cache file."); } else if (typeof param.typeJSON === "string") { return { vname: param.name, type: param.type, value: arg.toString(), }; } else { const values = []; param.typeJSON.argtypes.forEach((p, index) => { values.push(handleUnnamedParam(p, arg[index])); }); const argtypes = param.typeJSON.argtypes.map((x) => x.type); /* We use JSON.parse(JSON.strigify()) because we need to create a JSON with a constructor field. Typescript expects this constructor to have the same type as an object constructor which is not possible as it should be a string for our purposes. This trick allows forces the typescript compiler to enforce this. */ const value = JSON.parse(JSON.stringify({ constructor: param.typeJSON.ctor, argtypes, arguments: values, })); return { vname: param.name, type: param.type, value, }; } } function handleUnnamedParam(param, arg) { if (typeof param.typeJSON === "undefined") { throw new plugins_1.HardhatPluginError("hardhat-scilla-plugin", "Parameters were incorrectly parsed. Try clearing your scilla.cache file."); } else if (typeof param.typeJSON === "string") { return arg.toString(); } else { const values = []; param.typeJSON.argtypes.forEach((f, index) => { values.push(handleUnnamedParam(f, arg[index])); }); const argtypes = param.typeJSON.argtypes.map((x) => x.type); /* We use JSON.parse(JSON.strigify()) because we need to create a JSON with a constructor field. Typescript expects this constructor to have the same type as an object constructor which is not possible as it should be a string for our purposes. This trick allows forces the typescript compiler to enforce this. */ return JSON.parse(JSON.stringify({ vname: param.name, type: param.type, constructor: param.typeJSON.ctor, argtypes, arguments: values, })); } } function injectConnectors(setup, sc) { sc.connect = (signer) => { sc.executer = signer; // If account is not added already, add it to the list. if ((setup === null || setup === void 0 ? void 0 : setup.accounts.findIndex((acc) => acc.privateKey === signer.privateKey)) === -1) { setup === null || setup === void 0 ? void 0 : setup.zilliqa.wallet.addByPrivateKey(signer.privateKey); setup === null || setup === void 0 ? void 0 : setup.accounts.push(signer); } return sc; }; } // Inject proxy functions into a contract. function injectProxies(setup, contractInfo, sc) { contractInfo.parsedContract.transitions.forEach((transition) => { sc[transition.name] = async (...args) => { let callParams = { version: setup.version, gasPrice: setup.gasPrice, gasLimit: setup.gasLimit, amount: new util_1.BN(0), }; if (args.length === transition.params.length + 1) { // The last param is Tx info such as amount const txParams = args.pop(); callParams = { ...callParams, ...txParams }; } else if (args.length !== transition.params.length) { throw new Error(`Expected to receive ${transition.params.length} parameters for ${transition.name} but got ${args.length}`); } const values = []; transition.params.forEach((param, index) => { values.push(handleParam(param, args[index])); }); return sc_call(sc, transition.name, values, callParams); }; }); contractInfo.parsedContract.fields.forEach((field) => { sc[field.name] = async () => { const state = await sc.getState(); if ((0, ScillaParser_1.isNumeric)(field.type)) { return Number(state[field.name]); } return state[field.name]; }; }); if (contractInfo.parsedContract.constructorParams) { contractInfo.parsedContract.constructorParams.forEach((field) => { sc[field.name] = async () => { const states = await sc.getInit(); const state = states.filter((s) => s.vname === field.name)[0]; if ((0, ScillaParser_1.isNumeric)(field.type)) { return Number(state.value); } return state.value; }; }); } // Will shadow any transition named ctors. But done like this to avoid changing the signature of deploy. const parsedCtors = contractInfo.parsedContract.ctors; sc.ctors = (0, ScillaParser_1.generateTypeConstructors)(parsedCtors); } // call a smart contract's transition with given args and an amount to send from a given public key async function sc_call(sc, transition, args = [], callParams) { if (ScillaContractDeployer.setup === null) { throw new plugins_1.HardhatPluginError("hardhat-scilla-plugin", "Please call initZilliqa function."); } if (callParams.pubKey === undefined && sc.executer) { callParams.pubKey = sc.executer.publicKey; } return sc.call(transition, args, callParams, ScillaContractDeployer.setup.attempts, ScillaContractDeployer.setup.timeout, true); } //# sourceMappingURL=ScillaContractProxy.js.map