@alien_org/contract 0.0.17-beta

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 ADDED
@@ -0,0 +1,105 @@
1
+ # @alien_org/contract
2
+
3
+ Type definitions and version utilities for miniapp-host communication.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ bun add @alien_org/contract
9
+ ```
10
+
11
+ ## Exports
12
+
13
+ ### Types
14
+
15
+ ```typescript
16
+ import type {
17
+ // Method types
18
+ Methods, // Interface of all methods
19
+ MethodName, // Union of method names
20
+ MethodPayload, // Payload type for a method
21
+ CreateMethodPayload, // Helper for defining methods
22
+ MethodNameWithVersionedPayload, // Methods with versioned payloads
23
+ MethodVersionedPayload, // Versioned payload for a method
24
+
25
+ // Event types
26
+ Events, // Interface of all events
27
+ EventName, // Union of event names
28
+ EventPayload, // Payload type for an event
29
+ CreateEventPayload, // Helper for defining events
30
+
31
+ // Launch parameters
32
+ LaunchParams, // Host-injected params (authToken, contractVersion, etc.)
33
+ Platform, // 'ios' | 'android'
34
+
35
+ // Utilities
36
+ Version, // Semantic version string type
37
+ } from '@alien_org/contract';
38
+ ```
39
+
40
+ ### Constants
41
+
42
+ ```typescript
43
+ import { PLATFORMS, releases } from '@alien_org/contract';
44
+
45
+ PLATFORMS // ['ios', 'android']
46
+ releases // Record<Version, MethodName[]> - version to methods mapping
47
+ ```
48
+
49
+ ### Version Utilities
50
+
51
+ ```typescript
52
+ import {
53
+ isMethodSupported,
54
+ getMethodMinVersion,
55
+ getReleaseVersion,
56
+ } from '@alien_org/contract';
57
+
58
+ // Check if method is supported in a version
59
+ isMethodSupported('app:ready', '0.0.9'); // true
60
+ isMethodSupported('payment:request', '0.0.9'); // false
61
+
62
+ // Get minimum version that supports a method
63
+ getMethodMinVersion('app:ready'); // '0.0.9'
64
+ getMethodMinVersion('payment:request'); // '0.0.14'
65
+
66
+ // Get version where a method was introduced
67
+ getReleaseVersion('app:ready'); // '0.0.9'
68
+ ```
69
+
70
+ ## Available Methods
71
+
72
+ | Method | Since | Description |
73
+ |--------|-------|-------------|
74
+ | `app:ready` | 0.0.9 | Notify host that miniapp is ready |
75
+ | `miniapp:close.ack` | 0.0.14 | Acknowledge close request |
76
+ | `host.back.button:toggle` | 0.0.14 | Show/hide back button |
77
+ | `payment:request` | 0.0.14 | Request payment |
78
+
79
+ ## Available Events
80
+
81
+ | Event | Since | Description |
82
+ |-------|-------|-------------|
83
+ | `miniapp:close` | 0.0.14 | Host is closing miniapp |
84
+ | `host.back.button:clicked` | 0.0.14 | Back button was clicked |
85
+ | `payment:response` | 0.0.14 | Payment result |
86
+
87
+ ## LaunchParams
88
+
89
+ Parameters injected by the host app:
90
+
91
+ ```typescript
92
+ interface LaunchParams {
93
+ authToken: string | undefined; // JWT auth token
94
+ contractVersion: Version | undefined; // Host's contract version
95
+ hostAppVersion: string | undefined; // Host app version
96
+ platform: Platform | undefined; // 'ios' | 'android'
97
+ startParam: string | undefined; // Custom param (referrals, etc.)
98
+ }
99
+ ```
100
+
101
+ ## Adding New Methods/Events
102
+
103
+ 1. Define in `src/methods/definitions/methods.ts` or `src/events/definitions/events.ts`
104
+ 2. Add version mapping in `src/methods/versions/releases.ts`
105
+ 3. Build: `bun run build`
package/dist/index.cjs ADDED
@@ -0,0 +1,70 @@
1
+
2
+ //#region src/launch-params.ts
3
+ /**
4
+ * Supported platforms for miniapps.
5
+ */
6
+ const PLATFORMS = ["ios", "android"];
7
+
8
+ //#endregion
9
+ //#region src/methods/versions/releases.ts
10
+ const releases = {
11
+ "0.0.9": ["app:ready"],
12
+ "0.0.14": ["miniapp:close.ack", "host.back.button:toggle"]
13
+ };
14
+
15
+ //#endregion
16
+ //#region src/methods/versions/get-release-version.ts
17
+ function getReleaseVersion(method, payload) {
18
+ return Object.keys(releases).find((version) => {
19
+ const releaseItems = releases[version];
20
+ if (!releaseItems) return false;
21
+ return releaseItems.some((item) => {
22
+ if (payload) return typeof item === "object" && item.method === method && item.param === payload;
23
+ return item === method;
24
+ });
25
+ }) || null;
26
+ }
27
+
28
+ //#endregion
29
+ //#region src/methods/versions/index.ts
30
+ /**
31
+ * Check if a method is supported in a given version.
32
+ *
33
+ * @param method - The method name to check.
34
+ * @param version - The contract version (must be a valid version string, not undefined).
35
+ * @returns `true` if the method is supported in the given version, `false` otherwise.
36
+ *
37
+ * @remarks
38
+ * This function only accepts valid version strings. Version existence checks should be
39
+ * handled at a higher level before calling this function.
40
+ */
41
+ function isMethodSupported(method, version) {
42
+ const methods = releases[version];
43
+ if (!methods) return false;
44
+ return methods.some((m) => typeof m === "string" ? m === method : m.method === method);
45
+ }
46
+ /**
47
+ * Get the minimum version that supports a method.
48
+ * Returns undefined if method not found in any version.
49
+ */
50
+ function getMethodMinVersion(method) {
51
+ const sorted = Object.keys(releases).sort((a, b) => {
52
+ const [aMajor, aMinor, aPatch] = a.split(".").map(Number);
53
+ const [bMajor, bMinor, bPatch] = b.split(".").map(Number);
54
+ if (aMajor !== bMajor) return (aMajor ?? 0) - (bMajor ?? 0);
55
+ if (aMinor !== bMinor) return (aMinor ?? 0) - (bMinor ?? 0);
56
+ return (aPatch ?? 0) - (bPatch ?? 0);
57
+ });
58
+ for (const version of sorted) {
59
+ const methods = releases[version];
60
+ if (!methods) continue;
61
+ if (methods.some((m) => typeof m === "string" ? m === method : m.method === method)) return version;
62
+ }
63
+ }
64
+
65
+ //#endregion
66
+ exports.PLATFORMS = PLATFORMS;
67
+ exports.getMethodMinVersion = getMethodMinVersion;
68
+ exports.getReleaseVersion = getReleaseVersion;
69
+ exports.isMethodSupported = isMethodSupported;
70
+ exports.releases = releases;
@@ -0,0 +1,323 @@
1
+ //#region src/utils.d.ts
2
+ /**
3
+ * Adds a reqId field to the payload.
4
+ * @schema
5
+ */
6
+ type WithReqId<T> = T & {
7
+ /**
8
+ * Request identifier.
9
+ * @schema
10
+ */
11
+ reqId: string;
12
+ };
13
+ /**
14
+ * Semantic versioning type.
15
+ * @example
16
+ * type Version = '1.0.0';
17
+ */
18
+ type Version = `${number}.${number}.${number}`;
19
+ /**
20
+ * Extracts keys, that are present in the type if it is an object.
21
+ * @example
22
+ * type Keys = UnionKeys<{ a: string, b: number }>;
23
+ * // Keys = 'a' | 'b'
24
+ */
25
+ type UnionKeys<T> = T extends T ? keyof T : never;
26
+ /**
27
+ * Checks if a type is never.
28
+ * @example
29
+ * type IsNever = IsNever<never>;
30
+ * // IsNever = true
31
+ */
32
+ type IsNever<T> = [T] extends [never] ? true : false;
33
+ /**
34
+ * Conditional type.
35
+ * @example
36
+ * type If = If<true, 'true', 'false'>;
37
+ * // If = 'true'
38
+ */
39
+ type If<Cond extends boolean, True, False> = Cond extends true ? True : False;
40
+ /**
41
+ * Empty object type.
42
+ * @example
43
+ * type Empty = Empty;
44
+ * // Empty = {}
45
+ */
46
+ type Empty = Record<string, never>;
47
+ //#endregion
48
+ //#region src/events/types/payload.d.ts
49
+ /**
50
+ * Creates event payload types.
51
+ */
52
+ interface CreateEventPayload<Payload = never> {
53
+ payload: Payload;
54
+ }
55
+ //#endregion
56
+ //#region src/events/definitions/events.d.ts
57
+ /**
58
+ * Events interface defining all available events and their payloads.
59
+ * @since 0.0.1
60
+ * @schema
61
+ */
62
+ interface Events {
63
+ /**
64
+ * Miniapp close event, fired by the host app just before the miniapp is closed.
65
+ * @since 0.0.14
66
+ * @schema
67
+ */
68
+ 'miniapp:close': CreateEventPayload<Empty>;
69
+ /**
70
+ * Host app's back button clicked event.
71
+ * @since 0.0.14
72
+ * @schema
73
+ */
74
+ 'host.back.button:clicked': CreateEventPayload<Empty>;
75
+ /**
76
+ * Payment response event.
77
+ *
78
+ * Statuses:
79
+ * - `paid`: Payment successful, `txHash` included
80
+ * - `cancelled`: User manually cancelled/rejected the payment
81
+ * - `failed`: Error occurred (see `errorCode` for details)
82
+ *
83
+ * For instant fulfillment, your backend should fulfill on webhook receipt
84
+ * using the `invoice` from the request.
85
+ *
86
+ * @since 0.0.14
87
+ * @schema
88
+ */
89
+ 'payment:response': CreateEventPayload<WithReqId<{
90
+ /**
91
+ * Payment status.
92
+ * - `paid`: Success
93
+ * - `cancelled`: User rejected
94
+ * - `failed`: Error (check `errorCode`)
95
+ * @since 0.0.14
96
+ * @schema
97
+ */
98
+ status: 'paid' | 'cancelled' | 'failed';
99
+ /**
100
+ * Transaction hash (present when status is 'paid').
101
+ * @since 0.0.14
102
+ * @schema
103
+ */
104
+ txHash?: string;
105
+ /**
106
+ * Error code (present when status is 'failed').
107
+ * - `insufficient_balance`: User doesn't have enough tokens
108
+ * - `network_error`: Blockchain network issue
109
+ * - `pre_checkout_rejected`: Backend rejected the payment in pre-checkout
110
+ * - `pre_checkout_timeout`: Backend didn't respond to pre-checkout in time
111
+ * - `unknown`: Unexpected error
112
+ * @since 0.0.14
113
+ * @schema
114
+ */
115
+ errorCode?: 'insufficient_balance' | 'network_error' | 'pre_checkout_rejected' | 'pre_checkout_timeout' | 'unknown';
116
+ }>>;
117
+ }
118
+ //#endregion
119
+ //#region src/events/types/event-types.d.ts
120
+ type EventName = keyof Events;
121
+ type EventPayload<E extends EventName> = Events[E]['payload'];
122
+ //#endregion
123
+ //#region src/launch-params.d.ts
124
+ /**
125
+ * Supported platforms for miniapps.
126
+ */
127
+ declare const PLATFORMS: readonly ["ios", "android"];
128
+ /**
129
+ * Platform the miniapp is running on.
130
+ */
131
+ type Platform = (typeof PLATFORMS)[number];
132
+ /**
133
+ * Launch parameters injected by the host app.
134
+ */
135
+ interface LaunchParams {
136
+ /** JWT auth token injected by host app */
137
+ authToken: string | undefined;
138
+ /** Contract version supported by host app (semver) */
139
+ contractVersion: Version | undefined;
140
+ /** Host app version (e.g., '1.2.3') */
141
+ hostAppVersion: string | undefined;
142
+ /** Platform the miniapp is running on */
143
+ platform: Platform | undefined;
144
+ /**
145
+ * Custom start parameter injected by host app.
146
+ * Used for referral codes, campaign tracking, or custom routing.
147
+ */
148
+ startParam: string | undefined;
149
+ }
150
+ //#endregion
151
+ //#region src/methods/types/payload.d.ts
152
+ /**
153
+ * Creates method payload types.
154
+ */
155
+ interface CreateMethodPayload<Payload = never, VersionedPayload extends UnionKeys<Payload> = never> {
156
+ payload: Payload;
157
+ versionedPayload: VersionedPayload;
158
+ }
159
+ //#endregion
160
+ //#region src/methods/definitions/methods.d.ts
161
+ /**
162
+ * Methods interface defining all available methods and their payloads.
163
+ * @schema
164
+ */
165
+ interface Methods {
166
+ /**
167
+ * Miniapp ready method.
168
+ * Sent by the miniapp to notify the host app that it has loaded and is ready to be displayed.
169
+ * @since 0.0.1
170
+ * @schema
171
+ */
172
+ 'app:ready': CreateMethodPayload<Empty>;
173
+ /**
174
+ * Miniapp close acknowledgment method.
175
+ * Sent by the miniapp to notify the host app that it has completed cleanup and is ready to be closed.
176
+ * Note that if the miniapp takes longer than 10 seconds to close, the host app will force close the miniapp.
177
+ * @since 0.0.14
178
+ * @schema
179
+ */
180
+ 'miniapp:close.ack': CreateMethodPayload<Empty>;
181
+ /**
182
+ * Toggle host app's back button visibility.
183
+ * @since 0.0.14
184
+ * @schema
185
+ */
186
+ 'host.back.button:toggle': CreateMethodPayload<{
187
+ /**
188
+ * Whether to show or hide the back button.
189
+ * @since 0.0.14
190
+ * @schema
191
+ */
192
+ visible: boolean;
193
+ }>;
194
+ /**
195
+ * Request a payment from the user.
196
+ *
197
+ * The `invoice` field is your order/invoice ID for backend correlation.
198
+ * Your backend receives a webhook when user pays - fulfill the order
199
+ * immediately without waiting for chain confirmation.
200
+ *
201
+ * Optional display fields (`title`, `caption`, `iconUrl`, `quantity`)
202
+ * are shown on the payment approval screen.
203
+ *
204
+ * Set `test: true` for test mode - no real payment is made, but webhooks
205
+ * are fired with `test: true` flag. Use for development and testing.
206
+ *
207
+ * @since 0.0.14
208
+ * @schema
209
+ */
210
+ 'payment:request': CreateMethodPayload<WithReqId<{
211
+ /**
212
+ * The recipient's wallet address.
213
+ * @since 0.0.14
214
+ * @schema
215
+ */
216
+ recipient: string;
217
+ /**
218
+ * The amount to pay (in token's smallest unit, as string for precision).
219
+ * @since 0.0.14
220
+ * @schema
221
+ */
222
+ amount: string;
223
+ /**
224
+ * The token identifier (e.g., 'SOL', 'ALIEN', or contract address).
225
+ * @since 0.0.14
226
+ * @schema
227
+ */
228
+ token: string;
229
+ /**
230
+ * The network for the payment ('solana' or 'alien').
231
+ * @since 0.0.14
232
+ * @schema
233
+ */
234
+ network: string;
235
+ /**
236
+ * Your order/invoice ID for backend correlation and instant fulfillment.
237
+ * @since 0.0.14
238
+ * @schema
239
+ */
240
+ invoice: string;
241
+ /**
242
+ * Item title shown on the approval screen.
243
+ * @since 0.0.14
244
+ * @schema
245
+ */
246
+ title?: string;
247
+ /**
248
+ * Item description/caption shown on the approval screen.
249
+ * @since 0.0.14
250
+ * @schema
251
+ */
252
+ caption?: string;
253
+ /**
254
+ * Item icon URL shown on the approval screen.
255
+ * @since 0.0.14
256
+ * @schema
257
+ */
258
+ iconUrl?: string;
259
+ /**
260
+ * Quantity of items being purchased.
261
+ * @since 0.0.14
262
+ * @schema
263
+ */
264
+ quantity?: number;
265
+ /**
266
+ * Test mode flag. When true, no real payment is processed.
267
+ * The approval screen shows a test indicator, and webhooks
268
+ * include `test: true`. Use for development and testing.
269
+ * @since 0.0.14
270
+ * @schema
271
+ */
272
+ test?: boolean;
273
+ }>>;
274
+ }
275
+ //#endregion
276
+ //#region src/methods/types/method-types.d.ts
277
+ type MethodName = keyof Methods;
278
+ type MethodPayload<M$1 extends MethodName> = Methods[M$1]['payload'];
279
+ /**
280
+ * Method names which have versioned payload.
281
+ */
282
+ type MethodNameWithVersionedPayload = { [M in MethodName]: If<IsNever<Methods[M]['versionedPayload']>, never, M> }[MethodName];
283
+ /**
284
+ * Method payload which appear only in the specific version.
285
+ */
286
+ type MethodVersionedPayload<M$1 extends MethodNameWithVersionedPayload> = Methods[M$1]['versionedPayload'];
287
+ //#endregion
288
+ //#region src/methods/versions/get-release-version.d.ts
289
+ /**
290
+ * @returns Version of the specified method parameter release. Returns `null`
291
+ * if passed method or parameter are unknown.
292
+ * @param method - method name
293
+ * @param param - method parameter
294
+ */
295
+ declare function getReleaseVersion<M$1 extends MethodNameWithVersionedPayload>(method: M$1, payload: MethodVersionedPayload<M$1>): Version | null;
296
+ declare function getReleaseVersion(method: MethodName): Version | null;
297
+ //#endregion
298
+ //#region src/methods/versions/releases.d.ts
299
+ declare const releases: Record<Version, (MethodName | {
300
+ method: MethodNameWithVersionedPayload;
301
+ param: MethodVersionedPayload<MethodNameWithVersionedPayload>;
302
+ })[]>;
303
+ //#endregion
304
+ //#region src/methods/versions/index.d.ts
305
+ /**
306
+ * Check if a method is supported in a given version.
307
+ *
308
+ * @param method - The method name to check.
309
+ * @param version - The contract version (must be a valid version string, not undefined).
310
+ * @returns `true` if the method is supported in the given version, `false` otherwise.
311
+ *
312
+ * @remarks
313
+ * This function only accepts valid version strings. Version existence checks should be
314
+ * handled at a higher level before calling this function.
315
+ */
316
+ declare function isMethodSupported(method: MethodName, version: Version): boolean;
317
+ /**
318
+ * Get the minimum version that supports a method.
319
+ * Returns undefined if method not found in any version.
320
+ */
321
+ declare function getMethodMinVersion(method: MethodName): Version | undefined;
322
+ //#endregion
323
+ export { type CreateEventPayload, type CreateMethodPayload, type EventName, type EventPayload, type Events, type LaunchParams, type MethodName, type MethodNameWithVersionedPayload, type MethodPayload, type MethodVersionedPayload, type Methods, PLATFORMS, type Platform, type Version, getMethodMinVersion, getReleaseVersion, isMethodSupported, releases };
@@ -0,0 +1,323 @@
1
+ //#region src/utils.d.ts
2
+ /**
3
+ * Adds a reqId field to the payload.
4
+ * @schema
5
+ */
6
+ type WithReqId<T> = T & {
7
+ /**
8
+ * Request identifier.
9
+ * @schema
10
+ */
11
+ reqId: string;
12
+ };
13
+ /**
14
+ * Semantic versioning type.
15
+ * @example
16
+ * type Version = '1.0.0';
17
+ */
18
+ type Version = `${number}.${number}.${number}`;
19
+ /**
20
+ * Extracts keys, that are present in the type if it is an object.
21
+ * @example
22
+ * type Keys = UnionKeys<{ a: string, b: number }>;
23
+ * // Keys = 'a' | 'b'
24
+ */
25
+ type UnionKeys<T> = T extends T ? keyof T : never;
26
+ /**
27
+ * Checks if a type is never.
28
+ * @example
29
+ * type IsNever = IsNever<never>;
30
+ * // IsNever = true
31
+ */
32
+ type IsNever<T> = [T] extends [never] ? true : false;
33
+ /**
34
+ * Conditional type.
35
+ * @example
36
+ * type If = If<true, 'true', 'false'>;
37
+ * // If = 'true'
38
+ */
39
+ type If<Cond extends boolean, True, False> = Cond extends true ? True : False;
40
+ /**
41
+ * Empty object type.
42
+ * @example
43
+ * type Empty = Empty;
44
+ * // Empty = {}
45
+ */
46
+ type Empty = Record<string, never>;
47
+ //#endregion
48
+ //#region src/events/types/payload.d.ts
49
+ /**
50
+ * Creates event payload types.
51
+ */
52
+ interface CreateEventPayload<Payload = never> {
53
+ payload: Payload;
54
+ }
55
+ //#endregion
56
+ //#region src/events/definitions/events.d.ts
57
+ /**
58
+ * Events interface defining all available events and their payloads.
59
+ * @since 0.0.1
60
+ * @schema
61
+ */
62
+ interface Events {
63
+ /**
64
+ * Miniapp close event, fired by the host app just before the miniapp is closed.
65
+ * @since 0.0.14
66
+ * @schema
67
+ */
68
+ 'miniapp:close': CreateEventPayload<Empty>;
69
+ /**
70
+ * Host app's back button clicked event.
71
+ * @since 0.0.14
72
+ * @schema
73
+ */
74
+ 'host.back.button:clicked': CreateEventPayload<Empty>;
75
+ /**
76
+ * Payment response event.
77
+ *
78
+ * Statuses:
79
+ * - `paid`: Payment successful, `txHash` included
80
+ * - `cancelled`: User manually cancelled/rejected the payment
81
+ * - `failed`: Error occurred (see `errorCode` for details)
82
+ *
83
+ * For instant fulfillment, your backend should fulfill on webhook receipt
84
+ * using the `invoice` from the request.
85
+ *
86
+ * @since 0.0.14
87
+ * @schema
88
+ */
89
+ 'payment:response': CreateEventPayload<WithReqId<{
90
+ /**
91
+ * Payment status.
92
+ * - `paid`: Success
93
+ * - `cancelled`: User rejected
94
+ * - `failed`: Error (check `errorCode`)
95
+ * @since 0.0.14
96
+ * @schema
97
+ */
98
+ status: 'paid' | 'cancelled' | 'failed';
99
+ /**
100
+ * Transaction hash (present when status is 'paid').
101
+ * @since 0.0.14
102
+ * @schema
103
+ */
104
+ txHash?: string;
105
+ /**
106
+ * Error code (present when status is 'failed').
107
+ * - `insufficient_balance`: User doesn't have enough tokens
108
+ * - `network_error`: Blockchain network issue
109
+ * - `pre_checkout_rejected`: Backend rejected the payment in pre-checkout
110
+ * - `pre_checkout_timeout`: Backend didn't respond to pre-checkout in time
111
+ * - `unknown`: Unexpected error
112
+ * @since 0.0.14
113
+ * @schema
114
+ */
115
+ errorCode?: 'insufficient_balance' | 'network_error' | 'pre_checkout_rejected' | 'pre_checkout_timeout' | 'unknown';
116
+ }>>;
117
+ }
118
+ //#endregion
119
+ //#region src/events/types/event-types.d.ts
120
+ type EventName = keyof Events;
121
+ type EventPayload<E extends EventName> = Events[E]['payload'];
122
+ //#endregion
123
+ //#region src/launch-params.d.ts
124
+ /**
125
+ * Supported platforms for miniapps.
126
+ */
127
+ declare const PLATFORMS: readonly ["ios", "android"];
128
+ /**
129
+ * Platform the miniapp is running on.
130
+ */
131
+ type Platform = (typeof PLATFORMS)[number];
132
+ /**
133
+ * Launch parameters injected by the host app.
134
+ */
135
+ interface LaunchParams {
136
+ /** JWT auth token injected by host app */
137
+ authToken: string | undefined;
138
+ /** Contract version supported by host app (semver) */
139
+ contractVersion: Version | undefined;
140
+ /** Host app version (e.g., '1.2.3') */
141
+ hostAppVersion: string | undefined;
142
+ /** Platform the miniapp is running on */
143
+ platform: Platform | undefined;
144
+ /**
145
+ * Custom start parameter injected by host app.
146
+ * Used for referral codes, campaign tracking, or custom routing.
147
+ */
148
+ startParam: string | undefined;
149
+ }
150
+ //#endregion
151
+ //#region src/methods/types/payload.d.ts
152
+ /**
153
+ * Creates method payload types.
154
+ */
155
+ interface CreateMethodPayload<Payload = never, VersionedPayload extends UnionKeys<Payload> = never> {
156
+ payload: Payload;
157
+ versionedPayload: VersionedPayload;
158
+ }
159
+ //#endregion
160
+ //#region src/methods/definitions/methods.d.ts
161
+ /**
162
+ * Methods interface defining all available methods and their payloads.
163
+ * @schema
164
+ */
165
+ interface Methods {
166
+ /**
167
+ * Miniapp ready method.
168
+ * Sent by the miniapp to notify the host app that it has loaded and is ready to be displayed.
169
+ * @since 0.0.1
170
+ * @schema
171
+ */
172
+ 'app:ready': CreateMethodPayload<Empty>;
173
+ /**
174
+ * Miniapp close acknowledgment method.
175
+ * Sent by the miniapp to notify the host app that it has completed cleanup and is ready to be closed.
176
+ * Note that if the miniapp takes longer than 10 seconds to close, the host app will force close the miniapp.
177
+ * @since 0.0.14
178
+ * @schema
179
+ */
180
+ 'miniapp:close.ack': CreateMethodPayload<Empty>;
181
+ /**
182
+ * Toggle host app's back button visibility.
183
+ * @since 0.0.14
184
+ * @schema
185
+ */
186
+ 'host.back.button:toggle': CreateMethodPayload<{
187
+ /**
188
+ * Whether to show or hide the back button.
189
+ * @since 0.0.14
190
+ * @schema
191
+ */
192
+ visible: boolean;
193
+ }>;
194
+ /**
195
+ * Request a payment from the user.
196
+ *
197
+ * The `invoice` field is your order/invoice ID for backend correlation.
198
+ * Your backend receives a webhook when user pays - fulfill the order
199
+ * immediately without waiting for chain confirmation.
200
+ *
201
+ * Optional display fields (`title`, `caption`, `iconUrl`, `quantity`)
202
+ * are shown on the payment approval screen.
203
+ *
204
+ * Set `test: true` for test mode - no real payment is made, but webhooks
205
+ * are fired with `test: true` flag. Use for development and testing.
206
+ *
207
+ * @since 0.0.14
208
+ * @schema
209
+ */
210
+ 'payment:request': CreateMethodPayload<WithReqId<{
211
+ /**
212
+ * The recipient's wallet address.
213
+ * @since 0.0.14
214
+ * @schema
215
+ */
216
+ recipient: string;
217
+ /**
218
+ * The amount to pay (in token's smallest unit, as string for precision).
219
+ * @since 0.0.14
220
+ * @schema
221
+ */
222
+ amount: string;
223
+ /**
224
+ * The token identifier (e.g., 'SOL', 'ALIEN', or contract address).
225
+ * @since 0.0.14
226
+ * @schema
227
+ */
228
+ token: string;
229
+ /**
230
+ * The network for the payment ('solana' or 'alien').
231
+ * @since 0.0.14
232
+ * @schema
233
+ */
234
+ network: string;
235
+ /**
236
+ * Your order/invoice ID for backend correlation and instant fulfillment.
237
+ * @since 0.0.14
238
+ * @schema
239
+ */
240
+ invoice: string;
241
+ /**
242
+ * Item title shown on the approval screen.
243
+ * @since 0.0.14
244
+ * @schema
245
+ */
246
+ title?: string;
247
+ /**
248
+ * Item description/caption shown on the approval screen.
249
+ * @since 0.0.14
250
+ * @schema
251
+ */
252
+ caption?: string;
253
+ /**
254
+ * Item icon URL shown on the approval screen.
255
+ * @since 0.0.14
256
+ * @schema
257
+ */
258
+ iconUrl?: string;
259
+ /**
260
+ * Quantity of items being purchased.
261
+ * @since 0.0.14
262
+ * @schema
263
+ */
264
+ quantity?: number;
265
+ /**
266
+ * Test mode flag. When true, no real payment is processed.
267
+ * The approval screen shows a test indicator, and webhooks
268
+ * include `test: true`. Use for development and testing.
269
+ * @since 0.0.14
270
+ * @schema
271
+ */
272
+ test?: boolean;
273
+ }>>;
274
+ }
275
+ //#endregion
276
+ //#region src/methods/types/method-types.d.ts
277
+ type MethodName = keyof Methods;
278
+ type MethodPayload<M$1 extends MethodName> = Methods[M$1]['payload'];
279
+ /**
280
+ * Method names which have versioned payload.
281
+ */
282
+ type MethodNameWithVersionedPayload = { [M in MethodName]: If<IsNever<Methods[M]['versionedPayload']>, never, M> }[MethodName];
283
+ /**
284
+ * Method payload which appear only in the specific version.
285
+ */
286
+ type MethodVersionedPayload<M$1 extends MethodNameWithVersionedPayload> = Methods[M$1]['versionedPayload'];
287
+ //#endregion
288
+ //#region src/methods/versions/get-release-version.d.ts
289
+ /**
290
+ * @returns Version of the specified method parameter release. Returns `null`
291
+ * if passed method or parameter are unknown.
292
+ * @param method - method name
293
+ * @param param - method parameter
294
+ */
295
+ declare function getReleaseVersion<M$1 extends MethodNameWithVersionedPayload>(method: M$1, payload: MethodVersionedPayload<M$1>): Version | null;
296
+ declare function getReleaseVersion(method: MethodName): Version | null;
297
+ //#endregion
298
+ //#region src/methods/versions/releases.d.ts
299
+ declare const releases: Record<Version, (MethodName | {
300
+ method: MethodNameWithVersionedPayload;
301
+ param: MethodVersionedPayload<MethodNameWithVersionedPayload>;
302
+ })[]>;
303
+ //#endregion
304
+ //#region src/methods/versions/index.d.ts
305
+ /**
306
+ * Check if a method is supported in a given version.
307
+ *
308
+ * @param method - The method name to check.
309
+ * @param version - The contract version (must be a valid version string, not undefined).
310
+ * @returns `true` if the method is supported in the given version, `false` otherwise.
311
+ *
312
+ * @remarks
313
+ * This function only accepts valid version strings. Version existence checks should be
314
+ * handled at a higher level before calling this function.
315
+ */
316
+ declare function isMethodSupported(method: MethodName, version: Version): boolean;
317
+ /**
318
+ * Get the minimum version that supports a method.
319
+ * Returns undefined if method not found in any version.
320
+ */
321
+ declare function getMethodMinVersion(method: MethodName): Version | undefined;
322
+ //#endregion
323
+ export { type CreateEventPayload, type CreateMethodPayload, type EventName, type EventPayload, type Events, type LaunchParams, type MethodName, type MethodNameWithVersionedPayload, type MethodPayload, type MethodVersionedPayload, type Methods, PLATFORMS, type Platform, type Version, getMethodMinVersion, getReleaseVersion, isMethodSupported, releases };
package/dist/index.mjs ADDED
@@ -0,0 +1,65 @@
1
+ //#region src/launch-params.ts
2
+ /**
3
+ * Supported platforms for miniapps.
4
+ */
5
+ const PLATFORMS = ["ios", "android"];
6
+
7
+ //#endregion
8
+ //#region src/methods/versions/releases.ts
9
+ const releases = {
10
+ "0.0.9": ["app:ready"],
11
+ "0.0.14": ["miniapp:close.ack", "host.back.button:toggle"]
12
+ };
13
+
14
+ //#endregion
15
+ //#region src/methods/versions/get-release-version.ts
16
+ function getReleaseVersion(method, payload) {
17
+ return Object.keys(releases).find((version) => {
18
+ const releaseItems = releases[version];
19
+ if (!releaseItems) return false;
20
+ return releaseItems.some((item) => {
21
+ if (payload) return typeof item === "object" && item.method === method && item.param === payload;
22
+ return item === method;
23
+ });
24
+ }) || null;
25
+ }
26
+
27
+ //#endregion
28
+ //#region src/methods/versions/index.ts
29
+ /**
30
+ * Check if a method is supported in a given version.
31
+ *
32
+ * @param method - The method name to check.
33
+ * @param version - The contract version (must be a valid version string, not undefined).
34
+ * @returns `true` if the method is supported in the given version, `false` otherwise.
35
+ *
36
+ * @remarks
37
+ * This function only accepts valid version strings. Version existence checks should be
38
+ * handled at a higher level before calling this function.
39
+ */
40
+ function isMethodSupported(method, version) {
41
+ const methods = releases[version];
42
+ if (!methods) return false;
43
+ return methods.some((m) => typeof m === "string" ? m === method : m.method === method);
44
+ }
45
+ /**
46
+ * Get the minimum version that supports a method.
47
+ * Returns undefined if method not found in any version.
48
+ */
49
+ function getMethodMinVersion(method) {
50
+ const sorted = Object.keys(releases).sort((a, b) => {
51
+ const [aMajor, aMinor, aPatch] = a.split(".").map(Number);
52
+ const [bMajor, bMinor, bPatch] = b.split(".").map(Number);
53
+ if (aMajor !== bMajor) return (aMajor ?? 0) - (bMajor ?? 0);
54
+ if (aMinor !== bMinor) return (aMinor ?? 0) - (bMinor ?? 0);
55
+ return (aPatch ?? 0) - (bPatch ?? 0);
56
+ });
57
+ for (const version of sorted) {
58
+ const methods = releases[version];
59
+ if (!methods) continue;
60
+ if (methods.some((m) => typeof m === "string" ? m === method : m.method === method)) return version;
61
+ }
62
+ }
63
+
64
+ //#endregion
65
+ export { PLATFORMS, getMethodMinVersion, getReleaseVersion, isMethodSupported, releases };
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@alien_org/contract",
3
+ "version": "0.0.17-beta",
4
+ "type": "module",
5
+ "main": "./dist/index.cjs",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.mts",
8
+ "exports": {
9
+ ".": {
10
+ "import": {
11
+ "types": "./dist/index.d.mts",
12
+ "default": "./dist/index.mjs"
13
+ },
14
+ "require": {
15
+ "types": "./dist/index.d.cts",
16
+ "default": "./dist/index.cjs"
17
+ }
18
+ }
19
+ },
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "git+https://github.com/alien-id/miniapp-sdk.git",
26
+ "directory": "packages/contract"
27
+ },
28
+ "publishConfig": {
29
+ "registry": "https://registry.npmjs.org",
30
+ "access": "public"
31
+ },
32
+ "scripts": {
33
+ "build": "tsdown",
34
+ "test": "bun test tests",
35
+ "prepublishOnly": "bun run build",
36
+ "generate-schemas": "bun run scripts/generate-schemas.ts"
37
+ },
38
+ "devDependencies": {
39
+ "@types/bun": "^1.3.5",
40
+ "@types/node": "^25.0.3",
41
+ "tsdown": "^0.18.1",
42
+ "typescript-json-schema": "^0.67.1"
43
+ },
44
+ "peerDependencies": {
45
+ "typescript": "^5"
46
+ }
47
+ }