Repository navigation
Expand file tree
/
Copy pathclient.ts
More file actions
456 lines (394 loc) · 14.1 KB
/
Copy pathclient.ts
File metadata and controls
456 lines (394 loc) · 14.1 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import {
Account,
Address,
BASE_FEE,
nativeToScVal,
Operation,
rpc,
scValToNative,
TransactionBuilder,
xdr,
} from '@stellar/stellar-sdk';
import { explainContractError } from './errors';
import type {
ClientOptions,
Invoice,
ListedInvoice,
ProtocolConfig,
RpcServerLike,
SimulationLike,
SubmitInvoiceInput,
TransactionSigner,
WriteResult,
} from './types';
const READ_ACCOUNT = 'GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF';
const BASE_TIMEOUT_SECONDS = 60;
const POLL_ATTEMPTS = 30;
const PROTOCOL_CONFIG_CACHE_MS = 5 * 60 * 1000;
export class ILNClient {
private readonly contractId: string;
private readonly networkPassphrase: string;
private readonly server: RpcServerLike;
private readonly signer: TransactionSigner;
private protocolConfigCache: { expiresAt: number; value: ProtocolConfig } | null = null;
constructor(options: ClientOptions) {
this.contractId = options.contractId;
this.networkPassphrase = options.networkPassphrase;
this.signer = options.signer;
this.server =
options.server ??
new rpc.Server(options.rpcUrl, {
allowHttp: options.rpcUrl.startsWith('http://'),
});
}
async submitInvoice(input: SubmitInvoiceInput): Promise<{ invoiceId: bigint; txHash: string }> {
const freelancer = await this.signer.getPublicKey();
const transaction = await this.buildWriteTransaction(freelancer, 'submit_invoice', [
Address.fromString(freelancer).toScVal(),
Address.fromString(input.payer).toScVal(),
nativeToScVal(input.amount, { type: 'i128' }),
nativeToScVal(input.dueDate, { type: 'u64' }),
nativeToScVal(input.discountRate, { type: 'u32' }),
Address.fromString(input.tokenId).toScVal(),
]);
const simulation = await this.simulate(transaction, 'submit_invoice');
const invoiceId = this.toBigInt(
this.unwrapContractResult(this.extractRetval(simulation), 'submit_invoice')
);
const result = await this.signAndSend(transaction, freelancer);
return { invoiceId, txHash: result.hash };
}
async fundInvoice(invoiceId: bigint, amount?: bigint): Promise<WriteResult> {
const funder = await this.signer.getPublicKey();
const invoice = await this.getInvoice(invoiceId);
const remaining = invoice.amount - invoice.amountFunded;
const fundAmount = amount ?? remaining;
if (fundAmount <= 0n) {
throw new Error('Invoice does not have any remaining balance to fund.');
}
const transaction = await this.buildWriteTransaction(funder, 'fund_invoice', [
Address.fromString(funder).toScVal(),
nativeToScVal(invoiceId, { type: 'u64' }),
nativeToScVal(fundAmount, { type: 'i128' }),
]);
await this.simulate(transaction, 'fund_invoice');
return this.signAndSend(transaction, funder);
}
async markPaid(invoiceId: bigint): Promise<WriteResult> {
const payer = await this.signer.getPublicKey();
const transaction = await this.buildWriteTransaction(payer, 'mark_paid', [
nativeToScVal(invoiceId, { type: 'u64' }),
]);
await this.simulate(transaction, 'mark_paid');
return this.signAndSend(transaction, payer);
}
async getInvoice(invoiceId: bigint): Promise<Invoice> {
const transaction = this.buildReadTransaction('get_invoice', [
nativeToScVal(invoiceId, { type: 'u64' }),
]);
const simulation = await this.simulate(transaction, 'get_invoice');
return this.parseInvoice(
this.unwrapContractResult(this.extractRetval(simulation), 'get_invoice')
);
}
async getInvoiceCount(): Promise<bigint> {
const transaction = this.buildReadTransaction('get_invoice_count', []);
const simulation = await this.simulate(transaction, 'get_invoice_count');
return this.toBigInt(this.extractRetval(simulation));
}
async listInvoicesByAddress(address: string): Promise<ListedInvoice[]> {
const count = await this.getInvoiceCount();
const invoices: ListedInvoice[] = [];
for (let index = 1n; index <= count; index += 1n) {
const invoice = await this.getInvoice(index);
const role = matchRole(invoice, address);
if (role) {
invoices.push({ ...invoice, role });
}
}
return invoices;
}
async getVersion(): Promise<string> {
const transaction = this.buildReadTransaction('get_version', []);
const simulation = await this.simulate(transaction, 'get_version');
return String(this.unwrapContractResult(this.extractRetval(simulation), 'get_version'));
}
async getReputation(address: string): Promise<number> {
const transaction = this.buildReadTransaction('get_reputation', [
Address.fromString(address).toScVal(),
]);
const simulation = await this.simulate(transaction, 'get_reputation');
return this.toNumber(
this.unwrapContractResult(this.extractRetval(simulation), 'get_reputation'),
'reputation score'
);
}
async getProtocolConfig(): Promise<ProtocolConfig> {
const now = Date.now();
if (this.protocolConfigCache && this.protocolConfigCache.expiresAt > now) {
return this.protocolConfigCache.value;
}
const transaction = this.buildReadTransaction('get_protocol_config', []);
const simulation = await this.simulate(transaction, 'get_protocol_config');
const config = this.parseProtocolConfig(
this.unwrapContractResult(this.extractRetval(simulation), 'get_protocol_config')
);
this.protocolConfigCache = {
expiresAt: now + PROTOCOL_CONFIG_CACHE_MS,
value: config,
};
return config;
}
private buildReadTransaction(method: string, args: xdr.ScVal[]) {
return new TransactionBuilder(new Account(READ_ACCOUNT, '0'), {
fee: BASE_FEE,
networkPassphrase: this.networkPassphrase,
})
.addOperation(
Operation.invokeContractFunction({
args,
contract: this.contractId,
function: method,
})
)
.setTimeout(BASE_TIMEOUT_SECONDS)
.build();
}
private async buildWriteTransaction(sourceAddress: string, method: string, args: xdr.ScVal[]) {
const sourceAccount = (await this.server.getAccount(sourceAddress)) as Account;
return new TransactionBuilder(sourceAccount, {
fee: BASE_FEE,
networkPassphrase: this.networkPassphrase,
})
.addOperation(
Operation.invokeContractFunction({
args,
contract: this.contractId,
function: method,
})
)
.setTimeout(BASE_TIMEOUT_SECONDS)
.build();
}
private async simulate(transaction: unknown, method: string): Promise<SimulationLike> {
const simulation = (await this.server.simulateTransaction(transaction)) as SimulationLike;
if (simulation.error) {
throw new Error(`Simulation failed for ${method}: ${String(simulation.error)}`);
}
return simulation;
}
private extractRetval(simulation: SimulationLike): unknown {
if (!simulation.result?.retval) {
throw new Error('RPC simulation did not return a contract result.');
}
return scValToNative(simulation.result.retval as xdr.ScVal);
}
private unwrapContractResult(value: unknown, method: string): unknown {
if (!value || typeof value !== 'object') {
return value;
}
if ('ok' in value) {
return (value as { ok: unknown }).ok;
}
if ('Ok' in value) {
return (value as { Ok: unknown }).Ok;
}
if ('err' in value) {
throw this.contractError(method, (value as { err: unknown }).err);
}
if ('Err' in value) {
throw this.contractError(method, (value as { Err: unknown }).Err);
}
return value;
}
private contractError(method: string, raw: unknown): Error {
const code = typeof raw === 'number' ? raw : typeof raw === 'bigint' ? Number(raw) : null;
const details = code === null ? JSON.stringify(raw) : explainContractError(code);
return new Error(`Contract rejected ${method}: ${details}`);
}
private async signAndSend(
transaction: ReturnType<TransactionBuilder['build']>,
_sourceAddress: string
): Promise<WriteResult> {
let prepared: { toXDR(): string };
try {
prepared = await this.server.prepareTransaction(transaction);
} catch (error) {
throw new Error(
`Failed to prepare transaction: ${
error instanceof Error ? error.message : 'unknown RPC error'
}.`
);
}
const signedXdr = await this.signer.signTransaction(prepared.toXDR(), this.networkPassphrase);
const signedTransaction = TransactionBuilder.fromXDR(signedXdr, this.networkPassphrase);
const response = (await this.server.sendTransaction(signedTransaction)) as {
errorResultXdr?: string;
hash?: string;
status?: string;
};
if (!response.hash || !response.status) {
throw new Error('RPC server returned an invalid sendTransaction response.');
}
if (response.status !== 'PENDING' && response.status !== 'DUPLICATE') {
throw new Error(
`Transaction submission failed with status ${response.status}. ${
response.errorResultXdr ?? ''
}`.trim()
);
}
const finalStatus = (await this.server.pollTransaction(response.hash, {
attempts: POLL_ATTEMPTS,
})) as {
resultXdr?: string;
status?: string;
};
if (finalStatus.status !== rpc.Api.GetTransactionStatus.SUCCESS) {
throw new Error(`Transaction did not succeed. Final status: ${String(finalStatus.status)}.`);
}
return { hash: response.hash };
}
private parseInvoice(value: unknown): Invoice {
if (!value || typeof value !== 'object') {
throw new Error('Contract returned an invalid invoice payload.');
}
const invoice = value as Record<string, unknown>;
return {
amount: this.toBigInt(invoice.amount),
amountFunded: this.toBigInt(invoice.amount_funded ?? invoice.amountFunded ?? 0n),
discountRate: this.toNumber(invoice.discount_rate ?? invoice.discountRate, 'discount rate'),
dueDate: this.toNumber(invoice.due_date ?? invoice.dueDate, 'due date'),
freelancer: this.toString(invoice.freelancer, 'freelancer'),
fundedAt:
(invoice.funded_at === null || invoice.funded_at === undefined) &&
(invoice.fundedAt === null || invoice.fundedAt === undefined)
? null
: this.toNumber(invoice.funded_at ?? invoice.fundedAt, 'funded at'),
funder:
invoice.funder === null || invoice.funder === undefined
? null
: this.toString(invoice.funder, 'funder'),
id: this.toBigInt(invoice.id),
payer: this.toString(invoice.payer, 'payer'),
status: this.parseStatus(invoice.status),
token: this.toString(invoice.token, 'token'),
};
}
private parseProtocolConfig(value: unknown): ProtocolConfig {
if (!value || typeof value !== 'object') {
throw new Error('Contract returned an invalid protocol config payload.');
}
const config = value as Record<string, unknown>;
return {
minInvoiceAmount: this.toBigInt(
this.configValue(config, 'minInvoiceAmount', 'min_invoice_amount', 'MIN_INVOICE_AMOUNT')
),
maxDiscountRate: this.toNumber(
this.configValue(config, 'maxDiscountRate', 'max_discount_rate', 'MAX_DISCOUNT_RATE'),
'max discount rate'
),
protocolFeeBps: this.toNumber(
this.configValue(config, 'protocolFeeBps', 'protocol_fee_bps', 'PROTOCOL_FEE_BPS'),
'protocol fee bps'
),
minPayerReputation: this.toNumber(
this.configValue(
config,
'minPayerReputation',
'min_payer_reputation',
'MIN_PAYER_REPUTATION'
),
'min payer reputation'
),
decayRateBps: this.toNumber(
this.configValue(config, 'decayRateBps', 'decay_rate_bps', 'DECAY_RATE_BPS'),
'decay rate bps'
),
maxInvoiceDuration: this.optionalNumber(
config,
'maxInvoiceDuration',
'max_invoice_duration',
'MAX_INVOICE_DURATION'
),
minInvoiceDuration: this.optionalNumber(
config,
'minInvoiceDuration',
'min_invoice_duration',
'MIN_INVOICE_DURATION'
),
gracePeriodSeconds: this.optionalNumber(
config,
'gracePeriodSeconds',
'grace_period_seconds',
'GRACE_PERIOD_SECONDS'
),
};
}
private configValue(config: Record<string, unknown>, ...keys: string[]): unknown {
for (const key of keys) {
if (config[key] !== undefined) {
return config[key];
}
}
throw new Error(`Protocol config is missing ${keys[0]}.`);
}
private optionalNumber(config: Record<string, unknown>, ...keys: string[]): number | undefined {
for (const key of keys) {
if (config[key] !== undefined && config[key] !== null) {
return this.toNumber(config[key], key);
}
}
return undefined;
}
private parseStatus(value: unknown): string {
if (typeof value === 'string') {
return value;
}
if (typeof value === 'object' && value !== null) {
const keys = Object.keys(value);
if (keys.length === 1) {
return keys[0];
}
}
throw new Error('Contract returned an invalid invoice status.');
}
private toBigInt(value: unknown): bigint {
if (typeof value === 'bigint') {
return value;
}
if (typeof value === 'number') {
return BigInt(value);
}
if (typeof value === 'string') {
return BigInt(value);
}
throw new Error(`Expected bigint-like value, received ${typeof value}.`);
}
private toNumber(value: unknown, field: string): number {
if (typeof value === 'number') {
return value;
}
if (typeof value === 'bigint') {
return Number(value);
}
throw new Error(`Expected numeric ${field}, received ${typeof value}.`);
}
private toString(value: unknown, field: string): string {
if (typeof value === 'string') {
return value;
}
throw new Error(`Expected string ${field}, received ${typeof value}.`);
}
}
function matchRole(invoice: Invoice, address: string): ListedInvoice['role'] | null {
if (invoice.freelancer === address) {
return 'freelancer';
}
if (invoice.payer === address) {
return 'payer';
}
if (invoice.funder === address) {
return 'funder';
}
return null;
}