def test_calculate_payment_fee(self): # GIVEN a new Payment transaction payment = Payment(account=WALLET.classic_address, amount="100", destination=DESTINATION) # AFTER autofilling the transaction fee payment_signed = safe_sign_and_autofill_transaction( payment, WALLET, JSON_RPC_CLIENT) # THEN We expect the fee to be the default network fee (usually 10 drops) expected_fee = get_fee(JSON_RPC_CLIENT) self.assertEqual(payment_signed.fee, expected_fee)
def _autofill_transaction(transaction: Transaction, client: Client) -> Transaction: transaction_json = transaction.to_dict() if "sequence" not in transaction_json: sequence = get_next_valid_seq_number(transaction_json["account"], client) transaction_json["sequence"] = sequence if "fee" not in transaction_json: transaction_json["fee"] = get_fee(client) if "last_ledger_sequence" not in transaction_json: ledger_sequence = get_latest_validated_ledger_sequence(client) transaction_json[ "last_ledger_sequence"] = ledger_sequence + _LEDGER_OFFSET return Transaction.from_dict(transaction_json)
def test_reliable_submission_simple(self): WALLET.sequence = get_next_valid_seq_number(ACCOUNT, JSON_RPC_CLIENT) account_set = AccountSet( account=ACCOUNT, sequence=WALLET.sequence, set_flag=SET_FLAG, ) signed_account_set = safe_sign_and_autofill_transaction( account_set, WALLET, JSON_RPC_CLIENT) response = send_reliable_submission(signed_account_set, JSON_RPC_CLIENT) self.assertTrue(response.result["validated"]) self.assertEqual(response.result["meta"]["TransactionResult"], "tesSUCCESS") self.assertTrue(response.is_successful()) self.assertEqual(response.result["Fee"], get_fee(JSON_RPC_CLIENT)) WALLET.sequence += 1
def _calculate_fee_per_transaction_type( transaction: Transaction, client: Optional[Client] = None ) -> str: """ Calculate the total fee in drops for a transaction based on: - the network fee - the transaction condition https://xrpl.org/transaction-cost.html#special-transaction-costs Args: transaction: the Transaction to be submitted. client: the network client with which to submit the transaction. Returns: The expected Transaction fee in drops """ # Reference Transaction (Most transactions) if client is None: net_fee = 10 # 10 drops else: net_fee = int(get_fee(client)) # Usually 0.00001 XRP (10 drops) base_fee = net_fee # EscrowFinish Transaction with Fulfillment # https://xrpl.org/escrowfinish.html#escrowfinish-fields if transaction.transaction_type == TransactionType.ESCROW_FINISH: escrow_finish = cast(EscrowFinish, transaction) if escrow_finish.fulfillment is not None: fulfillment_bytes = escrow_finish.fulfillment.encode("ascii") # 10 drops × (33 + (Fulfillment size in bytes / 16)) base_fee = math.ceil(net_fee * (33 + (len(fulfillment_bytes) / 16))) # AccountDelete Transaction if transaction.transaction_type == TransactionType.ACCOUNT_DELETE: base_fee = _ACCOUNT_DELETE_FEE # Multi-signed Transaction # 10 drops × (1 + Number of Signatures Provided) if transaction.signers and len(transaction.signers) > 0: base_fee = net_fee * (1 + len(transaction.signers)) + base_fee # Round Up base_fee and return it as a String return str(math.ceil(base_fee))
def test_reliable_submission_payment(self): WALLET.sequence = get_next_valid_seq_number(ACCOUNT, JSON_RPC_CLIENT) payment_dict = { "account": ACCOUNT, "sequence": WALLET.sequence, "amount": "10", "destination": DESTINATION, } payment_transaction = Payment.from_dict(payment_dict) signed_payment_transaction = safe_sign_and_autofill_transaction( payment_transaction, WALLET, JSON_RPC_CLIENT) response = send_reliable_submission(signed_payment_transaction, JSON_RPC_CLIENT) self.assertTrue(response.result["validated"]) self.assertEqual(response.result["meta"]["TransactionResult"], "tesSUCCESS") self.assertTrue(response.is_successful()) self.assertEqual(response.result["Fee"], get_fee(JSON_RPC_CLIENT)) WALLET.sequence += 1
def test_calculate_escrow_finish_fee(self): # GIVEN a new EscrowFinish transaction escrow_finish = EscrowFinish( account=ACCOUNT, sequence=WALLET.sequence, owner=OWNER, offer_sequence=OFFER_SEQUENCE, condition=CONDITION, fulfillment=FULFILLMENT, ) # AFTER autofilling the transaction fee escrow_finish_signed = safe_sign_and_autofill_transaction( escrow_finish, WALLET, JSON_RPC_CLIENT) # AND calculating the expected fee with the formula # 10 drops × (33 + (Fulfillment size in bytes ÷ 16)) net_fee = int(get_fee(JSON_RPC_CLIENT)) fulfillment_in_bytes = FULFILLMENT.encode("ascii") expected_fee = net_fee * (33 + len(fulfillment_in_bytes) / 16) # THEN we expect the fee to be the calculation result above self.assertEqual(float(escrow_finish_signed.fee), float(expected_fee))
from tests.integration.it_utils import JSON_RPC_CLIENT from xrpl.ledger import get_fee from xrpl.models.amounts import IssuedCurrencyAmount from xrpl.models.transactions import OfferCreate, PaymentChannelCreate from xrpl.transaction import send_reliable_submission from xrpl.wallet import generate_faucet_wallet WALLET = generate_faucet_wallet(JSON_RPC_CLIENT) DESTINATION = generate_faucet_wallet(JSON_RPC_CLIENT) FEE = get_fee(JSON_RPC_CLIENT) OFFER = send_reliable_submission( OfferCreate( account=WALLET.classic_address, fee=FEE, sequence=WALLET.next_sequence_num, last_ledger_sequence=WALLET.next_sequence_num + 10, taker_gets="13100000", taker_pays=IssuedCurrencyAmount( currency="USD", issuer=WALLET.classic_address, value="10", ), ), WALLET, JSON_RPC_CLIENT, ) PAYMENT_CHANNEL = send_reliable_submission( PaymentChannelCreate( account=WALLET.classic_address, fee=FEE, sequence=WALLET.next_sequence_num,