tip_eth2.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input id="eth_tip_value" type="number" value="0.01" step="0.01" min="0"> | |
<a href="#" id="eth_tip_button" onclick="onEthTipButtonClick();"> | |
ETH を送ってこのサイトを応援! | |
</a> | |
<script type="text/javascript"> | |
// 必ず自分の Ethereum ウォレットのアドレスを指定してください。 | |
var ETH_ADDRESS = '0x96e530a7a5fdAf3b041BdB8c3ABF30EeD1621eA9'; | |
// 投げ銭または寄付で受け入れたい Ether のデフォルト値です(ETH 送信時に変更可能です) | |
var ETH_VALUE = '0.01'; | |
// ETH 送信の手数料(Gas)の初期値です(ETH 送信時に変更可能, 単位 wei) | |
var ETH_DEFAULT_GAS_PRICE = 21000000000; | |
// ETH のトランザクションが実行されたときに表示されるメッセージです。 | |
var ETH_SUCCESS_MESSAGE = '応援ありがとうございます!'; | |
// なんらかの理由でトランザクション実行に失敗したときに表示されるメッセージです。 | |
var ETH_ERROR_MESSAGE = 'エラーが発生しました'; | |
// MetaMask が使用できないときに表示されます。 | |
var ETH_WEB3_UNAVAILABLE_MESSAGE = ETH_ADDRESS | |
+ ' に Ether を送付して応援いただけると励みになります!よろしくお願いします。'; | |
</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; | |
} | |
#eth_tip_value { | |
width: 55px; | |
font-size: 18px; | |
} | |
</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 ethValue = document.getElementById('eth_tip_value').value; | |
var weiValue = web3.toWei(ethValue, 'ether'); | |
console.log('weiValue: ' + weiValue); | |
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