UNPKG

@pendulum-chain/api-solang

Version:

Interface to interact with smart contracts compiled via Solang

64 lines (63 loc) 2.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.basicDeployContract = basicDeployContract; const api_contract_1 = require("@polkadot/api-contract"); const submitExtrinsic_js_1 = require("./submitExtrinsic.js"); const contractRpc_js_1 = require("./contractRpc.js"); async function basicDeployContract({ api, abi, constructorArguments, constructorName, limits, signer, skipDryRunning, modifyExtrinsic, }) { const code = new api_contract_1.CodePromise(api, abi, undefined); constructorName = constructorName ?? "new"; try { abi.findConstructor(constructorName); } catch { throw new Error(`Contract has no constructor called ${constructorName}`); } let gasRequired; if (skipDryRunning === true) { gasRequired = api.createType("WeightV2", limits.gas); } else { const rpcResult = await (0, contractRpc_js_1.rpcInstantiate)({ api, abi, callerAddress: (0, submitExtrinsic_js_1.getSignerAddress)(signer), constructorName, limits, constructorArguments, }); const { output } = rpcResult; gasRequired = rpcResult.gasRequired; switch (output.type) { case "reverted": case "panic": return output; case "error": return { type: "error", error: output.description ?? "unknown" }; } } const { storageDeposit: storageDepositLimit } = limits; let extrinsic = code.tx[constructorName]({ gasLimit: gasRequired, storageDepositLimit }, ...constructorArguments); if (modifyExtrinsic) { extrinsic = modifyExtrinsic(extrinsic); } const { eventRecords, status, transactionFee } = await (0, submitExtrinsic_js_1.signAndSubmitExtrinsic)(extrinsic, signer); if (status.type === "error") { return { type: "error", error: `Contract could not be deployed: ${status.error}`, }; } let deploymentAddress = undefined; for (const eventRecord of eventRecords) { const { data, section, method } = eventRecord.event; if (section === "contracts" && method === "Instantiated") { const [, contract] = data; deploymentAddress = contract.toString(); } } if (deploymentAddress === undefined) { return { type: "error", error: "Contract address not found" }; } return { type: "success", deploymentAddress, eventRecords, transactionFee }; }