moltlaunch 2.5.0 → 2.6.0

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
@@ -553,7 +553,9 @@ async function getAgentByOwner(ownerAddress) {
553
553
  functionName: "balanceOf",
554
554
  args: [ownerAddress]
555
555
  });
556
- if (balance === 0n) return null;
556
+ if (balance === 0n) {
557
+ return getAgentByOwnerFromApi(ownerAddress);
558
+ }
557
559
  const logs = await client.getLogs({
558
560
  address: CONTRACTS.IDENTITY_REGISTRY,
559
561
  event: {
@@ -569,9 +571,23 @@ async function getAgentByOwner(ownerAddress) {
569
571
  fromBlock: 0n,
570
572
  toBlock: "latest"
571
573
  });
572
- if (logs.length === 0) return null;
573
- const lastLog = logs[logs.length - 1];
574
- return lastLog.args.agentId ?? null;
574
+ if (logs.length > 0) {
575
+ const lastLog = logs[logs.length - 1];
576
+ return lastLog.args.agentId ?? null;
577
+ }
578
+ return getAgentByOwnerFromApi(ownerAddress);
579
+ } catch {
580
+ return getAgentByOwnerFromApi(ownerAddress);
581
+ }
582
+ }
583
+ async function getAgentByOwnerFromApi(ownerAddress) {
584
+ try {
585
+ const res = await fetch(`${APIS.MOLTLAUNCH}/api/agents/by-wallet/${ownerAddress}`);
586
+ if (!res.ok) return null;
587
+ const data = await res.json();
588
+ if (!data.agents || data.agents.length === 0) return null;
589
+ const idStr = data.agents[0].id;
590
+ return BigInt(idStr.startsWith("0x") ? idStr : idStr);
575
591
  } catch {
576
592
  return null;
577
593
  }