forked from Stellar-IndigoPay/Stellar-IndigoPay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonthlyGiving.ts
More file actions
317 lines (286 loc) · 9.01 KB
/
Copy pathmonthlyGiving.ts
File metadata and controls
317 lines (286 loc) · 9.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/**
* lib/monthlyGiving.ts — On-chain recurring donation subscriptions (#81).
*
* Thin client for the Soroban contract's subscription entry points:
* reading a single subscription, enumerating due subscriptions for a
* donor across the whole subscription list, and the create/cancel
* sign-and-submit flows via the connected wallet.
*/
import {
Account,
Contract,
TransactionBuilder,
nativeToScVal,
scValToNative,
rpc,
} from "@stellar/stellar-sdk";
import {
server,
rpcServer,
NETWORK_PASSPHRASE,
CONTRACT_ID,
submitSorobanTransaction,
} from "@/lib/stellar";
import { signTransactionWithWallet } from "@/lib/wallet";
/** ~1 day at 5s/ledger — the contract's minimum allowed subscription interval. */
export const MIN_SUBSCRIPTION_INTERVAL_LEDGERS = 17280;
/** Stroops per whole XLM (7 decimal places). */
const STROOPS_PER_XLM = BigInt(10_000_000);
export interface OnChainSubscription {
donor: string;
projectId: string;
amountXLM: string;
intervalLedgers: number;
nextExecutionLedger: number;
active: boolean;
createdAtLedger: number;
}
export interface CreateMonthlySubscriptionParams {
donor: string;
projectId: string;
amountXLM: string;
intervalLedgers?: number;
}
export interface CreateMonthlySubscriptionResult {
subscription: OnChainSubscription | null;
error: string | null;
}
export interface CancelMonthlySubscriptionResult {
success: boolean;
error: string | null;
}
function formatStroopsToXLM(stroops: bigint): string {
const whole = stroops / STROOPS_PER_XLM;
const frac = stroops % STROOPS_PER_XLM;
return `${whole}.${frac.toString().padStart(7, "0")}`;
}
/** Maps a raw on-chain subscription record to our camelCase shape. */
function decodeSubscription(raw: any): OnChainSubscription {
const amountStroops =
typeof raw.amount === "bigint" ? raw.amount : BigInt(raw.amount);
return {
donor: raw.donor,
projectId: raw.project_id,
amountXLM: formatStroopsToXLM(amountStroops),
intervalLedgers: Number(raw.interval_ledgers),
nextExecutionLedger: Number(raw.next_execution),
active: Boolean(raw.active),
createdAtLedger: Number(raw.created_at),
};
}
/**
* Maps a Soroban simulation failure's raw host-error text to a friendly,
* user-facing message. Shared by create/cancel since each only ever
* triggers the branch relevant to its own contract entry point.
*/
function friendlySimulationError(rawError: string | undefined): string {
const msg = rawError ?? "";
if (/already exists/i.test(msg)) {
return "You already have an active subscription for this project.";
}
if (/not found/i.test(msg)) {
return "No subscription was found for this project.";
}
return "Something went wrong while contacting the network. Please try again.";
}
/**
* Simulates a read-only contract call. `sourceAccountId` only needs to be
* a syntactically valid account — simulation doesn't require it to be
* funded or to match any real signer.
*/
async function simulateReadCall(
sourceAccountId: string,
method: string,
args: unknown[],
) {
const account = new Account(sourceAccountId, "0");
const contract = new Contract(CONTRACT_ID);
const op = contract.call(method, ...args.map((a) => nativeToScVal(a)));
const tx = new TransactionBuilder(account, {
fee: "100",
networkPassphrase: NETWORK_PASSPHRASE,
})
.addOperation(op)
.setTimeout(30)
.build();
return rpcServer.simulateTransaction(tx);
}
/** Reads a single donor+project subscription, or null if none exists. */
export async function getMonthlySubscription(
donor: string,
projectId: string,
): Promise<OnChainSubscription | null> {
try {
const sim = await simulateReadCall(donor, "get_subscription", [
donor,
projectId,
]);
if (!rpc.Api.isSimulationSuccess(sim)) return null;
const raw = scValToNative((sim as any).result.retval);
if (!raw) return null;
return decodeSubscription(raw);
} catch {
return null;
}
}
/**
* Enumerates every subscription on the contract and returns only the ones
* belonging to `donor` that are active and due (next_execution has
* already passed). A single unreadable index is skipped rather than
* aborting the whole scan.
*/
export async function getDueMonthlySubscriptionsForDonor(
donor: string,
): Promise<OnChainSubscription[]> {
const due: OnChainSubscription[] = [];
try {
const countSim = await simulateReadCall(
donor,
"get_subscription_count",
[],
);
if (!rpc.Api.isSimulationSuccess(countSim)) return due;
const count = Number(scValToNative((countSim as any).result.retval));
const { sequence: currentLedger } = await rpcServer.getLatestLedger();
for (let i = 0; i < count; i++) {
try {
const sim = await simulateReadCall(donor, "get_subscription_by_index", [
i,
]);
if (!rpc.Api.isSimulationSuccess(sim)) continue;
const raw = scValToNative((sim as any).result.retval);
if (!raw) continue;
const sub = decodeSubscription(raw);
if (
sub.donor === donor &&
sub.active &&
sub.nextExecutionLedger <= currentLedger
) {
due.push(sub);
}
} catch {
continue;
}
}
} catch {
return due;
}
return due;
}
/**
* Builds, simulates, signs (via the connected wallet), and submits a
* `create_subscription` call, then refetches the resulting subscription.
*/
export async function createMonthlySubscription({
donor,
projectId,
amountXLM,
intervalLedgers = MIN_SUBSCRIPTION_INTERVAL_LEDGERS,
}: CreateMonthlySubscriptionParams): Promise<CreateMonthlySubscriptionResult> {
try {
const account = await server.loadAccount(donor);
const amountStroops = BigInt(
Math.round(Number.parseFloat(amountXLM) * 10_000_000),
);
const contract = new Contract(CONTRACT_ID);
const op = contract.call(
"create_subscription",
nativeToScVal(donor),
nativeToScVal(projectId),
nativeToScVal(amountStroops),
nativeToScVal(intervalLedgers),
);
const tx = new TransactionBuilder(account, {
fee: "100",
networkPassphrase: NETWORK_PASSPHRASE,
})
.addOperation(op)
.setTimeout(30)
.build();
const sim = await rpcServer.simulateTransaction(tx);
if (!rpc.Api.isSimulationSuccess(sim)) {
return {
subscription: null,
error: friendlySimulationError((sim as any).error),
};
}
const prepared = rpc.assembleTransaction(tx, sim).build();
const preparedXDR = prepared.toXDR();
const { signedXDR, error: signError } =
await signTransactionWithWallet(preparedXDR);
if (signError || !signedXDR) {
return { subscription: null, error: signError || "Signing failed." };
}
await submitSorobanTransaction(signedXDR);
const subscription = await getMonthlySubscription(donor, projectId);
return { subscription, error: null };
} catch (err) {
return {
subscription: null,
error:
err instanceof Error
? err.message
: "Something went wrong. Please try again.",
};
}
}
/**
* Builds, simulates, signs (via the connected wallet), and submits a
* `cancel_subscription` call.
*/
/**
* Legacy compatibility shim: the pre-migration localStorage-based
* subscription model tracked a client-side "paid" flag per subscription.
* On-chain, that state lives entirely on the contract (next_execution /
* active), updated when the recurring donation actually executes, so
* there's nothing for the client to persist here. Kept as a no-op purely
* so existing call sites (e.g. pages/projects/[id].tsx) keep compiling.
*/
export async function markMonthlySubscriptionPaid(
_subscriptionId: string,
_amountXLM: string,
): Promise<void> {
return;
}
export async function cancelMonthlySubscription(
donor: string,
projectId: string,
): Promise<CancelMonthlySubscriptionResult> {
try {
const account = await server.loadAccount(donor);
const contract = new Contract(CONTRACT_ID);
const op = contract.call(
"cancel_subscription",
nativeToScVal(donor),
nativeToScVal(projectId),
);
const tx = new TransactionBuilder(account, {
fee: "100",
networkPassphrase: NETWORK_PASSPHRASE,
})
.addOperation(op)
.setTimeout(30)
.build();
const sim = await rpcServer.simulateTransaction(tx);
if (!rpc.Api.isSimulationSuccess(sim)) {
return { success: false, error: friendlySimulationError((sim as any).error) };
}
const prepared = rpc.assembleTransaction(tx, sim).build();
const preparedXDR = prepared.toXDR();
const { signedXDR, error: signError } =
await signTransactionWithWallet(preparedXDR);
if (signError || !signedXDR) {
return { success: false, error: signError || "Signing failed." };
}
await submitSorobanTransaction(signedXDR);
return { success: true, error: null };
} catch (err) {
return {
success: false,
error:
err instanceof Error
? err.message
: "Something went wrong. Please try again.",
};
}
}