@blockrun/clawrouter 0.12.2 → 0.12.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/dist/index.js CHANGED
@@ -742,6 +742,7 @@ import {
742
742
  setTransactionMessageLifetimeUsingBlockhash,
743
743
  appendTransactionMessageInstructions,
744
744
  signTransactionMessageWithSigners,
745
+ addSignersToTransactionMessage,
745
746
  getSignatureFromTransaction,
746
747
  sendAndConfirmTransactionFactory,
747
748
  getProgramDerivedAddress,
@@ -843,7 +844,7 @@ async function sweepSolanaWallet(oldKeyBytes, newKeyBytes, rpcUrl) {
843
844
  };
844
845
  }
845
846
  let usdcBalance = 0n;
846
- let oldTokenAccount;
847
+ const oldTokenAccounts = [];
847
848
  try {
848
849
  const response = await rpc.getTokenAccountsByOwner(
849
850
  solAddress2(oldAddress),
@@ -856,7 +857,7 @@ async function sweepSolanaWallet(oldKeyBytes, newKeyBytes, rpcUrl) {
856
857
  const amount = BigInt(parsed.parsed.info.tokenAmount.amount);
857
858
  if (amount > 0n) {
858
859
  usdcBalance += amount;
859
- oldTokenAccount = account.pubkey;
860
+ oldTokenAccounts.push({ pubkey: account.pubkey, amount });
860
861
  }
861
862
  }
862
863
  }
@@ -887,13 +888,6 @@ async function sweepSolanaWallet(oldKeyBytes, newKeyBytes, rpcUrl) {
887
888
  usdcBalance
888
889
  };
889
890
  }
890
- if (!oldTokenAccount) {
891
- return {
892
- error: "Could not find USDC token account in old wallet.",
893
- oldAddress,
894
- newAddress
895
- };
896
- }
897
891
  try {
898
892
  const newAta = await getAssociatedTokenAddress(solAddress2(newAddress), mint);
899
893
  const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
@@ -904,21 +898,27 @@ async function sweepSolanaWallet(oldKeyBytes, newKeyBytes, rpcUrl) {
904
898
  solAddress2(newAddress),
905
899
  mint
906
900
  );
907
- const transferIx = buildTokenTransferInstruction(
908
- solAddress2(oldTokenAccount),
909
- newAta,
910
- oldSigner.address,
911
- // old wallet authorizes the token transfer
912
- usdcBalance
901
+ const transferIxs = oldTokenAccounts.map(
902
+ (acct) => buildTokenTransferInstruction(
903
+ solAddress2(acct.pubkey),
904
+ newAta,
905
+ oldSigner.address,
906
+ // old wallet authorizes the token transfer
907
+ acct.amount
908
+ )
913
909
  );
914
910
  const txMessage = pipe(
915
911
  createTransactionMessage({ version: 0 }),
916
912
  (msg) => setTransactionMessageFeePayer(newSigner.address, msg),
917
913
  // new wallet pays gas
918
914
  (msg) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, msg),
919
- (msg) => appendTransactionMessageInstructions([createAtaIx, transferIx], msg)
915
+ (msg) => appendTransactionMessageInstructions([createAtaIx, ...transferIxs], msg)
916
+ );
917
+ const txMessageWithSigners = addSignersToTransactionMessage(
918
+ [newSigner, oldSigner],
919
+ txMessage
920
920
  );
921
- const signedTx = await signTransactionMessageWithSigners(txMessage);
921
+ const signedTx = await signTransactionMessageWithSigners(txMessageWithSigners);
922
922
  const txSignature = getSignatureFromTransaction(signedTx);
923
923
  const wsUrl = url.replace("https://", "wss://").replace("http://", "ws://");
924
924
  const rpcSubscriptions = createSolanaRpcSubscriptions(wsUrl);
@@ -3289,9 +3289,15 @@ import { openSync, readSync, closeSync, fstatSync } from "fs";
3289
3289
  async function readTextFile(filePath) {
3290
3290
  const fh = await open(filePath, "r");
3291
3291
  try {
3292
- const buf = Buffer.alloc((await fh.stat()).size);
3293
- await fh.read(buf, 0, buf.length, 0);
3294
- return buf.toString("utf-8");
3292
+ const size = (await fh.stat()).size;
3293
+ const buf = Buffer.alloc(size);
3294
+ let offset = 0;
3295
+ while (offset < size) {
3296
+ const { bytesRead } = await fh.read(buf, offset, size - offset, offset);
3297
+ if (bytesRead === 0) break;
3298
+ offset += bytesRead;
3299
+ }
3300
+ return buf.subarray(0, offset).toString("utf-8");
3295
3301
  } finally {
3296
3302
  await fh.close();
3297
3303
  }
@@ -3299,9 +3305,15 @@ async function readTextFile(filePath) {
3299
3305
  function readTextFileSync(filePath) {
3300
3306
  const fd = openSync(filePath, "r");
3301
3307
  try {
3302
- const buf = Buffer.alloc(fstatSync(fd).size);
3303
- readSync(fd, buf);
3304
- return buf.toString("utf-8");
3308
+ const size = fstatSync(fd).size;
3309
+ const buf = Buffer.alloc(size);
3310
+ let offset = 0;
3311
+ while (offset < size) {
3312
+ const bytesRead = readSync(fd, buf, offset, size - offset, offset);
3313
+ if (bytesRead === 0) break;
3314
+ offset += bytesRead;
3315
+ }
3316
+ return buf.subarray(0, offset).toString("utf-8");
3305
3317
  } finally {
3306
3318
  closeSync(fd);
3307
3319
  }
@@ -3328,18 +3340,23 @@ async function parseLogFile(filePath) {
3328
3340
  try {
3329
3341
  const content = await readTextFile(filePath);
3330
3342
  const lines = content.trim().split("\n").filter(Boolean);
3331
- return lines.map((line) => {
3332
- const entry = JSON.parse(line);
3333
- return {
3334
- timestamp: entry.timestamp || (/* @__PURE__ */ new Date()).toISOString(),
3335
- model: entry.model || "unknown",
3336
- tier: entry.tier || "UNKNOWN",
3337
- cost: entry.cost || 0,
3338
- baselineCost: entry.baselineCost || entry.cost || 0,
3339
- savings: entry.savings || 0,
3340
- latencyMs: entry.latencyMs || 0
3341
- };
3342
- });
3343
+ const entries = [];
3344
+ for (const line of lines) {
3345
+ try {
3346
+ const entry = JSON.parse(line);
3347
+ entries.push({
3348
+ timestamp: entry.timestamp || (/* @__PURE__ */ new Date()).toISOString(),
3349
+ model: entry.model || "unknown",
3350
+ tier: entry.tier || "UNKNOWN",
3351
+ cost: entry.cost || 0,
3352
+ baselineCost: entry.baselineCost || entry.cost || 0,
3353
+ savings: entry.savings || 0,
3354
+ latencyMs: entry.latencyMs || 0
3355
+ });
3356
+ } catch {
3357
+ }
3358
+ }
3359
+ return entries;
3343
3360
  } catch {
3344
3361
  return [];
3345
3362
  }