/
app.component.ts
68 lines (54 loc) · 1.71 KB
/
app.component.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
import { Component, HostListener, NgZone } from '@angular/core';
import {Web3Service, MetaCoinService} from '../services/services'
import { canBeNumber } from '../util/validation';
declare var window: any;
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
// TODO add proper types these variables
account: any;
accounts: any;
balance: number;
sendingAmount: number;
recipientAddress: string;
status: string;
canBeNumber = canBeNumber;
constructor(
private _ngZone: NgZone,
private web3Service: Web3Service,
private metaCoinService: MetaCoinService,
) {
this.onReady();
}
onReady = () => {
// Get the initial account balance so it can be displayed.
this.web3Service.getAccounts().subscribe(accs => {
this.accounts = accs;
this.account = this.accounts[0];
// This is run from window:load and ZoneJS is not aware of it we
// need to use _ngZone.run() so that the UI updates on promise resolution
this._ngZone.run(() =>
this.refreshBalance()
);
}, err => alert(err))
};
refreshBalance = () => {
this.metaCoinService.getBalance(this.account)
.subscribe(value => {
this.balance = value
}, e => {this.setStatus('Error getting balance; see log.')})
};
setStatus = message => {
this.status = message;
};
sendCoin = () => {
this.setStatus('Initiating transaction... (please wait)');
this.metaCoinService.sendCoin(this.account, this.recipientAddress, this.sendingAmount)
.subscribe(() =>{
this.setStatus('Transaction complete!');
this.refreshBalance();
}, e => this.setStatus('Error sending coin; see log.'))
};
}