-
Notifications
You must be signed in to change notification settings - Fork 0
/
metamask.js
71 lines (65 loc) · 1.39 KB
/
metamask.js
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
/**
* Check if MetaMask is installed
*/
let checkInstalled = function (){
if(typeof web3 === 'undefined' || !web3.currentProvider.isMetaMask)
return false;
else
return true;
};
/**
* Get MetaMask account list
*/
let getAccounts = function (){
if(web3.eth.accounts.length == 0 || web3.eth.accounts == null)
console.log("Accounts not found or MetaMask probably unlocked");
else
return web3.eth.accounts;
};
/**
* Detect Network ID
* "1": Main Net
* "2": Morden
* "3": Ropsten
* "4": Rinkeby
* "42": Kovan
* other: unknown
* @param {callback} cb
*/
let getNetworkId = function(cb){
web3.version.getNetwork((err, netId) => {
if(err)
cb(err)
else
cb(null, netId);
});
};
/**
*
* @param {from account} _from
* @param {to account} _to
* @param {amount to send in Ether} _amount
* @param {gas amount} _gas
* @param {data to send} _data
* @param {callback function} cb
*/
let createTransaction = function(_from, _to, _amount, _gas, _data, cb) {
if(_to === undefined)
cb("Missing Parameters");
else if(_from === undefined)
_from = web3.eth.accounts[0];
web3.eth.sendTransaction({
from: _from,
to: _to,
value: web3.toWei(_amount || 0, "ether"),
gasPrice: 1000000000,
gas: _gas || 21000,
data: _data || null
}, (error, txHash) => {
if(error)
cb(error);
else{
cb(null, txHash);
}
});
};