Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Web3 = require('web3');
- var web3 = new Web3();
- var request = require('request-promise-native');
- var config = {
- user: 'vdice',
- password: 'xxxx',
- server: '192.168.15.1',
- port: 31337,
- database: 'vdice'
- };
- var mssql = require("mssql");
- testDb();
- web3.setProvider(new web3.providers.HttpProvider('http://192.168.15.1:8998'));
- console.log("Node block: " + web3.eth.blockNumber);
- var abi = [{"constant":false,"inputs":[],"name":"divest","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalAmountWagered","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"getBankroll","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_nSpins","type":"uint256"}],"name":"buySpins","outputs":[],"payable":true,"type":"function"},{"constant":true,"inputs":[{"name":"myid","type":"bytes32"}],"name":"getSpinsContainer","outputs":[{"name":"","type":"address"},{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_nSpins","type":"uint256"}],"name":"getMinAmountToWager","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"currentInvestor","type":"address"}],"name":"getProfitShare","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"numInvestors","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"currentInvestor","type":"address"}],"name":"getLossesShare","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"vote","type":"bool"}],"name":"voteEmergencyWithdrawal","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_isStopped","type":"bool"}],"name":"stop_or_resume_Contract","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"forceDivestOfAllInvestors","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"myid","type":"bytes32"},{"name":"result","type":"string"}],"name":"__callback","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_nSpins","type":"uint256"}],"name":"getMaxAmountToWager","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"myid","type":"bytes32"},{"name":"result","type":"string"},{"name":"_proof","type":"bytes"}],"name":"__callback","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"isStopped","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"investors","outputs":[{"name":"investorAddress","type":"address"},{"name":"amountInvested","type":"uint256"},{"name":"votedForEmergencyWithdrawal","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"searchSmallestInvestor","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"withdrawalAddress","type":"address"}],"name":"proposeEmergencyWithdrawal","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"currentInvestor","type":"address"}],"name":"forceDivestOfOneInvestor","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"proposedWithdrawal","outputs":[{"name":"toAddress","type":"address"},{"name":"atTime","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"investorsLosses","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"investorsProfit","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"investorIDs","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"getMinInvestment","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"acceptOwnership","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_minBet","type":"uint256"}],"name":"setMinBet","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"increaseInvestment","outputs":[],"payable":true,"type":"function"},{"constant":false,"inputs":[{"name":"_newOwner","type":"address"}],"name":"changeOwner","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"multipliers","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_probabilities","type":"uint256[]"},{"name":"_multipliers","type":"uint256[]"}],"name":"setConfiguration","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"invested","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"houseAddress","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_gas","type":"uint256"}],"name":"setSafeGas","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"executeEmergencyWithdrawal","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"probabilities","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"currentInvestor","type":"address"}],"name":"getBalance","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_newHouse","type":"address"}],"name":"changeHouse","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"newInvestor","outputs":[],"payable":true,"type":"function"},{"inputs":[{"name":"_verifierAddr","type":"address"}],"payable":false,"type":"constructor"},{"payable":true,"type":"fallback"},{"anonymous":false,"inputs":[{"indexed":true,"name":"myid","type":"bytes32"},{"indexed":true,"name":"playerAddress","type":"address"},{"indexed":false,"name":"amountWagered","type":"uint256"},{"indexed":false,"name":"nSpins","type":"uint256"}],"name":"LOG_newSpinsContainer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"myid","type":"bytes32"},{"indexed":true,"name":"playerAddress","type":"address"},{"indexed":false,"name":"spinIndex","type":"uint256"},{"indexed":false,"name":"numberDrawn","type":"uint256"},{"indexed":false,"name":"grossPayoutForSpin","type":"uint256"}],"name":"LOG_SpinExecuted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"myid","type":"bytes32"},{"indexed":true,"name":"playerAddress","type":"address"},{"indexed":false,"name":"netPayout","type":"uint256"}],"name":"LOG_SpinsContainerInfo","type":"event"},{"anonymous":false,"inputs":[],"name":"LOG_EmergencyWithdrawalProposed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"withdrawalAddress","type":"address"}],"name":"LOG_EmergencyWithdrawalFailed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"withdrawalAddress","type":"address"},{"indexed":false,"name":"amountWithdrawn","type":"uint256"}],"name":"LOG_EmergencyWithdrawalSucceeded","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"investor","type":"address"},{"indexed":false,"name":"vote","type":"bool"}],"name":"LOG_EmergencyWithdrawalVote","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"investor","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LOG_InvestorEntrance","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"investor","type":"address"},{"indexed":false,"name":"amount","type":"int256"}],"name":"LOG_InvestorCapitalUpdate","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"investor","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LOG_InvestorExit","type":"event"},{"anonymous":false,"inputs":[],"name":"LOG_EmergencyAutoStop","type":"event"},{"anonymous":false,"inputs":[],"name":"LOG_ZeroSend","type":"event"},{"anonymous":false,"inputs":[],"name":"LOG_ValueIsTooBig","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"addr","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"LOG_FailedSend","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"addr","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"LOG_SuccessfulSend","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"_gasLimit","type":"uint256"}],"name":"LOG_newGasLimit","type":"event"},{"anonymous":false,"inputs":[],"name":"LOG_ContractStopped","type":"event"},{"anonymous":false,"inputs":[],"name":"LOG_ContractResumed","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"oldAddr","type":"address"},{"indexed":false,"name":"newOwnerAddress","type":"address"}],"name":"LOG_OwnerAddressChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"oldAddr","type":"address"},{"indexed":false,"name":"newHouseAddress","type":"address"}],"name":"LOG_HouseAddressChanged","type":"event"}]
- var address = "0xDb9822bb9885d844d1986a8949fCe1a4ceb5F8B6";
- var contractInstance = web3.eth.contract(abi).at(address);
- startEvents(); // starting all required events
- async function getBetJson(id) {
- let success = false;
- id = id.replace("0x", "");
- let url = "https://api.oraclize.it/v1/contract/eth_mainnet_" + id + "/status";
- while (!success) {
- try {
- let body = JSON.parse(await request(url));
- let precomputedResult = body.result.checks[0].results[0].value;
- console.log(precomputedResult);
- let numberDrawn = 0;
- randomBytes = Buffer.from(precomputedResult, 'hex');
- for (let i = 0; i < randomBytes.length; i += 2) {
- numberDrawn = Math.floor(((randomBytes.readUInt8(i) * 256 + randomBytes.readUInt8(i + 1)) * 10000) / 2 ** 16);
- let spinIndex = i / 2;
- let result = {
- myid: "0x" + id,
- playerAddress: "",
- grossPayoutForSpin: 0,
- spinIndex: spinIndex,
- numberDrawn: numberDrawn
- };
- insertSpinExecuted(result);
- }
- success = true;
- } catch (e) {
- }
- }
- console.log(precomputedResult);
- };
- function startEvents() {
- let connection = new mssql.Connection(config, (err) => {
- var request = new mssql.Request(connection);
- request.query("select max(block) as block from LOG_Blocks", function(err, recordset) {
- if (err) console.log(err)
- var block = parseInt(recordset[0].block) - 1000;
- console.log("DB block: " + block);
- var LOG_SpinsContainerInfo = contractInstance.LOG_SpinsContainerInfo({
- fromBlock: block
- });
- LOG_SpinsContainerInfo.watch(function(error, result) {
- if (!error)
- insertSpinsContainerInfo(result["args"]);
- insertBlock(result.blockNumber);
- });
- var LOG_newSpinsContainer = contractInstance.LOG_newSpinsContainer({
- fromBlock: block
- });
- LOG_newSpinsContainer.watch(function(error, result) {
- if (!error)
- insertNewSpinsContainer(result["args"]);
- insertBlock(result.blockNumber);
- });
- var LOG_SpinExecuted = contractInstance.LOG_SpinExecuted({
- fromBlock: block
- });
- LOG_SpinExecuted.watch(function(error, result) {
- if (!error)
- insertSpinExecuted(result["args"]);
- insertBlock(result.blockNumber);
- });
- // var filter = web3.eth.filter('latest');
- //
- // filter.watch(function(error, result){
- // var block = web3.eth.getBlock(result, true);
- // insertBlock(block.number);
- // });
- })
- });
- }
- function insertNewSpinsContainer(o) {
- let connection = new mssql.Connection(config, (err) => {
- var request = new mssql.Request(connection);
- // query to the database and get the records
- request.query("if not exists (select * from dbo.LOG_newSpinsContainer where myid='" + o.myid + "') insert into dbo.LOG_newSpinsContainer (myid, playerAddress, amountWagered, nSpins, [date]) values ('" + o.myid + "', '" + o.playerAddress + "', '" + o.amountWagered + "', '" + o.nSpins + "', getutcdate())", function(err, recordset) {
- if (err) console.log(err)
- else {
- console.log("NewSpinsContainer: " + o.myid);
- getBetJson(o.myid);
- }
- })
- });
- }
- function insertSpinExecuted(o) {
- let connection = new mssql.Connection(config, (err) => {
- var request = new mssql.Request(connection);
- request.query(" if not exists (select * from dbo.LOG_SpinExecuted where myid='" + o.myid + "' and spinIndex='" + o.spinIndex + "') " +
- " insert into dbo.LOG_SpinExecuted (myid, playerAddress, spinIndex, numberDrawn, [date], grossPayoutForSpin) values ('" + o.myid + "', '" + o.playerAddress + "', '" + o.spinIndex + "', '" + o.numberDrawn + "', getutcdate(), '" + o.grossPayoutForSpin + "')" +
- " else " +
- " update dbo.LOG_SpinExecuted set grossPayoutForSpin='" + o.grossPayoutForSpin + "',playerAddress='" + o.playerAddress + "' where myid='" + o.myid + "' and spinIndex='" + o.spinIndex + "'",
- function(err, recordset) {
- if (err) console.log(err)
- else console.log("SpinExecuted: " + o.myid + " " + o.spinIndex);
- })
- });
- }
- function insertBlock(block) {
- let connection = new mssql.Connection(config, (err) => {
- var request = new mssql.Request(connection);
- request.query("if not exists (select * from dbo.LOG_Blocks where block=" + block + ") insert into dbo.LOG_Blocks (block, date) values (" + block + ", getutcdate())", function(err, recordset) {
- if (err) console.log(err)
- console.log('Processing block #' + block);
- })
- });
- }
- function insertSpinsContainerInfo(o) {
- let connection = new mssql.Connection(config, (err) => {
- var request = new mssql.Request(connection);
- request.query("if not exists (select * from dbo.LOG_SpinsContainerInfo where myid='" + o.myid + "') insert into dbo.LOG_SpinsContainerInfo (myid, playerAddress, netPayout, [date]) values ('" + o.myid + "', '" + o.playerAddress + "', '" + o.netPayout + "', getutcdate())", function(err, recordset) {
- if (err) console.log(err)
- else console.log("SpinsContainerInfo: " + o.myid);
- })
- });
- }
- function testDb() {
- let connection = new mssql.Connection(config, (err) => {
- var request = new mssql.Request(connection);
- request.query("select 1 as result", function(err, recordset) {
- if (err) console.log(err)
- else console.log("Database status: ok");
- });
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement