@tgbotpress/plugin-energy 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,3 @@
1
1
  # @tgbotpress/plugin-energy
2
2
 
3
- Energy management plugin package. The pure entries are `./units` for decimal energy amounts, `./poll` for wallet polling lower-bound timestamps, and `./demo-data` for clearly marked, read-only examples used by empty energy management pages. The examples are never written to the database or used by workers. Platform APIs, encrypted credentials, Tron polling, order workers and Telegram notifications remain host-owned until their transaction and key-management contracts are migrated.
3
+ Energy management plugin package. The `./initialize` entry seeds safe, disabled starter configuration into an empty energy setup when the host starts with this package version. It creates no fabricated orders, trades, users, wallet addresses, credentials, grants, or chain activity. The `./units`, `./poll`, and `./nl-api` entries provide shared business rules and API contracts.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tgbotpress/plugin-energy",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "type": "module",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org"
@@ -18,9 +18,13 @@
18
18
  "types": "./src/poll.d.ts",
19
19
  "import": "./src/poll.js"
20
20
  },
21
- "./demo-data": {
22
- "types": "./src/demo-data.d.ts",
23
- "import": "./src/demo-data.js"
21
+ "./initialize": {
22
+ "types": "./src/initialize.d.ts",
23
+ "import": "./src/initialize.js"
24
+ },
25
+ "./nl-api": {
26
+ "types": "./src/nl-api.d.ts",
27
+ "import": "./src/nl-api.js"
24
28
  },
25
29
  "./units.js": {
26
30
  "types": "./src/units.d.ts",
package/src/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  export const manifest = {
2
2
  id: 'energy',
3
- version: '0.1.2',
3
+ version: '0.1.4',
4
4
  hostRange: '>=1.0.0 <2.0.0',
5
5
  sdkVersion: '0.1.0',
6
6
  name: '能量管理',
@@ -0,0 +1,14 @@
1
+ export interface EnergyInitializationClient {
2
+ query<T = unknown>(sql: string, values?: unknown[]): Promise<{ rows: T[]; rowCount: number | null }>
3
+ release(): void
4
+ }
5
+
6
+ export interface EnergyInitializationPool {
7
+ connect(): Promise<EnergyInitializationClient>
8
+ }
9
+
10
+ export declare function initializeEnergyPlugin(pool: EnergyInitializationPool): Promise<{
11
+ skipped: boolean
12
+ reason?: 'plugin_inactive'
13
+ inserted: number
14
+ }>
@@ -0,0 +1,86 @@
1
+ const seedTable = 't_platform_energy_plugin_initialization'
2
+
3
+ async function seedOnce(client, seedKey, seed) {
4
+ const alreadyInitialized = await client.query(`SELECT 1 FROM ${seedTable} WHERE seed_key=$1`, [seedKey])
5
+ if (alreadyInitialized.rowCount) return false
6
+ const count = await seed()
7
+ await client.query(`INSERT INTO ${seedTable}(seed_key) VALUES($1) ON CONFLICT(seed_key) DO NOTHING`, [seedKey])
8
+ return Number(count || 0)
9
+ }
10
+
11
+ /**
12
+ * Add disabled, editable configuration rows for a new energy installation.
13
+ * The host calls this after database migrations and before serving requests.
14
+ * This intentionally never creates user, order, trade, payment, grant, wallet,
15
+ * credential, or chain-activity records.
16
+ */
17
+ export async function initializeEnergyPlugin(pool) {
18
+ const client = await pool.connect()
19
+ let inserted = 0
20
+ try {
21
+ await client.query('BEGIN')
22
+ await client.query("SELECT pg_advisory_xact_lock(hashtextextended('tgbotpress:plugin-energy:initialization',0))")
23
+ await client.query(`CREATE TABLE IF NOT EXISTS ${seedTable} (seed_key text PRIMARY KEY, completed_at timestamptz NOT NULL DEFAULT now())`)
24
+
25
+ const lifecycle = await client.query("SELECT state FROM t_platform_plugin_lifecycle WHERE plugin_id='energy'")
26
+ if (lifecycle.rows[0]?.state && lifecycle.rows[0].state !== 'active') {
27
+ await client.query('COMMIT')
28
+ return { skipped: true, reason: 'plugin_inactive', inserted: 0 }
29
+ }
30
+
31
+ inserted += Number(await seedOnce(client, 'platform:nl-api', async () => {
32
+ const existing = await client.query('SELECT 1 FROM t_energy_platform LIMIT 1')
33
+ if (existing.rowCount) return 0
34
+ await client.query(`INSERT INTO t_energy_platform
35
+ (poll_group,platform_name,status,platform_uid,platform_apikey,alert_platform_balance,comments,create_time)
36
+ VALUES('A',7,1,NULL,NULL,0,'插件初始化配置;请填写 NL-API 用户名和密钥后再启用',now())`)
37
+ return 1
38
+ }))
39
+
40
+ inserted += Number(await seedOnce(client, 'config:bind-a-send-n', async () => {
41
+ const existing = await client.query('SELECT 1 FROM t_bind_a_send_n_config WHERE bot_rid IS NULL LIMIT 1')
42
+ if (existing.rowCount) return 0
43
+ const admin = await client.query('SELECT id FROM t_admin ORDER BY id LIMIT 1')
44
+ if (!admin.rowCount) return 0
45
+ await client.query(`INSERT INTO t_bind_a_send_n_config
46
+ (bot_rid,is_enable,energy_per_send,activate_threshold,daily_limit,monthly_limit,n_addr_daily_limit,
47
+ audit_threshold,audit_enabled,whitelist,blacklist,retry_count,create_by,create_time)
48
+ VALUES(NULL,0,65000,0,9999,9999,9999,0,0,'','',3,$1,now())`, [admin.rows[0].id])
49
+ return 1
50
+ }))
51
+
52
+ const bots = await client.query('SELECT rid FROM t_telegram_bot ORDER BY rid')
53
+ for (const bot of bots.rows) {
54
+ const botRid = Number(bot.rid)
55
+ inserted += Number(await seedOnce(client, `bot:${botRid}`, async () => {
56
+ const existing = await client.query('SELECT 1 FROM t_energy_platform_bot WHERE bot_rid=$1 LIMIT 1', [botRid])
57
+ if (existing.rowCount) return 0
58
+ await client.query(`INSERT INTO t_energy_platform_bot
59
+ (bot_rid,poll_group,status,is_open_ai_trusteeship,is_open_bishu,comments,create_time)
60
+ VALUES($1,'A',1,'N','N','插件初始化配置;未绑定钱包,未启用',now())`, [botRid])
61
+ return 1
62
+ }))
63
+
64
+ inserted += Number(await seedOnce(client, `packages:${botRid}`, async () => {
65
+ const existing = await client.query('SELECT 1 FROM t_energy_platform_package WHERE bot_rid=$1 LIMIT 1', [botRid])
66
+ if (existing.rowCount) return 0
67
+ await client.query(`INSERT INTO t_energy_platform_package
68
+ (bot_rid,package_type,package_name,energy_amount,energy_day,trx_price,agent_trx_price,status,seq_sn,
69
+ callback_data,show_notes,create_time)
70
+ VALUES
71
+ ($1,1,'65,000 能量 · 1 天',65000,1,0,0,1,0,$2,'',now()),
72
+ ($1,1,'131,000 能量 · 3 天',131000,3,0,0,1,1,$3,'',now())`,
73
+ [botRid, `energy_init_${botRid}_65k_1d`, `energy_init_${botRid}_131k_3d`])
74
+ return 2
75
+ }))
76
+ }
77
+
78
+ await client.query('COMMIT')
79
+ return { skipped: false, inserted }
80
+ } catch (error) {
81
+ await client.query('ROLLBACK').catch(() => undefined)
82
+ throw error
83
+ } finally {
84
+ client.release()
85
+ }
86
+ }
@@ -0,0 +1,5 @@
1
+ export type NlApiRechargeAction = 'history' | 'create'
2
+ export type NlApiRechargeRequest = { action: 'history'; username: string; password: string } | { action: 'create'; username: string; password: string; amountTrx: number }
3
+ export declare const NL_API_DEFAULT_BASE_URL: string
4
+ export declare const NL_API_ENDPOINTS: Readonly<{ accountInfo: string; energyRental: string; rechargeOrders: string }>
5
+ export declare function createNlApiRechargeRequest(action: NlApiRechargeAction, username: string, password: string, amountTrx?: number): NlApiRechargeRequest
package/src/nl-api.js ADDED
@@ -0,0 +1,19 @@
1
+ export const NL_API_DEFAULT_BASE_URL = 'https://tgnl-home.hfz.pw'
2
+
3
+ export const NL_API_ENDPOINTS = Object.freeze({
4
+ accountInfo: '/v1/get_api_user_info',
5
+ energyRental: '/v1/delegate_tran',
6
+ rechargeOrders: '/v1/api-recharge-orders',
7
+ })
8
+
9
+ export function createNlApiRechargeRequest(action, username, password, amountTrx) {
10
+ if (action !== 'history' && action !== 'create') throw new TypeError('invalid NL-API recharge action')
11
+ if (typeof username !== 'string' || !username.trim() || typeof password !== 'string' || !password) {
12
+ throw new TypeError('NL-API username and password are required')
13
+ }
14
+ if (action === 'history') return { action, username: username.trim(), password }
15
+ if (!Number.isSafeInteger(amountTrx) || amountTrx < 1 || amountTrx > 1_000_000) {
16
+ throw new RangeError('NL-API recharge amount must be an integer from 1 to 1000000')
17
+ }
18
+ return { action, username: username.trim(), password, amountTrx }
19
+ }
@@ -1,3 +0,0 @@
1
- export type EnergyDemoPage = 'platforms' | 'bots' | 'packages' | 'orders' | 'trades' | 'quickOrders' | 'trusteeship' | 'bishu' | 'bindConfig' | 'bindings' | 'grants'
2
- export type EnergyDemoRecord = { title: string; fields: Array<[string, string]> }
3
- export declare const energyDemoData: Readonly<Record<EnergyDemoPage, EnergyDemoRecord[]>>
package/src/demo-data.js DELETED
@@ -1,38 +0,0 @@
1
- export const energyDemoData = Object.freeze({
2
- platforms: [
3
- { title: 'NL-API 平台', fields: [['轮询组', 'A 组'], ['API 用户', 'demo_energy_user'], ['余额', '0 TRX'], ['API 密钥', '未配置'], ['状态', '示例 · 未启用']] },
4
- { title: '自质押平台', fields: [['轮询组', 'B 组'], ['钱包地址', 'TDEMO-WALLET-001'], ['可用能量', '0'], ['余额预警', '50 TRX'], ['状态', '示例 · 未启用']] },
5
- ],
6
- bots: [
7
- { title: '示例机器人配置', fields: [['机器人', '@demo_energy_bot(未绑定)'], ['轮询组', 'A 组'], ['收能地址', 'TDEMO-RECEIVE-001'], ['自动托管', '关闭'], ['笔数套餐', '关闭']] },
8
- ],
9
- packages: [
10
- { title: '基础能量套餐', fields: [['能量数量', '65,000'], ['租期', '1 天'], ['售价', '示例价格'], ['机器人', '示例机器人(未绑定)'], ['状态', '示例 · 未上架']] },
11
- { title: '进阶能量套餐', fields: [['能量数量', '131,000'], ['租期', '3 天'], ['售价', '示例价格'], ['机器人', '示例机器人(未绑定)'], ['状态', '示例 · 未上架']] },
12
- ],
13
- orders: [
14
- { title: '已完成订单样例', fields: [['订单号', 'DEMO-ENERGY-ORDER-001'], ['用户', 'TG_DEMO_001'], ['能量', '65,000'], ['收能地址', 'TDEMO-RECEIVE-001'], ['状态', '演示完成']] },
15
- ],
16
- trades: [
17
- { title: 'USDT 入账样例', fields: [['交易 Hash', 'DEMO_TX_USDT_001'], ['币种 / 金额', 'USDT · 10.00'], ['发送地址', 'TDEMO-SENDER-001'], ['接收地址', 'TDEMO-RECEIVER-001'], ['处理状态', '演示成功']] },
18
- { title: 'TRX 入账样例', fields: [['交易 Hash', 'DEMO_TX_TRX_001'], ['币种 / 金额', 'TRX · 2.50'], ['发送地址', 'TDEMO-SENDER-002'], ['接收地址', 'TDEMO-RECEIVER-002'], ['处理状态', '演示待处理']] },
19
- ],
20
- quickOrders: [
21
- { title: '快捷订单样例', fields: [['订单号', 'DEMO-QUICK-ORDER-001'], ['用户', 'TG_DEMO_001'], ['能量 / 租期', '65,000 · 1 天'], ['支付方式', 'TRX(示例)'], ['状态', '演示完成']] },
22
- ],
23
- trusteeship: [
24
- { title: '智能托管样例', fields: [['用户', 'TG_DEMO_001'], ['监控钱包', 'TDEMO-WALLET-002'], ['单次能量', '65,000'], ['累计购买', '0'], ['状态', '演示 · 未启用']] },
25
- ],
26
- bishu: [
27
- { title: '笔数能量样例', fields: [['用户', 'TG_DEMO_002'], ['钱包地址', 'TDEMO-WALLET-003'], ['已购买次数', '2'], ['最大次数', '10'], ['状态', '演示记录']] },
28
- ],
29
- bindConfig: [
30
- { title: '绑 A 送 N 配置样例', fields: [['单次发放', '65,000 能量'], ['激活门槛', '0 TRX'], ['每日上限', '9,999'], ['审核', '关闭'], ['状态', '仅示例,未保存']] },
31
- ],
32
- bindings: [
33
- { title: '绑定关系样例', fields: [['用户', 'TG_DEMO_003'], ['A 地址', 'TDEMO-A-ADDRESS'], ['N 地址', 'TDEMO-N-ADDRESS'], ['绑定状态', '演示绑定'], ['链上操作', '未执行']] },
34
- ],
35
- grants: [
36
- { title: '能量发放样例', fields: [['发放单号', 'DEMO-GRANT-001'], ['用户', 'TG_DEMO_003'], ['发放数量', '65,000'], ['审核状态', '演示待审核'], ['交易 Hash', '未发起']] },
37
- ],
38
- })