UNPKG

@ton-actions/tondev-contest

Version:

TON Dev Environment

93 lines 3.63 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAccount = void 0; const appkit_1 = require("@tonclient/appkit"); const registry_1 = require("../network/registry"); const core_1 = require("@tonclient/core"); const signer_1 = require("../signer"); const fs_1 = __importDefault(require("fs")); const registry_2 = require("../signer/registry"); const param_parser_1 = require("./param-parser"); function findExisting(paths) { return paths.find(x => fs_1.default.existsSync(x)); } function loadContract(filePath) { filePath = filePath.trim(); const lowered = filePath.toLowerCase(); let basePath; if (lowered.endsWith(".tvc") || lowered.endsWith(".abi")) { basePath = filePath.slice(0, -4); } else if (lowered.endsWith(".abi.json")) { basePath = filePath.slice(0, -9); } else { basePath = filePath; } const tvcPath = findExisting([`${basePath}.tvc`]); const abiPath = findExisting([`${basePath}.abi.json`, `${basePath}.abi`]); const tvc = tvcPath ? fs_1.default.readFileSync(tvcPath).toString("base64") : undefined; const abi = abiPath ? JSON.parse(fs_1.default.readFileSync(abiPath, "utf8")) : undefined; if (!abi) { throw new Error("ABI file missing."); } return { abi, tvc, }; } async function getAccount(terminal, args) { var _a, _b; const address = (_a = args.address) !== null && _a !== void 0 ? _a : ""; const network = new registry_1.NetworkRegistry().get(args.network); const client = new core_1.TonClient({ network: { endpoints: network.endpoints, }, }); const contract = args.file !== "" ? loadContract(args.file) : { abi: {} }; const signerArg = args.signer.trim().toLowerCase(); const signers = new registry_2.SignerRegistry(); let signerItem; if (signerArg === "none") { signerItem = undefined; } else if (signerArg === "" && !signers.default && address !== "") { signerItem = undefined; } else { signerItem = signers.get(signerArg); } const signer = signerItem ? await signer_1.createSigner(signerItem.name) : core_1.signerNone(); const options = { signer, client, }; const abiData = (_b = contract.abi.data) !== null && _b !== void 0 ? _b : []; if (abiData.length > 0 && args.data !== "") { options.initData = param_parser_1.ParamParser.components({ name: "data", type: "tuple", components: abiData }, args.data); } if (address !== "") { options.address = address; } const account = new appkit_1.Account(contract, options); terminal.log("\nConfiguration\n"); terminal.log(` Network: ${network.name} (${registry_1.NetworkRegistry.getEndpointsSummary(network)})`); terminal.log(` Signer: ${signerItem ? `${signerItem.name} (public ${signerItem.keys.public})` : "None"}\n`); if (address === "" && abiData.length > 0 && !options.initData) { terminal.log(`Address: Can't calculate address: additional deploying data required.`); } else { terminal.log(`Address: ${await account.getAddress()}${address === "" ? " (calculated from TVC and signer public)" : ""}`); } return account; } exports.getAccount = getAccount; //# sourceMappingURL=accounts.js.map