@fangorn-network/sdk 0.1.0-dev → 0.1.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.
Files changed (96) hide show
  1. package/lib/cli/cli.js +328 -128
  2. package/lib/cli/index.d.ts +4 -1
  3. package/lib/cli/index.js +20 -2
  4. package/lib/config.d.ts +111 -48
  5. package/lib/config.js +39 -11
  6. package/lib/contracts/app-registry/abi.d.ts +1265 -0
  7. package/lib/contracts/app-registry/abi.js +1454 -0
  8. package/lib/contracts/app-registry/index.d.ts +161 -0
  9. package/lib/contracts/app-registry/index.js +274 -0
  10. package/lib/contracts/data-registry/abi.d.ts +19 -195
  11. package/lib/contracts/data-registry/abi.js +206 -409
  12. package/lib/contracts/data-registry/index.d.ts +2 -126
  13. package/lib/contracts/data-registry/index.js +1 -174
  14. package/lib/contracts/index.d.ts +7 -4
  15. package/lib/contracts/index.js +7 -4
  16. package/lib/contracts/logs.d.ts +1 -1
  17. package/lib/contracts/logs.js +1 -1
  18. package/lib/contracts/{settlement-registry → membership-registry}/abi.d.ts +636 -301
  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 +10 -0
  23. package/lib/contracts/types.js +12 -0
  24. package/lib/crypto/aes.d.ts +2 -2
  25. package/lib/crypto/aes.js +4 -4
  26. package/lib/crypto/encryption.d.ts +109 -8
  27. package/lib/crypto/encryption.js +114 -16
  28. package/lib/engine/car.d.ts +1 -5
  29. package/lib/engine/car.js +2 -2
  30. package/lib/engine/index.d.ts +14 -33
  31. package/lib/engine/index.js +41 -98
  32. package/lib/engine/records.d.ts +8 -19
  33. package/lib/engine/records.js +7 -81
  34. package/lib/engine/test-helpers.d.ts +3 -2
  35. package/lib/engine/test-helpers.js +5 -7
  36. package/lib/erc8004.js +10 -7
  37. package/lib/fangorn.d.ts +86 -65
  38. package/lib/fangorn.js +153 -115
  39. package/lib/feed.d.ts +8 -7
  40. package/lib/feed.js +8 -7
  41. package/lib/harness/examples/hackmd/build-markdown-graph.d.ts +1 -0
  42. package/lib/harness/examples/hackmd/build-markdown-graph.js +17 -0
  43. package/lib/harness/index.d.ts +58 -0
  44. package/lib/harness/index.js +73 -0
  45. package/lib/index.d.ts +13 -10
  46. package/lib/index.js +13 -10
  47. package/lib/node_modules/{@jridgewell → .pnpm/@jridgewell_sourcemap-codec@1.5.5/node_modules/@jridgewell}/sourcemap-codec/dist/sourcemap-codec.js +1 -1
  48. package/lib/node_modules/{@vitest → .pnpm/@vitest_expect@4.0.18/node_modules/@vitest}/expect/dist/index.js +8 -8
  49. package/lib/node_modules/{@vitest → .pnpm/@vitest_pretty-format@4.0.18/node_modules/@vitest}/pretty-format/dist/index.js +2 -2
  50. package/lib/node_modules/{@vitest → .pnpm/@vitest_runner@4.0.18/node_modules/@vitest}/runner/dist/chunk-tasks.js +2 -2
  51. package/lib/node_modules/{@vitest → .pnpm/@vitest_runner@4.0.18/node_modules/@vitest}/runner/dist/index.js +4 -4
  52. package/lib/node_modules/{@vitest → .pnpm/@vitest_snapshot@4.0.18/node_modules/@vitest}/snapshot/dist/index.js +4 -4
  53. package/lib/node_modules/{@vitest → .pnpm/@vitest_spy@4.0.18/node_modules/@vitest}/spy/dist/index.js +1 -1
  54. package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/chunk-_commonjsHelpers.js +1 -1
  55. package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/chunk-pathe.M-eThtNZ.js +1 -1
  56. package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/diff.js +3 -3
  57. package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/display.js +2 -2
  58. package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/error.js +2 -2
  59. package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/helpers.js +1 -1
  60. package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/serialize.js +1 -1
  61. package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/source-map.js +1 -1
  62. package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/timers.js +1 -1
  63. package/lib/node_modules/{chai → .pnpm/chai@6.2.2/node_modules/chai}/index.js +1 -1
  64. package/lib/node_modules/{magic-string → .pnpm/magic-string@0.30.21/node_modules/magic-string}/dist/magic-string.es.js +2 -2
  65. package/lib/node_modules/{pathe → .pnpm/pathe@2.0.3/node_modules/pathe}/dist/shared/pathe.M-eThtNZ.js +1 -1
  66. package/lib/node_modules/{tinyrainbow → .pnpm/tinyrainbow@3.0.3/node_modules/tinyrainbow}/dist/index.js +1 -1
  67. package/lib/node_modules/{vitest → .pnpm/vitest@4.0.18_@types_node@24.10.1_jiti@2.6.1_terser@5.51.2_tsx@4.21.0_yaml@2.8.2/node_modules/vitest}/dist/chunks/_commonjsHelpers.D26ty3Ew.js +1 -1
  68. package/lib/node_modules/{vitest → .pnpm/vitest@4.0.18_@types_node@24.10.1_jiti@2.6.1_terser@5.51.2_tsx@4.21.0_yaml@2.8.2/node_modules/vitest}/dist/chunks/date.Bq6ZW5rf.js +1 -1
  69. package/lib/node_modules/{vitest → .pnpm/vitest@4.0.18_@types_node@24.10.1_jiti@2.6.1_terser@5.51.2_tsx@4.21.0_yaml@2.8.2/node_modules/vitest}/dist/chunks/utils.DvEY5TfP.js +2 -2
  70. package/lib/node_modules/{vitest → .pnpm/vitest@4.0.18_@types_node@24.10.1_jiti@2.6.1_terser@5.51.2_tsx@4.21.0_yaml@2.8.2/node_modules/vitest}/dist/chunks/vi.2VT5v0um.js +11 -11
  71. package/lib/providers/storage/signed-url.d.ts +10 -2
  72. package/lib/providers/storage/signed-url.js +15 -5
  73. package/lib/providers/storage/types.d.ts +1 -1
  74. package/lib/providers/storage/utils.js +3 -5
  75. package/lib/registry.d.ts +118 -0
  76. package/lib/registry.js +184 -0
  77. package/lib/test/mem-registry.d.ts +19 -14
  78. package/lib/test/mem-registry.js +37 -44
  79. package/lib/test/sdk-suite.d.ts +4 -1
  80. package/lib/test/sdk-suite.js +89 -18
  81. package/lib/test/testbed.d.ts +5227 -5218
  82. package/lib/test/testbed.js +56 -29
  83. package/lib/types/index.d.ts +11 -6
  84. package/package.json +1 -1
  85. package/lib/cli/sell.d.ts +0 -28
  86. package/lib/cli/sell.js +0 -95
  87. package/lib/cli/stage.d.ts +0 -19
  88. package/lib/cli/stage.js +0 -94
  89. package/lib/contracts/settlement-registry/abi.js +0 -962
  90. package/lib/contracts/settlement-registry/index.d.ts +0 -143
  91. package/lib/contracts/settlement-registry/index.js +0 -213
  92. package/lib/crypto/paid.d.ts +0 -104
  93. package/lib/crypto/paid.js +0 -147
  94. package/lib/test/key-server.d.ts +0 -12
  95. package/lib/test/key-server.js +0 -30
  96. /package/lib/node_modules/{@vitest → .pnpm/@vitest_runner@4.0.18/node_modules/@vitest}/runner/dist/utils.js +0 -0
