Skip to content

Instantly share code, notes, and snippets.

@hm0429
Created April 28, 2018 07:24
Show Gist options
  • Save hm0429/b3358249d6a1a669e143426625cb7cd5 to your computer and use it in GitHub Desktop.
Save hm0429/b3358249d6a1a669e143426625cb7cd5 to your computer and use it in GitHub Desktop.
tip_eth.html
<a href="#" id="eth_tip_button" onclick="onEthTipButtonClick();">
TIP $ETH
</a>
<script type="text/javascript">
// set your Ethereum wallet address.
var ETH_ADDRESS = '0x96e530a7a5fdAf3b041BdB8c3ABF30EeD1621eA9';
// set ETH amount that you want to receive
var ETH_VALUE = '0.01';
// set a default gas price
var ETH_DEFAULT_GAS_PRICE = 21000000000;
// set a message to show when Ethereum transaction is successfully sent.
var ETH_SUCCESS_MESSAGE = 'Thank you!';
// set a message to show when Ethereum transaction is failed.
var ETH_ERROR_MESSAGE = 'Something went wrong.';
// set a message to show when MetaMask is not available.
var ETH_WEB3_UNAVAILABLE_MESSAGE = 'Please send ETH to ' + ETH_ADDRESS + ' for helping this website. Thanks you!';
</script>
<style>
#eth_tip_button {
position: relative;
display: inline-block;
padding: 0.2em 0.4em;
border-radius: 3px;
font-weight: 500;
text-decoration: none;
color: #ffffff;
background: #00a968;
}
</style>
<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@0.20.6/dist/web3.min.js"></script>
<script type="text/javascript">
function onEthTipButtonClick() {
var isWeb3Available = false;
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
isWeb3Available = true;
}
if (isWeb3Available && web3.eth.defaultAccount) {
sendEther();
} else {
alert(ETH_WEB3_UNAVAILABLE_MESSAGE);
}
};
function sendEther() {
var weiValue = web3.toWei(ETH_VALUE, 'ether');
var transactionObj = {
to: ETH_ADDRESS,
value: weiValue,
gasPrice: ETH_DEFAULT_GAS_PRICE
};
web3.eth.sendTransaction(transactionObj, function(error, txHash) {
if (error) {
alert(ETH_ERROR_MESSAGE);
} else {
alert(ETH_SUCCESS_MESSAGE);
}
});
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment