@fangorn-network/sdk 2026.9.22-dev → 2026.10.19-dev

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.
Files changed (89) hide show
  1. package/README.md +177 -192
  2. package/lib/cli/cli.d.ts +0 -0
  3. package/lib/cli/cli.js +165 -194
  4. package/lib/config.d.ts +32 -27
  5. package/lib/config.js +17 -7
  6. package/lib/contracts/app-registry/abi.d.ts +1033 -277
  7. package/lib/contracts/app-registry/abi.js +1214 -335
  8. package/lib/contracts/app-registry/index.d.ts +105 -65
  9. package/lib/contracts/app-registry/index.js +182 -175
  10. package/lib/contracts/data-registry/abi.d.ts +166 -187
  11. package/lib/contracts/data-registry/abi.js +207 -237
  12. package/lib/contracts/data-registry/index.d.ts +4 -151
  13. package/lib/contracts/data-registry/index.js +5 -279
  14. package/lib/contracts/index.d.ts +6 -7
  15. package/lib/contracts/index.js +6 -8
  16. package/lib/contracts/logs.d.ts +4 -1
  17. package/lib/contracts/logs.js +9 -2
  18. package/lib/contracts/membership-registry/abi.d.ts +1145 -0
  19. package/lib/contracts/membership-registry/abi.js +1333 -0
  20. package/lib/contracts/membership-registry/index.d.ts +79 -0
  21. package/lib/contracts/membership-registry/index.js +104 -0
  22. package/lib/contracts/types.d.ts +3 -1
  23. package/lib/contracts/types.js +3 -1
  24. package/lib/contracts/write.d.ts +34 -2
  25. package/lib/contracts/write.js +48 -1
  26. package/lib/crypto/encryption.js +4 -4
  27. package/lib/engine/car.d.ts +2 -2
  28. package/lib/engine/car.js +10 -5
  29. package/lib/engine/index.d.ts +99 -143
  30. package/lib/engine/index.js +156 -295
  31. package/lib/engine/records.d.ts +110 -0
  32. package/lib/engine/records.js +165 -0
  33. package/lib/engine/test-helpers.d.ts +7 -14
  34. package/lib/engine/test-helpers.js +7 -18
  35. package/lib/fangorn.d.ts +129 -156
  36. package/lib/fangorn.js +250 -250
  37. package/lib/feed.d.ts +7 -4
  38. package/lib/feed.js +4 -1
  39. package/lib/index.d.ts +9 -9
  40. package/lib/index.js +8 -9
  41. package/lib/node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.js +77 -0
  42. package/lib/node_modules/@vitest/expect/dist/index.js +1414 -0
  43. package/lib/node_modules/@vitest/pretty-format/dist/index.js +858 -0
  44. package/lib/node_modules/@vitest/runner/dist/chunk-tasks.js +51 -0
  45. package/lib/node_modules/@vitest/runner/dist/index.js +1199 -0
  46. package/lib/node_modules/@vitest/runner/dist/utils.js +3 -0
  47. package/lib/node_modules/@vitest/snapshot/dist/index.js +1075 -0
  48. package/lib/node_modules/@vitest/spy/dist/index.js +353 -0
  49. package/lib/node_modules/@vitest/utils/dist/chunk-_commonjsHelpers.js +7 -0
  50. package/lib/node_modules/@vitest/utils/dist/chunk-pathe.M-eThtNZ.js +81 -0
  51. package/lib/node_modules/@vitest/utils/dist/diff.js +1345 -0
  52. package/lib/node_modules/@vitest/utils/dist/display.js +534 -0
  53. package/lib/node_modules/@vitest/utils/dist/error.js +30 -0
  54. package/lib/node_modules/@vitest/utils/dist/helpers.js +118 -0
  55. package/lib/node_modules/@vitest/utils/dist/serialize.js +76 -0
  56. package/lib/node_modules/@vitest/utils/dist/source-map.js +93 -0
  57. package/lib/node_modules/@vitest/utils/dist/timers.js +36 -0
  58. package/lib/node_modules/chai/index.js +2874 -0
  59. package/lib/node_modules/magic-string/dist/magic-string.es.js +939 -0
  60. package/lib/node_modules/pathe/dist/shared/pathe.M-eThtNZ.js +81 -0
  61. package/lib/node_modules/tinyrainbow/dist/index.js +85 -0
  62. package/lib/node_modules/vitest/dist/chunks/_commonjsHelpers.D26ty3Ew.js +5 -0
  63. package/lib/node_modules/vitest/dist/chunks/date.Bq6ZW5rf.js +50 -0
  64. package/lib/node_modules/vitest/dist/chunks/utils.DvEY5TfP.js +44 -0
  65. package/lib/node_modules/vitest/dist/chunks/vi.2VT5v0um.js +2578 -0
  66. package/lib/providers/storage/signed-url.d.ts +16 -6
  67. package/lib/providers/storage/signed-url.js +20 -8
  68. package/lib/registry.d.ts +116 -0
  69. package/lib/registry.js +182 -0
  70. package/lib/test/mem-registry.d.ts +51 -0
  71. package/lib/test/mem-registry.js +107 -0
  72. package/lib/test/sdk-suite.d.ts +13 -0
  73. package/lib/test/sdk-suite.js +226 -0
  74. package/lib/test/testbed.d.ts +16411 -61
  75. package/lib/test/testbed.js +190 -84
  76. package/lib/types/index.d.ts +13 -11
  77. package/package.json +89 -73
  78. package/lib/contracts/data-registry/test.d.ts +0 -1
  79. package/lib/contracts/data-registry/test.js +0 -1
  80. package/lib/contracts/settlement-registry/abi.d.ts +0 -466
  81. package/lib/contracts/settlement-registry/abi.js +0 -535
  82. package/lib/contracts/settlement-registry/index.d.ts +0 -163
  83. package/lib/contracts/settlement-registry/index.js +0 -264
  84. package/lib/contracts/subscription-registry/abi.d.ts +0 -179
  85. package/lib/contracts/subscription-registry/abi.js +0 -195
  86. package/lib/contracts/subscription-registry/index.d.ts +0 -59
  87. package/lib/contracts/subscription-registry/index.js +0 -116
  88. package/lib/engine/graph.d.ts +0 -70
  89. package/lib/engine/graph.js +0 -73
