Esempio n. 1
0
    def render(self):
        self.logger.info('开始补充手续费进程')
        for pid, p in self.project_addresses.items():
            project_address = p['address']
            for ck, coin in runtime.coins.items():
                if coin['coin_name'] == self.COIN_NAME:
                    self.logger.warning('币种名称为: {}, 不需要补充手续费!'.format(
                        coin['coin_name']))
                    continue
                contract = coin['contract']
                offset, count = 0, self.BALANCE_QUERY_NUMBER
                for s in range(0, len(project_address), count):
                    addresses = project_address[offset:count]
                    balances = self.rpc.get_balance(addresses, contract)
                    balances_sum = sum([
                        digit.hex_to_int(balance) for balance in balances
                        if balance
                    ])
                    if not balances_sum:
                        self.logger.info("本 {} 个地址无额外, 不需要补充手续费".format(
                            len(addresses)))
                        continue
                    for idx, balance in enumerate(balances):
                        balance_int = hex_to_int(balance)
                        if not balance_int:
                            continue
                        balance_eth_int = hex_to_int(
                            self.rpc.get_balance(address=addresses[idx]))

                        if hasattr(config, 'GAS'):
                            gas = config.GAS
                        else:
                            gas = self.rpc.get_smart_fee(contract=contract)
                        if hasattr(config, 'GAS_PRICE'):
                            gas_price = config.GAS_PRICE
                        else:
                            gas_price = self.rpc.gas_price()
                        if gas is None:
                            self.logger.info("未找到合适 gas . {}".format(gas))
                            continue
                        if gas_price is None:
                            self.logger.info(
                                "未找到合适 gas_price . {}".format(gas_price))
                            continue
                        gas, gas_price = hex_to_int(gas), hex_to_int(gas_price)
                        fee = gas * gas_price

                        if balance_eth_int > fee:
                            self.logger.info("地址: {} 手续费足够, 不需要补充手续费".format(
                                addresses[idx]))
                            continue

                        render_amount = int(config.COLLECTION_MIN_ETH * 1e18)

                        tx_hash = self.rpc.send_transaction(
                            sender=config.RENDER_ADDRESS,
                            receiver=addresses[idx],
                            value=render_amount,
                            passphrase=p['passphrase'],
                            gas=gas,
                            gas_price=gas_price,
                            contract=contract)
                        if not tx_hash:
                            self.logger.error("给地址: {} 补充手续费失败".format(
                                addresses[idx]))
                            continue
                        self.logger.info("给地址: {} 补充手续费成功".format(
                            addresses[idx]))
                        with runtime.app.app_context():
                            saved = Transaction.add_transaction(
                                coin_id=coin['coin_id'],
                                tx_hash=tx_hash,
                                block_time=datetime.now().timestamp(),
                                sender=config.RENDER_ADDRESS,
                                receiver=addresses[idx],
                                amount=render_amount,
                                status=TxStatusEnum.UNKNOWN.value,
                                type=TxTypeEnum.RENDER.value,
                                block_id=-1,
                                height=-1,
                                gas=gas,
                                gas_price=gas_price,
                                contract=contract)
        self.logger.info('结束补充手续费进程')
Esempio n. 2
0
    def collection(self):
        self.logger.info('开始归集进程')
        for pid, p in self.project_addresses.items():
            project_address = p['address']
            for ck, coin in runtime.coins.items():
                contract = coin['contract']
                offset, count = 0, self.BALANCE_QUERY_NUMBER
                for s in range(0, len(project_address), count):
                    addresses = project_address[offset:count]
                    balances = self.rpc.get_balance(addresses, contract)
                    balances_sum = sum([
                        digit.hex_to_int(balance) for balance in balances
                        if balance
                    ])
                    if not balances_sum:
                        self.logger.info("本 {} 个地址不需要归集".format(
                            len(addresses)))
                        continue
                    for idx, balance in enumerate(balances):
                        balance_int = hex_to_int(balance)
                        if not balance_int:
                            continue
                        if hasattr(config, 'GAS'):
                            gas = config.GAS
                        else:
                            gas = self.rpc.get_smart_fee(contract=contract)

                        if hasattr(config, 'GAS_PRICE'):
                            gas_price = config.GAS_PRICE
                        else:
                            gas_price = self.rpc.gas_price()

                        if gas is None:
                            self.logger.info("未找到合适 gas . {}".format(gas))
                            continue
                        if gas_price is None:
                            self.logger.info(
                                "未找到合适 gas_price . {}".format(gas_price))
                            continue

                        gas, gas_price = hex_to_int(gas), hex_to_int(gas_price)
                        fee = gas * gas_price
                        if coin['symbol'] == 'ETH':
                            send_value = max(
                                balance_int -
                                max(int(config.COLLECTION_MIN_ETH * 1e18),
                                    fee), 0)
                        else:
                            send_value = balance_int

                        if send_value <= 0:
                            self.logger.info("地址 {} 需要归集金额低于 0".format(
                                addresses[idx]))
                            continue

                        tx_hash = self.rpc.send_transaction(
                            sender=addresses[idx],
                            receiver=config.COLLECTION_ADDRESS,
                            value=send_value,
                            passphrase=p['passphrase'],
                            gas=gas,
                            gas_price=gas_price,
                            contract=contract)
                        with runtime.app.app_context():
                            saved = Transaction.add_transaction(
                                coin_id=coin['coin_id'],
                                tx_hash=tx_hash,
                                block_time=datetime.now().timestamp(),
                                sender=addresses[idx],
                                receiver=config.COLLECTION_ADDRESS,
                                amount=send_value,
                                status=TxStatusEnum.UNKNOWN.value,
                                type=TxTypeEnum.COLLECTION.value,
                                block_id=-1,
                                height=-1,
                                gas=gas,
                                gas_price=gas_price,
                                contract=contract)

                    offset += count
        self.logger.info("结束归集进程")