-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
51 lines (49 loc) · 1.5 KB
/
utils.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
// Originally written for Titan react-pack
export function unlock(w3, addr, pw) {
return new Promise((resolve, reject) => {
w3.personal.unlockAccount(addr, pw, 999999, (err, unlock) => {
if (err) reject(err);
else if (unlock && unlock === true) {
resolve(addr);
} else {
// Return as an object to match our error structure elsewhere
reject({ message: 'unlock fail' });
}
});
});
}
export const wait = ms => {
return new Promise((resolve, reject) => {
// If an invalid argument is passed, reject the promise
// If nothing is passed, resolve the promise immediately
if (typeof ms !== 'number' && ms !== undefined) {
reject(new Error('ms must be a number'));
}
// Wrap the call to resolve in a function so we can pass it a value
setTimeout(() => resolve(ms), ms);
});
};
export function pollForTransactionReceipt(w3, hashString) {
return new Promise((resolve, reject) => {
function checkForReceipt() {
console.log('Checking if transaction has been mined...');
w3.eth.getTransactionReceipt(hashString, (err, res) => {
if (err) {
reject(err);
} else {
if (res === null) {
// Wait, then
return (
wait(5000)
// Then, check again
.then(() => checkForReceipt())
);
}
resolve(res);
}
});
}
// Start checking if the transaction has been mined
checkForReceipt();
});
}