@@ -1,27 +1,16 @@
1
1
  import { PublisherStatus } from "../types.js";
2
- import { PreparedTx } from "../data-registry/index.js";
2
+ import { PreparedTx } from "../write.js";
3
3
  import { Address, Hash, Hex, PublicClient, WalletClient } from "viem";
4
4
 
5
5
  //#region src/contracts/app-registry/index.d.ts
6
-
7
- /**
8
- * The information required of a publisher in order to join an app
9
- */
10
- interface AppJoinInfo {
11
- /** The app's terms hash (zero means no terms); e.g. an IPFS cid */
12
- termsHash: Hex;
13
- /** A uri pointing to the terms: e.g. an IPFS gateway+CID */
14
- termsUri: string;
15
- /** publisher defined joiners fee */
16
- fee: bigint;
17
- /** app publisher status (active, unregistered, suspended) */
18
- status: PublisherStatus;
19
- /** Active AND up-to-date with term agreements */
20
- registered: boolean;
21
- /** The terms hash this publisher actually accepted, or zero. */
22
- acceptedTerms: Hex;
23
- /** The protocol admin has taken the whole app down — nobody is registered. */
24
- appSuspended: boolean;
6
+ /** What the upload gate needs to decide whether to serve a publisher under an app. */
7
+ interface AppAccess {
8
+ /** May publish to the app now (see `canPublish`). */
9
+ canPublish: boolean;
10
+ /** The app's owner, or the zero address for an unclaimed app. */
11
+ owner: Address;
12
+ /** Unix seconds the app's subscription expires at; 0 if unclaimed or cancelled. */
13
+ expiresAt: bigint;
25
14
  }
26
15
  /** One `AppAgentChanged` event: an app owner pointed their app at a card. */
27
16
  interface AppAgentLog {
@@ -30,71 +19,98 @@ interface AppAgentLog {
30
19
  blockNumber: bigint;
31
20
  transactionHash: Hash;
32
21
  }
33
- /** True when the publisher joined but the app's terms have since changed. */
34
- declare function needsReacceptance(info: AppJoinInfo): boolean;
35
22
  /**
36
- * Client for the AppRegistry
23
+ * Client for the AppRegistry's app management: apps and their ERC-5643
24
+ * subscriptions, the publishers each app accepts, the agent card, and the
25
+ * protocol admin's powers. What an app presents — its members, less exclusions — is
26
+ * read through the registry (`ChainRegistry`), not this client.
27
+ *
28
+ * Scoped to one app, which may be unset: then only the calls that take no app work.
37
29
  */
38
30
  declare class AppRegistryClient {
39
31
  private contractAddress;
40
32
  private appId;
41
33
  private publicClient;
42
34
  private walletClient;
43
- constructor(contractAddress: Address, appId: Hex, publicClient: PublicClient, walletClient: WalletClient);
44
- getAppId(): Hex;
45
- setAppId(appId: Hex): void;
35
+ constructor(contractAddress: Address, appId: Hex | undefined, publicClient: PublicClient, walletClient: WalletClient);
36
+ getAppId(): Hex | undefined;
37
+ setAppId(appId: Hex | undefined): void;
46
38
  getAddress(): Address;
39
+ /** This client's app, or a clear error for a call that needs one. */
40
+ private app;
41
+ /** The app's subscription token: an app's token id is its app id. */
42
+ tokenId(appId?: Hex): bigint;
47
43
  private executeWrite;
44
+ private read;
48
45
  /**
49
- * try to register an app (if it has not been claimed)
46
+ * Claim this app, if nobody has. Claiming IS subscribing: it pays one period
47
+ * of `subscriptionFee()` in USDC and mints the app's token to the caller,
48
+ * who becomes the app's first accepted publisher.
49
+ *
50
+ * The fee read now is approved and passed as the most the claim may cost,
51
+ * so a fee raised in the meantime makes the claim revert instead of charging
52
+ * more.
50
53
  */
51
- registerApp(termsHash: Hex, termsUri: string, fee?: bigint): Promise<Hash>;
54
+ registerApp(): Promise<Hash>;
52
55
  /**
53
- * Publish new terms for this app.
56
+ * Pay for one more period (the owner, or an address they approved for the
57
+ * app's token). Allowed once less than a period is left; the period is added
58
+ * to the current expiry, or to now if it has passed.
54
59
  *
55
- * Every registered must accept the new terms in order to publish again
60
+ * The standard gives the contract no maximum-fee argument, so the allowance
61
+ * is the cap: exactly the fee read now is approved.
62
+ */
63
+ renewSubscription(): Promise<Hash>;
64
+ /**
65
+ * End this app's subscription now (owner only): the expiry becomes zero and
66
+ * nothing is refunded. Until it is renewed the app provides no storage and includes nothing.
56
67
  */
57
- setAppTerms(termsHash: Hex, termsUri: string): Promise<Hash>;
68
+ cancelSubscription(): Promise<Hash>;
69
+ /** Unix seconds this app's subscription expires at; 0 once cancelled. Reverts for an unclaimed app. */
70
+ expiresAt(appId?: Hex): Promise<bigint>;
71
+ /** True while less than one period is left, or the subscription has expired. */
72
+ isRenewable(): Promise<boolean>;
73
+ /** How long one payment lasts, in seconds. */
74
+ subscriptionPeriod(): Promise<bigint>;
75
+ /** What claiming or renewing an app costs, in USDC base units (6 decimals), not wei. */
76
+ subscriptionFee(): Promise<bigint>;
77
+ /** The ERC-20 token (USDC) every fee is paid in. */
78
+ usdc(): Promise<Address>;
79
+ getAppOwner(appId?: Hex): Promise<Address>;
58
80
  /**
59
81
  * Point this app at its ERC-8004 agent card.
60
82
  *
61
- * Safe to call whenever the card moves. It is a separate field from the terms
62
- * URI for one reason: publishers are bound to `termsHash`, so anything sharing
63
- * that slot unregisters every publisher in the app the moment it changes (see
64
- * `needsReacceptance`). A card holds endpoints and pubkeys, which rotate.
83
+ * Safe to call whenever the card moves: it leaves the app's publishers alone.
65
84
  */
66
85
  setAppAgentUri(agentUri: string): Promise<Hash>;
67
- setAppFee(fee: bigint): Promise<Hash>;
68
- /**
69
- * Suspend a publisher from this app
70
- * only callable by the app owner
71
- */
72
- suspendForApp(publisher: Address): Promise<Hash>;
73
- reinstateForApp(publisher: Address): Promise<Hash>;
86
+ /** Send the join fees the caller is still owed, from the old join flow, to `to`. */
87
+ withdrawJoinFees(to?: Address): Promise<Hash>;
88
+ /** Join fees still owed to `owner` from the old join flow, in USDC base units. Nothing adds to it. */
89
+ joinFeesOwed(owner?: Address): Promise<bigint>;
90
+ isAppSuspended(): Promise<boolean>;
91
+ /** The agent card URI of this app, or of `appId`. Empty if none is set. */
92
+ appAgentUri(appId?: Hex): Promise<string>;
74
93
  /**
75
- * Suspend an app and all publish capabilities
94
+ * Accept a publisher into this app (owner only). That is the whole
95
+ * membership: from here it uploads with storage the app provides, and the app
96
+ * includes its data, while `canPublish` holds. A suspended publisher comes
97
+ * back with `setPublisherSuspended(publisher, false)`.
76
98
  */
77
- suspendApp(): Promise<Hash>;
78
- reinstateApp(): Promise<Hash>;
79
- admin(): Promise<Address>;
99
+ acceptPublisher(publisher: Address): Promise<Hash>;
80
100
  /**
81
- * Register as a publisher within an app
82
- * Registration = accepting its current terms
101
+ * Suspend an accepted publisher from this app, or reinstate them (owner
102
+ * only). An owner cannot suspend themselves.
83
103
  */
84
- registerForApp(): Promise<Hash>;
104
+ setPublisherSuspended(publisher: Address, suspended: boolean): Promise<Hash>;
105
+ /** `publisher`'s standing in this app. */
106
+ status(publisher: Address): Promise<PublisherStatus>;
85
107
  /**
86
- * prepare the tx to register as a publisher for an app
108
+ * May `publisher` publish to this app now: accepted and not suspended, not
109
+ * banned network-wide, and the app is live and paid up.
87
110
  */
88
- prepareRegisterForApp(publisher: Address): Promise<PreparedTx & {
89
- value: Hex;
90
- }>;
91
- getAppOwner(): Promise<Address>;
92
- isRegisteredForApp(publisher: Address): Promise<boolean>;
93
- isAppSuspended(): Promise<boolean>;
94
- appTerms(): Promise<Hex>;
95
- appTermsUri(): Promise<string>;
96
- /** The agent card URI of this app, or of `appId`. Empty if none is set. */
97
- appAgentUri(appId?: Hex): Promise<string>;
111
+ canPublish(publisher: Address, appId?: Hex): Promise<boolean>;
112
+ /** The single oracle the upload gate reads for one publisher under this app. */
113
+ access(publisher: Address, appId?: Hex): Promise<AppAccess>;
98
114
  /**
99
115
  * `AppAgentChanged` events, oldest first. The last one for an app is its
100
116
  * current card; an empty `agentUri` means the owner unset it.
@@ -110,11 +126,35 @@ declare class AppRegistryClient {
110
126
  fromBlock: bigint;
111
127
  toBlock?: bigint;
112
128
  }): Promise<AppAgentLog[]>;
113
- appFee(): Promise<bigint>;
114
- acceptedTerms(publisher: Address): Promise<Hex>;
115
- joinInfo(publisher: Address): Promise<AppJoinInfo>;
116
- private readJoinInfoTuple;
129
+ admin(): Promise<Address>;
130
+ /** The DataRegistry this contract names. */
131
+ dataRegistry(): Promise<Address>;
132
+ /** Whether the protocol admin has banned `wallet` network-wide. */
133
+ isGloballySuspended(wallet: Address): Promise<boolean>;
134
+ /**
135
+ * Ban `wallet` network-wide, or lift the ban. A banned wallet cannot claim an
136
+ * app or be accepted into one, and no app stores or includes it; a banned
137
+ * owner's app keeps running, but they cannot accept anyone or move its agent card.
138
+ */
139
+ setGlobalSuspension(wallet: Address, suspended: boolean): Promise<Hash>;
140
+ /** Take this app down for every publisher at once, or restore it. */
141
+ setAppSuspended(suspended: boolean): Promise<Hash>;
142
+ setDataRegistry(registry: Address): Promise<Hash>;
143
+ setSubscriptionFee(fee: bigint): Promise<Hash>;
144
+ /** Applies from each app's next payment. */
145
+ setSubscriptionPeriod(period: bigint): Promise<Hash>;
146
+ /**
147
+ * Send `amount` of `token` to `to`. Of USDC, only the protocol's share: what
148
+ * app owners are owed in join fees stays (`ExceedsProtocolBalance`).
149
+ */
150
+ withdrawToken(token: Address, to: Address, amount: bigint): Promise<Hash>;
151
+ /**
152
+ * Fees are pulled with `transferFrom`, so the registry needs an allowance.
153
+ * Without one the call reverts with a fee error that reads like a pricing
154
+ * problem but is really an allowance one. Approves exactly `amount`.
155
+ */
156
+ private approveUsdc;
117
157
  private senderAddress;
118
158
  }
119
159
  //#endregion
120
- export { AppAgentLog, AppJoinInfo, AppRegistryClient, needsReacceptance };
160
+ export { AppAccess, AppAgentLog, AppRegistryClient, type PreparedTx };
@@ -1,16 +1,16 @@
1
1
  import { APP_REGISTRY_ABI } from "./abi.js";
2
2
  import { requireWallet, sendWrite } from "../write.js";
3
3
  import { getLogsInWindows } from "../logs.js";
4
- import { PublisherStatus } from "../types.js";
5
- import { encodeFunctionData, toHex } from "viem";
4
+ import { erc20Abi, hexToBigInt } from "viem";
6
5
 
7
6
  //#region src/contracts/app-registry/index.ts
8
- /** True when the publisher joined but the app's terms have since changed. */
9
- function needsReacceptance(info) {
10
- return !info.registered && info.status === PublisherStatus.ACTIVE && info.acceptedTerms !== info.termsHash;
11
- }
12
7
  /**
13
- * Client for the AppRegistry
8
+ * Client for the AppRegistry's app management: apps and their ERC-5643
9
+ * subscriptions, the publishers each app accepts, the agent card, and the
10
+ * protocol admin's powers. What an app presents — its members, less exclusions — is
11
+ * read through the registry (`ChainRegistry`), not this client.
12
+ *
13
+ * Scoped to one app, which may be unset: then only the calls that take no app work.
14
14
  */
15
15
  var AppRegistryClient = class {
16
16
  constructor(contractAddress, appId, publicClient, walletClient) {
@@ -28,164 +28,153 @@ var AppRegistryClient = class {
28
28
  getAddress() {
29
29
  return this.contractAddress;
30
30
  }
31
- executeWrite(functionName, args, value) {
32
- return sendWrite(this.publicClient, requireWallet(this.walletClient, functionName), this.contractAddress, APP_REGISTRY_ABI, functionName, args, value);
31
+ /** This client's app, or a clear error for a call that needs one. */
32
+ app() {
33
+ if (!this.appId) throw new Error("no app chosen: pass `appId` to Fangorn.create() or call setAppId()");
34
+ return this.appId;
35
+ }
36
+ /** The app's subscription token: an app's token id is its app id. */
37
+ tokenId(appId = this.app()) {
38
+ return hexToBigInt(appId);
39
+ }
40
+ executeWrite(functionName, args) {
41
+ return sendWrite(this.publicClient, requireWallet(this.walletClient, functionName), this.contractAddress, APP_REGISTRY_ABI, functionName, args);
42
+ }
43
+ read(functionName, args) {
44
+ return this.publicClient.readContract({
45
+ address: this.contractAddress,
46
+ abi: APP_REGISTRY_ABI,
47
+ functionName,
48
+ args
49
+ });
33
50
  }
34
51
  /**
35
- * try to register an app (if it has not been claimed)
52
+ * Claim this app, if nobody has. Claiming IS subscribing: it pays one period
53
+ * of `subscriptionFee()` in USDC and mints the app's token to the caller,
54
+ * who becomes the app's first accepted publisher.
55
+ *
56
+ * The fee read now is approved and passed as the most the claim may cost,
57
+ * so a fee raised in the meantime makes the claim revert instead of charging
58
+ * more.
36
59
  */
37
- async registerApp(termsHash, termsUri, fee = 0n) {
38
- return this.executeWrite("registerApp", [
39
- this.appId,
40
- termsHash,
41
- termsUri,
42
- fee
43
- ]);
60
+ async registerApp() {
61
+ const fee = await this.subscriptionFee();
62
+ await this.approveUsdc(fee);
63
+ return this.executeWrite("registerApp", [this.app(), fee]);
44
64
  }
45
65
  /**
46
- * Publish new terms for this app.
66
+ * Pay for one more period (the owner, or an address they approved for the
67
+ * app's token). Allowed once less than a period is left; the period is added
68
+ * to the current expiry, or to now if it has passed.
47
69
  *
48
- * Every registered must accept the new terms in order to publish again
70
+ * The standard gives the contract no maximum-fee argument, so the allowance
71
+ * is the cap: exactly the fee read now is approved.
49
72
  */
50
- async setAppTerms(termsHash, termsUri) {
51
- return this.executeWrite("setAppTerms", [
52
- this.appId,
53
- termsHash,
54
- termsUri
73
+ async renewSubscription() {
74
+ const [renewable, period, fee] = await Promise.all([
75
+ this.isRenewable(),
76
+ this.subscriptionPeriod(),
77
+ this.subscriptionFee()
55
78
  ]);
79
+ if (!renewable) {
80
+ const expiry = await this.expiresAt();
81
+ throw new Error(`app ${this.app()} cannot be renewed until less than one period is left — it expires ${(/* @__PURE__ */ new Date(Number(expiry) * 1e3)).toISOString()}`);
82
+ }
83
+ await this.approveUsdc(fee);
84
+ return this.executeWrite("renewSubscription", [this.tokenId(), period]);
56
85
  }
57
86
  /**
58
- * Point this app at its ERC-8004 agent card.
59
- *
60
- * Safe to call whenever the card moves. It is a separate field from the terms
61
- * URI for one reason: publishers are bound to `termsHash`, so anything sharing
62
- * that slot unregisters every publisher in the app the moment it changes (see
63
- * `needsReacceptance`). A card holds endpoints and pubkeys, which rotate.
87
+ * End this app's subscription now (owner only): the expiry becomes zero and
88
+ * nothing is refunded. Until it is renewed the app provides no storage and includes nothing.
64
89
  */
65
- async setAppAgentUri(agentUri) {
66
- return this.executeWrite("setAppAgentUri", [this.appId, agentUri]);
90
+ async cancelSubscription() {
91
+ return this.executeWrite("cancelSubscription", [this.tokenId()]);
67
92
  }
68
- async setAppFee(fee) {
69
- return this.executeWrite("setAppFee", [this.appId, fee]);
93
+ /** Unix seconds this app's subscription expires at; 0 once cancelled. Reverts for an unclaimed app. */
94
+ async expiresAt(appId = this.app()) {
95
+ return this.read("expiresAt", [this.tokenId(appId)]);
70
96
  }
71
- /**
72
- * Suspend a publisher from this app
73
- * only callable by the app owner
74
- */
75
- async suspendForApp(publisher) {
76
- return this.executeWrite("suspendForApp", [this.appId, publisher]);
97
+ /** True while less than one period is left, or the subscription has expired. */
98
+ async isRenewable() {
99
+ return this.read("isRenewable", [this.tokenId()]);
100
+ }
101
+ /** How long one payment lasts, in seconds. */
102
+ async subscriptionPeriod() {
103
+ return this.read("subscriptionPeriod", []);
104
+ }
105
+ /** What claiming or renewing an app costs, in USDC base units (6 decimals), not wei. */
106
+ async subscriptionFee() {
107
+ return this.read("subscriptionFee", []);
108
+ }
109
+ /** The ERC-20 token (USDC) every fee is paid in. */
110
+ async usdc() {
111
+ return this.read("usdc", []);
77
112
  }
78
- async reinstateForApp(publisher) {
79
- return this.executeWrite("reinstateForApp", [this.appId, publisher]);
113
+ async getAppOwner(appId = this.app()) {
114
+ return this.read("getAppOwner", [appId]);
80
115
  }
81
116
  /**
82
- * Suspend an app and all publish capabilities
117
+ * Point this app at its ERC-8004 agent card.
118
+ *
119
+ * Safe to call whenever the card moves: it leaves the app's publishers alone.
83
120
  */
84
- async suspendApp() {
85
- return this.executeWrite("suspendApp", [this.appId]);
121
+ async setAppAgentUri(agentUri) {
122
+ return this.executeWrite("setAppAgentUri", [this.app(), agentUri]);
86
123
  }
87
- async reinstateApp() {
88
- return this.executeWrite("reinstateApp", [this.appId]);
124
+ /** Send the join fees the caller is still owed, from the old join flow, to `to`. */
125
+ async withdrawJoinFees(to = this.senderAddress()) {
126
+ return this.executeWrite("withdrawJoinFees", [to]);
89
127
  }
90
- async admin() {
91
- return this.publicClient.readContract({
92
- address: this.contractAddress,
93
- abi: APP_REGISTRY_ABI,
94
- functionName: "admin"
95
- });
128
+ /** Join fees still owed to `owner` from the old join flow, in USDC base units. Nothing adds to it. */
129
+ async joinFeesOwed(owner = this.senderAddress()) {
130
+ return this.read("joinFeesOwed", [owner]);
131
+ }
132
+ async isAppSuspended() {
133
+ return this.read("isAppSuspended", [this.app()]);
134
+ }
135
+ /** The agent card URI of this app, or of `appId`. Empty if none is set. */
136
+ async appAgentUri(appId = this.app()) {
137
+ return this.read("appAgentUri", [appId]);
96
138
  }
97
139
  /**
98
- * Register as a publisher within an app
99
- * Registration = accepting its current terms
140
+ * Accept a publisher into this app (owner only). That is the whole
141
+ * membership: from here it uploads with storage the app provides, and the app
142
+ * includes its data, while `canPublish` holds. A suspended publisher comes
143
+ * back with `setPublisherSuspended(publisher, false)`.
100
144
  */
101
- async registerForApp() {
102
- const [termsHash, , fee] = await this.readJoinInfoTuple(this.senderAddress());
103
- if (termsHash === ZERO_HASH) throw new Error(`app ${this.appId} has published no terms — it cannot be joined until its owner sets them`);
104
- return this.executeWrite("registerForApp", [this.appId, termsHash], fee);
145
+ async acceptPublisher(publisher) {
146
+ return this.executeWrite("acceptPublisher", [this.app(), publisher]);
105
147
  }
106
148
  /**
107
- * prepare the tx to register as a publisher for an app
149
+ * Suspend an accepted publisher from this app, or reinstate them (owner
150
+ * only). An owner cannot suspend themselves.
108
151
  */
109
- async prepareRegisterForApp(publisher) {
110
- const [termsHash, , fee] = await this.readJoinInfoTuple(publisher);
111
- if (termsHash === ZERO_HASH) throw new Error(`app ${this.appId} has published no terms — it cannot be joined until its owner sets them`);
112
- const data = encodeFunctionData({
113
- abi: APP_REGISTRY_ABI,
114
- functionName: "registerForApp",
115
- args: [this.appId, termsHash]
116
- });
117
- const fees = await this.publicClient.estimateFeesPerGas();
118
- let gas;
119
- try {
120
- gas = await this.publicClient.estimateGas({
121
- account: publisher,
122
- to: this.contractAddress,
123
- data,
124
- value: fee
125
- }) * 3n / 2n;
126
- } catch (err) {
127
- const message = err instanceof Error ? err.message : String(err);
128
- if (/revert/i.test(message)) throw new Error(`joining would revert — this wallet may already be registered, or suspended from this app: ${message}`);
129
- gas = 1000000n;
130
- }
131
- return {
132
- to: this.contractAddress,
133
- data,
134
- chainId: this.publicClient.chain?.id,
135
- gas: toHex(gas),
136
- maxFeePerGas: toHex(fees.maxFeePerGas * 2n),
137
- maxPriorityFeePerGas: toHex(fees.maxPriorityFeePerGas),
138
- value: toHex(fee)
139
- };
140
- }
141
- async getAppOwner() {
142
- return this.publicClient.readContract({
143
- address: this.contractAddress,
144
- abi: APP_REGISTRY_ABI,
145
- functionName: "getAppOwner",
146
- args: [this.appId]
147
- });
148
- }
149
- async isRegisteredForApp(publisher) {
150
- return this.publicClient.readContract({
151
- address: this.contractAddress,
152
- abi: APP_REGISTRY_ABI,
153
- functionName: "isRegisteredForApp",
154
- args: [this.appId, publisher]
155
- });
156
- }
157
- async isAppSuspended() {
158
- return this.publicClient.readContract({
159
- address: this.contractAddress,
160
- abi: APP_REGISTRY_ABI,
161
- functionName: "isAppSuspended",
162
- args: [this.appId]
163
- });
152
+ async setPublisherSuspended(publisher, suspended) {
153
+ return this.executeWrite("setPublisherSuspended", [
154
+ this.app(),
155
+ publisher,
156
+ suspended
157
+ ]);
164
158
  }
165
- async appTerms() {
166
- return this.publicClient.readContract({
167
- address: this.contractAddress,
168
- abi: APP_REGISTRY_ABI,
169
- functionName: "appTerms",
170
- args: [this.appId]
171
- });
159
+ /** `publisher`'s standing in this app. */
160
+ async status(publisher) {
161
+ return await this.read("statusForApp", [this.app(), publisher]);
172
162
  }
173
- async appTermsUri() {
174
- return this.publicClient.readContract({
175
- address: this.contractAddress,
176
- abi: APP_REGISTRY_ABI,
177
- functionName: "appTermsUri",
178
- args: [this.appId]
179
- });
163
+ /**
164
+ * May `publisher` publish to this app now: accepted and not suspended, not
165
+ * banned network-wide, and the app is live and paid up.
166
+ */
167
+ async canPublish(publisher, appId = this.app()) {
168
+ return this.read("canPublish", [appId, publisher]);
180
169
  }
181
- /** The agent card URI of this app, or of `appId`. Empty if none is set. */
182
- async appAgentUri(appId = this.appId) {
183
- return this.publicClient.readContract({
184
- address: this.contractAddress,
185
- abi: APP_REGISTRY_ABI,
186
- functionName: "appAgentUri",
187
- args: [appId]
188
- });
170
+ /** The single oracle the upload gate reads for one publisher under this app. */
171
+ async access(publisher, appId = this.app()) {
172
+ const [canPublish, owner, expiresAt] = await this.read("access", [appId, publisher]);
173
+ return {
174
+ canPublish,
175
+ owner,
176
+ expiresAt
177
+ };
189
178
  }
190
179
  /**
191
180
  * `AppAgentChanged` events, oldest first. The last one for an app is its
@@ -213,46 +202,65 @@ var AppRegistryClient = class {
213
202
  transactionHash: log.transactionHash
214
203
  }));
215
204
  }
216
- async appFee() {
217
- return this.publicClient.readContract({
218
- address: this.contractAddress,
219
- abi: APP_REGISTRY_ABI,
220
- functionName: "appFee",
221
- args: [this.appId]
222
- });
205
+ async admin() {
206
+ return this.read("admin", []);
223
207
  }
224
- async acceptedTerms(publisher) {
225
- return this.publicClient.readContract({
226
- address: this.contractAddress,
227
- abi: APP_REGISTRY_ABI,
228
- functionName: "acceptedTerms",
229
- args: [this.appId, publisher]
230
- });
208
+ /** The DataRegistry this contract names. */
209
+ async dataRegistry() {
210
+ return this.read("dataRegistry", []);
211
+ }
212
+ /** Whether the protocol admin has banned `wallet` network-wide. */
213
+ async isGloballySuspended(wallet) {
214
+ return this.read("globalSuspensions", [wallet]);
215
+ }
216
+ /**
217
+ * Ban `wallet` network-wide, or lift the ban. A banned wallet cannot claim an
218
+ * app or be accepted into one, and no app stores or includes it; a banned
219
+ * owner's app keeps running, but they cannot accept anyone or move its agent card.
220
+ */
221
+ async setGlobalSuspension(wallet, suspended) {
222
+ return this.executeWrite("setGlobalSuspension", [wallet, suspended]);
223
+ }
224
+ /** Take this app down for every publisher at once, or restore it. */
225
+ async setAppSuspended(suspended) {
226
+ return this.executeWrite("setAppSuspended", [this.app(), suspended]);
227
+ }
228
+ async setDataRegistry(registry) {
229
+ return this.executeWrite("setDataRegistry", [registry]);
231
230
  }
232
- async joinInfo(publisher) {
233
- const [tuple, acceptedTerms, appSuspended] = await Promise.all([
234
- this.readJoinInfoTuple(publisher),
235
- this.acceptedTerms(publisher),
236
- this.isAppSuspended()
231
+ async setSubscriptionFee(fee) {
232
+ return this.executeWrite("setSubscriptionFee", [fee]);
233
+ }
234
+ /** Applies from each app's next payment. */
235
+ async setSubscriptionPeriod(period) {
236
+ return this.executeWrite("setSubscriptionPeriod", [period]);
237
+ }
238
+ /**
239
+ * Send `amount` of `token` to `to`. Of USDC, only the protocol's share: what
240
+ * app owners are owed in join fees stays (`ExceedsProtocolBalance`).
241
+ */
242
+ async withdrawToken(token, to, amount) {
243
+ return this.executeWrite("withdrawToken", [
244
+ token,
245
+ to,
246
+ amount
237
247
  ]);
238
- const [termsHash, termsUri, fee, status, registered] = tuple;
239
- return {
240
- termsHash,
241
- termsUri,
242
- fee,
243
- status,
244
- registered,
245
- acceptedTerms,
246
- appSuspended
247
- };
248
248
  }
249
- async readJoinInfoTuple(publisher) {
250
- return this.publicClient.readContract({
251
- address: this.contractAddress,
252
- abi: APP_REGISTRY_ABI,
253
- functionName: "joinInfo",
254
- args: [this.appId, publisher]
255
- });
249
+ /**
250
+ * Fees are pulled with `transferFrom`, so the registry needs an allowance.
251
+ * Without one the call reverts with a fee error that reads like a pricing
252
+ * problem but is really an allowance one. Approves exactly `amount`.
253
+ */
254
+ async approveUsdc(amount) {
255
+ if (amount === 0n) return;
256
+ const token = await this.usdc();
257
+ if (await this.publicClient.readContract({
258
+ address: token,
259
+ abi: erc20Abi,
260
+ functionName: "allowance",
261
+ args: [this.senderAddress(), this.contractAddress]
262
+ }) >= amount) return;
263
+ await sendWrite(this.publicClient, requireWallet(this.walletClient, "approve"), token, erc20Abi, "approve", [this.contractAddress, amount]);
256
264
  }
257
265
  senderAddress() {
258
266
  const account = this.walletClient.account;
@@ -260,7 +268,6 @@ var AppRegistryClient = class {
260
268
  return account.address;
261
269
  }
262
270
  };
263
- const ZERO_HASH = `0x${"0".repeat(64)}`;
264
271
 
265
272
  //#endregion
266
- export { AppRegistryClient, needsReacceptance };
273
+ export { AppRegistryClient };