Advertisement
Guest User

slot

a guest
Jul 4th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Web3 = require('web3');
  2.  
  3. var web3 = new Web3();
  4.  
  5. var request = require('request-promise-native');
  6.  
  7. var config = {
  8.     user: 'vdice',
  9.     password: 'xxxx',
  10.     server: '192.168.15.1',
  11.     port: 31337,
  12.     database: 'vdice'
  13. };
  14.  
  15. var mssql = require("mssql");
  16.  
  17. testDb();
  18.  
  19. web3.setProvider(new web3.providers.HttpProvider('http://192.168.15.1:8998'));
  20.  
  21. console.log("Node block: " + web3.eth.blockNumber);
  22.  
  23. 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"}]
  24.  
  25. var address = "0xDb9822bb9885d844d1986a8949fCe1a4ceb5F8B6";
  26.  
  27. var contractInstance = web3.eth.contract(abi).at(address);
  28.  
  29. startEvents(); // starting all required events
  30.  
  31. async function getBetJson(id) {
  32.     let success = false;
  33.     id = id.replace("0x", "");
  34.     let url = "https://api.oraclize.it/v1/contract/eth_mainnet_" + id + "/status";
  35.  
  36.     while (!success) {
  37.         try {
  38.             let body = JSON.parse(await request(url));
  39.             let precomputedResult = body.result.checks[0].results[0].value;
  40.  
  41.             console.log(precomputedResult);
  42.  
  43.             let numberDrawn = 0;
  44.             randomBytes = Buffer.from(precomputedResult, 'hex');
  45.  
  46.             for (let i = 0; i < randomBytes.length; i += 2) {
  47.                 numberDrawn = Math.floor(((randomBytes.readUInt8(i) * 256 + randomBytes.readUInt8(i + 1)) * 10000) / 2 ** 16);
  48.                 let spinIndex = i / 2;
  49.  
  50.                 let result = {
  51.                     myid: "0x" + id,
  52.                     playerAddress: "",
  53.                     grossPayoutForSpin: 0,
  54.                     spinIndex: spinIndex,
  55.                     numberDrawn: numberDrawn
  56.                 };
  57.                 insertSpinExecuted(result);
  58.             }
  59.  
  60.  
  61.             success = true;
  62.         } catch (e) {
  63.  
  64.         }
  65.     }
  66.  
  67.     console.log(precomputedResult);
  68. };
  69.  
  70.  
  71. function startEvents() {
  72.     let connection = new mssql.Connection(config, (err) => {
  73.         var request = new mssql.Request(connection);
  74.  
  75.  
  76.         request.query("select max(block) as block from LOG_Blocks", function(err, recordset) {
  77.  
  78.             if (err) console.log(err)
  79.  
  80.             var block = parseInt(recordset[0].block) - 1000;
  81.  
  82.             console.log("DB block: " + block);
  83.  
  84.             var LOG_SpinsContainerInfo = contractInstance.LOG_SpinsContainerInfo({
  85.                 fromBlock: block
  86.             });
  87.  
  88.             LOG_SpinsContainerInfo.watch(function(error, result) {
  89.                 if (!error)
  90.                     insertSpinsContainerInfo(result["args"]);
  91.  
  92.                 insertBlock(result.blockNumber);
  93.             });
  94.  
  95.             var LOG_newSpinsContainer = contractInstance.LOG_newSpinsContainer({
  96.                 fromBlock: block
  97.             });
  98.  
  99.             LOG_newSpinsContainer.watch(function(error, result) {
  100.                 if (!error)
  101.                     insertNewSpinsContainer(result["args"]);
  102.  
  103.                 insertBlock(result.blockNumber);
  104.             });
  105.  
  106.             var LOG_SpinExecuted = contractInstance.LOG_SpinExecuted({
  107.                 fromBlock: block
  108.             });
  109.  
  110.             LOG_SpinExecuted.watch(function(error, result) {
  111.                 if (!error)
  112.                     insertSpinExecuted(result["args"]);
  113.  
  114.                 insertBlock(result.blockNumber);
  115.             });
  116.  
  117.             //          var filter = web3.eth.filter('latest');
  118.             //
  119.             //          filter.watch(function(error, result){
  120.             //            var block = web3.eth.getBlock(result, true);
  121.             //            insertBlock(block.number);
  122.             //          });
  123.         })
  124.     });
  125. }
  126.  
  127.  
  128. function insertNewSpinsContainer(o) {
  129.     let connection = new mssql.Connection(config, (err) => {
  130.         var request = new mssql.Request(connection);
  131.  
  132.         // query to the database and get the records
  133.         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) {
  134.             if (err) console.log(err)
  135.             else {
  136.                 console.log("NewSpinsContainer: " + o.myid);
  137.  
  138.                 getBetJson(o.myid);
  139.             }
  140.  
  141.  
  142.         })
  143.  
  144.     });
  145. }
  146.  
  147.  
  148. function insertSpinExecuted(o) {
  149.  
  150.     let connection = new mssql.Connection(config, (err) => {
  151.         var request = new mssql.Request(connection);
  152.  
  153.  
  154.         request.query(" if not exists (select * from dbo.LOG_SpinExecuted where myid='" + o.myid + "' and spinIndex='" + o.spinIndex + "') " +
  155.             "    insert into dbo.LOG_SpinExecuted (myid, playerAddress, spinIndex, numberDrawn, [date], grossPayoutForSpin) values ('" + o.myid + "', '" + o.playerAddress + "', '" + o.spinIndex + "', '" + o.numberDrawn + "', getutcdate(), '" + o.grossPayoutForSpin + "')" +
  156.             " else " +
  157.             "    update dbo.LOG_SpinExecuted set grossPayoutForSpin='" + o.grossPayoutForSpin + "',playerAddress='" + o.playerAddress + "' where myid='" + o.myid + "' and spinIndex='" + o.spinIndex + "'",
  158.             function(err, recordset) {
  159.  
  160.                 if (err) console.log(err)
  161.                 else console.log("SpinExecuted: " + o.myid + " " + o.spinIndex);
  162.             })
  163.     });
  164. }
  165.  
  166.  
  167. function insertBlock(block) {
  168.  
  169.     let connection = new mssql.Connection(config, (err) => {
  170.         var request = new mssql.Request(connection);
  171.  
  172.  
  173.         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) {
  174.  
  175.             if (err) console.log(err)
  176.             console.log('Processing block #' + block);
  177.         })
  178.     });
  179. }
  180.  
  181.  
  182.  
  183. function insertSpinsContainerInfo(o) {
  184.  
  185.     let connection = new mssql.Connection(config, (err) => {
  186.         var request = new mssql.Request(connection);
  187.  
  188.         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) {
  189.  
  190.             if (err) console.log(err)
  191.             else console.log("SpinsContainerInfo: " + o.myid);
  192.         })
  193.     });
  194. }
  195.  
  196. function testDb() {
  197.  
  198.     let connection = new mssql.Connection(config, (err) => {
  199.         var request = new mssql.Request(connection);
  200.  
  201.         request.query("select 1 as result", function(err, recordset) {
  202.  
  203.             if (err) console.log(err)
  204.             else console.log("Database status: ok");
  205.         });
  206.     });
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement