forked from etherspot/etherspot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
07-payment-hub-bridge.ts
158 lines (131 loc) · 3.58 KB
/
07-payment-hub-bridge.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
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
import { utils } from 'ethers';
import { NetworkNames, Sdk } from '../../src';
import { logger, randomPrivateKey, topUpAccount } from './common';
async function main(): Promise<void> {
const hubPrivateKey = randomPrivateKey();
const senderPrivateKey = randomPrivateKey();
const hubLocalASdk = new Sdk({
privateKey: hubPrivateKey,
});
const hubLocalBSdk = new Sdk(
{
privateKey: hubPrivateKey,
},
{
networkName: NetworkNames.LocalB,
},
);
const senderLocalASdk = new Sdk({
privateKey: senderPrivateKey,
});
const senderLocalBSdk = new Sdk(
{
privateKey: senderPrivateKey,
},
{
networkName: NetworkNames.LocalB,
},
);
const recipientLocalBSdk = new Sdk(
{
privateKey: randomPrivateKey(),
},
{
networkName: NetworkNames.LocalB,
},
);
await recipientLocalBSdk.computeContractAccount();
const { state: hubLocalAState } = hubLocalASdk;
const { state: hubLocalBState } = hubLocalBSdk;
const { state: senderLocalAState } = senderLocalASdk;
const { state: recipientLocalBState } = recipientLocalBSdk;
const { accountAddress: hub } = hubLocalAState;
const { accountAddress: recipient } = recipientLocalBState;
await topUpAccount(hubLocalAState.p2pPaymentDepositAddress, '10');
await topUpAccount(hubLocalBState.p2pPaymentDepositAddress, '10', NetworkNames.LocalB);
await topUpAccount(senderLocalAState.p2pPaymentDepositAddress, '7');
logger.log(
'payment hub localA',
await hubLocalASdk.updatePaymentHub({
liquidity: utils.parseEther('7'),
}),
);
logger.log(
'payment hub localB',
await hubLocalBSdk.updatePaymentHub({
liquidity: utils.parseEther('7'),
}),
);
logger.log(
'payment hub localB bridge',
await hubLocalBSdk.activatePaymentHubBridge({
acceptedNetworkName: NetworkNames.LocalA,
}),
);
logger.log(
'sender localA deposit',
await senderLocalASdk.updatePaymentHubDeposit({
hub,
totalAmount: utils.parseEther('5'),
}),
);
logger.log(
'sender localA deposit',
await senderLocalASdk.transferPaymentHubDeposit({
hub,
targetHub: hub,
targetNetworkName: NetworkNames.LocalB,
value: utils.parseEther('4'),
}),
);
logger.log(
'sender localB deposit',
await senderLocalBSdk.getPaymentHubDeposit({
hub,
}),
);
logger.log(
'payment hub localB payment (sender > recipient 2 ETH)',
await senderLocalBSdk.createPaymentHubPayment({
hub,
recipient,
value: utils.parseEther('2'),
}),
);
logger.log(
'recipient localB deposit',
await recipientLocalBSdk.getPaymentHubDeposit({
hub,
}),
);
logger.log(
'recipient localB deposit',
await recipientLocalBSdk.updatePaymentHubDeposit({
hub,
}),
);
const {
items: [paymentHubChannel],
} = await recipientLocalBSdk.getP2PPaymentChannels();
const { hash } = paymentHubChannel;
logger.log('payment hub localB recipient p2p channel', paymentHubChannel);
logger.log(
'payment hub localB recipient p2p channel (signed)',
await hubLocalBSdk.signP2PPaymentChannel({
hash,
}),
);
logger.log(
'batch',
await recipientLocalBSdk.batchCommitP2PPaymentChannel({
hash,
deposit: false,
}),
);
logger.log('estimated batch', await recipientLocalBSdk.estimateGatewayBatch());
logger.log('submitted batch', await recipientLocalBSdk.submitGatewayBatch());
logger.log('recipient balances', await recipientLocalBSdk.getAccountBalances());
}
main()
.catch(logger.error)
.finally(() => process.exit());