@sage-protocol/sdk 0.1.11 → 0.1.14

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
@@ -621,7 +621,7 @@ const totalByTag = await sdk.prompt.getByTagCount({ provider, registry, tagHash
621
621
  const page = await sdk.prompt.listByTagPage({ provider, registry, tagHash, offset: 0, limit: 25 });
622
622
  ```
623
623
 
624
- Operator SubDAO helpers
624
+ Team SubDAO helpers
625
625
  ```js
626
626
  // One-click create + operatorize
627
627
  const res = await sdk.subdao.createOperatorSubDAO({
@@ -629,7 +629,7 @@ const res = await sdk.subdao.createOperatorSubDAO({
629
629
  factoryAddress: FACTORY,
630
630
  operator: '0xSafeOrEOA',
631
631
  name: 'My SubDAO',
632
- description: 'Operator controlled',
632
+ description: 'Team controlled (Timelock + Safe/EOA executor)',
633
633
  accessModel: 0,
634
634
  minStakeAmount: 0n,
635
635
  burnAmount: 1500n * 10n**18n,
@@ -14,7 +14,7 @@ var require_package = __commonJS({
14
14
  "package.json"(exports, module) {
15
15
  module.exports = {
16
16
  name: "@sage-protocol/sdk",
17
- version: "0.1.10",
17
+ version: "0.1.14",
18
18
  description: "Backend-agnostic SDK for interacting with the Sage Protocol (governance, SubDAOs, tokens).",
19
19
  main: "dist/index.cjs",
20
20
  module: "dist/index.mjs",
@@ -70,6 +70,7 @@ var require_package = __commonJS({
70
70
  release: "yarn build && npm publish --workspace @sage-protocol/sdk"
71
71
  },
72
72
  dependencies: {
73
+ "content-hash": "^2.5.2",
73
74
  "@merit-systems/echo-typescript-sdk": "^1.0.17",
74
75
  "@whetstone-research/doppler-sdk": "^0.0.1-alpha.40",
75
76
  ai: "^3.2.3",
@@ -143,11 +144,22 @@ var require_abi = __commonJS({
143
144
  "function stats() view returns (uint128 totalSubDAOsCreated, uint128 totalBurnedForCreation)",
144
145
  // On-chain enumeration fallback (naming follows FactoryCoreFacet)
145
146
  "function getSubDAOCount() view returns (uint256)",
146
- "function subDaos(uint256) view returns (address)"
147
+ "function subDaos(uint256) view returns (address)",
148
+ // ISubDAOFactory interface functions
149
+ "function getAllSubDAOs() view returns (address[])",
150
+ "function getSubDAORegistry(address subdaoAddress) view returns (address)",
151
+ "function getRegistrySubDAO(address registryAddress) view returns (address)",
152
+ "function getFactoryStats() view returns (uint256 totalSubDAOs, uint256 totalBurned, uint256 averageBurnPerSubDAO)",
153
+ "function isSubDAO(address subdaoAddress) view returns (bool)"
147
154
  ];
148
155
  var FactoryWrite = [
149
156
  "function createSubDAO(string name, string description, uint8 accessModel, uint256 minStakeAmount, uint256 burnAmount) returns (address subDAO, address registry)",
150
157
  "function createSubDAOWithStable(string name, string description, uint8 accessModel, uint256 minStakeAmount, (uint256 value,uint256 deadline,uint8 v,bytes32 r,bytes32 s) permit) returns (address subDAO, address registry)",
158
+ "function createSubDAOWithParams(string name, string description, uint8 accessModel, uint256 minStakeAmount, uint256 burnAmount, uint256 votingDelay, uint256 votingPeriod, uint256 proposalThreshold, uint256 quorumPercentage, uint8 initialForkPolicy, uint8 initialMembershipPolicy, string profileCID) returns (address subDAO, address registry)",
159
+ "function createSubDAOOperatorWithStable(string name, string description, uint8 accessModel, uint256 minStakeAmount, address operatorExecutor, address operatorAdmin, (uint256 value,uint256 deadline,uint8 v,bytes32 r,bytes32 s) permit) returns (address subDAO, address registry)",
160
+ "function createSubDAOOperatorWithStableAdvanced(string name, string description, uint8 accessModel, uint256 minStakeAmount, address operatorExecutor, address operatorAdmin, bool anyoneExec, bool governorProposer, (uint256 value,uint256 deadline,uint8 v,bytes32 r,bytes32 s) permit) returns (address subDAO, address registry)",
161
+ "function createSubDAOOperator(string name, string description, uint8 accessModel, uint256 minStakeAmount, uint256 burnAmount, address operatorExecutor, address operatorAdmin) returns (address subDAO, address registry)",
162
+ "function createSubDAOOperatorAdvanced(string name, string description, uint8 accessModel, uint256 minStakeAmount, uint256 burnAmount, address operatorExecutor, address operatorAdmin, bool anyoneExec, bool governorProposer) returns (address subDAO, address registry)",
151
163
  "function createForkedSubDAO(string newName, string newDescription, string originalName, address originalSubDAO, address forker) returns (address subDAO, address registry)",
152
164
  "function createForkedSubDAOWithStable(string newName, string newDescription, string originalName, address originalSubDAO, address forker, uint64 authorizationNonce, (uint256 value,uint256 deadline,uint8 v,bytes32 r,bytes32 s) permit) returns (address subDAO, address registry)"
153
165
  ];
@@ -378,6 +390,32 @@ var require_utils = __commonJS({
378
390
  var require_subgraph = __commonJS({
379
391
  "src/browser/subgraph.js"(exports, module) {
380
392
  var { getAddress } = require_utils();
393
+ function sanitizeOrderBy(orderBy, allowed, fallback) {
394
+ const value = (orderBy || "").toString();
395
+ return allowed.includes(value) ? value : fallback;
396
+ }
397
+ function sanitizeOrderDirection(direction, fallback = "desc") {
398
+ const v = (direction || "").toString().toLowerCase();
399
+ return v === "asc" ? "asc" : "desc";
400
+ }
401
+ function safeGetAddress(value) {
402
+ try {
403
+ return getAddress(value);
404
+ } catch {
405
+ return null;
406
+ }
407
+ }
408
+ function mapSafe(list, mapper) {
409
+ const out = [];
410
+ for (const item of list || []) {
411
+ try {
412
+ const v = mapper(item);
413
+ if (v != null) out.push(v);
414
+ } catch {
415
+ }
416
+ }
417
+ return out;
418
+ }
381
419
  async function query(url, document, variables) {
382
420
  if (!url) throw new Error("subgraph url required");
383
421
  const controller = new AbortController();
@@ -417,15 +455,21 @@ var require_subgraph = __commonJS({
417
455
  }
418
456
  }
419
457
  `, { governor: String(governor).toLowerCase(), first, skip });
420
- return (data?.proposals || []).map((p) => ({
421
- id: BigInt(p.id),
422
- proposer: getAddress(p.proposer),
423
- description: p.description,
424
- createdAt: Number(p.createdAt || 0),
425
- targets: (p.targets || []).map(getAddress),
426
- values: (p.values || []).map((value) => BigInt(String(value))),
427
- calldatas: p.calldatas || []
428
- }));
458
+ return mapSafe(data?.proposals, (p) => {
459
+ const proposer = safeGetAddress(p.proposer);
460
+ if (!proposer) return null;
461
+ const targets = (p.targets || []).map((t) => safeGetAddress(t)).filter(Boolean);
462
+ if (!targets.length && (p.targets || []).length) return null;
463
+ return {
464
+ id: BigInt(p.id),
465
+ proposer,
466
+ description: p.description,
467
+ createdAt: Number(p.createdAt || 0),
468
+ targets,
469
+ values: (p.values || []).map((value) => BigInt(String(value))),
470
+ calldatas: p.calldatas || []
471
+ };
472
+ });
429
473
  }
430
474
  var STATE_STRING_TO_NUMBER = {
431
475
  PENDING: 0,
@@ -442,6 +486,8 @@ var require_subgraph = __commonJS({
442
486
  async function listProposalsFiltered({ url, governor, states, fromTimestamp, toTimestamp, first = 20, skip = 0, orderBy = "createdAt", orderDirection = "desc" }) {
443
487
  const govLower = governor ? String(governor).toLowerCase() : null;
444
488
  const statesUpper = Array.isArray(states) && states.length ? states.map((s) => String(s).toUpperCase()) : null;
489
+ const safeOrderBy = sanitizeOrderBy(orderBy, ["createdAt", "updatedAt", "eta"], "createdAt");
490
+ const safeOrderDirection = sanitizeOrderDirection(orderDirection, "desc");
445
491
  const doc = `
446
492
  query($first: Int!, $skip: Int!, $governor: Bytes, $states: [String!], $from: Int, $to: Int) {
447
493
  proposals(
@@ -453,8 +499,8 @@ var require_subgraph = __commonJS({
453
499
  }
454
500
  first: $first
455
501
  skip: $skip
456
- orderBy: ${orderBy}
457
- orderDirection: ${orderDirection}
502
+ orderBy: ${safeOrderBy}
503
+ orderDirection: ${safeOrderDirection}
458
504
  ) {
459
505
  id
460
506
  proposer
@@ -475,19 +521,23 @@ var require_subgraph = __commonJS({
475
521
  if (fromTimestamp !== void 0) variables.from = Number(fromTimestamp);
476
522
  if (toTimestamp !== void 0) variables.to = Number(toTimestamp);
477
523
  const data = await query(url, doc, variables);
478
- return (data?.proposals || []).map((p) => {
524
+ return mapSafe(data?.proposals, (p) => {
479
525
  const stateStr = String(p.state || "").toUpperCase();
480
526
  const stateNum = STATE_STRING_TO_NUMBER[stateStr] != null ? STATE_STRING_TO_NUMBER[stateStr] : null;
527
+ const proposer = safeGetAddress(p.proposer);
528
+ if (!proposer) return null;
529
+ const targets = (p.targets || []).map((t) => safeGetAddress(t)).filter(Boolean);
530
+ if (!targets.length && (p.targets || []).length) return null;
481
531
  return {
482
532
  id: BigInt(p.id),
483
- proposer: getAddress(p.proposer),
533
+ proposer,
484
534
  description: p.description || "",
485
535
  createdAt: Number(p.createdAt || 0),
486
536
  updatedAt: Number(p.updatedAt || 0),
487
537
  state: stateStr,
488
538
  stateNum,
489
539
  eta: p.eta ? BigInt(String(p.eta)) : null,
490
- targets: (p.targets || []).map(getAddress),
540
+ targets,
491
541
  values: (p.values || []).map((value) => BigInt(String(value))),
492
542
  calldatas: p.calldatas || []
493
543
  };
@@ -506,19 +556,120 @@ var require_subgraph = __commonJS({
506
556
  }
507
557
  }
508
558
  `, { first, skip });
509
- return (data?.libraries || []).map((lib) => ({
510
- id: lib.id,
511
- manifestCID: lib.manifestCID,
512
- subdao: getAddress(lib.subDAO),
513
- proposer: getAddress(lib.proposer),
514
- createdAt: Number(lib.createdAt || 0)
515
- }));
559
+ return mapSafe(data?.libraries, (lib) => {
560
+ const sub = safeGetAddress(lib.subDAO);
561
+ const proposer = safeGetAddress(lib.proposer);
562
+ if (!sub || !proposer) return null;
563
+ return {
564
+ id: lib.id,
565
+ manifestCID: lib.manifestCID,
566
+ subdao: sub,
567
+ proposer,
568
+ createdAt: Number(lib.createdAt || 0)
569
+ };
570
+ });
571
+ }
572
+ async function getSubdaoLibraries({ url, subdao, first = 20, skip = 0 }) {
573
+ if (!url) throw new Error("subgraph url required");
574
+ const hasFilter = !!subdao;
575
+ const whereClause = hasFilter ? "where:{ subdao:$subdao }" : "";
576
+ const doc = `
577
+ query($subdao:Bytes,$first:Int!,$skip:Int!){
578
+ subDAOLibraryPointers(
579
+ ${whereClause}
580
+ first:$first,
581
+ skip:$skip,
582
+ orderBy: updatedAt,
583
+ orderDirection: desc
584
+ ){
585
+ id
586
+ subdao
587
+ libraryId
588
+ manifestCID
589
+ previousCID
590
+ promptCount
591
+ updatedAt
592
+ }
593
+ }
594
+ `;
595
+ const variables = {
596
+ first: Math.min(Math.max(1, Number(first || 20)), 100),
597
+ skip
598
+ };
599
+ if (hasFilter) {
600
+ const addr = safeGetAddress(subdao);
601
+ if (!addr) throw new Error("invalid subdao address");
602
+ variables.subdao = addr.toLowerCase();
603
+ }
604
+ const data = await query(url, doc, variables);
605
+ return mapSafe(data?.subDAOLibraryPointers, (row) => {
606
+ const id = row?.id;
607
+ const manifestCID = row?.manifestCID;
608
+ const sub = safeGetAddress(row?.subdao);
609
+ if (!id || !manifestCID || !sub) return null;
610
+ return {
611
+ id: String(id),
612
+ subdao: sub,
613
+ libraryId: String(row.libraryId || "main"),
614
+ manifestCID: String(manifestCID),
615
+ previousCID: row.previousCID || null,
616
+ promptCount: row.promptCount != null ? Number(row.promptCount) : null,
617
+ updatedAt: row.updatedAt != null ? Number(row.updatedAt) : null
618
+ };
619
+ });
620
+ }
621
+ async function getSubdaoPrompts({ url, registry, first = 50, skip = 0, orderBy = "updatedAt", orderDirection = "desc" }) {
622
+ if (!url) throw new Error("subgraph url required");
623
+ const reg = safeGetAddress(registry);
624
+ if (!reg) throw new Error("invalid registry address");
625
+ const safeOrderBy = sanitizeOrderBy(orderBy, ["updatedAt"], "updatedAt");
626
+ const safeOrderDirection = sanitizeOrderDirection(orderDirection, "desc");
627
+ const doc = `
628
+ query($registry:Bytes!,$first:Int!,$skip:Int!){
629
+ prompts(
630
+ where:{ registry:$registry },
631
+ first:$first,
632
+ skip:$skip,
633
+ orderBy: ${safeOrderBy},
634
+ orderDirection: ${safeOrderDirection}
635
+ ){
636
+ id
637
+ key
638
+ cid
639
+ version
640
+ author
641
+ registry
642
+ updatedAt
643
+ }
644
+ }
645
+ `;
646
+ const data = await query(url, doc, {
647
+ registry: reg.toLowerCase(),
648
+ first: Math.min(Math.max(1, Number(first || 50)), 100),
649
+ skip
650
+ });
651
+ return mapSafe(data?.prompts, (p) => {
652
+ const author = safeGetAddress(p.author);
653
+ const regAddr = safeGetAddress(p.registry);
654
+ if (!author || !regAddr) return null;
655
+ return {
656
+ id: String(p.id),
657
+ key: String(p.key),
658
+ cid: String(p.cid),
659
+ version: BigInt(p.version || "0"),
660
+ author,
661
+ registry: regAddr,
662
+ updatedAt: Number(p.updatedAt || 0)
663
+ };
664
+ });
516
665
  }
517
666
  module.exports = {
518
667
  query,
519
668
  listProposals,
520
669
  listProposalsFiltered,
521
670
  listLibraries,
671
+ getSubdaoLibraries,
672
+ getSubdaoPrompts,
522
673
  /**
523
674
  * Canonical proposal timeline. Tries common fields first, then event-style fallbacks.
524
675
  * Returns { id, createdAt, queuedAt, executedAt, canceledAt, eta, state } (numbers/strings may be null when unavailable).
@@ -584,86 +735,127 @@ var require_subgraph = __commonJS({
584
735
  async listLiquidityAddPlans({ url, subdao = null, first = 50, skip = 0, orderBy = "blockTimestamp", orderDirection = "desc" }) {
585
736
  if (!url) throw new Error("subgraph url required");
586
737
  const filters = [];
587
- if (subdao) filters.push(`subdao: "${String(getAddress(subdao)).toLowerCase()}"`);
738
+ if (subdao) {
739
+ const addr = safeGetAddress(subdao);
740
+ if (!addr) throw new Error("invalid subdao address");
741
+ filters.push(`subdao: "${addr.toLowerCase()}"`);
742
+ }
588
743
  const where = filters.length ? `where: { ${filters.join(", ")} }` : "";
744
+ const safeOrderBy = sanitizeOrderBy(orderBy, ["blockTimestamp", "blockNumber"], "blockTimestamp");
745
+ const safeOrderDirection = sanitizeOrderDirection(orderDirection, "desc");
589
746
  const doc = `
590
747
  query($first:Int!,$skip:Int!){
591
- liquidityAddPlans(${where} first:$first, skip:$skip, orderBy: ${orderBy}, orderDirection: ${orderDirection}){
748
+ liquidityAddPlans(${where} first:$first, skip:$skip, orderBy: ${safeOrderBy}, orderDirection: ${safeOrderDirection}){
592
749
  id subdao pool sxxxToken stableToken sxxxAmount stableAmount lpRecipient blockNumber blockTimestamp transactionHash
593
750
  }
594
751
  }
595
752
  `;
596
753
  const data = await query(url, doc, { first, skip });
597
- return (data?.liquidityAddPlans || []).map((e) => ({
598
- id: String(e.id),
599
- subdao: getAddress(e.subdao),
600
- pool: getAddress(e.pool),
601
- sxxxToken: getAddress(e.sxxxToken),
602
- stableToken: getAddress(e.stableToken),
603
- sxxxAmount: BigInt(String(e.sxxxAmount)),
604
- stableAmount: BigInt(String(e.stableAmount)),
605
- lpRecipient: getAddress(e.lpRecipient),
606
- blockNumber: Number(e.blockNumber || 0),
607
- blockTimestamp: Number(e.blockTimestamp || 0),
608
- transactionHash: e.transactionHash
609
- }));
754
+ return mapSafe(data?.liquidityAddPlans, (e) => {
755
+ const sub = safeGetAddress(e.subdao);
756
+ const pool = safeGetAddress(e.pool);
757
+ const sxxxToken = safeGetAddress(e.sxxxToken);
758
+ const stableToken = safeGetAddress(e.stableToken);
759
+ const lpRecipient = safeGetAddress(e.lpRecipient);
760
+ if (!sub || !pool || !sxxxToken || !stableToken || !lpRecipient) return null;
761
+ return {
762
+ id: String(e.id),
763
+ subdao: sub,
764
+ pool,
765
+ sxxxToken,
766
+ stableToken,
767
+ sxxxAmount: BigInt(String(e.sxxxAmount)),
768
+ stableAmount: BigInt(String(e.stableAmount)),
769
+ lpRecipient,
770
+ blockNumber: Number(e.blockNumber || 0),
771
+ blockTimestamp: Number(e.blockTimestamp || 0),
772
+ transactionHash: e.transactionHash
773
+ };
774
+ });
610
775
  },
611
776
  async listLiquidityRemovePlans({ url, subdao = null, first = 50, skip = 0, orderBy = "blockTimestamp", orderDirection = "desc" }) {
612
777
  if (!url) throw new Error("subgraph url required");
613
778
  const filters = [];
614
- if (subdao) filters.push(`subdao: "${String(getAddress(subdao)).toLowerCase()}"`);
779
+ if (subdao) {
780
+ const addr = safeGetAddress(subdao);
781
+ if (!addr) throw new Error("invalid subdao address");
782
+ filters.push(`subdao: "${addr.toLowerCase()}"`);
783
+ }
615
784
  const where = filters.length ? `where: { ${filters.join(", ")} }` : "";
785
+ const safeOrderBy = sanitizeOrderBy(orderBy, ["blockTimestamp", "blockNumber"], "blockTimestamp");
786
+ const safeOrderDirection = sanitizeOrderDirection(orderDirection, "desc");
616
787
  const doc = `
617
788
  query($first:Int!,$skip:Int!){
618
- liquidityRemovePlans(${where} first:$first, skip:$skip, orderBy: ${orderBy}, orderDirection: ${orderDirection}){
789
+ liquidityRemovePlans(${where} first:$first, skip:$skip, orderBy: ${safeOrderBy}, orderDirection: ${safeOrderDirection}){
619
790
  id subdao pool lpToken lpAmount recipient blockNumber blockTimestamp transactionHash
620
791
  }
621
792
  }
622
793
  `;
623
794
  const data = await query(url, doc, { first, skip });
624
- return (data?.liquidityRemovePlans || []).map((e) => ({
625
- id: String(e.id),
626
- subdao: getAddress(e.subdao),
627
- pool: getAddress(e.pool),
628
- lpToken: getAddress(e.lpToken),
629
- lpAmount: BigInt(String(e.lpAmount)),
630
- recipient: getAddress(e.recipient),
631
- blockNumber: Number(e.blockNumber || 0),
632
- blockTimestamp: Number(e.blockTimestamp || 0),
633
- transactionHash: e.transactionHash
634
- }));
795
+ return mapSafe(data?.liquidityRemovePlans, (e) => {
796
+ const sub = safeGetAddress(e.subdao);
797
+ const pool = safeGetAddress(e.pool);
798
+ const lpToken = safeGetAddress(e.lpToken);
799
+ const recipient = safeGetAddress(e.recipient);
800
+ if (!sub || !pool || !lpToken || !recipient) return null;
801
+ return {
802
+ id: String(e.id),
803
+ subdao: sub,
804
+ pool,
805
+ lpToken,
806
+ lpAmount: BigInt(String(e.lpAmount)),
807
+ recipient,
808
+ blockNumber: Number(e.blockNumber || 0),
809
+ blockTimestamp: Number(e.blockTimestamp || 0),
810
+ transactionHash: e.transactionHash
811
+ };
812
+ });
635
813
  },
636
814
  async listPromptsByTag({ url, tagsHash, registry = null, first = 50, skip = 0, orderBy = "updatedAt", orderDirection = "desc" }) {
637
815
  if (!url) throw new Error("subgraph url required");
638
816
  const clauses = [`tagsHash: "${String(tagsHash)}"`];
639
- if (registry) clauses.push(`registry: "${String(getAddress(registry)).toLowerCase()}"`);
817
+ if (registry) {
818
+ const addr = safeGetAddress(registry);
819
+ if (!addr) throw new Error("invalid registry address");
820
+ clauses.push(`registry: "${addr.toLowerCase()}"`);
821
+ }
640
822
  const where = `where: { ${clauses.join(", ")} }`;
823
+ const safeOrderBy = sanitizeOrderBy(orderBy, ["updatedAt"], "updatedAt");
824
+ const safeOrderDirection = sanitizeOrderDirection(orderDirection, "desc");
641
825
  const doc = `
642
826
  query($first:Int!,$skip:Int!) {
643
- prompts(${where} first:$first, skip:$skip, orderBy: ${orderBy}, orderDirection: ${orderDirection}) {
827
+ prompts(${where} first:$first, skip:$skip, orderBy: ${safeOrderBy}, orderDirection: ${safeOrderDirection}) {
644
828
  id key cid version author registry updatedAt tagsHash
645
829
  }
646
830
  }
647
831
  `;
648
832
  const data = await query(url, doc, { first, skip });
649
- return (data?.prompts || []).map((p) => ({
650
- id: String(p.id),
651
- key: String(p.key),
652
- cid: String(p.cid),
653
- version: BigInt(p.version || "0"),
654
- author: getAddress(p.author),
655
- registry: getAddress(p.registry),
656
- updatedAt: Number(p.updatedAt || 0),
657
- tagsHash: String(p.tagsHash || "")
658
- }));
833
+ return mapSafe(data?.prompts, (p) => {
834
+ const author = safeGetAddress(p.author);
835
+ const regAddr = safeGetAddress(p.registry);
836
+ if (!author || !regAddr) return null;
837
+ return {
838
+ id: String(p.id),
839
+ key: String(p.key),
840
+ cid: String(p.cid),
841
+ version: BigInt(p.version || "0"),
842
+ author,
843
+ registry: regAddr,
844
+ updatedAt: Number(p.updatedAt || 0),
845
+ tagsHash: String(p.tagsHash || "")
846
+ };
847
+ });
659
848
  },
660
849
  // Prompt helpers (registry scoped)
661
850
  async listRegistryPrompts({ url, registry, first = 50, skip = 0, orderBy = "updatedAt", orderDirection = "desc" }) {
662
851
  if (!url) throw new Error("subgraph url required");
663
- const reg = getAddress(registry);
852
+ const reg = safeGetAddress(registry);
853
+ if (!reg) throw new Error("invalid registry address");
854
+ const safeOrderBy = sanitizeOrderBy(orderBy, ["updatedAt"], "updatedAt");
855
+ const safeOrderDirection = sanitizeOrderDirection(orderDirection, "desc");
664
856
  const doc = `
665
857
  query($first:Int!,$skip:Int!,$registry:Bytes!) {
666
- prompts(where:{ registry: $registry }, first:$first, skip:$skip, orderBy: ${orderBy}, orderDirection: ${orderDirection}) {
858
+ prompts(where:{ registry: $registry }, first:$first, skip:$skip, orderBy: ${safeOrderBy}, orderDirection: ${safeOrderDirection}) {
667
859
  id
668
860
  key
669
861
  cid
@@ -675,19 +867,25 @@ var require_subgraph = __commonJS({
675
867
  }
676
868
  `;
677
869
  const data = await query(url, doc, { first, skip, registry: reg.toLowerCase() });
678
- return (data?.prompts || []).map((p) => ({
679
- id: String(p.id),
680
- key: String(p.key),
681
- cid: String(p.cid),
682
- version: BigInt(p.version || "0"),
683
- author: getAddress(p.author),
684
- registry: getAddress(p.registry),
685
- updatedAt: Number(p.updatedAt || 0)
686
- }));
870
+ return mapSafe(data?.prompts, (p) => {
871
+ const author = safeGetAddress(p.author);
872
+ const regAddr = safeGetAddress(p.registry);
873
+ if (!author || !regAddr) return null;
874
+ return {
875
+ id: String(p.id),
876
+ key: String(p.key),
877
+ cid: String(p.cid),
878
+ version: BigInt(p.version || "0"),
879
+ author,
880
+ registry: regAddr,
881
+ updatedAt: Number(p.updatedAt || 0)
882
+ };
883
+ });
687
884
  },
688
885
  async getPromptByKey({ url, registry, key }) {
689
886
  if (!url) throw new Error("subgraph url required");
690
- const reg = getAddress(registry);
887
+ const reg = safeGetAddress(registry);
888
+ if (!reg) throw new Error("invalid registry address");
691
889
  const doc = `
692
890
  query($registry:Bytes!,$key:String!) {
693
891
  prompts(where:{ registry: $registry, key: $key }, first:1) {
@@ -703,15 +901,19 @@ var require_subgraph = __commonJS({
703
901
  `;
704
902
  const data = await query(url, doc, { registry: reg.toLowerCase(), key: String(key) });
705
903
  const p = (data?.prompts || [])[0];
706
- return p ? {
904
+ if (!p) return null;
905
+ const author = safeGetAddress(p.author);
906
+ const regAddr = safeGetAddress(p.registry);
907
+ if (!author || !regAddr) return null;
908
+ return {
707
909
  id: String(p.id),
708
910
  key: String(p.key),
709
911
  cid: String(p.cid),
710
912
  version: BigInt(p.version || "0"),
711
- author: getAddress(p.author),
712
- registry: getAddress(p.registry),
913
+ author,
914
+ registry: regAddr,
713
915
  updatedAt: Number(p.updatedAt || 0)
714
- } : null;
916
+ };
715
917
  },
716
918
  async getProposalById({ url, id }) {
717
919
  if (!url) throw new Error("subgraph url required");
@@ -719,18 +921,26 @@ var require_subgraph = __commonJS({
719
921
  const data = await query(url, doc, { id: String(id) });
720
922
  const p = data?.proposal;
721
923
  if (!p) return null;
722
- return {
723
- id: BigInt(p.id),
724
- proposer: getAddress(p.proposer),
725
- description: p.description || "",
726
- createdAt: Number(p.createdAt || 0),
727
- updatedAt: Number(p.updatedAt || 0),
728
- state: String(p.state || ""),
729
- eta: p.eta ? BigInt(String(p.eta)) : null,
730
- targets: (p.targets || []).map(getAddress),
731
- values: (p.values || []).map((v) => BigInt(String(v))),
732
- calldatas: p.calldatas || []
733
- };
924
+ try {
925
+ const proposer = safeGetAddress(p.proposer);
926
+ if (!proposer) return null;
927
+ const targets = (p.targets || []).map((t) => safeGetAddress(t)).filter(Boolean);
928
+ if (!targets.length && (p.targets || []).length) return null;
929
+ return {
930
+ id: BigInt(p.id),
931
+ proposer,
932
+ description: p.description || "",
933
+ createdAt: Number(p.createdAt || 0),
934
+ updatedAt: Number(p.updatedAt || 0),
935
+ state: String(p.state || ""),
936
+ eta: p.eta ? BigInt(String(p.eta)) : null,
937
+ targets,
938
+ values: (p.values || []).map((v) => BigInt(String(v))),
939
+ calldatas: p.calldatas || []
940
+ };
941
+ } catch {
942
+ return null;
943
+ }
734
944
  }
735
945
  };
736
946
  }