forked from etherspot/etherspot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05-p2p-payments.ts
78 lines (63 loc) · 1.91 KB
/
05-p2p-payments.ts
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
import { Wallet } from 'ethers';
import { GatewayKnownOps, Sdk } from '../../src';
import { logger, topUpAccount } from './common';
async function main(): Promise<void> {
const senderSdk = new Sdk(Wallet.createRandom());
const recipientSdk = new Sdk(Wallet.createRandom());
const { state: senderState } = senderSdk;
const { state: recipientState } = recipientSdk;
logger.log(
'sender contract account',
await senderSdk.computeContractAccount({
sync: false,
}),
);
logger.log(
'recipient contract account',
await recipientSdk.computeContractAccount({
sync: false,
}),
);
await topUpAccount(recipientState.accountAddress, '0.5');
await topUpAccount(senderState.accountAddress, '0.5');
await topUpAccount(senderState.p2pPaymentDepositAddress, '2');
const partialPaymentValue = 100;
const partialPaymentCount = 5;
let hash: string;
for (let index = 1; index <= partialPaymentCount; index++) {
const totalAmount = index * partialPaymentValue;
const paymentChannel = await senderSdk.updateP2PPaymentChannel({
totalAmount,
recipient: recipientState.accountAddress,
});
if (!hash) {
({ hash } = paymentChannel);
}
logger.log(`payment channel #${index}`, paymentChannel);
}
logger.log(
'batch',
await recipientSdk.batchCommitP2PPaymentChannel({
hash,
deposit: true,
}),
);
logger.log('estimated batch', await recipientSdk.estimateGatewayBatch());
logger.log('submitted batch', await recipientSdk.submitGatewayBatch());
logger.log(
'batch',
await senderSdk.batchWithdrawP2PPaymentDeposit({
amount: 100,
}),
);
logger.log('estimated batch', await senderSdk.estimateGatewayBatch());
logger.log(
'estimated op',
await senderSdk.estimateGatewayKnownOp({
op: GatewayKnownOps.WithdrawP2PDeposit,
}),
);
}
main()
.catch(logger.error)
.finally(() => process.exit());