Skip to content

Instantly share code, notes, and snippets.

@flockonus
Created December 22, 2017 19:39
Show Gist options
  • Save flockonus/b958adac0c11874d1d2f9eb23adfbf0c to your computer and use it in GitHub Desktop.
Save flockonus/b958adac0c11874d1d2f9eb23adfbf0c to your computer and use it in GitHub Desktop.
const Web3 = require('web3');
const fs = require('fs');
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8945'));
web3.BigNumber.config({ EXPONENTIAL_AT: 1000 });
LunarAddress = '0x43fb95c7afa1ac1e721f33c695b2a0a94c7ddab2';
LunarAbi = [{"constant":false,"inputs":[{"name":"enabled","type":"bool"}],"name":"setSubdivisionEnabled","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"id","type":"uint256"},{"name":"metadata","type":"string"},{"name":"forSale","type":"bool"},{"name":"newPrice","type":"uint256"}],"name":"purchase","outputs":[{"name":"","type":"bool"}],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[],"name":"numPlots","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalOwned","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"newBeneficiary","type":"address"}],"name":"setBeneficiary","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"initialPrice","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"withdraw","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"addr","type":"address"},{"name":"idx","type":"uint256"}],"name":"tokensOfOwnerByIndex","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"tradingEnabled","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"id","type":"uint256"},{"name":"newData","type":"string"}],"name":"setMetadata","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalPurchases","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"plots","outputs":[{"name":"owner","type":"address"},{"name":"price","type":"uint256"},{"name":"forSale","type":"bool"},{"name":"metadata","type":"string"},{"name":"disabled","type":"bool"},{"name":"subdivision","type":"uint8"},{"name":"parentID","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"id","type":"uint256"}],"name":"ownerOf","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"subdivisionEnabled","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"addr","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_priceInWei","type":"uint256"}],"name":"setInitialPrice","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"feePercentage","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"id","type":"uint256"},{"name":"forSale1","type":"bool"},{"name":"forSale2","type":"bool"},{"name":"price1","type":"uint256"},{"name":"price2","type":"uint256"},{"name":"metadata1","type":"string"},{"name":"metadata2","type":"string"}],"name":"subdivide","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_percentage","type":"uint8"}],"name":"setFeePercentage","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"id","type":"uint256"},{"name":"forSale","type":"bool"},{"name":"newPrice","type":"uint256"}],"name":"setPrice","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"enabled","type":"bool"}],"name":"setTradingEnabled","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"},{"name":"","type":"uint256"}],"name":"plotsOwned","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"maxSubdivisions","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"id","type":"uint256"},{"name":"newOwner","type":"address"},{"name":"newData","type":"string"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"id","type":"uint256"}],"name":"isUnowned","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_numPlots","type":"uint256"},{"name":"_initialPriceInWei","type":"uint256"},{"name":"_feePercentage","type":"uint8"},{"name":"_tradingEnabled","type":"bool"},{"name":"_subdivisionEnabled","type":"bool"},{"name":"_maxSubdivisions","type":"uint8"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":true,"name":"_to","type":"address"},{"indexed":false,"name":"id","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"_from","type":"address"},{"indexed":false,"name":"id","type":"uint256"},{"indexed":false,"name":"price","type":"uint256"}],"name":"Purchase","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"_from","type":"address"},{"indexed":false,"name":"id","type":"uint256"},{"indexed":false,"name":"newPrice","type":"uint256"}],"name":"PriceChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"_from","type":"address"},{"indexed":false,"name":"id","type":"uint256"},{"indexed":false,"name":"newData","type":"string"}],"name":"MetadataUpdated","type":"event"}]
const Contract = web3.eth.contract(LunarAbi);
const instance = Contract.at(LunarAddress);
const TOP = 399;
const callAsync = async (method, ...args) => {
return new Promise(function (resolve, reject) {
instance[method](...args, (err, res) => {
if (err) reject(err);
else resolve(res);
});
});
};
async function run() {
// [{"name":"owner","type":"address"},{"name":"price","type":"uint256"},{"name":"forSale","type":"bool"},{"name":"metadata","type":"string"},{"name":"disabled","type":"bool"},{"name":"subdivision","type":"uint8"},{"name":"parentID","type":"uint256"}]
const list = [];
for (var i = 0; i < TOP; i++) {
const plot = await callAsync('plots', i);
const [owner, price, forSale, disabled] = plot;
const data = {owner, price: price.div('1000000000000000000').toNumber(), forSale, disabled};
data.id = i;
console.log(i, data);
list.push(data);
}
fs.writeFileSync('moon.json', JSON.stringify(list,null,2));
}
function analise() {
let list = require('./moon.json');
console.log(list.sort((a,b) => a.price - b.price));
let sum = 0;
list.slice(0,100).forEach(x => sum += x.price);
console.log('(100 cheaper) Sum value:', sum);
console.log('(100 cheaper) Average:', sum / 100);
}
// run().then(console.log, console.log);
analise();
// allEvents();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment