Last active
January 20, 2018 17:48
-
-
Save byronaltice/c4aa5bc59624a0dfefbea0e3e18eedaa to your computer and use it in GitHub Desktop.
Total Balance Owed on EtherCraft
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
//Written by Casey Scarborough | |
//@caseyscarborough | |
// Change the invested amount to your amount in ETH invested into the ethercraft website/game | |
// https://ethercraft.io/#/inventory/0x87bf5750f4ca59dac8455ec37286967bb8388d13 | |
// Paste below line of code into chrome developer to pull directly from gist | |
// function f() {var s = document.createElement('script');s.setAttribute('src', 'https://cdn.rawgit.com/byronaltice/c4aa5bc59624a0dfefbea0e3e18eedaa/raw/7f522ed173d7bc69db42b9d9e176f62d6a83e6bf/ethercraft.js');document.head.appendChild(s);return;}f(); | |
var showIndividual = true, | |
oneEth = 1000000000000000000, | |
invested = 1.1, | |
amountWithdrawn = .7, | |
executed = false, | |
nonZeroBalancesChecked = 0, | |
items = {}, | |
totals = { | |
"King's Robe": { | |
invested: 0.1, | |
withdrawn: 0.196 | |
}, | |
"King's Crown": { | |
invested: 0.1, | |
withdrawn: 0.2105 | |
}, | |
"Powerful Acidic Solution": { | |
invested: 0.01, | |
withdrawn: 0 | |
}, | |
"Powerful Basic Solution": { | |
invested: 0.01, | |
withdrawn: 0 | |
}, | |
"Mana Crystal": { | |
invested: 0.01, | |
withdrawn: 0 | |
}, | |
"Mana Crystal Powder": { | |
invested: 0.01, | |
withdrawn: 0 | |
}, | |
"Merchant's Enchanted Amulet": { | |
invested: 0.25, | |
withdrawn: 0.275 | |
}, | |
"Merchant's Enchanted Bag": { | |
invested: 0.1, | |
withdrawn: 0.093 | |
}, | |
"Merchant's Enchanted Doublet": { | |
invested: 0.1, | |
withdrawn: 0.122 | |
}, | |
"Merchant's Enchanted Knickers": { | |
invested: 0.1, | |
withdrawn: 0.122 | |
}, | |
"Merchant's Enchanted Potion": { | |
invested: 0.2, | |
withdrawn: 0.313 | |
}, | |
"Merchant's Enchanted Shield": { | |
invested: 0.1, | |
withdrawn: 0.121 | |
}, | |
"Jolly Boots": { | |
invested: 0.05, | |
withdrawn: 0.064 | |
}, | |
"Stone Skin Amulet": { | |
invested: 0.1, | |
withdrawn: 0.123 | |
}, | |
"Jolly Suit": { | |
invested: 0.05, | |
withdrawn: 0.069 | |
}, | |
"Jolly Hat": { | |
invested: 0.05, | |
withdrawn: 0.076 | |
} | |
}, | |
totalEnchantedItemsHeld = Object.keys(totals).length; | |
function roundToDecimalPlaces(num, decimals) { | |
var multiplier = 10 ^ decimals; | |
return parseFloat(num).toFixed(decimals); | |
} | |
function addColumn(value, length, addColumnBar, addSpaceIfNotNegative) { | |
var s = ""; | |
var spaceAdded = addSpaceIfNotNegative && value.length > 0 && value.indexOf("-") != 0; | |
if (spaceAdded) { | |
s += " "; | |
} | |
s += value; | |
var subtractLength = spaceAdded ? length - 1 : length; | |
for (var i = 0; i < subtractLength - String(value).length; i++) { | |
s += " "; | |
} | |
if (addColumnBar) { | |
s += "|| "; | |
} | |
return s; | |
} | |
function getTotalBalance() { | |
var totalBalance = 0; | |
console.log("Name || Invested || Withdrawn || Balance || Profits || Percent Gained"); | |
console.log("==============================||===========||=============||=============||==============||================"); | |
for (item in totals) { | |
var t = totals[item]; | |
totalBalance += t.balance; | |
t.profits = roundToDecimalPlaces(((t.balance + t.withdrawn) - t.invested), 4); | |
t.profitsPercent = roundToDecimalPlaces((((t.balance + t.withdrawn) / t.invested) * 100) - 100, 4); | |
var s = addColumn(item, 30, true); | |
s += addColumn(t.invested + " ETH", 10, true); | |
s += addColumn(t.withdrawn + " ETH", 12, true); | |
s += addColumn(t.balance + " ETH", 12, true); | |
s += addColumn(t.profits + " ETH", 13, true, true); | |
s += addColumn(t.profitsPercent + "%", 14, false, true); | |
console.log(s); | |
} | |
console.log("==============================||===========||=============||=============||==============||================"); | |
return totalBalance | |
} | |
function getBalanceOwed(address, name) { | |
var thisShop = web3.eth.contract(Enchanted).at(address); | |
var accountAddress = web3.eth.defaultAccount; | |
thisShop.object.call(function (err, tokenAddress) { | |
var token = web3.eth.contract(ERC20).at(tokenAddress); | |
thisShop.balanceOwed.call(accountAddress, function (err, balanceOwed) { | |
thisShop.excessEth.call(accountAddress, function (err, excessEth) { | |
thisShop.latestBalanceCheck.call(accountAddress, function (err, latestBalanceCheck) { | |
thisShop.itemReturn.call(function (err, itemReturn) { | |
token.totalSupply.call(function (err, totalSupply) { | |
thisShop.itemsOwned.call(accountAddress, function (err, itemsOwned) { | |
var returnsUnaccounted = totalSupply.minus(latestBalanceCheck).dividedBy(oneEth).times(itemReturn).times(itemsOwned.dividedBy(oneEth)); | |
var shopBalance = balanceOwed.plus(excessEth).plus(returnsUnaccounted); | |
var balance = parseFloat(shopBalance.dividedBy(oneEth).toFixed(5)); | |
if (balance != 0 || (totals[name] && totals[name].withdrawn > 0)) { | |
totals[name].balance = balance; | |
nonZeroBalancesChecked += 1; | |
console.debug("Non-zero balances: " + nonZeroBalancesChecked); | |
execute(); | |
} | |
}); | |
}); | |
}); | |
}); | |
}); | |
}); | |
}); | |
} | |
function execute() { | |
if (nonZeroBalancesChecked == totalEnchantedItemsHeld && !executed) { | |
executed = true; | |
var total = getTotalBalance(); | |
console.log("Total balance: " + roundToDecimalPlaces(total, 4) + " ETH"); | |
if (amountWithdrawn > 0) { | |
console.log("Total withdrawn: " + roundToDecimalPlaces(amountWithdrawn, 4) + " ETH"); | |
console.log("Total balance including withdrawn: " + roundToDecimalPlaces(total + amountWithdrawn, 4) + " ETH"); | |
} | |
if (invested != 0) { | |
console.log("Total investment: " + roundToDecimalPlaces(invested, 4) + " ETH"); | |
console.log("You're at " + roundToDecimalPlaces((((((total + amountWithdrawn) / invested)) * 100) - 100), 4) + '% of your initial investment.'); | |
console.log("You've made " + roundToDecimalPlaces(total + amountWithdrawn - invested, 4) + " ETH."); | |
} | |
} | |
} | |
for (var t in totals) { | |
if (totals.hasOwnProperty(t)) { | |
amountWithdrawn += totals[t].withdrawn; | |
invested += totals[t].invested; | |
} | |
} | |
for (var i in itemDB) { | |
var item = itemDB[i]; | |
if (item.enchanted) { | |
getBalanceOwed(item.address, item.strings[0].name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment