This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
payments.ts
354 lines (306 loc) · 9.02 KB
/
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
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
/*
Copyright (C) 2018-present evan GmbH.
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License, version 3,
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see http://www.gnu.org/licenses/ or
write to the Free Software Foundation, Inc., 51 Franklin Street,
Fifth Floor, Boston, MA, 02110-1301 USA, or download the license from
the following URL: https://evan.network/license/
*/
import {
getDomainName,
lightwallet,
System
} from '@evan.network/ui-dapp-browser';
import {
ChangeDetectorRef,
Component,
DomSanitizer,
NavController,
OnInit,
TranslateService,
ViewChild,
Http, Response, RequestOptions, Headers,
Input,
} from '@evan.network/ui-angular-libs';
import {
AnimationDefinition,
AsyncComponent,
createOpacityTransition,
createRouterTransition,
createTabSlideTransition,
EvanAddressBookService,
EvanAlertService,
EvanBCCService,
EvanVerificationService,
EvanCoreService,
EvanPaymentService,
EvanQueue,
EvanToastService,
EvanTranslationService,
QueueId,
} from '@evan.network/ui-angular-core';
/**************************************************************************************************/
@Component({
selector: 'evan-payments-detail',
templateUrl: 'payments.html',
animations: [
createOpacityTransition(),
createTabSlideTransition()
]
})
/**
* Shows the current payment channel information for the ipfs payments
*/
export class EvanProfilePaymentsComponent extends AsyncComponent {
/**
* show the base loading symbol.
*/
public loading: boolean;
/**
* holds the payment channel details for the current account
*/
private paymentDetails: any = {};
/**
* opened payment channels.
*/
private paymentChannels: any = {
channels: [ ],
};
/**
* Current input values.
*/
private paymentChannelForm: any = { topUp: 0 };
/**
* this.bcc.web3.utils.fromWei
*/
public toEve: Function;
/**
* Function to unsubscribe from queue results.
*/
private queueWatcher: Function;
/**
* QueueId for quick accessing queue add & watch.
*/
private paymentQueueId: QueueId;
/**
* is currently the payment dispatcher running?
*/
private dispatcherRunning: boolean;
/**
* is currently an error occured by loading data?
*/
private error: any;
/**
* current active visbile action tab
*/
private actionTab: number = 0;
/**
* currently selectec channel for actions
*/
private activeChannel: any;
/**
* all active channels
*/
private activeChannels: any;
/**
* Balancer input reference
*/
@ViewChild('balanceInput') balanceInput: any;
constructor(
private _DomSanitizer: DomSanitizer,
private addressBookService: EvanAddressBookService,
private alertService: EvanAlertService,
private bcc: EvanBCCService,
private verificationsService: EvanVerificationService,
private core: EvanCoreService,
private http: Http,
private paymentService: EvanPaymentService,
private queue: EvanQueue,
private ref: ChangeDetectorRef,
private toastService: EvanToastService,
private translateService: EvanTranslationService,
) {
super(ref);
}
/**
* Set initial values and load the profile information
*
* @return {Promise<void>} resolved when done
*/
async _ngOnInit() {
this.toEve = this.bcc.web3.utils.fromWei;
// setup channel manager
this.bcc.payments.setChannelManager(this.paymentService.paymentChannelManagerAccountId);
// watch for updates and reload the ui data
this.paymentQueueId = new QueueId(`profile.${ getDomainName() }`, 'paymentDispatcher'),
this.queueWatcher = await this.queue.onQueueFinish(this.paymentQueueId, async (reload) => {
// show loading
if (reload) {
this.loading = true;
this.ref.detectChanges();
}
// load the data
await this.loadPaymentDetails();
await this.loadPaymentChannels();
// check if something is loading
this.dispatcherRunning = this.queue.getQueueEntry(this.paymentQueueId, true).data.length > 0;
// remove loading
if (reload) {
this.loading = false;
this.ref.detectChanges();
}
});
}
/**
* Load the payment details for the current account
*/
async loadPaymentDetails() {
try {
const status = await this.paymentService.requestPaymentAgent('status/get');
// parse correct value for estimated values
status.monthlyPayments = Math.floor(status.monthlyPayments).toString();
status.fundsAvailable = Math.floor(status.fundsAvailable).toString();
status.estimatedFunds = Math.floor(status.fundsAvailable / status.monthlyPayments);
// save status to components scope
this.paymentDetails = status;
} catch (ex) {
this.error = ex.message;
this.core.utils.log(ex.message, 'error');
}
}
/**
* Load available payment channels for the current user.
*/
async loadPaymentChannels() {
try {
this.paymentChannels = await this.paymentService.requestPaymentAgent('channel/get');
// find active channels
this.activeChannels = this.paymentChannels.channels
.filter(channel => channel.state === 'OPEN' || channel.state === 'UNCONFIRMED');
// preselect first active channel for actions
this.activeChannel = this.activeChannels.length > 0 ? this.activeChannels[0] : null;
} catch (ex) {
this.error = ex.message;
this.core.utils.log(ex.message, 'error');
}
}
/**
* Trigger the payment queue to create open a new payment channel, if no was registered before.
*/
async createPaymentChannel() {
try {
await this
.alertService.showSubmitAlert(
'_dappprofile.payment.topup-alert.title',
{
key: '_dappprofile.payment.topup-alert.description',
translateOptions: {
eve: this.paymentChannelForm.value
}
},
'_dappprofile.payment.topup-alert.cancel',
'_dappprofile.payment.topup-alert.submit',
);
} catch (ex) {
return;
}
this.runPaymentQueue({
type: 'openChannel',
eve: this.paymentChannelForm.value,
});
// reset input
this.paymentChannelForm.value = 0;
}
/**
* Add eve to a payment channel.
*/
async topupPaymentChannel(channel) {
try {
await this
.alertService.showSubmitAlert(
'_dappprofile.payment.topup-alert.title',
{
key: '_dappprofile.payment.topup-alert.description',
translateOptions: {
eve: this.paymentChannelForm.topUp
}
},
'_dappprofile.payment.topup-alert.cancel',
'_dappprofile.payment.topup-alert.submit',
);
} catch (ex) {
return;
}
this.runPaymentQueue({
type: 'topUp',
eve: this.paymentChannelForm.topUp,
channel: this.activeChannel,
});
// reset input
this.paymentChannelForm.topUp = 0;
}
/**
* Trigger ipfs hash removal.
*/
async removeHash() {
this.runPaymentQueue({
type: 'removeHash',
hash: this.paymentChannelForm.hash,
});
}
/**
* Run the payment queue and show loading symbols / disable buttons.
*
* @param {any} data queue entry data that should be pushed to the queue
*/
runPaymentQueue(data: any) {
// trigger dispatcher
this.queue.addQueueData(this.paymentQueueId, data);
// disable buttons and show loading
this.dispatcherRunning = true;
this.ref.detectChanges();
}
/**
* Actives the new chosen tab.
*
* @param {number} index index of the tab that should be displayed.
*/
activateActionTab(index: number) {
this.actionTab = index;
this.ref.detectChanges();
setTimeout(() => this.ref.detectChanges(), 500);
}
/**
* Transform incoming bytes to a displayable version.
*
* @param {number} bytes bytes that should be parsed
* @param {number} decimals amount of decimals, that should be displayed
* @return {string} parsed displayable size
*/
formatBytes(bytes: number, decimals: number) {
if (bytes === 0) {
return '0 Bytes';
}
const k = 1024,
dm = decimals <= 0 ? 0 : decimals || 2,
sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
/**
* Check if a ipfs hash is valid.
*
* @param {string} hash the hash that should be checked.
* @return {boolean} true / false
*/
validIpfsHash(hash: string = '') {
return hash.length === 46 && hash.indexOf('Qm') === 0;
}
}