From 5ad408918e780f41786352db38d09abe9cdf81f9 Mon Sep 17 00:00:00 2001 From: Sergei Voronezhskii Date: Wed, 13 Sep 2023 11:19:08 +0300 Subject: [PATCH] cli as module --- packages/cli/cli.ts | 138 +++++++++++++++++++++++++++++++ packages/cli/index.ts | 144 ++------------------------------- packages/cli/package-lock.json | 14 ++-- packages/cli/package.json | 3 +- packages/cli/touch.ts | 18 +++-- 5 files changed, 164 insertions(+), 153 deletions(-) create mode 100755 packages/cli/cli.ts mode change 100755 => 100644 packages/cli/index.ts diff --git a/packages/cli/cli.ts b/packages/cli/cli.ts new file mode 100755 index 00000000..6f0d7a4d --- /dev/null +++ b/packages/cli/cli.ts @@ -0,0 +1,138 @@ +#!/usr/bin/env node + +import { program } from "commander" +import { TonClient } from "@eversdk/core" +import { libNode } from "@eversdk/lib-node" +import { toString as qrcode } from "qrcode" +import { promisify } from "util" +import dotenv from "dotenv" + +import { myParseInt, getEnv } from "./utils" +import { graphql } from "./graphql" +import { kamikadze } from "./kamikadze" +import { + touch, + DEFAULT_TOUCH_MAX_BALANCE, + DEFAULT_TOUCH_TRY_COUNT, + DEFAULT_TOUCH_TRY_SLEEP, +} from "./touch" +import { compile } from "./compile" +import { DEFAULT_TOPUP_BALANCE, deploy } from "./giver" + +dotenv.config() + +TonClient.useBinaryLibrary(libNode) + +program + .name("evercloud") + .description("Evercloud CLI") + .version(getEnv("npm_package_version") as string) + +program.option("-d, --debug", "Output debug information (time)") + +program + .command("compile") + .alias("c") + .argument("", "Path to Contract source file (.sol)") + .description( + "Compile contract, wrap it into js, generate d.ts (depends on `npx` tool, should be in PATH)", + ) + .action(compile) + +program.command("deploy").alias("d").description("Deploy giver").action(deploy) + +program + .command("graphql") + .alias("q") + .argument("", "query text (also can be subscription or mutation)") + .description("Sending a query to a GraphQL endpoint.") + .option( + "-c, --count ", + "Specify the number of events to wait.", + myParseInt, + 0, + ) + .action(graphql) + +program + .command("kamikadze") + .alias("k") + .description( + "Run the Kamikaze contract, which triggers the self-destruct process by calling the `sendAllMoney` function.", + ) + .option( + "-v, --value ", + "Top up the Kamikaze contract before deploying it in nanotokens.", + myParseInt, + DEFAULT_TOPUP_BALANCE, + ) + .action(kamikadze) + +program + .command("qrcode") + .alias("qr") + .argument("") + .description( + "Generate a QR code using the provided text argument and output it to the console.", + ) + .action(async req => { + console.log(await promisify(qrcode)(req)) + }) + +program + .command("touch") + .alias("t") + .description( + "Run the Touch contract, which increases local state variable timestamp.", + ) + .option( + "-v, --value ", + "Top up the Touch contract before deploying it in nanotokens.", + myParseInt, + DEFAULT_TOUCH_MAX_BALANCE, + ) + .option( + "-c, --try-count ", + "If the GraphQL response does not contain a valid value, retry the request a maximum of `tryCount` times.", + myParseInt, + DEFAULT_TOUCH_TRY_COUNT, + ) + .option( + "-s, --try-sleep ", + "If the GraphQL response does not contain a valid value, sleep for `trySleep` milisecodes before next try.", + myParseInt, + DEFAULT_TOUCH_TRY_SLEEP, + ) + .action(touch) + +program.addHelpText( + "after", + ` +Examples: + +$ evercloud c ./contracts/Kamikadze.sol + +$ evercloud q "query{blockchain{blocks(last:1 workchain:-1){edges{node{seq_no hash}}}}}" + +$ evercloud q "subscription{blocks(filter:{workchain_id:{eq:-1}}){seq_no id}}" + +$ EVERCLOUD_GIVER_TYPE=v2 TON_NETWORK_ADDRESS=https://devnet.evercloud.dev/ TON_GIVER_ADDRESS=
TON_GIVER_SECRET= evercloud k + +$ evercloud qr 0:66cb703cd63dd0b9eafbce702a9f838211ba1ea5ccce101dc81b98114d824b8a + +$ evercloud t -d +`, +) + +program + .parseAsync(process.argv) + .then(cmd => { + if (cmd.opts().debug) { + console.log(`finished in ${process.uptime().toFixed(2)}s`) + } + process.exit(0) + }) + .catch(error => { + console.dir(error, { showHidden: false, depth: null }) + process.exit(1) + }) diff --git a/packages/cli/index.ts b/packages/cli/index.ts old mode 100755 new mode 100644 index 6f0d7a4d..44efc278 --- a/packages/cli/index.ts +++ b/packages/cli/index.ts @@ -1,138 +1,6 @@ -#!/usr/bin/env node - -import { program } from "commander" -import { TonClient } from "@eversdk/core" -import { libNode } from "@eversdk/lib-node" -import { toString as qrcode } from "qrcode" -import { promisify } from "util" -import dotenv from "dotenv" - -import { myParseInt, getEnv } from "./utils" -import { graphql } from "./graphql" -import { kamikadze } from "./kamikadze" -import { - touch, - DEFAULT_TOUCH_MAX_BALANCE, - DEFAULT_TOUCH_TRY_COUNT, - DEFAULT_TOUCH_TRY_SLEEP, -} from "./touch" -import { compile } from "./compile" -import { DEFAULT_TOPUP_BALANCE, deploy } from "./giver" - -dotenv.config() - -TonClient.useBinaryLibrary(libNode) - -program - .name("evercloud") - .description("Evercloud CLI") - .version(getEnv("npm_package_version") as string) - -program.option("-d, --debug", "Output debug information (time)") - -program - .command("compile") - .alias("c") - .argument("", "Path to Contract source file (.sol)") - .description( - "Compile contract, wrap it into js, generate d.ts (depends on `npx` tool, should be in PATH)", - ) - .action(compile) - -program.command("deploy").alias("d").description("Deploy giver").action(deploy) - -program - .command("graphql") - .alias("q") - .argument("", "query text (also can be subscription or mutation)") - .description("Sending a query to a GraphQL endpoint.") - .option( - "-c, --count ", - "Specify the number of events to wait.", - myParseInt, - 0, - ) - .action(graphql) - -program - .command("kamikadze") - .alias("k") - .description( - "Run the Kamikaze contract, which triggers the self-destruct process by calling the `sendAllMoney` function.", - ) - .option( - "-v, --value ", - "Top up the Kamikaze contract before deploying it in nanotokens.", - myParseInt, - DEFAULT_TOPUP_BALANCE, - ) - .action(kamikadze) - -program - .command("qrcode") - .alias("qr") - .argument("") - .description( - "Generate a QR code using the provided text argument and output it to the console.", - ) - .action(async req => { - console.log(await promisify(qrcode)(req)) - }) - -program - .command("touch") - .alias("t") - .description( - "Run the Touch contract, which increases local state variable timestamp.", - ) - .option( - "-v, --value ", - "Top up the Touch contract before deploying it in nanotokens.", - myParseInt, - DEFAULT_TOUCH_MAX_BALANCE, - ) - .option( - "-c, --try-count ", - "If the GraphQL response does not contain a valid value, retry the request a maximum of `tryCount` times.", - myParseInt, - DEFAULT_TOUCH_TRY_COUNT, - ) - .option( - "-s, --try-sleep ", - "If the GraphQL response does not contain a valid value, sleep for `trySleep` milisecodes before next try.", - myParseInt, - DEFAULT_TOUCH_TRY_SLEEP, - ) - .action(touch) - -program.addHelpText( - "after", - ` -Examples: - -$ evercloud c ./contracts/Kamikadze.sol - -$ evercloud q "query{blockchain{blocks(last:1 workchain:-1){edges{node{seq_no hash}}}}}" - -$ evercloud q "subscription{blocks(filter:{workchain_id:{eq:-1}}){seq_no id}}" - -$ EVERCLOUD_GIVER_TYPE=v2 TON_NETWORK_ADDRESS=https://devnet.evercloud.dev/ TON_GIVER_ADDRESS=
TON_GIVER_SECRET= evercloud k - -$ evercloud qr 0:66cb703cd63dd0b9eafbce702a9f838211ba1ea5ccce101dc81b98114d824b8a - -$ evercloud t -d -`, -) - -program - .parseAsync(process.argv) - .then(cmd => { - if (cmd.opts().debug) { - console.log(`finished in ${process.uptime().toFixed(2)}s`) - } - process.exit(0) - }) - .catch(error => { - console.dir(error, { showHidden: false, depth: null }) - process.exit(1) - }) +export * from "./compile" +export * from "./giver" +export * from "./graphql" +export * from "./kamikadze" +export * from "./touch" +export * from "./utils" diff --git a/packages/cli/package-lock.json b/packages/cli/package-lock.json index 2956fcf5..b397db58 100644 --- a/packages/cli/package-lock.json +++ b/packages/cli/package-lock.json @@ -19,7 +19,7 @@ "tvm-solidity-parser": "^0.14.75" }, "bin": { - "evercloud": "index.js" + "evercloud": "cli.js" }, "devDependencies": { "@types/node": "^18.16.2", @@ -224,17 +224,17 @@ } }, "node_modules/@eversdk/core": { - "version": "1.43.0", - "resolved": "https://registry.npmjs.org/@eversdk/core/-/core-1.43.0.tgz", - "integrity": "sha512-AqllftXeAmxTV15lSc63ZXO8VWlXVlCD2HEP3xYeGyFvS0J0vt9EPD1K+DL1QbGk5YTOWwWOoG+VJY/nlckS0A==", + "version": "1.44.3", + "resolved": "https://registry.npmjs.org/@eversdk/core/-/core-1.44.3.tgz", + "integrity": "sha512-Poq1CWw9oqH0hm55eSJLZDXtgIxB6spN4QRtiRWoo44ZlsUNTrRTrnAg+qIHfkmqO/6EDjPnxs15mOg9vDG3pg==", "engines": { "node": ">=6" } }, "node_modules/@eversdk/lib-node": { - "version": "1.43.0", - "resolved": "https://registry.npmjs.org/@eversdk/lib-node/-/lib-node-1.43.0.tgz", - "integrity": "sha512-P7dHIfNN9uM0llH6CN5d+dSSZw5C8lRaoGfj8NdzCfYW6AdxYA176otxTqfrbLlFFMMK+XznetW8WvrvdyeHdA==", + "version": "1.44.3", + "resolved": "https://registry.npmjs.org/@eversdk/lib-node/-/lib-node-1.44.3.tgz", + "integrity": "sha512-70Ir1aVj+OQA8l/hbo3ihBiEVgzD1O4k0C4FQCC7Qfv0s3DuKdenrPd+Qqlgm92cWG5g7AV7FNojiIuurwV2Sg==", "hasInstallScript": true, "engines": { "node": ">=6" diff --git a/packages/cli/package.json b/packages/cli/package.json index 6743ffa4..b241152d 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -24,10 +24,11 @@ "test": "npm run clean; npx ts-node index.ts" }, "bin": { - "evercloud": "./index.js" + "evercloud": "./cli.js" }, "files": [ "index.js", + "cli.js", "compile.js", "giver.js", "graphql.js", diff --git a/packages/cli/touch.ts b/packages/cli/touch.ts index f40cab9f..3f83545e 100644 --- a/packages/cli/touch.ts +++ b/packages/cli/touch.ts @@ -2,7 +2,7 @@ import { Account } from "@eversdk/appkit" import { getDefaultEndpoints, sleep } from "./utils" import { Giver, DEFAULT_TOPUP_BALANCE } from "./giver" import { performance } from "node:perf_hooks" -import { TonClient } from "@eversdk/core" +import { TonClient, Signer } from "@eversdk/core" import * as Touch from "./contracts/Touch.js" export const DEFAULT_TOUCH_MAX_BALANCE = 100 * 1e9 @@ -13,6 +13,7 @@ export async function touch(options: { value: number tryCount: number trySleep: number + signer: Signer }) { const sdk = new TonClient({ abi: { @@ -28,14 +29,17 @@ export async function touch(options: { const giver = await Giver.create(sdk) const touch = new Account(Touch, { client: sdk, - signer: giver.account.signer, + signer: options.signer ?? giver.account.signer, }) const touchAccount = await touch.getAccount() - const { balance } = touchAccount + const balance = await touch.getBalance() const address = await touch.getAddress() - if (BigInt(balance ?? 0) < DEFAULT_TOPUP_BALANCE) { - await giver.sendTo(address, options.value) + if (balance && BigInt(balance) < DEFAULT_TOPUP_BALANCE) { + await giver.sendTo(address, options.value ?? DEFAULT_TOUCH_MAX_BALANCE) + console.log("Touch topup:", address) + } else if (!balance) { + await giver.sendTo(address, DEFAULT_TOPUP_BALANCE) console.log("Touch topup:", address) } @@ -57,7 +61,7 @@ export async function touch(options: { console.log(`Touch ok: ${response.transaction.id}`) let tryCounter = 0 - while (tryCounter < options.tryCount) { + while (tryCounter < options.tryCount ?? DEFAULT_TOUCH_TRY_COUNT) { touch.refresh() // Force to take frech boc from graphql const balanceAfter = BigInt((await touch.getBalance()) ?? 0) const timestampNew = BigInt( @@ -85,7 +89,7 @@ export async function touch(options: { console.log( `Touch local state has not been updated (try: ${++tryCounter})`, ) - await sleep(options.trySleep) + await sleep(options.trySleep ?? DEFAULT_TOUCH_TRY_SLEEP) } } }