-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfloatEthTip.js
74 lines (65 loc) · 2.27 KB
/
floatEthTip.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
72
73
74
function sendEth(fromAddress, toAddress, etherValue) {
var weiValue = etherValue * Math.pow(10, 18);
var weiValueHex = weiValue.toString(16);
if (window.ethereum == undefined) {
console.log('[ethTip] sendEth called without window.ethereum object');
return;
}
params = [{
to: toAddress,
from: fromAddress,
value: weiValueHex
}];
window.ethereum.request({
method: 'eth_sendTransaction',
params: params
})
.then((result) => {
console.log('[ethTip] User completed Tx: https://etherscan.io/tx/' + result);
alert('Thanks for the eth! https://etherscan.io/tx/' + result);
})
.catch((error) => {
console.log('[ethTip] ' + error['message']);
});
}
function connectAndSendEth(toAddress, etherValue) {
if (window.ethereum == undefined) {
console.log('[ethTip] connectAndSendEth called without window.ethereum object');
return;
}
window.ethereum.request(
{ method: 'eth_requestAccounts' }
)
.then((accounts) => {
if (accounts.length > 0) {
sendEth(accounts[0], toAddress, etherValue);
}
})
.catch((error) => {
console.log('[ethTip] Error: ' + JSON.stringify(error));
});
}
function getElementByTagAttributeAndValue(tag, attribute, value) {
var elements = document.getElementsByTagName(tag);
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
var attributeValue = element.getAttribute(attribute);
if (attributeValue == value) {
return element;
}
}
return null;
}
document.addEventListener('DOMContentLoaded', (event) => {
var ethAddressElement = getElementByTagAttributeAndValue('meta', 'name', 'ethAddress');
var ethTipElement = getElementByTagAttributeAndValue('meta', 'name', 'ethTip');
var toAddress = ethAddressElement? ethAddressElement.getAttribute('content'): null;
var etherTip = ethTipElement? ethTipElement.getAttribute('content'): null;
if (window.ethereum != undefined && toAddress != undefined && etherTip != undefined) {
var etherTipButton = document.getElementByClassName('ethTipButton');
etherTipButton.classList.add('active');
etherTipButton.addEventListener('click', (event) => {
connectAndSendEth(toAddress, etherTip);
});
}
});