@@ -0,0 +1,161 @@
1
+ import { PublisherStatus } from "../types.js";
2
+ import { PreparedTx } from "../write.js";
3
+ import { Address, Hash, Hex, PublicClient, WalletClient } from "viem";
4
+
5
+ //#region src/contracts/app-registry/index.d.ts
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;
14
+ }
15
+ /** One `AppAgentChanged` event: an app owner pointed their app at a card. */
16
+ interface AppAgentLog {
17
+ appId: Hex;
18
+ agentUri: string;
19
+ blockNumber: bigint;
20
+ transactionHash: Hash;
21
+ }
22
+ /**
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.
29
+ */
30
+ declare class AppRegistryClient {
31
+ private contractAddress;
32
+ private appId;
33
+ private publicClient;
34
+ private walletClient;
35
+ private logWindow?;
36
+ constructor(contractAddress: Address, appId: Hex | undefined, publicClient: PublicClient, walletClient: WalletClient, logWindow?: bigint | undefined);
37
+ getAppId(): Hex | undefined;
38
+ setAppId(appId: Hex | undefined): void;
39
+ getAddress(): Address;
40
+ /** This client's app, or a clear error for a call that needs one. */
41
+ private app;
42
+ /** The app's subscription token: an app's token id is its app id. */
43
+ tokenId(appId?: Hex): bigint;
44
+ private executeWrite;
45
+ private read;
46
+ /**
47
+ * Claim this app, if nobody has. Claiming IS subscribing: it pays one period
48
+ * of `subscriptionFee()` in USDC and mints the app's token to the caller,
49
+ * who becomes the app's first accepted publisher.
50
+ *
51
+ * The fee read now is approved and passed as the most the claim may cost,
52
+ * so a fee raised in the meantime makes the claim revert instead of charging
53
+ * more.
54
+ */
55
+ registerApp(): Promise<Hash>;
56
+ /**
57
+ * Pay for one more period (the owner, or an address they approved for the
58
+ * app's token). Allowed once less than a period is left; the period is added
59
+ * to the current expiry, or to now if it has passed.
60
+ *
61
+ * The standard gives the contract no maximum-fee argument, so the allowance
62
+ * is the cap: exactly the fee read now is approved.
63
+ */
64
+ renewSubscription(): Promise<Hash>;
65
+ /**
66
+ * End this app's subscription now (owner only): the expiry becomes zero and
67
+ * nothing is refunded. Until it is renewed the app provides no storage and includes nothing.
68
+ */
69
+ cancelSubscription(): Promise<Hash>;
70
+ /** Unix seconds this app's subscription expires at; 0 once cancelled. Reverts for an unclaimed app. */
71
+ expiresAt(appId?: Hex): Promise<bigint>;
72
+ /** True while less than one period is left, or the subscription has expired. */
73
+ isRenewable(): Promise<boolean>;
74
+ /** How long one payment lasts, in seconds. */
75
+ subscriptionPeriod(): Promise<bigint>;
76
+ /** What claiming or renewing an app costs, in USDC base units (6 decimals), not wei. */
77
+ subscriptionFee(): Promise<bigint>;
78
+ /** The ERC-20 token (USDC) every fee is paid in. */
79
+ usdc(): Promise<Address>;
80
+ getAppOwner(appId?: Hex): Promise<Address>;
81
+ /**
82
+ * Point this app at its ERC-8004 agent card.
83
+ *
84
+ * Safe to call whenever the card moves: it leaves the app's publishers alone.
85
+ */
86
+ setAppAgentUri(agentUri: string): Promise<Hash>;
87
+ /** Send the join fees the caller is still owed, from the old join flow, to `to`. */
88
+ withdrawJoinFees(to?: Address): Promise<Hash>;
89
+ /** Join fees still owed to `owner` from the old join flow, in USDC base units. Nothing adds to it. */
90
+ joinFeesOwed(owner?: Address): Promise<bigint>;
91
+ isAppSuspended(): Promise<boolean>;
92
+ /** The agent card URI of this app, or of `appId`. Empty if none is set. */
93
+ appAgentUri(appId?: Hex): Promise<string>;
94
+ /**
95
+ * Accept a publisher into this app (owner only). That is the whole
96
+ * membership: from here it uploads with storage the app provides, and the app
97
+ * includes its data, while `canPublish` holds. A suspended publisher comes
98
+ * back with `setPublisherSuspended(publisher, false)`.
99
+ */
100
+ acceptPublisher(publisher: Address): Promise<Hash>;
101
+ /**
102
+ * Suspend an accepted publisher from this app, or reinstate them (owner
103
+ * only). An owner cannot suspend themselves.
104
+ */
105
+ setPublisherSuspended(publisher: Address, suspended: boolean): Promise<Hash>;
106
+ /** `publisher`'s standing in this app. */
107
+ status(publisher: Address): Promise<PublisherStatus>;
108
+ /**
109
+ * May `publisher` publish to this app now: accepted and not suspended, not
110
+ * banned network-wide, and the app is live and paid up.
111
+ */
112
+ canPublish(publisher: Address, appId?: Hex): Promise<boolean>;
113
+ /** The single oracle the upload gate reads for one publisher under this app. */
114
+ access(publisher: Address, appId?: Hex): Promise<AppAccess>;
115
+ /**
116
+ * `AppAgentChanged` events, oldest first. The last one for an app is its
117
+ * current card; an empty `agentUri` means the owner unset it.
118
+ *
119
+ * Leave `appId` out to list every app that has ever set a card. The chain is
120
+ * the directory: one windowed log scan, no indexer and no registry namespace.
121
+ * `fromBlock` is required because scanning from genesis on an L2 costs
122
+ * hundreds of thousands of RPC calls. Pass the AppRegistry's deploy block, or
123
+ * a block you know to be before the app's registration.
124
+ */
125
+ getAppAgentLogs(opts: {
126
+ appId?: Hex;
127
+ fromBlock: bigint;
128
+ toBlock?: bigint;
129
+ }): Promise<AppAgentLog[]>;
130
+ admin(): Promise<Address>;
131
+ /** The DataRegistry this contract names. */
132
+ dataRegistry(): Promise<Address>;
133
+ /** Whether the protocol admin has banned `wallet` network-wide. */
134
+ isGloballySuspended(wallet: Address): Promise<boolean>;
135
+ /**
136
+ * Ban `wallet` network-wide, or lift the ban. A banned wallet cannot claim an
137
+ * app or be accepted into one, and no app stores or includes it; a banned
138
+ * owner's app keeps running, but they cannot accept anyone or move its agent card.
139
+ */
140
+ setGlobalSuspension(wallet: Address, suspended: boolean): Promise<Hash>;
141
+ /** Take this app down for every publisher at once, or restore it. */
142
+ setAppSuspended(suspended: boolean): Promise<Hash>;
143
+ setDataRegistry(registry: Address): Promise<Hash>;
144
+ setSubscriptionFee(fee: bigint): Promise<Hash>;
145
+ /** Applies from each app's next payment. */
146
+ setSubscriptionPeriod(period: bigint): Promise<Hash>;
147
+ /**
148
+ * Send `amount` of `token` to `to`. Of USDC, only the protocol's share: what
149
+ * app owners are owed in join fees stays (`ExceedsProtocolBalance`).
150
+ */
151
+ withdrawToken(token: Address, to: Address, amount: bigint): Promise<Hash>;
152
+ /**
153
+ * Fees are pulled with `transferFrom`, so the registry needs an allowance.
154
+ * Without one the call reverts with a fee error that reads like a pricing
155
+ * problem but is really an allowance one. Approves exactly `amount`.
156
+ */
157
+ private approveUsdc;
158
+ private senderAddress;
159
+ }
160
+ //#endregion
161
+ export { AppAccess, AppAgentLog, AppRegistryClient, type PreparedTx };
@@ -0,0 +1,274 @@
1
+ import { APP_REGISTRY_ABI } from "./abi.js";
2
+ import { requireWallet, sendWrite } from "../write.js";
3
+ import { getLogsInWindows } from "../logs.js";
4
+ import { erc20Abi, hexToBigInt } from "viem";
5
+
6
+ //#region src/contracts/app-registry/index.ts
7
+ /**
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
+ */
15
+ var AppRegistryClient = class {
16
+ constructor(contractAddress, appId, publicClient, walletClient, logWindow) {
17
+ this.contractAddress = contractAddress;
18
+ this.appId = appId;
19
+ this.publicClient = publicClient;
20
+ this.walletClient = walletClient;
21
+ this.logWindow = logWindow;
22
+ }
23
+ getAppId() {
24
+ return this.appId;
25
+ }
26
+ setAppId(appId) {
27
+ this.appId = appId;
28
+ }
29
+ getAddress() {
30
+ return this.contractAddress;
31
+ }
32
+ /** This client's app, or a clear error for a call that needs one. */
33
+ app() {
34
+ if (!this.appId) throw new Error("no app chosen: pass `appId` to Fangorn.create() or call setAppId()");
35
+ return this.appId;
36
+ }
37
+ /** The app's subscription token: an app's token id is its app id. */
38
+ tokenId(appId = this.app()) {
39
+ return hexToBigInt(appId);
40
+ }
41
+ executeWrite(functionName, args) {
42
+ return sendWrite(this.publicClient, requireWallet(this.walletClient, functionName), this.contractAddress, APP_REGISTRY_ABI, functionName, args);
43
+ }
44
+ read(functionName, args) {
45
+ return this.publicClient.readContract({
46
+ address: this.contractAddress,
47
+ abi: APP_REGISTRY_ABI,
48
+ functionName,
49
+ args
50
+ });
51
+ }
52
+ /**
53
+ * Claim this app, if nobody has. Claiming IS subscribing: it pays one period
54
+ * of `subscriptionFee()` in USDC and mints the app's token to the caller,
55
+ * who becomes the app's first accepted publisher.
56
+ *
57
+ * The fee read now is approved and passed as the most the claim may cost,
58
+ * so a fee raised in the meantime makes the claim revert instead of charging
59
+ * more.
60
+ */
61
+ async registerApp() {
62
+ const fee = await this.subscriptionFee();
63
+ await this.approveUsdc(fee);
64
+ return this.executeWrite("registerApp", [this.app(), fee]);
65
+ }
66
+ /**
67
+ * Pay for one more period (the owner, or an address they approved for the
68
+ * app's token). Allowed once less than a period is left; the period is added
69
+ * to the current expiry, or to now if it has passed.
70
+ *
71
+ * The standard gives the contract no maximum-fee argument, so the allowance
72
+ * is the cap: exactly the fee read now is approved.
73
+ */
74
+ async renewSubscription() {
75
+ const [renewable, period, fee] = await Promise.all([
76
+ this.isRenewable(),
77
+ this.subscriptionPeriod(),
78
+ this.subscriptionFee()
79
+ ]);
80
+ if (!renewable) {
81
+ const expiry = await this.expiresAt();
82
+ 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()}`);
83
+ }
84
+ await this.approveUsdc(fee);
85
+ return this.executeWrite("renewSubscription", [this.tokenId(), period]);
86
+ }
87
+ /**
88
+ * End this app's subscription now (owner only): the expiry becomes zero and
89
+ * nothing is refunded. Until it is renewed the app provides no storage and includes nothing.
90
+ */
91
+ async cancelSubscription() {
92
+ return this.executeWrite("cancelSubscription", [this.tokenId()]);
93
+ }
94
+ /** Unix seconds this app's subscription expires at; 0 once cancelled. Reverts for an unclaimed app. */
95
+ async expiresAt(appId = this.app()) {
96
+ return this.read("expiresAt", [this.tokenId(appId)]);
97
+ }
98
+ /** True while less than one period is left, or the subscription has expired. */
99
+ async isRenewable() {
100
+ return this.read("isRenewable", [this.tokenId()]);
101
+ }
102
+ /** How long one payment lasts, in seconds. */
103
+ async subscriptionPeriod() {
104
+ return this.read("subscriptionPeriod", []);
105
+ }
106
+ /** What claiming or renewing an app costs, in USDC base units (6 decimals), not wei. */
107
+ async subscriptionFee() {
108
+ return this.read("subscriptionFee", []);
109
+ }
110
+ /** The ERC-20 token (USDC) every fee is paid in. */
111
+ async usdc() {
112
+ return this.read("usdc", []);
113
+ }
114
+ async getAppOwner(appId = this.app()) {
115
+ return this.read("getAppOwner", [appId]);
116
+ }
117
+ /**
118
+ * Point this app at its ERC-8004 agent card.
119
+ *
120
+ * Safe to call whenever the card moves: it leaves the app's publishers alone.
121
+ */
122
+ async setAppAgentUri(agentUri) {
123
+ return this.executeWrite("setAppAgentUri", [this.app(), agentUri]);
124
+ }
125
+ /** Send the join fees the caller is still owed, from the old join flow, to `to`. */
126
+ async withdrawJoinFees(to = this.senderAddress()) {
127
+ return this.executeWrite("withdrawJoinFees", [to]);
128
+ }
129
+ /** Join fees still owed to `owner` from the old join flow, in USDC base units. Nothing adds to it. */
130
+ async joinFeesOwed(owner = this.senderAddress()) {
131
+ return this.read("joinFeesOwed", [owner]);
132
+ }
133
+ async isAppSuspended() {
134
+ return this.read("isAppSuspended", [this.app()]);
135
+ }
136
+ /** The agent card URI of this app, or of `appId`. Empty if none is set. */
137
+ async appAgentUri(appId = this.app()) {
138
+ return this.read("appAgentUri", [appId]);
139
+ }
140
+ /**
141
+ * Accept a publisher into this app (owner only). That is the whole
142
+ * membership: from here it uploads with storage the app provides, and the app
143
+ * includes its data, while `canPublish` holds. A suspended publisher comes
144
+ * back with `setPublisherSuspended(publisher, false)`.
145
+ */
146
+ async acceptPublisher(publisher) {
147
+ return this.executeWrite("acceptPublisher", [this.app(), publisher]);
148
+ }
149
+ /**
150
+ * Suspend an accepted publisher from this app, or reinstate them (owner
151
+ * only). An owner cannot suspend themselves.
152
+ */
153
+ async setPublisherSuspended(publisher, suspended) {
154
+ return this.executeWrite("setPublisherSuspended", [
155
+ this.app(),
156
+ publisher,
157
+ suspended
158
+ ]);
159
+ }
160
+ /** `publisher`'s standing in this app. */
161
+ async status(publisher) {
162
+ return await this.read("statusForApp", [this.app(), publisher]);
163
+ }
164
+ /**
165
+ * May `publisher` publish to this app now: accepted and not suspended, not
166
+ * banned network-wide, and the app is live and paid up.
167
+ */
168
+ async canPublish(publisher, appId = this.app()) {
169
+ return this.read("canPublish", [appId, publisher]);
170
+ }
171
+ /** The single oracle the upload gate reads for one publisher under this app. */
172
+ async access(publisher, appId = this.app()) {
173
+ const [canPublish, owner, expiresAt] = await this.read("access", [appId, publisher]);
174
+ return {
175
+ canPublish,
176
+ owner,
177
+ expiresAt
178
+ };
179
+ }
180
+ /**
181
+ * `AppAgentChanged` events, oldest first. The last one for an app is its
182
+ * current card; an empty `agentUri` means the owner unset it.
183
+ *
184
+ * Leave `appId` out to list every app that has ever set a card. The chain is
185
+ * the directory: one windowed log scan, no indexer and no registry namespace.
186
+ * `fromBlock` is required because scanning from genesis on an L2 costs
187
+ * hundreds of thousands of RPC calls. Pass the AppRegistry's deploy block, or
188
+ * a block you know to be before the app's registration.
189
+ */
190
+ async getAppAgentLogs(opts) {
191
+ return (await getLogsInWindows(this.publicClient, opts.fromBlock, opts.toBlock, (from, to) => this.publicClient.getContractEvents({
192
+ address: this.contractAddress,
193
+ abi: APP_REGISTRY_ABI,
194
+ eventName: "AppAgentChanged",
195
+ args: opts.appId ? { app_id: opts.appId } : {},
196
+ fromBlock: from,
197
+ toBlock: to,
198
+ strict: true
199
+ }), this.logWindow)).map((log) => ({
200
+ appId: log.args.app_id,
201
+ agentUri: log.args.agent_uri,
202
+ blockNumber: log.blockNumber,
203
+ transactionHash: log.transactionHash
204
+ }));
205
+ }
206
+ async admin() {
207
+ return this.read("admin", []);
208
+ }
209
+ /** The DataRegistry this contract names. */
210
+ async dataRegistry() {
211
+ return this.read("dataRegistry", []);
212
+ }
213
+ /** Whether the protocol admin has banned `wallet` network-wide. */
214
+ async isGloballySuspended(wallet) {
215
+ return this.read("globalSuspensions", [wallet]);
216
+ }
217
+ /**
218
+ * Ban `wallet` network-wide, or lift the ban. A banned wallet cannot claim an
219
+ * app or be accepted into one, and no app stores or includes it; a banned
220
+ * owner's app keeps running, but they cannot accept anyone or move its agent card.
221
+ */
222
+ async setGlobalSuspension(wallet, suspended) {
223
+ return this.executeWrite("setGlobalSuspension", [wallet, suspended]);
224
+ }
225
+ /** Take this app down for every publisher at once, or restore it. */
226
+ async setAppSuspended(suspended) {
227
+ return this.executeWrite("setAppSuspended", [this.app(), suspended]);
228
+ }
229
+ async setDataRegistry(registry) {
230
+ return this.executeWrite("setDataRegistry", [registry]);
231
+ }
232
+ async setSubscriptionFee(fee) {
233
+ return this.executeWrite("setSubscriptionFee", [fee]);
234
+ }
235
+ /** Applies from each app's next payment. */
236
+ async setSubscriptionPeriod(period) {
237
+ return this.executeWrite("setSubscriptionPeriod", [period]);
238
+ }
239
+ /**
240
+ * Send `amount` of `token` to `to`. Of USDC, only the protocol's share: what
241
+ * app owners are owed in join fees stays (`ExceedsProtocolBalance`).
242
+ */
243
+ async withdrawToken(token, to, amount) {
244
+ return this.executeWrite("withdrawToken", [
245
+ token,
246
+ to,
247
+ amount
248
+ ]);
249
+ }
250
+ /**
251
+ * Fees are pulled with `transferFrom`, so the registry needs an allowance.
252
+ * Without one the call reverts with a fee error that reads like a pricing
253
+ * problem but is really an allowance one. Approves exactly `amount`.
254
+ */
255
+ async approveUsdc(amount) {
256
+ if (amount === 0n) return;
257
+ const token = await this.usdc();
258
+ if (await this.publicClient.readContract({
259
+ address: token,
260
+ abi: erc20Abi,
261
+ functionName: "allowance",
262
+ args: [this.senderAddress(), this.contractAddress]
263
+ }) >= amount) return;
264
+ await sendWrite(this.publicClient, requireWallet(this.walletClient, "approve"), token, erc20Abi, "approve", [this.contractAddress, amount]);
265
+ }
266
+ senderAddress() {
267
+ const account = this.walletClient.account;
268
+ if (!account) throw new Error("Account required");
269
+ return account.address;
270
+ }
271
+ };
272
+
273
+ //#endregion
274
+ export { AppRegistryClient };