Ejemplo n.º 1
0
 public static void updateLastParsedBlock(Integer block_index) {
   Database db = Database.getInstance();
   db.executeUpdate(
       "REPLACE INTO sys_parameters (para_name,para_value) values ('last_parsed_block','"
           + block_index.toString()
           + "');");
 }
Ejemplo n.º 2
0
 public static void credit(
     String address,
     String asset,
     BigInteger amount,
     String callingFunction,
     String event,
     Integer blockIndex) {
   Database db = Database.getInstance();
   if (hasBalance(address, asset)) {
     BigInteger existingAmount = getBalance(address, asset);
     BigInteger newAmount = existingAmount.add(amount);
     db.executeUpdate(
         "update balances set amount='"
             + newAmount.toString()
             + "' where address='"
             + address
             + "' and asset='"
             + asset
             + "';");
   } else {
     db.executeUpdate(
         "insert into balances(address, asset, amount) values('"
             + address
             + "','"
             + asset
             + "','"
             + amount.toString()
             + "');");
   }
   db.executeUpdate(
       "insert into credits(address, asset, amount, calling_function, event, block_index) values('"
           + address
           + "','"
           + asset
           + "','"
           + amount.toString()
           + "', '"
           + callingFunction
           + "', '"
           + event
           + "', '"
           + blockIndex.toString()
           + "');");
 }
Ejemplo n.º 3
0
  public void add(String f1, String f2, String f3, String f4, String f5, String f6, String f7) {
    Database DB = new Database();

    this.field1 = f1;
    this.field2 = f2;
    this.field3 = f3;
    this.field4 = f4;
    this.field5 = f5;
    this.field6 = f6;
    this.field7 = f7;

    sql =
        "insert into AssetsTrjn(JourNo,FromAcc,AssetsID,RegDate,PersonID,Usefor,Other) values ('"
            + field1
            + "','"
            + field2
            + "','"
            + field3
            + "','"
            + field4
            + "','"
            + field5
            + "','"
            + field6
            + "','"
            + field7
            + "')";
    try {
      DB.OpenConn();
      DB.executeUpdate(sql);
    } catch (Exception e) {
      //			System.out.println(e);
      JOptionPane.showMessageDialog(null, "啊哦,保存失败了~", "错误", JOptionPane.ERROR_MESSAGE);
    } finally {
      DB.closeStmt();
      DB.closeConn();
    }
  }
Ejemplo n.º 4
0
  public static void parse(Integer txIndex, List<Byte> message) {
    Database db = Database.getInstance();
    ResultSet rs =
        db.executeQuery("select * from transactions where tx_index=" + txIndex.toString());
    try {
      if (rs.next()) {
        String source = rs.getString("source");
        String destination = rs.getString("destination");
        BigInteger btcAmount = BigInteger.valueOf(rs.getLong("btc_amount"));
        BigInteger fee = BigInteger.valueOf(rs.getLong("fee"));
        Integer blockIndex = rs.getInt("block_index");
        String txHash = rs.getString("tx_hash");

        ResultSet rsCheck =
            db.executeQuery("select * from bets where tx_index='" + txIndex.toString() + "'");
        if (rsCheck.next()) return;

        if (message.size() == length) {
          ByteBuffer byteBuffer = ByteBuffer.allocate(length);
          for (byte b : message) {
            byteBuffer.put(b);
          }
          BigInteger bet = BigInteger.valueOf(byteBuffer.getLong(0));
          Double chance = byteBuffer.getDouble(8);
          Double payout = byteBuffer.getDouble(16);
          Double houseEdge = Config.houseEdge;
          // PROTOCOL CHANGE
          Double oldHouseEdge = 0.02;
          Boolean payoutChanceCongruent =
              Util.roundOff(chance, 6) == Util.roundOff(100.0 / (payout / (1.0 - houseEdge)), 6)
                  || Util.roundOff(chance, 6)
                      == Util.roundOff(100.0 / (payout / (1.0 - oldHouseEdge)), 6);
          BigInteger chaSupply = Util.chaSupplyForBetting();
          String validity = "invalid";
          if (!source.equals("")
              && bet.compareTo(BigInteger.ZERO) > 0
              && chance > 0.0
              && chance < 100.0
              && payout > 1.0
              && payoutChanceCongruent) {
            if (bet.compareTo(Util.getBalance(source, "CHA")) <= 0) {
              if ((payout - 1.0) * bet.doubleValue() < chaSupply.doubleValue() * Config.maxProfit) {
                validity = "valid";
                Util.debit(source, "CHA", bet, "Debit bet amount", txHash, blockIndex);
              }
            }
          }
          db.executeUpdate(
              "insert into bets(tx_index, tx_hash, block_index, source, bet, chance, payout, profit, cha_supply, validity) values('"
                  + txIndex.toString()
                  + "','"
                  + txHash
                  + "','"
                  + blockIndex.toString()
                  + "','"
                  + source
                  + "','"
                  + bet.toString()
                  + "','"
                  + chance.toString()
                  + "','"
                  + payout.toString()
                  + "','0','"
                  + chaSupply.toString()
                  + "','"
                  + validity
                  + "')");
        }
      }
    } catch (SQLException e) {
    }
  }
