@argonprotocol/mainchain 1.1.0-rc.4 → 1.1.0-rc.6

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.
@@ -32,13 +32,16 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
32
32
  var clis_exports = {};
33
33
  __export(clis_exports, {
34
34
  accountCli: () => accountCli,
35
+ accountsetFromCli: () => accountsetFromCli,
35
36
  addGlobalArgs: () => addGlobalArgs,
36
37
  applyEnv: () => applyEnv,
37
38
  bitcoinCli: () => bitcoinCli,
38
39
  buildCli: () => buildCli,
39
40
  globalOptions: () => globalOptions,
41
+ keyringFromFile: () => keyringFromFile,
40
42
  liquidityCli: () => liquidityCli,
41
43
  miningCli: () => miningCli,
44
+ saveKeyringPair: () => saveKeyringPair,
42
45
  vaultCli: () => vaultCli
43
46
  });
44
47
  module.exports = __toCommonJS(clis_exports);
@@ -79,8 +82,6 @@ __export(index_exports, {
79
82
  getClient: () => getClient,
80
83
  getTickFromHeader: () => getTickFromHeader,
81
84
  gettersToObject: () => gettersToObject,
82
- keyringFromCli: () => keyringFromCli,
83
- keyringFromFile: () => keyringFromFile,
84
85
  keyringFromSuri: () => keyringFromSuri,
85
86
  mnemonicGenerate: () => import_util_crypto.mnemonicGenerate,
86
87
  waitForLoad: () => waitForLoad
@@ -456,11 +457,8 @@ var AccountRegistry = class _AccountRegistry {
456
457
  // src/Accountset.ts
457
458
  var process2 = __toESM(require("process"), 1);
458
459
 
459
- // src/AccountMiners.ts
460
- var import_node_events2 = __toESM(require("events"), 1);
461
-
462
460
  // src/BlockWatch.ts
463
- var import_node_events = __toESM(require("events"), 1);
461
+ var import_nanoevents = require("nanoevents");
464
462
  function getTickFromHeader(client, header) {
465
463
  for (const x of header.digest.logs) {
466
464
  if (x.isPreRuntime) {
@@ -490,7 +488,7 @@ var BlockWatch = class {
490
488
  this.options.shouldLog ??= true;
491
489
  this.options.finalizedBlocks ??= false;
492
490
  }
493
- events = new import_node_events.default();
491
+ events = (0, import_nanoevents.createNanoEvents)();
494
492
  obligationsById = {};
495
493
  obligationIdByUtxoId = {};
496
494
  unsubscribe;
@@ -667,10 +665,10 @@ var MiningRotations = class {
667
665
  }));
668
666
  this.genesisTick ??= await client.query.ticks.genesisTick().then((x) => x.toNumber());
669
667
  const ticksBetweenSlots = this.miningConfig.ticksBetweenSlots;
670
- const slot1BiddingStartTick = this.genesisTick + this.miningConfig.slotBiddingStartAfterTicks;
671
- if (tick < slot1BiddingStartTick) return 0;
672
- const ticksSinceBiddingStart = tick - slot1BiddingStartTick;
673
- return Math.floor(ticksSinceBiddingStart / ticksBetweenSlots) + 1;
668
+ const slot1StartTick = this.genesisTick + this.miningConfig.slotBiddingStartAfterTicks + ticksBetweenSlots;
669
+ if (tick < slot1StartTick) return 0;
670
+ const ticksSinceSlot1 = tick - slot1StartTick;
671
+ return Math.floor(ticksSinceSlot1 / ticksBetweenSlots);
674
672
  }
675
673
  async getForHeader(client, header) {
676
674
  if (header.number.toNumber() === 0) return 0;
@@ -681,6 +679,7 @@ var MiningRotations = class {
681
679
  };
682
680
 
683
681
  // src/AccountMiners.ts
682
+ var import_nanoevents2 = require("nanoevents");
684
683
  var AccountMiners = class _AccountMiners {
685
684
  constructor(accountset, registeredMiners, options = { shouldLog: false }) {
686
685
  this.accountset = accountset;
@@ -693,7 +692,7 @@ var AccountMiners = class _AccountMiners {
693
692
  };
694
693
  }
695
694
  }
696
- events = new import_node_events2.default();
695
+ events = (0, import_nanoevents2.createNanoEvents)();
697
696
  miningRotations;
698
697
  trackedAccountsByAddress = {};
699
698
  async watch() {
@@ -808,7 +807,7 @@ var AccountMiners = class _AccountMiners {
808
807
  };
809
808
 
810
809
  // src/Accountset.ts
811
- var Accountset = class _Accountset {
810
+ var Accountset = class {
812
811
  txSubmitterPair;
813
812
  isProxy = false;
814
813
  seedAddress;
@@ -1191,24 +1190,6 @@ var Accountset = class _Accountset {
1191
1190
  await accountMiners.watch();
1192
1191
  return accountMiners;
1193
1192
  }
1194
- static async fromCli(program, proxyForAddress) {
1195
- const parentOptions = program.parent?.optsWithGlobals();
1196
- const keypair = await keyringFromCli(parentOptions);
1197
- const client = getClient(parentOptions.mainchainUrl);
1198
- if (proxyForAddress) {
1199
- return new _Accountset({
1200
- client,
1201
- isProxy: true,
1202
- seedAddress: proxyForAddress,
1203
- txSubmitter: keypair
1204
- });
1205
- } else {
1206
- return new _Accountset({
1207
- seedAccount: keypair,
1208
- client
1209
- });
1210
- }
1211
- }
1212
1193
  };
1213
1194
  function getDefaultSubaccountRange() {
1214
1195
  try {
@@ -1420,8 +1401,8 @@ var Vault = class {
1420
1401
  };
1421
1402
 
1422
1403
  // src/VaultMonitor.ts
1423
- var import_node_events3 = require("events");
1424
1404
  var import_console_table_printer2 = require("console-table-printer");
1405
+ var import_nanoevents3 = require("nanoevents");
1425
1406
  var VaultMonitor = class {
1426
1407
  constructor(accountset, alerts = {}, options = {}) {
1427
1408
  this.accountset = accountset;
@@ -1451,7 +1432,7 @@ var VaultMonitor = class {
1451
1432
  this.printBids(header.hash);
1452
1433
  });
1453
1434
  }
1454
- events = new import_node_events3.EventEmitter();
1435
+ events = (0, import_nanoevents3.createNanoEvents)();
1455
1436
  vaultsById = {};
1456
1437
  blockWatch;
1457
1438
  mainchain;
@@ -2329,47 +2310,13 @@ var BitcoinLocks = class _BitcoinLocks {
2329
2310
  };
2330
2311
 
2331
2312
  // src/keyringUtils.ts
2332
- var import_node_fs = require("fs");
2333
- var os = __toESM(require("os"), 1);
2334
- var { readFile, writeFile } = import_node_fs.promises;
2335
- async function keyringFromCli(opts) {
2336
- if (opts.accountSuri) {
2337
- return keyringFromSuri(opts.accountSuri);
2338
- }
2339
- if (opts.accountFilePath) {
2340
- return keyringFromFile({
2341
- filePath: opts.accountFilePath,
2342
- passphrase: opts.accountPassphrase
2343
- });
2344
- }
2345
- throw new Error(
2346
- "No ACCOUNT account loaded (either ACCOUNT_SURI or ACCOUNT_JSON_PATH required)"
2347
- );
2348
- }
2349
2313
  function keyringFromSuri(suri, cryptoType = "sr25519") {
2350
2314
  return new import_api.Keyring({ type: cryptoType }).createFromUri(suri);
2351
2315
  }
2352
- async function keyringFromFile(opts) {
2353
- if (!opts.filePath) {
2354
- throw new Error(
2355
- "No ACCOUNT account loaded (either ACCOUNT_SURI or ACCOUNT_JSON_PATH required)"
2356
- );
2357
- }
2358
- const path = opts.filePath.replace("~", os.homedir());
2359
- const json = JSON.parse(await readFile(path, "utf-8"));
2360
- const mainAccount = new import_api.Keyring().createFromJson(json);
2361
- mainAccount.decodePkcs8(opts.passphrase);
2362
- return mainAccount;
2363
- }
2364
- async function createKeyringPair(opts) {
2365
- const { filePath, passphrase, cryptoType } = opts;
2316
+ function createKeyringPair(opts) {
2317
+ const { cryptoType } = opts;
2366
2318
  const seed = (0, import_util_crypto.mnemonicGenerate)();
2367
- const keyring = keyringFromSuri(seed, cryptoType);
2368
- if (filePath) {
2369
- const json = keyring.toJson(passphrase);
2370
- await writeFile(filePath, JSON.stringify(json, null, 2));
2371
- }
2372
- return keyring;
2319
+ return keyringFromSuri(seed, cryptoType);
2373
2320
  }
2374
2321
 
2375
2322
  // src/index.ts
@@ -2391,14 +2338,14 @@ async function getClient(host) {
2391
2338
  // src/clis/accountCli.ts
2392
2339
  var import_console_table_printer4 = require("console-table-printer");
2393
2340
  var import_util_crypto2 = require("@polkadot/util-crypto");
2394
- var import_node_fs2 = require("fs");
2341
+ var import_node_fs = require("fs");
2395
2342
  var process3 = __toESM(require("process"), 1);
2396
2343
  function accountCli() {
2397
2344
  const program = new import_extra_typings.Command("accounts").description(
2398
2345
  "Manage subaccounts from a single keypair"
2399
2346
  );
2400
2347
  program.command("watch").description("Watch for blocks closed by subaccounts").action(async () => {
2401
- const accountset = await Accountset.fromCli(program);
2348
+ const accountset = await accountsetFromCli(program);
2402
2349
  const accountMiners = await accountset.watchBlocks();
2403
2350
  accountMiners.events.on("mined", (_block, mined) => {
2404
2351
  console.log("Your accounts authored a block", mined);
@@ -2409,7 +2356,7 @@ function accountCli() {
2409
2356
  });
2410
2357
  program.command("list", { isDefault: true }).description("Show subaccounts").option("--addresses", "Just show a list of ids").action(async ({ addresses }) => {
2411
2358
  const { subaccounts } = globalOptions(program);
2412
- const accountset = await Accountset.fromCli(program);
2359
+ const accountset = await accountsetFromCli(program);
2413
2360
  if (addresses) {
2414
2361
  const addresses2 = accountset.addresses;
2415
2362
  console.log(addresses2.join(","));
@@ -2440,7 +2387,7 @@ function accountCli() {
2440
2387
  "Register the keys to a url (normally this is localhost)"
2441
2388
  ).action(async ({ registerKeysTo, path }) => {
2442
2389
  const { accountPassphrase, accountSuri, accountFilePath } = globalOptions(program);
2443
- const accountset = await Accountset.fromCli(program);
2390
+ const accountset = await accountsetFromCli(program);
2444
2391
  process3.env.KEYS_MNEMONIC ||= (0, import_util_crypto.mnemonicGenerate)();
2445
2392
  if (registerKeysTo) {
2446
2393
  await accountset.registerKeys(registerKeysTo);
@@ -2460,7 +2407,7 @@ function accountCli() {
2460
2407
  envfile += line + "\n";
2461
2408
  }
2462
2409
  }
2463
- (0, import_node_fs2.writeFileSync)(path, envfile);
2410
+ (0, import_node_fs.writeFileSync)(path, envfile);
2464
2411
  console.log("Created env file at", path);
2465
2412
  process3.exit();
2466
2413
  });
@@ -2481,7 +2428,7 @@ function accountCli() {
2481
2428
  "--print-only",
2482
2429
  "Output as curl commands instead of direct registration"
2483
2430
  ).action(async (nodeRpcUrl, { printOnly }) => {
2484
- const accountset = await Accountset.fromCli(program);
2431
+ const accountset = await accountsetFromCli(program);
2485
2432
  if (printOnly) {
2486
2433
  const { gran, seal } = accountset.keys();
2487
2434
  const commands = [];
@@ -2515,7 +2462,7 @@ function accountCli() {
2515
2462
  "Restrict this operation to a subset of the subaccounts (eg, 0-10)",
2516
2463
  parseSubaccountRange
2517
2464
  ).action(async ({ subaccounts }) => {
2518
- const accountset = await Accountset.fromCli(program);
2465
+ const accountset = await accountsetFromCli(program);
2519
2466
  const result = await accountset.consolidate(subaccounts);
2520
2467
  (0, import_console_table_printer4.printTable)(result);
2521
2468
  process3.exit(0);
@@ -2534,7 +2481,7 @@ function vaultCli() {
2534
2481
  "Monitor vaults and manage securitization"
2535
2482
  );
2536
2483
  program.command("list", { isDefault: true }).description("Show current state of vaults").action(async () => {
2537
- const accountset = await Accountset.fromCli(program);
2484
+ const accountset = await accountsetFromCli(program);
2538
2485
  const vaults = new VaultMonitor(accountset, void 0, {
2539
2486
  vaultOnlyWatchMode: true
2540
2487
  });
@@ -2550,7 +2497,7 @@ function vaultCli() {
2550
2497
  "The tip to include with the transaction",
2551
2498
  parseFloat
2552
2499
  ).action(async ({ tip, argons, vaultId, ratio }) => {
2553
- const accountset = await Accountset.fromCli(program);
2500
+ const accountset = await accountsetFromCli(program);
2554
2501
  const client = await accountset.client;
2555
2502
  const resolvedTip = tip ? BigInt(tip * 1e6) : 0n;
2556
2503
  const microgons = BigInt(argons * 1e6);
@@ -2611,7 +2558,7 @@ function vaultCli() {
2611
2558
  "Bitcoin pubkey must be 66 characters (add 0x in front optionally)"
2612
2559
  );
2613
2560
  }
2614
- const accountset = await Accountset.fromCli(program);
2561
+ const accountset = await accountsetFromCli(program);
2615
2562
  const client = await accountset.client;
2616
2563
  const resolvedTip = tip ? BigInt(tip * 1e6) : 0n;
2617
2564
  const microgons = BigInt(argons * 1e6);
@@ -2694,7 +2641,7 @@ function miningCli() {
2694
2641
  "Watch mining seats or setup bidding"
2695
2642
  );
2696
2643
  program.command("list", { isDefault: true }).description("Monitor all miners").action(async () => {
2697
- const accountset = await Accountset.fromCli(program);
2644
+ const accountset = await accountsetFromCli(program);
2698
2645
  const bids = new MiningBids(accountset.client);
2699
2646
  const api = await accountset.client;
2700
2647
  let lastMiners = {};
@@ -2790,7 +2737,7 @@ function miningCli() {
2790
2737
  bidIncrement,
2791
2738
  proxyForAddress
2792
2739
  }) => {
2793
- const accountset = await Accountset.fromCli(program, proxyForAddress);
2740
+ const accountset = await accountsetFromCli(program, proxyForAddress);
2794
2741
  let cohortBidder;
2795
2742
  const miningBids = new MiningBids(accountset.client, false);
2796
2743
  const maxCohortSize = await miningBids.maxCohortSize();
@@ -2877,7 +2824,7 @@ function miningCli() {
2877
2824
  ).action(async ({ outfile, proxyPassphrase, feeArgons }) => {
2878
2825
  const { mainchainUrl } = globalOptions(program);
2879
2826
  const client = await getClient(mainchainUrl);
2880
- const keyringPair = await createKeyringPair({
2827
+ const keyringPair = await saveKeyringPair({
2881
2828
  filePath: outfile,
2882
2829
  passphrase: proxyPassphrase
2883
2830
  });
@@ -2891,7 +2838,7 @@ function miningCli() {
2891
2838
  ]);
2892
2839
  let keypair;
2893
2840
  try {
2894
- const accountset = await Accountset.fromCli(program);
2841
+ const accountset = await accountsetFromCli(program);
2895
2842
  keypair = accountset.txSubmitterPair;
2896
2843
  } catch (e) {
2897
2844
  const polkadotLink = `https://polkadot.js.org/apps/?rpc=${mainchainUrl}#/extrinsics/decode/${tx.toHex()}`;
@@ -2919,7 +2866,7 @@ function liquidityCli() {
2919
2866
  "Monitor or bond to liquidity pools"
2920
2867
  );
2921
2868
  program.command("list", { isDefault: true }).description("Show or watch the vault bid pool rewards").action(async () => {
2922
- const accountset = await Accountset.fromCli(program);
2869
+ const accountset = await accountsetFromCli(program);
2923
2870
  const bidPool = new BidPool(
2924
2871
  accountset.client,
2925
2872
  accountset.txSubmitterPair
@@ -2935,7 +2882,7 @@ function liquidityCli() {
2935
2882
  "The tip to include with the transaction",
2936
2883
  parseFloat
2937
2884
  ).action(async ({ tip, argons, vaultId }) => {
2938
- const accountset = await Accountset.fromCli(program);
2885
+ const accountset = await accountsetFromCli(program);
2939
2886
  const resolvedTip = tip ? BigInt(tip * 1e6) : 0n;
2940
2887
  const microgons = BigInt(argons * 1e6);
2941
2888
  const bidPool = new BidPool(
@@ -2963,7 +2910,7 @@ function liquidityCli() {
2963
2910
  parseFloat
2964
2911
  ).action(async ({ maxArgons, minPctSharing, tip }) => {
2965
2912
  const maxAmountPerSlot = BigInt(maxArgons * 1e6);
2966
- const accountset = await Accountset.fromCli(program);
2913
+ const accountset = await accountsetFromCli(program);
2967
2914
  const vaults = new VaultMonitor(
2968
2915
  accountset,
2969
2916
  {
@@ -3012,7 +2959,7 @@ function bitcoinCli() {
3012
2959
  "Alert when bitcoin space exceeds this amount",
3013
2960
  parseFloat
3014
2961
  ).description("Watch for bitcoin space available").action(async ({ argons }) => {
3015
- const accountset = await Accountset.fromCli(program);
2962
+ const accountset = await accountsetFromCli(program);
3016
2963
  const bot = new VaultMonitor(accountset, {
3017
2964
  bitcoinSpaceAvailable: argons ? BigInt(argons * 1e6) : 1n
3018
2965
  });
@@ -3044,7 +2991,7 @@ function bitcoinCli() {
3044
2991
  0
3045
2992
  ).action(async ({ argons, bitcoinXpub, maxLockFee, tip }) => {
3046
2993
  const amountToLock = BigInt(argons * 1e6);
3047
- const accountset = await Accountset.fromCli(program);
2994
+ const accountset = await accountsetFromCli(program);
3048
2995
  await BitcoinLocks.waitForSpace(accountset, {
3049
2996
  argonAmount: amountToLock,
3050
2997
  bitcoinXpub,
@@ -3062,6 +3009,32 @@ function bitcoinCli() {
3062
3009
  return program;
3063
3010
  }
3064
3011
 
3012
+ // src/clis/keyringStore.ts
3013
+ var import_node_fs2 = require("fs");
3014
+ var os = __toESM(require("os"), 1);
3015
+ var { readFile, writeFile } = import_node_fs2.promises;
3016
+ async function keyringFromFile(opts) {
3017
+ if (!opts.filePath) {
3018
+ throw new Error(
3019
+ "No ACCOUNT account loaded (either ACCOUNT_SURI or ACCOUNT_JSON_PATH required)"
3020
+ );
3021
+ }
3022
+ const path = opts.filePath.replace("~", os.homedir());
3023
+ const json = JSON.parse(await readFile(path, "utf-8"));
3024
+ const mainAccount = new import_api.Keyring().createFromJson(json);
3025
+ mainAccount.decodePkcs8(opts.passphrase);
3026
+ return mainAccount;
3027
+ }
3028
+ async function saveKeyringPair(opts) {
3029
+ const { filePath, passphrase, cryptoType } = opts;
3030
+ const keyring = createKeyringPair({ cryptoType });
3031
+ if (filePath) {
3032
+ const json = keyring.toJson(passphrase);
3033
+ await writeFile(filePath, JSON.stringify(json, null, 2));
3034
+ }
3035
+ return keyring;
3036
+ }
3037
+
3065
3038
  // src/clis/index.ts
3066
3039
  function globalOptions(program) {
3067
3040
  return program.optsWithGlobals();
@@ -3091,6 +3064,38 @@ function buildCli() {
3091
3064
  ).env("SUBACCOUNT_RANGE").argParser(parseSubaccountRange)
3092
3065
  ).addCommand(accountCli()).addCommand(vaultCli()).addCommand(miningCli()).addCommand(liquidityCli()).addCommand(bitcoinCli());
3093
3066
  }
3067
+ async function accountsetFromCli(program, proxyForAddress) {
3068
+ const opts = program.parent?.optsWithGlobals();
3069
+ let keypair;
3070
+ if (opts.accountSuri) {
3071
+ keypair = keyringFromSuri(opts.accountSuri);
3072
+ }
3073
+ if (opts.accountFilePath) {
3074
+ keypair = await keyringFromFile({
3075
+ filePath: opts.accountFilePath,
3076
+ passphrase: opts.accountPassphrase
3077
+ });
3078
+ }
3079
+ if (!keypair) {
3080
+ throw new Error(
3081
+ "No ACCOUNT account loaded (either ACCOUNT_SURI or ACCOUNT_JSON_PATH required)"
3082
+ );
3083
+ }
3084
+ const client = getClient(opts.mainchainUrl);
3085
+ if (proxyForAddress) {
3086
+ return new Accountset({
3087
+ client,
3088
+ isProxy: true,
3089
+ seedAddress: proxyForAddress,
3090
+ txSubmitter: keypair
3091
+ });
3092
+ } else {
3093
+ return new Accountset({
3094
+ seedAccount: keypair,
3095
+ client
3096
+ });
3097
+ }
3098
+ }
3094
3099
  function addGlobalArgs(program) {
3095
3100
  for (const command of program.commands) {
3096
3101
  command.configureHelp({
@@ -3121,13 +3126,16 @@ function applyEnv(program) {
3121
3126
  // Annotate the CommonJS export names for ESM import in node:
3122
3127
  0 && (module.exports = {
3123
3128
  accountCli,
3129
+ accountsetFromCli,
3124
3130
  addGlobalArgs,
3125
3131
  applyEnv,
3126
3132
  bitcoinCli,
3127
3133
  buildCli,
3128
3134
  globalOptions,
3135
+ keyringFromFile,
3129
3136
  liquidityCli,
3130
3137
  miningCli,
3138
+ saveKeyringPair,
3131
3139
  vaultCli
3132
3140
  });
3133
3141
  //# sourceMappingURL=index.cjs.map