forked from etherspot/etherspot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06-payment-hub.ts
106 lines (90 loc) · 2.62 KB
/
06-payment-hub.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
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
import { utils } from 'ethers';
import { Sdk } from '../../src';
import { logger, topUpAccount, randomWallet } from './common';
async function main(): Promise<void> {
const hubSdk = new Sdk(randomWallet());
const senderSdk = new Sdk(randomWallet());
const recipientSdk = new Sdk(randomWallet());
const { state: hubState } = hubSdk;
const { state: senderState } = senderSdk;
const { state: recipientState } = recipientSdk;
await recipientSdk.computeContractAccount();
await topUpAccount(hubState.p2pPaymentDepositAddress, '10');
await topUpAccount(senderState.p2pPaymentDepositAddress, '5');
await topUpAccount(recipientState.accountAddress, '1');
const { accountAddress: hub } = hubState;
const { accountAddress: sender } = senderState;
const { accountAddress: recipient } = recipientState;
logger.log(
'payment hub',
await hubSdk.updatePaymentHub({
liquidity: utils.parseEther('5'),
}),
);
logger.log(
'payment hub sender deposit',
await senderSdk.updatePaymentHubDeposit({
hub,
totalAmount: utils.parseEther('5'),
}),
);
logger.log(
'payment hub payment (sender > recipient 1 ETH)',
await senderSdk.createPaymentHubPayment({
hub,
recipient,
value: utils.parseEther('1'),
}),
);
logger.log(
'payment hub payment (sender > recipient 2 ETH)',
await senderSdk.createPaymentHubPayment({
hub,
recipient,
value: utils.parseEther('2'),
}),
);
logger.log(
'payment hub payment (recipient > sender 1.5 ETH)',
await recipientSdk.createPaymentHubPayment({
hub,
recipient: sender,
value: utils.parseEther('1.5'),
}),
);
logger.log(
'payment hub recipient deposit',
await recipientSdk.getPaymentHubDeposit({
hub,
}),
);
logger.log(
'payment hub recipient deposit (updated)',
await recipientSdk.updatePaymentHubDeposit({
hub,
}),
);
const {
items: [paymentHubChannel],
} = await recipientSdk.getP2PPaymentChannels();
const { hash } = paymentHubChannel;
logger.log('payment hub recipient p2p channel', paymentHubChannel);
logger.log(
'payment hub recipient p2p channel (signed)',
await hubSdk.signP2PPaymentChannel({
hash,
}),
);
logger.log(
'batch',
await recipientSdk.batchCommitP2PPaymentChannel({
hash,
}),
);
logger.log('estimated batch', await recipientSdk.estimateGatewayBatch());
logger.log('submitted batch', await recipientSdk.submitGatewayBatch());
logger.log('recipient balances', await recipientSdk.getAccountBalances());
}
main()
.catch(logger.error)
.finally(() => process.exit());