def build_transaction(self, transaction_id, wallet, amount, asset=bytom["BTM_asset"]): """ Build Bytom refund transaction. :param transaction_id: Bytom fund transaction id to redeem. :type transaction_id: str :param wallet: Bytom sender wallet. :type wallet: bytom.wallet.Wallet :param amount: Bytom amount to withdraw. :type amount: int :param asset: Bytom asset id, defaults to BTM asset. :type asset: str :returns: RefundTransaction -- Bytom refund transaction instance. >>> from swap.providers.bytom.transaction import RefundTransaction >>> from swap.providers.bytom.wallet import Wallet >>> sender_wallet = Wallet(network="testnet").from_mnemonic("indicate warm sock mistake code spot acid ribbon sing over taxi toast") >>> refund_transaction = RefundTransaction(network="testnet") >>> refund_transaction.build_transaction(transaction_id="481c00212c552fbdf537fcc88c1006a69bdd3130f593965f6ff4f91818a1c6e1", wallet=sender_wallet, amount=10000, asset="ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") <swap.providers.bytom.transaction.RefundTransaction object at 0x0409DAF0> """ # Checking parameter instances if not isinstance(transaction_id, str): raise TypeError( "invalid transaction id instance, only takes Bytom string type" ) if not isinstance(wallet, Wallet): raise TypeError( "invalid wallet instance, only takes Bytom Wallet class") if not isinstance(amount, int): raise TypeError("invalid asset instance, only takes integer type") if not isinstance(asset, str): raise TypeError("invalid amount instance, only takes string type") # Finding htlc utxo id. utxo_id = find_p2wsh_utxo(transaction_id=transaction_id, network=self.network) if utxo_id is None: raise ValueError( "invalid transaction id, there is no smart contact") if not wallet.guid(): raise ValueError("can't find sender wallet guid from wallet") elif not isinstance(wallet.guid(), str): raise TypeError("invalid sender guid type, only takes string type") # Setting wallet GUID self.guid = wallet.guid() # Actions inputs, outputs = list(), list() # Input action inputs.append(spend_utxo_action(utxo=utxo_id)) # Output action outputs.append( control_address_action( asset=asset, amount=amount, address=wallet.address() if wallet.address() else list_address( guid=wallet.guid(), limit=1, network=self.network)["result"]["data"][0]["address"])) # Transaction tx = dict(guid=self.guid, inputs=inputs, outputs=outputs, fee=bytom["fee"], confirmations=bytom["confirmations"]) # Building transaction self.transaction = build_transaction(tx=tx, network=self.network) self._type = "bytom_refund_unsigned" return self
def build_transaction(self, transaction_id, wallet, amount, asset=bytom["BTM_asset"]): """ Build Bytom claim transaction. :param transaction_id: Bytom fund transaction id to redeem. :type transaction_id: str :param wallet: Bytom recipient wallet. :type wallet: bytom.wallet.Wallet :param amount: Bytom amount to withdraw. :type amount: int :param asset: Bytom asset id, defaults to BTM asset. :type asset: str :returns: ClaimTransaction -- Bytom claim transaction instance. >>> from swap.providers.bytom.transaction import ClaimTransaction >>> from swap.providers.bytom.wallet import Wallet >>> recipient_wallet = Wallet(network="testnet").from_mnemonic("hint excuse upgrade sleep easily deputy erase cluster section other ugly limit") >>> claim_transaction = ClaimTransaction(network="testnet") >>> claim_transaction.build_transaction(transaction_id="1006a6f537fcc4888c65f6ff4f91818a1c6e19bdd3130f59391c00212c552fbd", wallet=recipient_wallet, amount=10000, asset="ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") <swap.providers.bytom.transaction.ClaimTransaction object at 0x0409DAF0> """ # Checking parameter instances if not isinstance(transaction_id, str): raise TypeError( "invalid transaction id instance, only takes Bytom string type" ) if not isinstance(wallet, Wallet): raise TypeError( "invalid wallet instance, only takes Bytom Wallet class") if not isinstance(amount, int): raise TypeError("invalid asset instance, only takes integer type") if not isinstance(asset, str): raise TypeError("invalid amount instance, only takes string type") # Finding htlc utxo id. utxo_id = find_p2wsh_utxo(transaction_id=transaction_id, network=self.network) if utxo_id is None: raise ValueError( "invalid transaction id, there is no pay to witness script hash" ) if not wallet.guid(): raise ValueError("can't find recipient wallet guid from wallet") elif not isinstance(wallet.guid(), str): raise TypeError( "invalid recipient guid type, only takes string type") # Setting wallet GUID self.guid = wallet.guid() # Actions inputs, outputs = list(), list() # Input action inputs.append(spend_utxo_action(utxo=utxo_id)) # Output action outputs.append( control_address_action( asset=asset, amount=amount, address=wallet.address() if wallet.address() else list_address( guid=wallet.guid(), limit=1, network=self.network)["result"]["data"][0]["address"])) # Transaction tx = dict(guid=self.guid, inputs=inputs, outputs=outputs, fee=bytom["fee"], confirmations=bytom["confirmations"]) # Building transaction self.transaction = build_transaction(tx=tx, network=self.network) self._type = "bytom_claim_unsigned" return self
address=wallet.address(vapor=VAPOR), asset=ASSET, amount=amount_converter(0.0001, "BTM2NEU"), confirmations=1, network=NETWORK, vapor=VAPOR) print("Estimated Transaction Fee:", estimated_transaction_fee) # Build Bytom advanced transaction unsigned_advanced_transaction.build_transaction( wallet.address(vapor=VAPOR), # address [ spend_utxo(utxo=find_p2wsh_utxo( transaction_id= "049d4c26bb15885572c16e0eefac5b2f4d0fde50eaf90f002272d39507ff315b", network=NETWORK, vapor=VAPOR)) ], # inputs [ control_address(asset=ASSET, amount=0.0001, address=wallet.address(vapor=VAPOR), symbol="BTM") ], # outputs estimated_transaction_fee, # fee 1, # confirmations False, # forbid_chain_tx ) print("\nUnsigned Advanced Transaction Fee:",
def test_transaction(): estimated_transaction_fee: int = estimate_transaction_fee( address=_["wallet"]["vapor_address"]["mainnet"], asset=ASSET, amount=amount_converter(0.0001, "BTM2NEU"), confirmations=1, network=_["network"], vapor=True) assert isinstance(estimated_transaction_fee, int) assert estimated_transaction_fee == 449000 unsigned_transaction: Transaction = Transaction( network=_["network"], vapor=True ).build_transaction( address=_["wallet"]["vapor_address"]["mainnet"], inputs=[ spend_utxo(utxo=find_p2wsh_utxo( transaction_id= "675392fcbc1867e247add457597611717229e5d2c46a53c44e3e61d6ce351474", network=_["network"], vapor=True)) ], outputs=[ control_address(asset=ASSET, amount=amount_converter(0.0001, "BTM2NEU"), address=_["wallet"]["vapor_address"]["mainnet"], symbol="NEU", vapor=True) ], fee=estimated_transaction_fee, confirmations=1, forbid_chain_tx=False) assert unsigned_transaction.fee( ) == _["transaction"]["transaction"]["unsigned"]["fee"] assert unsigned_transaction.confirmations( ) == _["transaction"]["transaction"]["unsigned"]["confirmations"] assert unsigned_transaction.hash( ) == _["transaction"]["transaction"]["unsigned"]["hash"] assert unsigned_transaction.raw( ) == _["transaction"]["transaction"]["unsigned"]["raw"] assert unsigned_transaction.json( ) == _["transaction"]["transaction"]["unsigned"]["json"] assert unsigned_transaction.unsigned_datas( False) == _["transaction"]["transaction"]["unsigned"]["unsigned_datas"] assert unsigned_transaction.signatures( ) == _["transaction"]["transaction"]["unsigned"]["signatures"] signed_transaction = unsigned_transaction.sign( xprivate_key=_["wallet"]["xprivate_key"], indexes=_["wallet"]["indexes"]) assert signed_transaction.fee( ) == _["transaction"]["transaction"]["signed"]["fee"] assert signed_transaction.confirmations( ) == _["transaction"]["transaction"]["signed"]["confirmations"] assert signed_transaction.hash( ) == _["transaction"]["transaction"]["signed"]["hash"] assert signed_transaction.raw( ) == _["transaction"]["transaction"]["signed"]["raw"] assert signed_transaction.json( ) == _["transaction"]["transaction"]["signed"]["json"] assert signed_transaction.unsigned_datas( False) == _["transaction"]["transaction"]["signed"]["unsigned_datas"] assert signed_transaction.signatures( ) == _["transaction"]["transaction"]["signed"]["signatures"]
def test_advanced_transaction(): estimated_transaction_fee: int = estimate_transaction_fee( address=_["wallet"]["address"]["mainnet"], asset=ASSET, amount=amount_converter(0.0001, "BTM2NEU"), confirmations=1, network=_["network"], vapor=False ) assert isinstance(estimated_transaction_fee, int) assert estimated_transaction_fee == 449000 unsigned_advanced_transaction: AdvancedTransaction = AdvancedTransaction( network=_["network"], vapor=False ).build_transaction( address=_["wallet"]["address"]["mainnet"], inputs=[ spend_utxo( utxo=find_p2wsh_utxo( transaction_id="049d4c26bb15885572c16e0eefac5b2f4d0fde50eaf90f002272d39507ff315b", network=_["network"], vapor=False ) ) ], outputs=[ control_address( asset=ASSET, amount=10_000, address=_["wallet"]["address"]["mainnet"], symbol="NEU", vapor=False ) ], fee=estimated_transaction_fee, confirmations=1, forbid_chain_tx=False ) assert unsigned_advanced_transaction.fee() == _["transaction"]["advanced_transaction"]["unsigned"]["fee"] assert unsigned_advanced_transaction.confirmations() == _["transaction"]["advanced_transaction"]["unsigned"]["confirmations"] assert unsigned_advanced_transaction.hash() == _["transaction"]["advanced_transaction"]["unsigned"]["hash"] assert unsigned_advanced_transaction.raw() == _["transaction"]["advanced_transaction"]["unsigned"]["raw"] assert unsigned_advanced_transaction.json() == _["transaction"]["advanced_transaction"]["unsigned"]["json"] assert unsigned_advanced_transaction.unsigned_datas(False) == _["transaction"]["advanced_transaction"]["unsigned"]["unsigned_datas"] assert unsigned_advanced_transaction.signatures() == _["transaction"]["advanced_transaction"]["unsigned"]["signatures"] signed_advanced_transaction = unsigned_advanced_transaction.sign( xprivate_key=_["wallet"]["xprivate_key"], indexes=_["wallet"]["indexes"] ) assert signed_advanced_transaction.fee() == _["transaction"]["advanced_transaction"]["signed"]["fee"] assert signed_advanced_transaction.confirmations() == _["transaction"]["advanced_transaction"]["signed"]["confirmations"] assert signed_advanced_transaction.hash() == _["transaction"]["advanced_transaction"]["signed"]["hash"] assert signed_advanced_transaction.raw() == _["transaction"]["advanced_transaction"]["signed"]["raw"] assert signed_advanced_transaction.json() == _["transaction"]["advanced_transaction"]["signed"]["json"] assert signed_advanced_transaction.unsigned_datas(False) == _["transaction"]["advanced_transaction"]["signed"]["unsigned_datas"] assert signed_advanced_transaction.signatures() == _["transaction"]["advanced_transaction"]["signed"]["signatures"]
address=wallet.address(vapor=VAPOR), asset=ASSET, amount=amount_converter(0.0001, "BTM2NEU"), confirmations=1, network=NETWORK, vapor=VAPOR) print("Estimated Transaction Fee:", estimated_transaction_fee) # Build Bytom transaction unsigned_transaction.build_transaction( address=wallet.address(vapor=VAPOR), inputs=[ spend_utxo(utxo=find_p2wsh_utxo( transaction_id= "675392fcbc1867e247add457597611717229e5d2c46a53c44e3e61d6ce351474", network=NETWORK, vapor=VAPOR)) ], outputs=[ control_address(asset=ASSET, amount=amount_converter(0.0001, "BTM2NEU"), address=wallet.address(vapor=VAPOR), symbol="NEU", vapor=VAPOR) ], fee=estimated_transaction_fee, confirmations=1) print("\nUnsigned Transaction Fee:", unsigned_transaction.fee()) print("Unsigned Transaction Confirmations:",