@profullstack/coinpay 0.3.9 → 0.3.10
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/bin/coinpay.js +146 -0
- package/package.json +1 -1
- package/src/client.js +87 -0
- package/src/escrow.js +245 -0
- package/src/index.js +21 -0
package/bin/coinpay.js
CHANGED
|
@@ -161,6 +161,15 @@ ${colors.cyan}Commands:${colors.reset}
|
|
|
161
161
|
backup-seed Encrypt seed phrase to GPG file
|
|
162
162
|
decrypt-backup <file> Decrypt a GPG backup file
|
|
163
163
|
|
|
164
|
+
${colors.bright}escrow${colors.reset}
|
|
165
|
+
create Create a new escrow
|
|
166
|
+
get <id> Get escrow status
|
|
167
|
+
list List escrows
|
|
168
|
+
release <id> Release funds to beneficiary
|
|
169
|
+
refund <id> Refund funds to depositor
|
|
170
|
+
dispute <id> Open a dispute
|
|
171
|
+
events <id> Get escrow audit log
|
|
172
|
+
|
|
164
173
|
${colors.bright}webhook${colors.reset}
|
|
165
174
|
logs <business-id> Get webhook logs
|
|
166
175
|
test <business-id> Send test webhook
|
|
@@ -732,6 +741,139 @@ async function main() {
|
|
|
732
741
|
showHelp();
|
|
733
742
|
return;
|
|
734
743
|
}
|
|
744
|
+
|
|
745
|
+
async function handleEscrow(subcommand, args, flags) {
|
|
746
|
+
const client = getClient();
|
|
747
|
+
|
|
748
|
+
switch (subcommand) {
|
|
749
|
+
case 'create': {
|
|
750
|
+
const chain = flags.chain || flags.blockchain;
|
|
751
|
+
const amount = parseFloat(flags.amount);
|
|
752
|
+
const depositor = flags.depositor || flags['depositor-address'];
|
|
753
|
+
const beneficiary = flags.beneficiary || flags['beneficiary-address'];
|
|
754
|
+
|
|
755
|
+
if (!chain || !amount || !depositor || !beneficiary) {
|
|
756
|
+
print.error('Required: --chain, --amount, --depositor, --beneficiary');
|
|
757
|
+
process.exit(1);
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
print.info(`Creating escrow: ${amount} ${chain}`);
|
|
761
|
+
print.info(` Depositor: ${depositor}`);
|
|
762
|
+
print.info(` Beneficiary: ${beneficiary}`);
|
|
763
|
+
|
|
764
|
+
const escrow = await client.createEscrow({
|
|
765
|
+
chain,
|
|
766
|
+
amount,
|
|
767
|
+
depositorAddress: depositor,
|
|
768
|
+
beneficiaryAddress: beneficiary,
|
|
769
|
+
metadata: flags.metadata ? JSON.parse(flags.metadata) : undefined,
|
|
770
|
+
expiresInHours: flags['expires-in'] ? parseFloat(flags['expires-in']) : undefined,
|
|
771
|
+
});
|
|
772
|
+
|
|
773
|
+
print.success(`Escrow created: ${escrow.id}`);
|
|
774
|
+
print.info(` Deposit to: ${escrow.escrowAddress}`);
|
|
775
|
+
print.info(` Status: ${escrow.status}`);
|
|
776
|
+
print.info(` Expires: ${escrow.expiresAt}`);
|
|
777
|
+
print.warn(` Release Token: ${escrow.releaseToken}`);
|
|
778
|
+
print.warn(` Beneficiary Token: ${escrow.beneficiaryToken}`);
|
|
779
|
+
print.warn(' ⚠️ Save these tokens! They cannot be recovered.');
|
|
780
|
+
|
|
781
|
+
if (flags.json) console.log(JSON.stringify(escrow, null, 2));
|
|
782
|
+
break;
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
case 'get': {
|
|
786
|
+
const id = args[0];
|
|
787
|
+
if (!id) { print.error('Escrow ID required'); process.exit(1); }
|
|
788
|
+
|
|
789
|
+
const escrow = await client.getEscrow(id);
|
|
790
|
+
print.success(`Escrow ${escrow.id}`);
|
|
791
|
+
print.info(` Status: ${escrow.status}`);
|
|
792
|
+
print.info(` Chain: ${escrow.chain}`);
|
|
793
|
+
print.info(` Amount: ${escrow.amount}`);
|
|
794
|
+
print.info(` Escrow Address: ${escrow.escrowAddress}`);
|
|
795
|
+
print.info(` Depositor: ${escrow.depositorAddress}`);
|
|
796
|
+
print.info(` Beneficiary: ${escrow.beneficiaryAddress}`);
|
|
797
|
+
if (escrow.depositedAmount) print.info(` Deposited: ${escrow.depositedAmount}`);
|
|
798
|
+
if (escrow.depositTxHash) print.info(` Deposit TX: ${escrow.depositTxHash}`);
|
|
799
|
+
if (escrow.settlementTxHash) print.info(` Settlement TX: ${escrow.settlementTxHash}`);
|
|
800
|
+
|
|
801
|
+
if (flags.json) console.log(JSON.stringify(escrow, null, 2));
|
|
802
|
+
break;
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
case 'list': {
|
|
806
|
+
const result = await client.listEscrows({
|
|
807
|
+
status: flags.status,
|
|
808
|
+
depositor: flags.depositor,
|
|
809
|
+
beneficiary: flags.beneficiary,
|
|
810
|
+
limit: flags.limit ? parseInt(flags.limit) : 20,
|
|
811
|
+
});
|
|
812
|
+
|
|
813
|
+
print.info(`Escrows (${result.total} total):`);
|
|
814
|
+
for (const e of result.escrows) {
|
|
815
|
+
console.log(` ${e.id} | ${e.status} | ${e.amount} ${e.chain} | ${e.createdAt}`);
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
if (flags.json) console.log(JSON.stringify(result, null, 2));
|
|
819
|
+
break;
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
case 'release': {
|
|
823
|
+
const id = args[0];
|
|
824
|
+
const token = flags.token || flags['release-token'];
|
|
825
|
+
if (!id || !token) { print.error('Required: <id> --token <release_token>'); process.exit(1); }
|
|
826
|
+
|
|
827
|
+
const escrow = await client.releaseEscrow(id, token);
|
|
828
|
+
print.success(`Escrow ${id} released → ${escrow.beneficiaryAddress}`);
|
|
829
|
+
break;
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
case 'refund': {
|
|
833
|
+
const id = args[0];
|
|
834
|
+
const token = flags.token || flags['release-token'];
|
|
835
|
+
if (!id || !token) { print.error('Required: <id> --token <release_token>'); process.exit(1); }
|
|
836
|
+
|
|
837
|
+
const escrow = await client.refundEscrow(id, token);
|
|
838
|
+
print.success(`Escrow ${id} refunded → ${escrow.depositorAddress}`);
|
|
839
|
+
break;
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
case 'dispute': {
|
|
843
|
+
const id = args[0];
|
|
844
|
+
const token = flags.token || flags['release-token'] || flags['beneficiary-token'];
|
|
845
|
+
const reason = flags.reason;
|
|
846
|
+
if (!id || !token || !reason) {
|
|
847
|
+
print.error('Required: <id> --token <token> --reason "description"');
|
|
848
|
+
process.exit(1);
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
const escrow = await client.disputeEscrow(id, token, reason);
|
|
852
|
+
print.success(`Escrow ${id} disputed`);
|
|
853
|
+
print.info(` Reason: ${escrow.disputeReason}`);
|
|
854
|
+
break;
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
case 'events': {
|
|
858
|
+
const id = args[0];
|
|
859
|
+
if (!id) { print.error('Escrow ID required'); process.exit(1); }
|
|
860
|
+
|
|
861
|
+
const events = await client.getEscrowEvents(id);
|
|
862
|
+
print.info(`Events for escrow ${id}:`);
|
|
863
|
+
for (const e of events) {
|
|
864
|
+
console.log(` ${e.createdAt} | ${e.eventType} | ${e.actor || 'system'}`);
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
if (flags.json) console.log(JSON.stringify(events, null, 2));
|
|
868
|
+
break;
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
default:
|
|
872
|
+
print.error(`Unknown escrow command: ${subcommand}`);
|
|
873
|
+
print.info('Available: create, get, list, release, refund, dispute, events');
|
|
874
|
+
process.exit(1);
|
|
875
|
+
}
|
|
876
|
+
}
|
|
735
877
|
|
|
736
878
|
try {
|
|
737
879
|
switch (command) {
|
|
@@ -751,6 +893,10 @@ async function main() {
|
|
|
751
893
|
await handleRates(subcommand, args, flags);
|
|
752
894
|
break;
|
|
753
895
|
|
|
896
|
+
case 'escrow':
|
|
897
|
+
await handleEscrow(subcommand, args, flags);
|
|
898
|
+
break;
|
|
899
|
+
|
|
754
900
|
case 'webhook':
|
|
755
901
|
await handleWebhook(subcommand, args, flags);
|
|
756
902
|
break;
|
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -396,6 +396,93 @@ export class CoinPayClient {
|
|
|
396
396
|
}),
|
|
397
397
|
});
|
|
398
398
|
}
|
|
399
|
+
|
|
400
|
+
// ── Escrow Methods ──────────────────────────────────────
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Create a new escrow
|
|
404
|
+
* @param {Object} params - See escrow.js createEscrow for full params
|
|
405
|
+
* @returns {Promise<Object>} Created escrow with releaseToken
|
|
406
|
+
*/
|
|
407
|
+
async createEscrow(params) {
|
|
408
|
+
const { createEscrow } = await import('./escrow.js');
|
|
409
|
+
return createEscrow(this, params);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Get escrow status
|
|
414
|
+
* @param {string} escrowId
|
|
415
|
+
* @returns {Promise<Object>}
|
|
416
|
+
*/
|
|
417
|
+
async getEscrow(escrowId) {
|
|
418
|
+
const { getEscrow } = await import('./escrow.js');
|
|
419
|
+
return getEscrow(this, escrowId);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* List escrows with filters
|
|
424
|
+
* @param {Object} [filters]
|
|
425
|
+
* @returns {Promise<Object>}
|
|
426
|
+
*/
|
|
427
|
+
async listEscrows(filters) {
|
|
428
|
+
const { listEscrows } = await import('./escrow.js');
|
|
429
|
+
return listEscrows(this, filters);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Release escrow funds to beneficiary
|
|
434
|
+
* @param {string} escrowId
|
|
435
|
+
* @param {string} releaseToken
|
|
436
|
+
* @returns {Promise<Object>}
|
|
437
|
+
*/
|
|
438
|
+
async releaseEscrow(escrowId, releaseToken) {
|
|
439
|
+
const { releaseEscrow } = await import('./escrow.js');
|
|
440
|
+
return releaseEscrow(this, escrowId, releaseToken);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* Refund escrow to depositor
|
|
445
|
+
* @param {string} escrowId
|
|
446
|
+
* @param {string} releaseToken
|
|
447
|
+
* @returns {Promise<Object>}
|
|
448
|
+
*/
|
|
449
|
+
async refundEscrow(escrowId, releaseToken) {
|
|
450
|
+
const { refundEscrow } = await import('./escrow.js');
|
|
451
|
+
return refundEscrow(this, escrowId, releaseToken);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* Dispute an escrow
|
|
456
|
+
* @param {string} escrowId
|
|
457
|
+
* @param {string} token - release_token or beneficiary_token
|
|
458
|
+
* @param {string} reason - At least 10 characters
|
|
459
|
+
* @returns {Promise<Object>}
|
|
460
|
+
*/
|
|
461
|
+
async disputeEscrow(escrowId, token, reason) {
|
|
462
|
+
const { disputeEscrow } = await import('./escrow.js');
|
|
463
|
+
return disputeEscrow(this, escrowId, token, reason);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* Get escrow audit log
|
|
468
|
+
* @param {string} escrowId
|
|
469
|
+
* @returns {Promise<Array>}
|
|
470
|
+
*/
|
|
471
|
+
async getEscrowEvents(escrowId) {
|
|
472
|
+
const { getEscrowEvents } = await import('./escrow.js');
|
|
473
|
+
return getEscrowEvents(this, escrowId);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* Poll escrow until target status
|
|
478
|
+
* @param {string} escrowId
|
|
479
|
+
* @param {Object} [options] - { targetStatus, intervalMs, timeoutMs }
|
|
480
|
+
* @returns {Promise<Object>}
|
|
481
|
+
*/
|
|
482
|
+
async waitForEscrow(escrowId, options) {
|
|
483
|
+
const { waitForEscrow } = await import('./escrow.js');
|
|
484
|
+
return waitForEscrow(this, escrowId, options);
|
|
485
|
+
}
|
|
399
486
|
}
|
|
400
487
|
|
|
401
488
|
export default CoinPayClient;
|
package/src/escrow.js
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Escrow SDK Module
|
|
3
|
+
*
|
|
4
|
+
* Anonymous, non-custodial escrow for crypto payments.
|
|
5
|
+
* Both humans and AI agents can create/fund/release/dispute escrows.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* import { CoinPayClient } from '@profullstack/coinpay';
|
|
9
|
+
*
|
|
10
|
+
* const client = new CoinPayClient({ apiKey: 'your-key' });
|
|
11
|
+
*
|
|
12
|
+
* // Create escrow
|
|
13
|
+
* const escrow = await client.createEscrow({
|
|
14
|
+
* chain: 'SOL',
|
|
15
|
+
* amount: 0.5,
|
|
16
|
+
* depositorAddress: 'depositor-wallet',
|
|
17
|
+
* beneficiaryAddress: 'worker-wallet',
|
|
18
|
+
* metadata: { job: 'Code review', deadline: '2026-02-10' }
|
|
19
|
+
* });
|
|
20
|
+
* // Save escrow.releaseToken — needed to release/refund
|
|
21
|
+
*
|
|
22
|
+
* // Check status
|
|
23
|
+
* const status = await client.getEscrow(escrow.id);
|
|
24
|
+
*
|
|
25
|
+
* // Release funds to worker
|
|
26
|
+
* await client.releaseEscrow(escrow.id, escrow.releaseToken);
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Create a new escrow
|
|
31
|
+
* @param {CoinPayClient} client - API client instance
|
|
32
|
+
* @param {Object} params - Escrow parameters
|
|
33
|
+
* @param {string} params.chain - Blockchain (BTC, ETH, SOL, POL, etc.)
|
|
34
|
+
* @param {number} params.amount - Crypto amount to escrow
|
|
35
|
+
* @param {string} params.depositorAddress - Wallet address for refunds
|
|
36
|
+
* @param {string} params.beneficiaryAddress - Wallet address for releases
|
|
37
|
+
* @param {string} [params.arbiterAddress] - Optional dispute resolver address
|
|
38
|
+
* @param {Object} [params.metadata] - Job details, milestones, etc.
|
|
39
|
+
* @param {number} [params.expiresInHours] - Deposit window (default: 24h)
|
|
40
|
+
* @returns {Promise<Object>} Created escrow with releaseToken and beneficiaryToken
|
|
41
|
+
*/
|
|
42
|
+
export async function createEscrow(client, {
|
|
43
|
+
chain,
|
|
44
|
+
amount,
|
|
45
|
+
depositorAddress,
|
|
46
|
+
beneficiaryAddress,
|
|
47
|
+
arbiterAddress,
|
|
48
|
+
metadata,
|
|
49
|
+
expiresInHours,
|
|
50
|
+
}) {
|
|
51
|
+
const body = {
|
|
52
|
+
chain,
|
|
53
|
+
amount,
|
|
54
|
+
depositor_address: depositorAddress,
|
|
55
|
+
beneficiary_address: beneficiaryAddress,
|
|
56
|
+
};
|
|
57
|
+
if (arbiterAddress) body.arbiter_address = arbiterAddress;
|
|
58
|
+
if (metadata) body.metadata = metadata;
|
|
59
|
+
if (expiresInHours) body.expires_in_hours = expiresInHours;
|
|
60
|
+
|
|
61
|
+
const data = await client.request('/escrow', {
|
|
62
|
+
method: 'POST',
|
|
63
|
+
body: JSON.stringify(body),
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
id: data.id,
|
|
68
|
+
escrowAddress: data.escrow_address,
|
|
69
|
+
chain: data.chain,
|
|
70
|
+
amount: data.amount,
|
|
71
|
+
amountUsd: data.amount_usd,
|
|
72
|
+
status: data.status,
|
|
73
|
+
depositorAddress: data.depositor_address,
|
|
74
|
+
beneficiaryAddress: data.beneficiary_address,
|
|
75
|
+
releaseToken: data.release_token,
|
|
76
|
+
beneficiaryToken: data.beneficiary_token,
|
|
77
|
+
metadata: data.metadata,
|
|
78
|
+
expiresAt: data.expires_at,
|
|
79
|
+
createdAt: data.created_at,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Get escrow status
|
|
85
|
+
* @param {CoinPayClient} client
|
|
86
|
+
* @param {string} escrowId
|
|
87
|
+
* @returns {Promise<Object>} Escrow status (public view, no tokens)
|
|
88
|
+
*/
|
|
89
|
+
export async function getEscrow(client, escrowId) {
|
|
90
|
+
const data = await client.request(`/escrow/${escrowId}`);
|
|
91
|
+
return normalizeEscrow(data);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* List escrows with filters
|
|
96
|
+
* @param {CoinPayClient} client
|
|
97
|
+
* @param {Object} [filters]
|
|
98
|
+
* @param {string} [filters.status] - Filter by status
|
|
99
|
+
* @param {string} [filters.depositor] - Filter by depositor address
|
|
100
|
+
* @param {string} [filters.beneficiary] - Filter by beneficiary address
|
|
101
|
+
* @param {number} [filters.limit] - Results per page (default: 20)
|
|
102
|
+
* @param {number} [filters.offset] - Offset for pagination
|
|
103
|
+
* @returns {Promise<Object>} { escrows, total, limit, offset }
|
|
104
|
+
*/
|
|
105
|
+
export async function listEscrows(client, filters = {}) {
|
|
106
|
+
const params = new URLSearchParams();
|
|
107
|
+
if (filters.status) params.set('status', filters.status);
|
|
108
|
+
if (filters.depositor) params.set('depositor', filters.depositor);
|
|
109
|
+
if (filters.beneficiary) params.set('beneficiary', filters.beneficiary);
|
|
110
|
+
if (filters.limit) params.set('limit', String(filters.limit));
|
|
111
|
+
if (filters.offset) params.set('offset', String(filters.offset));
|
|
112
|
+
|
|
113
|
+
const data = await client.request(`/escrow?${params.toString()}`);
|
|
114
|
+
return {
|
|
115
|
+
escrows: (data.escrows || []).map(normalizeEscrow),
|
|
116
|
+
total: data.total,
|
|
117
|
+
limit: data.limit,
|
|
118
|
+
offset: data.offset,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Release escrow funds to beneficiary
|
|
124
|
+
* @param {CoinPayClient} client
|
|
125
|
+
* @param {string} escrowId
|
|
126
|
+
* @param {string} releaseToken - Secret token from escrow creation
|
|
127
|
+
* @returns {Promise<Object>} Updated escrow
|
|
128
|
+
*/
|
|
129
|
+
export async function releaseEscrow(client, escrowId, releaseToken) {
|
|
130
|
+
const data = await client.request(`/escrow/${escrowId}/release`, {
|
|
131
|
+
method: 'POST',
|
|
132
|
+
body: JSON.stringify({ release_token: releaseToken }),
|
|
133
|
+
});
|
|
134
|
+
return normalizeEscrow(data);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Refund escrow to depositor
|
|
139
|
+
* @param {CoinPayClient} client
|
|
140
|
+
* @param {string} escrowId
|
|
141
|
+
* @param {string} releaseToken
|
|
142
|
+
* @returns {Promise<Object>} Updated escrow
|
|
143
|
+
*/
|
|
144
|
+
export async function refundEscrow(client, escrowId, releaseToken) {
|
|
145
|
+
const data = await client.request(`/escrow/${escrowId}/refund`, {
|
|
146
|
+
method: 'POST',
|
|
147
|
+
body: JSON.stringify({ release_token: releaseToken }),
|
|
148
|
+
});
|
|
149
|
+
return normalizeEscrow(data);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Dispute an escrow
|
|
154
|
+
* @param {CoinPayClient} client
|
|
155
|
+
* @param {string} escrowId
|
|
156
|
+
* @param {string} token - release_token or beneficiary_token
|
|
157
|
+
* @param {string} reason - Dispute reason (min 10 chars)
|
|
158
|
+
* @returns {Promise<Object>} Updated escrow
|
|
159
|
+
*/
|
|
160
|
+
export async function disputeEscrow(client, escrowId, token, reason) {
|
|
161
|
+
const data = await client.request(`/escrow/${escrowId}/dispute`, {
|
|
162
|
+
method: 'POST',
|
|
163
|
+
body: JSON.stringify({ token, reason }),
|
|
164
|
+
});
|
|
165
|
+
return normalizeEscrow(data);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Get escrow event log
|
|
170
|
+
* @param {CoinPayClient} client
|
|
171
|
+
* @param {string} escrowId
|
|
172
|
+
* @returns {Promise<Array>} Array of events
|
|
173
|
+
*/
|
|
174
|
+
export async function getEscrowEvents(client, escrowId) {
|
|
175
|
+
const data = await client.request(`/escrow/${escrowId}/events`);
|
|
176
|
+
return (data.events || []).map(e => ({
|
|
177
|
+
id: e.id,
|
|
178
|
+
escrowId: e.escrow_id,
|
|
179
|
+
eventType: e.event_type,
|
|
180
|
+
actor: e.actor,
|
|
181
|
+
details: e.details,
|
|
182
|
+
createdAt: e.created_at,
|
|
183
|
+
}));
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Poll escrow until it reaches a target status
|
|
188
|
+
* @param {CoinPayClient} client
|
|
189
|
+
* @param {string} escrowId
|
|
190
|
+
* @param {Object} [options]
|
|
191
|
+
* @param {string} [options.targetStatus] - Status to wait for (default: 'funded')
|
|
192
|
+
* @param {number} [options.intervalMs] - Poll interval (default: 10000)
|
|
193
|
+
* @param {number} [options.timeoutMs] - Max wait time (default: 3600000 = 1h)
|
|
194
|
+
* @returns {Promise<Object>} Escrow when target status reached
|
|
195
|
+
*/
|
|
196
|
+
export async function waitForEscrow(client, escrowId, options = {}) {
|
|
197
|
+
const {
|
|
198
|
+
targetStatus = 'funded',
|
|
199
|
+
intervalMs = 10000,
|
|
200
|
+
timeoutMs = 3600000,
|
|
201
|
+
} = options;
|
|
202
|
+
|
|
203
|
+
const start = Date.now();
|
|
204
|
+
|
|
205
|
+
while (Date.now() - start < timeoutMs) {
|
|
206
|
+
const escrow = await getEscrow(client, escrowId);
|
|
207
|
+
|
|
208
|
+
if (escrow.status === targetStatus) return escrow;
|
|
209
|
+
if (['settled', 'refunded', 'expired'].includes(escrow.status)) return escrow;
|
|
210
|
+
|
|
211
|
+
await new Promise(resolve => setTimeout(resolve, intervalMs));
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
throw new Error(`Escrow ${escrowId} did not reach status '${targetStatus}' within ${timeoutMs}ms`);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// ── Helpers ──
|
|
218
|
+
|
|
219
|
+
function normalizeEscrow(data) {
|
|
220
|
+
return {
|
|
221
|
+
id: data.id,
|
|
222
|
+
escrowAddress: data.escrow_address,
|
|
223
|
+
chain: data.chain,
|
|
224
|
+
amount: data.amount,
|
|
225
|
+
amountUsd: data.amount_usd,
|
|
226
|
+
feeAmount: data.fee_amount,
|
|
227
|
+
depositedAmount: data.deposited_amount,
|
|
228
|
+
status: data.status,
|
|
229
|
+
depositorAddress: data.depositor_address,
|
|
230
|
+
beneficiaryAddress: data.beneficiary_address,
|
|
231
|
+
arbiterAddress: data.arbiter_address,
|
|
232
|
+
depositTxHash: data.deposit_tx_hash,
|
|
233
|
+
settlementTxHash: data.settlement_tx_hash,
|
|
234
|
+
metadata: data.metadata,
|
|
235
|
+
disputeReason: data.dispute_reason,
|
|
236
|
+
disputeResolution: data.dispute_resolution,
|
|
237
|
+
createdAt: data.created_at,
|
|
238
|
+
fundedAt: data.funded_at,
|
|
239
|
+
releasedAt: data.released_at,
|
|
240
|
+
settledAt: data.settled_at,
|
|
241
|
+
disputedAt: data.disputed_at,
|
|
242
|
+
refundedAt: data.refunded_at,
|
|
243
|
+
expiresAt: data.expires_at,
|
|
244
|
+
};
|
|
245
|
+
}
|
package/src/index.js
CHANGED
|
@@ -36,6 +36,17 @@ import {
|
|
|
36
36
|
WebhookEvent,
|
|
37
37
|
} from './webhooks.js';
|
|
38
38
|
|
|
39
|
+
import {
|
|
40
|
+
createEscrow,
|
|
41
|
+
getEscrow,
|
|
42
|
+
listEscrows,
|
|
43
|
+
releaseEscrow,
|
|
44
|
+
refundEscrow,
|
|
45
|
+
disputeEscrow,
|
|
46
|
+
getEscrowEvents,
|
|
47
|
+
waitForEscrow,
|
|
48
|
+
} from './escrow.js';
|
|
49
|
+
|
|
39
50
|
export {
|
|
40
51
|
// Client
|
|
41
52
|
CoinPayClient,
|
|
@@ -45,6 +56,16 @@ export {
|
|
|
45
56
|
getPayment,
|
|
46
57
|
listPayments,
|
|
47
58
|
|
|
59
|
+
// Escrow functions
|
|
60
|
+
createEscrow,
|
|
61
|
+
getEscrow,
|
|
62
|
+
listEscrows,
|
|
63
|
+
releaseEscrow,
|
|
64
|
+
refundEscrow,
|
|
65
|
+
disputeEscrow,
|
|
66
|
+
getEscrowEvents,
|
|
67
|
+
waitForEscrow,
|
|
68
|
+
|
|
48
69
|
// Constants
|
|
49
70
|
Blockchain,
|
|
50
71
|
Cryptocurrency, // Deprecated, use Blockchain
|