当前位置: 首页>>代码示例>>Python>>正文


Python Transaction.getRecommendedTransactionFee方法代码示例

本文整理汇总了Python中transaction.Transaction.getRecommendedTransactionFee方法的典型用法代码示例。如果您正苦于以下问题:Python Transaction.getRecommendedTransactionFee方法的具体用法?Python Transaction.getRecommendedTransactionFee怎么用?Python Transaction.getRecommendedTransactionFee使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在transaction.Transaction的用法示例。


在下文中一共展示了Transaction.getRecommendedTransactionFee方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: from transaction import Transaction [as 别名]
# 或者: from transaction.Transaction import getRecommendedTransactionFee [as 别名]

#.........这里部分代码省略.........
        print('...input {} is {} BTC from {}'.format(n, Bitcoin.format_money(unspent['value']), bitcoin_input_address))
        tx.addInput(tx_input)

    # setup the outputs. a trigger address isn't really needed, since the encryption
    # key can actually be used as the trigger (only those interested will be able to find
    # the message, anyway)

    # cost of the transaction is (targets + pieces/3 + sacrifice) * SPECIAL_SATOSHI
    # peices/3 because we include 3 pieces per output
    outputs_count = (len(bitcoin_delivery_addresses) + math.ceil(len(bitcoin_message_pieces) / 3))
    approx_tx_cost = MINIMUM_SACRIFICE + outputs_count * SPECIAL_SATOSHI
    if approx_tx_cost > total_input_amount:
        raise Exception("not enough inputs provided")

    tx_change_output = None
    tx_change_output_n = None
    if total_input_amount > approx_tx_cost:
        print('...output (change) to {}'.format(bitcoin_change_address))
        tx_change_output = TransactionOutput(bitcoin_change_address, amount=total_input_amount - approx_tx_cost)
        tx_change_output_n = tx.addOutput(tx_change_output)

    # The recipient will know how to handle this if they see their key...
    for i, bitcoin_delivery_address in enumerate(bitcoin_delivery_addresses):
        print('...output (target) to {}'.format(bitcoin_delivery_address))
        tx_output = TransactionOutput(bitcoin_delivery_address, amount=SPECIAL_SATOSHI)
        tx.addOutput(tx_output)

    for i in range(0, len(bitcoin_message_pieces), 3):
        pieces = bitcoin_message_pieces[i:i+3]

        d = b''.join([p[1:] for p in pieces])
        header = None
        if i == 0:
            header = d[:5]
            d = d[5:]
        if (i + 3) >= len(bitcoin_message_pieces):
            if padding > 0:
                d = d[:-padding]

        print('...output (message) to multisig 1-of-{}{}'.format(len(pieces), ' (header={})'.format(header) if header is not None else ''))
        tx_output = TransactionOutput(amount=SPECIAL_SATOSHI)
        tx_output.setMultisig(pieces, 1)
        tx.addOutput(tx_output)

    # we should now be able to figure out how much in fees is required now that the tx is built
    recommended_fee = max(MINIMUM_SACRIFICE * SPECIAL_SATOSHI, tx.getRecommendedTransactionFee(per_kb=SACRIFICE_PER_KB))
    recommended_tx_cost = recommended_fee + outputs_count * SPECIAL_SATOSHI
    if recommended_tx_cost > total_input_amount:
        raise Exception("not enough inputs provided ({} BTC required)".format(Bitcoin.format_money(recommended_tx_cost)))
    
    if tx_change_output is not None and recommended_tx_cost == total_input_amount:
        # We can remove the output
        tx.removeOutput(tx_change_output_n)
        tx_change_output = None
        
    if recommended_tx_cost < total_input_amount:
        if tx_change_output is None:
            print('...output (change) to {}'.format(bitcoin_change_address))
            tx_change_output = TransactionOutput(bitcoin_change_address)
            tx_change_output_n = tx.addOutput(tx_change_output)
        tx_change_output.amount = total_input_amount - recommended_tx_cost

    print('...the fee for this transaction is {} BTC'.format(Bitcoin.format_money(tx.totalInput() - tx.totalOutput())))
    print('...the total sent is {} BTC (change = {})'.format(Bitcoin.format_money(tx.totalInput()), Bitcoin.format_money(0 if tx_change_output is None else tx.outputs[tx_change_output_n].amount)))
    
    # sign all inputs
    tx.sign()

    print('...the transaction is {} bytes.'.format(tx.size()))

    # Finally, do something with the transaction
    print('\n*** Step 6. The transaction is built. What would you like to do with it?')
    while True:
        print('...1. Show JSON')
        print('...2. Show HEX')
        print('...3. Push (via blockchain.info/pushtx)')
        print('...4. Quit')
        try:
            command = int(input('? ')) - 1
            assert command >= 0 and command < 4

            if command == 0:
                print(json.dumps(tx.as_dict()))
            elif command == 1:
                print(Bitcoin.bytes_to_hexstring(tx.serialize(), reverse=False))
            elif command == 2:
                err = push_transaction(tx.serialize())
                if isinstance(err, bool):
                    print("...pushed {}".format(Bitcoin.bytes_to_hexstring(tx.hash())))
                else:
                    print("...error pushing:", err)
            elif command == 3:
                break
        except EOFError:
            break
        except:
            print('Try again.')
            pass

    print("...exiting")
开发者ID:Sphere2013,项目名称:Bitmsg,代码行数:104,代码来源:buildmessage.py


注:本文中的transaction.Transaction.getRecommendedTransactionFee方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。