UNPKG

locklift-private-deploy

Version:

Locklift plugin that enables you to deploy smart contracts using private RPC endpoint.

64 lines (63 loc) 3.32 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PrivateDeployer = void 0; const axios_1 = __importDefault(require("axios")); const nekoton_wasm_1 = require("nekoton-wasm"); class PrivateDeployer { constructor(locklift, privateRPC) { this.deployContract = async (args) => { const { tvc, abi } = this.locklift.factory.getContractArtifacts(args.contract); return this._deployContract(abi, { tvc: args.tvc || tvc, workchain: args.workchain, publicKey: args.publicKey, initParams: args.initParams, }, args.constructorParams, args.value); }; this._deployContract = async (abi, deployParams, constructorParams, value) => { const expectedAddress = await this.locklift.provider.getExpectedAddress(abi, deployParams); await this.locklift.utils.errorExtractor(this.locklift.giver.sendTo(expectedAddress, value)); const stateInit = await this.locklift.provider.getStateInit(abi, deployParams); const signer = (await this.locklift.keystore.getSigner("0")); // @ts-ignore const signatureId = locklift.context.network.config.connection.id; const signedMessage = await this.prepareSignedMessage(expectedAddress, signer, JSON.stringify(abi), stateInit.stateInit, JSON.parse(JSON.stringify(constructorParams)), signatureId); const subscription = new this.locklift.provider.Subscriber(); const txProm = subscription.transactions(expectedAddress).first(); try { await this.sendMessage(this.privateRPC, signedMessage.boc); } catch (err) { throw new Error(`sendMessage to rpc endpoint: ${err}`); } const transactions = (await txProm).transactions; await subscription.unsubscribe(); const tx = { transaction: transactions[0] }; const contract = new this.locklift.provider.Contract(abi, expectedAddress); return { contract, tx }; }; this.prepareSignedMessage = async (contract, signer, abi, stateInit, constructorParams, signatureId = 1000, TIMEOUT = 60) => { const clock = new nekoton_wasm_1.ClockWithOffset(); const repackedRecipient = (0, nekoton_wasm_1.repackAddress)(contract.toString()); const unsignedMessage = (0, nekoton_wasm_1.createExternalMessage)(clock, repackedRecipient, abi, "constructor", stateInit, constructorParams, signer.publicKey, TIMEOUT); const signature = await signer.sign(unsignedMessage.hash, signatureId); return unsignedMessage.sign(signature); }; this.sendMessage = async (url, msgBoc) => { return axios_1.default.post(url, { jsonrpc: "2.0", id: 1, method: "sendMessage", params: { message: msgBoc, }, }); }; this.locklift = locklift; this.privateRPC = privateRPC; } } exports.PrivateDeployer = PrivateDeployer;