-
Notifications
You must be signed in to change notification settings - Fork 0
/
web3V2.service.ts
executable file
·81 lines (64 loc) · 2.09 KB
/
web3V2.service.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
import {Injectable, OnInit} from '@angular/core';
// import * as Web3 from 'web3';
// import Web3 from 'web3';
// const Web3 = require('web3');
// const web3 = new Web3(Web3.givenProvider || "ws://localhost:8546");
declare let require: any;
declare let window: any;
let tokenAbi = require('./HealthDRS.json');
@Injectable()
export class Web3Service {
private _account: string = null;
private _web3: any;
private _tokenContract: any;
private _tokenContractAddress: string = "0xbfBBd01Ae2eA4BFc777F6ea3A2Ad4843c7a104FB";
constructor() {
if (typeof window.web3 !== 'undefined') {
// Use Mist/MetaMask's provider
this._web3 = new Web3(window.web3.currentProvider);
if (this._web3.version.network !== '4') {
alert('Please connect to the Rinkeby network');
}
} else {
console.warn(
'Please use a dapp browser like mist or MetaMask plugin for chrome'
);
}
this._tokenContract = this._web3.eth.contract(tokenAbi).at(this._tokenContractAddress);
}
test(): any {
};
private async getAccount(): Promise<string> {
if (this._account == null) {
this._account = await new Promise((resolve, reject) => {
this._web3.eth.getAccounts((err, accs) => {
if (err != null) {
alert('There was an error fetching your accounts.');
return;
}
if (accs.length === 0) {
alert(
'Couldn\'t get any accounts! Make sure your Ethereum client is configured correctly.'
);
return;
}
resolve(accs[0]);
})
}) as string;
this._web3.eth.defaultAccount = this._account;
}
return Promise.resolve(this._account);
}
public async getUserBalance(): Promise<number> {
let account = await this.getAccount();
return new Promise((resolve, reject) => {
let _web3 = this._web3;
this._tokenContract.balanceOf.call(account, function (err, result) {
if(err != null) {
reject(err);
}
resolve(_web3.fromWei(result));
});
}) as Promise<number>;
}
}