Ejemplo n.º 5
0
  public static void resolve() {
    // resolve bets

    logger.info("Resolving bets");

    Database db = Database.getInstance();
    ResultSet rs =
        db.executeQuery(
            "select block_time,blocks.block_index as block_index,tx_index,tx_hash,source,bet,payout,chance,cha_supply from bets,blocks where bets.block_index=blocks.block_index and bets.validity='valid' and bets.resolved IS NOT 'true';");
    // if (true) return;

    SimpleDateFormat dateFormatStandard = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
    SimpleDateFormat dateFormatDateTimeLotto = new SimpleDateFormat("MM/dd/yyyy HH:mm");
    SimpleDateFormat dateFormatDateLotto = new SimpleDateFormat("dd/MM/yyyy");
    SimpleDateFormat dateFormatHour = new SimpleDateFormat("HH");
    SimpleDateFormat dateFormatMinute = new SimpleDateFormat("mm");
    TimeZone newYork = TimeZone.getTimeZone("America/New_York");
    TimeZone UTC = TimeZone.getTimeZone("UTC");
    dateFormatStandard.setTimeZone(UTC);
    dateFormatDateLotto.setTimeZone(newYork);
    dateFormatDateTimeLotto.setTimeZone(newYork);
    dateFormatHour.setTimeZone(newYork);
    dateFormatMinute.setTimeZone(newYork);

    try {
      while (rs.next()) {
        String source = rs.getString("source");
        String txHash = rs.getString("tx_hash");
        Integer txIndex = rs.getInt("tx_index");
        Integer blockIndex = rs.getInt("block_index");
        BigInteger bet = BigInteger.valueOf(rs.getLong("bet"));
        Double payout = rs.getDouble("payout");
        Double chance = rs.getDouble("chance");
        BigInteger chaSupply = BigInteger.valueOf(rs.getLong("cha_supply"));
        Date blockTime = new Date((long) rs.getLong("block_time") * 1000);

        logger.info("Attempting to resolve bet " + txHash);

        String lottoDate = dateFormatDateLotto.format(blockTime);
        Integer hour = Integer.parseInt(dateFormatHour.format(blockTime));
        Integer minute = Integer.parseInt(dateFormatMinute.format(blockTime));
        if (hour >= 23 && minute >= 56) {
          lottoDate = dateFormatDateLotto.format(addDays(blockTime, 1));
        }
        String lottoUrl =
            "http://nylottery.ny.gov/wps/PA_NYSLNumberCruncher/NumbersServlet?game=quick&action=winningnumbers&startSearchDate="
                + lottoDate
                + "&endSearchDate=&pageNo=&last=&perPage=999&sort=";
        String lottoPage = Util.getPage(lottoUrl);
        logger.info("Getting lottery numbers " + lottoUrl);
        try {
          ObjectMapper objectMapper = new ObjectMapper();
          objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
          LottoResult lottoMap = objectMapper.readValue(lottoPage, LottoResult.class);

          for (LottoDraw draw : lottoMap.draw) {
            Date time = dateFormatDateTimeLotto.parse(draw.date);
            if (time.after(blockTime)) {
              logger.info("Found lottery numbers we can use to resolve bet");
              BigInteger denominator =
                  combinations(BigInteger.valueOf(80), BigInteger.valueOf(20))
                      .subtract(BigInteger.ONE);
              BigInteger n = BigInteger.ZERO;
              BigInteger i = BigInteger.ONE;
              for (BigInteger number : draw.numbersDrawn) {
                n = n.add(combinations(number.subtract(BigInteger.ONE), i));
                i = i.add(BigInteger.ONE);
              }
              Double rollA = n.doubleValue() / (denominator.doubleValue());
              Double rollB =
                  (new BigInteger(txHash.substring(10, txHash.length()), 16))
                          .mod(BigInteger.valueOf(1000000000))
                          .doubleValue()
                      / 1000000000.0;
              Double roll = (rollA + rollB) % 1.0;
              roll = roll * 100.0;

              logger.info("Roll = " + roll.toString() + ", chance = " + chance.toString());

              BigInteger profit = BigInteger.ZERO;
              if (roll < chance) {
                logger.info("The bet is a winner");
                // win
                profit =
                    new BigDecimal(
                            bet.doubleValue()
                                * (payout.doubleValue() - 1.0)
                                * chaSupply.doubleValue()
                                / (chaSupply.doubleValue()
                                    - bet.doubleValue() * payout.doubleValue()))
                        .toBigInteger();
                BigInteger credit = profit.add(bet);
                Util.credit(source, "CHA", credit, "Bet won", txHash, blockIndex);
              } else {
                logger.info("The bet is a loser");
                // lose
                profit = bet.multiply(BigInteger.valueOf(-1));
              }
              db.executeUpdate(
                  "update bets set profit='"
                      + profit.toString()
                      + "', rolla='"
                      + (rollA * 100.0)
                      + "', rollb='"
                      + (rollB * 100.0)
                      + "', roll='"
                      + roll
                      + "', resolved='true' where tx_index='"
                      + txIndex
                      + "';");
              break;
            }
          }
        } catch (Exception e) {
          logger.error(e.toString());
        }
      }
    } catch (SQLException e) {
      logger.error(e.toString());
    }
  }
Ejemplo n.º 6
0
 public static void dropTable(String name) throws SQLException, ClassNotFoundException {
   Database.executeUpdate("DROP TABLE " + name + ";");
 }
Ejemplo n.º 7
0
 public static void createTable(String name, String fields)
     throws SQLException, ClassNotFoundException {
   Database.executeUpdate("CREATE TABLE " + name + " (" + fields + ");");
 }