def get_node_block_detail_from_block_num(self, block_num=None, target_parameter_list=None): """ 根据区块号,获取节点详情 :param block_num: :param target_parameter_list: :return: """ print("get_node_block_detail_from_block_num") print("server local time: " + str(time.time())) if self.ec is None: self.ec = TokenNodeConfModel.get_eos_node_script(script_unit=_ONE_S) if block_num: print("block_num: " + str(block_num)) block_num = int(block_num) block_detail = self.ec.http_get_block_detail(block_num) else: block_detail = self.ec.http_get_latest_block() ret_dict = {} if block_detail: print("timestamp: " + block_detail.get("timestamp", "")) if not target_parameter_list: return block_detail else: for i in target_parameter_list: ret_dict[i] = block_detail.get(i, "") return ret_dict return ret_dict
def __init__(self): self.ec = TokenNodeConfModel.get_eos_node_server() self.http_base_url = "https://api.eospark.com" self.rpc_base_wallet_url = "http://127.0.0.1:8899/v1/wallet" self.http_api_key = "779b305423258ed0fec69b29a06aeb4f" self.eos_pub_key = get_wallet_pub_key() self.wallet_key = get_wallet_wallet_key() self.expired_second = 120
def get_account_related_trx_info(self, page=None): if not self.ec: self.ec = TokenNodeConfModel.get_eos_node_script(script_unit=_TWO_S) account_related_trx_info = self.ec.http_get_account_related_trx_info(page=page, account=self.eos_gather_address) if not account_related_trx_info.get("trace_count", ""): raise Exception("EOSPark node get_account_related_trx_info has error!") return account_related_trx_info
def eth_send_tx(sign, sub_private_key): try: w3 = TokenNodeConfModel.get_eth_node_server() signed_txn = w3.eth.account.signTransaction(sign, sub_private_key) tx_hash_hex_bytes = w3.eth.sendRawTransaction(signed_txn.rawTransaction) tx_hash = hexbytes_to_str(tx_hash_hex_bytes) do_result = True except Exception as e: if isinstance(e, dict): tx_hash = json.dumps(e) else: tx_hash = str(e) do_result = False print("tx_hash=") print(tx_hash) return tx_hash, do_result
def btc_send_tx(sign, sub_private_key, calc): try: rpc = TokenNodeConfModel.get_btc_node_server() new_tx_encoded = rpc.signrawtransactionwithkey(sign, [sub_private_key]) do_result = True if calc: return new_tx_encoded.get("hex"), do_result tx_hash = rpc.sendrawtransaction(new_tx_encoded.get("hex"), False) except Exception as e: if isinstance(e, dict): tx_hash = json.dumps(e) else: tx_hash = str(e) do_result = False print("tx_hash=") print(tx_hash) return tx_hash, do_result
def lottery_instant_adduce(cls, base_time_stamp, user_lottery_num_list, imaginary_num, faker=True, times=6): """ 一元夺宝 即时版 :param base_time_stamp: 收到的请求时间 float类型 时间戳 :param user_lottery_num_list: 判断用户是否中奖的号码段 例: [12, 45] :param imaginary_num: hash16进制之后取摩用的总虚数,当前btc价格加上溢价 :param faker: 你猜这个是干嘛的:) 传True就特殊处理,反之不处理 :param times: 查询区块的次数,默认6次 :return: """ ec = TokenNodeConfModel.get_eos_node_script(script_unit=_ZERO_S) node_data = ec.http_get_latest_block() latest_block = node_data.get("latest_block", {}) block_num = None begin, end = user_lottery_num_list counter = _ONE for i in range(_HUNDRED): if latest_block: if counter > int(times): break counter += _ONE block_num = latest_block["block_num"] block_hash = latest_block["id"] timestamp = latest_block["timestamp"] block_timestamp = str_to_timestamp(timestamp) if block_timestamp >= base_time_stamp: lottery_num = int(block_hash, 16) % imaginary_num + _ONE if (end < lottery_num or lottery_num < begin) or not faker: return { "block_num": block_num, "block_hash": block_hash, "timestamp": timestamp, "lottery_num": lottery_num, } if block_num: block_num += _ONE latest_block = ec.http_get_block_detail(block_num) if not latest_block: block_num -= _ONE else: latest_block = ec.http_get_latest_block() time.sleep(0.5) return {}
def __init__(self): self.ec = TokenNodeConfModel.get_eos_node_script( script_unit=_ZERO_S) # 获取eos connect连接
info_dict[key] = value return info_dict # node_uri = "http://13.115.154.149:8545" # 测试 # node_uri = "http://3.16.26.56:8545" # 测试俄亥俄 # node_uri = "http://52.194.21.55:8545" # 正式 # w3 = Web3(Web3.HTTPProvider(node_uri)) from models.token_node_conf_model import TokenNodeConfModel coin_id = "60" mold = "server" w3 = TokenNodeConfModel.get_eth_node_server() w3.middleware_stack.inject(geth_poa_middleware, layer=0) block_num = w3.eth.blockNumber print(block_num) # block = convert_info_to_json(w3.eth.getBlock(block_num)) # # print(block) # node_status = w3.txpool.status # pending = int(node_status.pending, 16) # 16进制转10进制 # tx_count = w3.eth.getTransactionCount("0x630D7dC95C2BB0baFcd748B017741ec434Fd3d65") # tx_count_pending = w3.eth.getTransactionCount("0x630D7dC95C2BB0baFcd748B017741ec434Fd3d65", "pending")
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.block_flag = 0 # 定时器状态,默认0:未启动,1:运行中 # self.rpc = btc_rpc_client() self.rpc = TokenNodeConfModel.get_btc_node_script()
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.rpc = TokenNodeConfModel.get_btc_node_server() self.block_no = 0 self.confirm_num = 2 self.block_map = None
def dice_sold_out_new(self, dice_id): change_type_31 = '31' # 扣款 change_type_32 = '32' # 返还 change_type_33 = '33' # 中奖 reward_quantity = decimal.Decimal(0) with MysqlTools().session_scope() as session: account_service = AccountService() dice_info = session.query(DiceParticipateInModel). \ filter(DiceParticipateInModel._id == dice_id).first() if dice_info is None: self.return_error(40021) ec = TokenNodeConfModel.get_eos_node_script(script_unit=_THREE_S) latest_eos_block_info = ec.http_get_latest_block() # print('latest_eos_block_info=-=-=', latest_eos_block_info) dice_info.block_no = latest_eos_block_info.get('block_num', None) dice_info.block_timestamp = latest_eos_block_info.get( 'timestamp', None) dice_info.block_hash = latest_eos_block_info.get('id', None) if dice_info.block_no is None or \ dice_info.block_timestamp is None or \ dice_info.block_hash is None: self.return_error(40024) else: dice_result = -1 banker_dice = int(dice_info.block_hash, 16) % 3 # 0石头 1剪刀 2布 user_dice = dice_info.user_dice # 0石头 1剪刀 2布 if banker_dice == user_dice: dice_result = 1 # 0用户胜 1平局 2庄家胜 -1未知 else: if banker_dice == 0 and user_dice == 1: dice_result = 2 elif banker_dice == 0 and user_dice == 2: dice_result = 0 elif banker_dice == 1 and user_dice == 0: dice_result = 0 elif banker_dice == 1 and user_dice == 2: dice_result = 2 elif banker_dice == 2 and user_dice == 0: dice_result = 2 elif banker_dice == 2 and user_dice == 1: dice_result = 0 dice_info.banker_dice = banker_dice dice_info.dice_result = dice_result # session.flush() if dice_result in [0, 1]: change_type = change_type_32 dice_config = session.query(DiceConfigModel). \ filter(DiceConfigModel.support_token_id == dice_info.bet_token).first() if dice_config is None: return self.return_error(40022) if dice_result == 0: change_type = change_type_33 # 用户获胜 中奖金额为投注金额+奖励金额(投注金额=奖励金额) 扣除手续费 reward_quantity = ( dice_info.reward_quantity + dice_info.bet_number ) * (100 - dice_config.handling_fee) / 100 dice_info.reward_quantity = reward_quantity elif dice_result == 1: change_type = change_type_32 # 平局 中奖金额为投注金额 reward_quantity = dice_info.bet_number result = account_service.do_win( session, dice_info.user_id, dice_info.reward_token, reward_quantity, 'dice_win' + str(dice_info._id), dice_info.user_dice, dice_info.dice_serial, change_type) if isinstance(result, int) is False: raise_logger( "dice分钱失败" + "user:"******"game_publish_lottery", "info") session.rollback() return False if result != 0: raise_logger( "dice分钱失败" + "user:"******"game_publish_lottery", "info") session.rollback() return False # ret_info = dice_info # print(ret_info) session.commit() session.refresh(dice_info) # session.expunge(dice_info) response = { 'dice_result': dice_info.dice_result, 'banker_dice': dice_info.banker_dice, 'user_dice': dice_info.user_dice, # 'dice_timestamp': timestamp_to_str(dice_info.dice_timestamp, format_style="%Y-%m-%d %H:%M:%S"), 'dice_id': str(dice_info._id), 'dice_serial': dice_info.dice_serial, 'reward_token': self.get_coin_name(dice_info.reward_token), 'reward_quantity': decimal_to_str(reward_quantity, 6) # 'reward_quantity': decimal_to_str( # str((dice_info.bet_number + dice_info.bet_number) * decimal.Decimal(0.99)), 6) } if dice_info.reward_token == int(_COIN_ID_USDT): response['reward_quantity'] = decimal_to_str(reward_quantity, 4) return response
def dice_chip_in_fast(self, user_id, user_channel_id, coin_id, bet_amount, user_dice): change_type_31 = '31' # 扣款 change_type_32 = '32' # 返还 change_type_33 = '33' # 中奖 with MysqlTools().session_scope() as session: # 查询用户资产 account_service = AccountService() user_info = account_service.get_inner_user_account_by_token( session, user_id, coin_id) if isinstance(user_info, int): if user_info == 20001: return self.return_error(20001) balance = get_decimal(user_info.get("balance"), digits=8, decimal_type="down") # 账户余额 < 需要下注金额 bet_number = get_decimal(bet_amount, digits=4, decimal_type="down") if balance < bet_number: return self.return_error(60005) dice_serial = dice_generate_phase() # 获取最新eos区块,计算开奖区块高度 dice_time = get_timestamp() ec = TokenNodeConfModel.get_eos_node_script(script_unit=_THREE_S) latest_eos_block_info = ec.http_get_latest_block() # print('latest_eos_block_info=-=-=', latest_eos_block_info) block_no = latest_eos_block_info['block_num'] dice_part_in_model = DiceParticipateInModel( dice_serial=dice_serial, user_id=user_id, user_dice=user_dice, channel=user_channel_id, bet_number=bet_number, bet_token=coin_id, reward_token=coin_id, reward_quantity=bet_number, dice_timestamp=dice_time, block_no=block_no) session.add(dice_part_in_model) session.flush() # 提交扣款申请 dice_part_id = dice_part_in_model._id result = account_service.do_bet( session, user_id, coin_id, bet_number, 'dice_bet' + str(dice_part_in_model._id), 1, dice_serial, user_channel_id, change_type_31) if result != 0: session.rollback() raise_logger("do_bet result" + str(result), "game_bet_in", "info") # -------------------------------- 开奖 -------------------------------- # block_timestamp = latest_eos_block_info['timestamp'] block_hash = latest_eos_block_info['id'] dice_result = -1 banker_dice = int(block_hash, 16) % 3 # 0石头 1剪刀 2布 user_dice = int(user_dice) if banker_dice == user_dice: dice_result = 1 # 0用户胜 1平局 2庄家胜 -1未知 else: if banker_dice == 0 and user_dice == 1: dice_result = 2 elif banker_dice == 0 and user_dice == 2: dice_result = 0 elif banker_dice == 1 and user_dice == 0: dice_result = 0 elif banker_dice == 1 and user_dice == 2: dice_result = 2 elif banker_dice == 2 and user_dice == 0: dice_result = 2 elif banker_dice == 2 and user_dice == 1: dice_result = 0 dice_part_in_model.banker_dice = banker_dice dice_part_in_model.dice_result = dice_result dice_part_in_model.block_hash = block_hash dice_part_in_model.block_timestamp = block_timestamp # session.flush() change_type = change_type_32 # ------------ 中奖控制 ------------ # winning_control = 0 if dice_result in [0, 1]: win_dice_num = session.query(func.count(DiceParticipateInModel._id)). \ filter(DiceParticipateInModel.user_id == user_id, DiceParticipateInModel.dice_result == 0, func.date_format(DiceParticipateInModel.created_at, "%Y-%m-%d") == func.date_format(get_utc_now(), "%Y-%m-%d")).first() if win_dice_num[0] >= decimal.Decimal(500): raise_logger( "dice winning control userid = " + str(user_id), "game_bet_in", "info") dice_part_in_model.banker_dice = user_dice dice_part_in_model.dice_result = 1 dice_result = 1 winning_control = 1 # ------------ 中奖控制结束 ------------ # if dice_result in [0, 1]: reward_quantity = 0 dice_config = session.query(DiceConfigModel). \ filter(DiceConfigModel.support_token_id == dice_part_in_model.bet_token).first() if dice_config is None: return self.return_error(40022) if dice_result == 0: change_type = change_type_33 # 用户获胜 中奖金额为投注金额+奖励金额(投注金额=奖励金额) 扣除手续费 reward_quantity = (dice_part_in_model.reward_quantity + dice_part_in_model.bet_number) * ( 100 - dice_config.handling_fee) / 100 elif dice_result == 1: change_type = change_type_32 # 平局 中奖金额为投注金额 reward_quantity = dice_part_in_model.bet_number result = account_service.do_win( session, dice_part_in_model.user_id, dice_part_in_model.reward_token, reward_quantity, 'dice_win' + str(dice_part_in_model._id), dice_part_in_model.user_dice, dice_part_in_model.dice_serial, change_type) if isinstance(result, int) is False: raise_logger( "dice分钱失败" + "user:"******"game_publish_lottery", "info") session.rollback() return False if result != 0: raise_logger( "dice分钱失败" + "user:"******"game_publish_lottery", "info") session.rollback() return False # -------------------------------- 开奖结束 -------------------------------- # session.commit() if winning_control == 1: return self.return_error(40023) return { 'dice_result': dice_result, 'banker_dice': banker_dice, 'user_dice': user_dice, 'dice_timestamp': timestamp_to_str(dice_time, format_style="%Y-%m-%d %H:%M:%S"), 'dice_id': str(dice_part_id), 'dice_serial': dice_serial, 'reward_token': self.get_coin_name(coin_id), 'reward_quantity': decimal_to_str( str((bet_number + bet_number) * decimal.Decimal(0.99)), 6) }
def block_chain_connect(self): # w3 = Web3(Web3.HTTPProvider(self.node_url)) # w3.middleware_stack.inject(geth_poa_middleware, layer=0) w3 = TokenNodeConfModel.get_eth_node_script() return w3