Skip to content

Instantly share code, notes, and snippets.

@schancel
Created May 16, 2019 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schancel/b9f715f330e6821628c82256de325cd0 to your computer and use it in GitHub Desktop.
Save schancel/b9f715f330e6821628c82256de325cd0 to your computer and use it in GitHub Desktop.
// Tree fiddy
defaultDonation = 3.50;
function getBCHPrice () {
return new Promise((resolve, reject) => {
jQuery.getJSON('https://index-api.bitcoin.com/api/v0/cash/price/usd', function (result) {
if (result.price != '') {
var singleDollarValue = result.price / 100;
var singleDollarSatoshis = 100000000 / singleDollarValue * defaultDonation;
resolve(singleDollarSatoshis);
} else {
reject(new Error(result.error));
}
});
});
};
function addListener(badgerButton, price) {
badgerButton.addEventListener('click', function(event) {
if (typeof web4bch !== 'undefined') {
web4bch = new Web4Bch(web4bch.currentProvider)
var txParams = {
to: badgerButton.getAttribute("data-to"),
from: web4bch.bch.defaultAccount,
value: price
}
web4bch.bch.sendTransaction(txParams, (err, price) => {
if (err) return
var paywallId = badgerButton.getAttribute("data-paywall-id")
if (paywallId) {
var paywall = document.getElementById("paywall")
paywall.style.display = "block"
}
var successCallback = badgerButton.getAttribute("data-success-callback")
if (successCallback) {
window[successCallback](price)
}
})
} else {
window.open('https://badgerwallet.cash')
}
});
}
jQuery(window).on('load', function(){
getBCHPrice().then(function(res) {
var badgerButtons = document.body.getElementsByClassName("badger-button")
for (var i = 0; i < badgerButtons.length; i++) {
var badgerButton = badgerButtons[i]
addListener(badgerButtons[i], res)
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment