From b0ad9d6d1ad6761684bc777d6e9a10972d542b45 Mon Sep 17 00:00:00 2001 From: Alexandru Popenta Date: Mon, 1 Jul 2024 11:40:59 +0300 Subject: [PATCH] some more refactoring --- multiversx_sdk/core/tokens.py | 8 +++++++- multiversx_sdk/core/tokens_test.py | 10 +++++++++- .../smart_contract_transactions_factory.py | 11 +++++------ .../transfer_transactions_factory.py | 16 +++------------- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/multiversx_sdk/core/tokens.py b/multiversx_sdk/core/tokens.py index 2b38001f..a33afb01 100644 --- a/multiversx_sdk/core/tokens.py +++ b/multiversx_sdk/core/tokens.py @@ -2,7 +2,8 @@ from multiversx_sdk.core.codec import (decode_unsigned_number, encode_unsigned_number) -from multiversx_sdk.core.constants import TOKEN_RANDOM_SEQUENCE_LENGTH +from multiversx_sdk.core.constants import ( + EGLD_IDENTIFIER_FOR_MULTI_ESDTNFT_TRANSFER, TOKEN_RANDOM_SEQUENCE_LENGTH) from multiversx_sdk.core.errors import (BadUsageError, InvalidTokenIdentifierError) from multiversx_sdk.core.interfaces import IToken, ITokenIdentifierParts @@ -20,6 +21,11 @@ def __init__(self, token: IToken, amount: int) -> None: self.token = token self.amount = amount + @staticmethod + def new_from_native_amount(amount: int) -> "TokenTransfer": + native_token = Token(EGLD_IDENTIFIER_FOR_MULTI_ESDTNFT_TRANSFER) + return TokenTransfer(native_token, amount) + class TokenIdentifierParts: def __init__(self, ticker: str, random_sequence: str, nonce: int) -> None: diff --git a/multiversx_sdk/core/tokens_test.py b/multiversx_sdk/core/tokens_test.py index 5ab6d4fa..ba118298 100644 --- a/multiversx_sdk/core/tokens_test.py +++ b/multiversx_sdk/core/tokens_test.py @@ -2,7 +2,7 @@ from multiversx_sdk.core.errors import BadUsageError from multiversx_sdk.core.tokens import (Token, TokenComputer, - TokenIdentifierParts) + TokenIdentifierParts, TokenTransfer) class TestTokenComputer: @@ -86,3 +86,11 @@ def test_compute_extended_identifier_from_parts(self): assert fungible_identifier == "FNG-123456" assert nft_identifier == "NFT-987654-0a" + + +def test_token_transfer_from_native_amount(): + transfer = TokenTransfer.new_from_native_amount(1000000000000000000) + + assert transfer.token.identifier == "EGLD-000000" + assert transfer.token.nonce == 0 + assert transfer.amount == 1000000000000000000 diff --git a/multiversx_sdk/core/transactions_factories/smart_contract_transactions_factory.py b/multiversx_sdk/core/transactions_factories/smart_contract_transactions_factory.py index f0c86258..81715a15 100644 --- a/multiversx_sdk/core/transactions_factories/smart_contract_transactions_factory.py +++ b/multiversx_sdk/core/transactions_factories/smart_contract_transactions_factory.py @@ -5,12 +5,12 @@ from multiversx_sdk.abi.typesystem import is_list_of_typed_values from multiversx_sdk.core.address import Address from multiversx_sdk.core.code_metadata import CodeMetadata -from multiversx_sdk.core.constants import ( - ARGS_SEPARATOR, CONTRACT_DEPLOY_ADDRESS, - EGLD_IDENTIFIER_FOR_MULTI_ESDTNFT_TRANSFER, VM_TYPE_WASM_VM) +from multiversx_sdk.core.constants import (ARGS_SEPARATOR, + CONTRACT_DEPLOY_ADDRESS, + VM_TYPE_WASM_VM) from multiversx_sdk.core.interfaces import IAddress, ITokenTransfer from multiversx_sdk.core.serializer import arg_to_string, args_to_buffers -from multiversx_sdk.core.tokens import Token, TokenComputer, TokenTransfer +from multiversx_sdk.core.tokens import TokenComputer, TokenTransfer from multiversx_sdk.core.transaction import Transaction from multiversx_sdk.core.transactions_factories.token_transfers_data_builder import \ TokenTransfersDataBuilder @@ -92,8 +92,7 @@ def create_transaction_for_execute(self, receiver = contract if native_transfer_amount and number_of_tokens: - native_token = Token(EGLD_IDENTIFIER_FOR_MULTI_ESDTNFT_TRANSFER) - native_tranfer = TokenTransfer(native_token, native_transfer_amount) + native_tranfer = TokenTransfer.new_from_native_amount(native_transfer_amount) transfers.append(native_tranfer) native_transfer_amount = 0 diff --git a/multiversx_sdk/core/transactions_factories/transfer_transactions_factory.py b/multiversx_sdk/core/transactions_factories/transfer_transactions_factory.py index 2b54ef49..717cfcb2 100644 --- a/multiversx_sdk/core/transactions_factories/transfer_transactions_factory.py +++ b/multiversx_sdk/core/transactions_factories/transfer_transactions_factory.py @@ -1,10 +1,8 @@ -from typing import List, Optional, Protocol, Sequence, Union +from typing import List, Optional, Protocol, Sequence -from multiversx_sdk.core.constants import \ - EGLD_IDENTIFIER_FOR_MULTI_ESDTNFT_TRANSFER from multiversx_sdk.core.errors import BadUsageError from multiversx_sdk.core.interfaces import IAddress, ITokenTransfer -from multiversx_sdk.core.tokens import Token, TokenComputer, TokenTransfer +from multiversx_sdk.core.tokens import TokenComputer, TokenTransfer from multiversx_sdk.core.transaction import Transaction from multiversx_sdk.core.transactions_factories.token_transfers_data_builder import \ TokenTransfersDataBuilder @@ -101,7 +99,7 @@ def create_transaction_for_transfer(self, token_transfers = list(token_transfers) if token_transfers else [] - native_transfer = self._create_native_transfer(native_amount) + native_transfer = TokenTransfer.new_from_native_amount(native_amount) if native_amount else None token_transfers.append(native_transfer) if native_transfer else None return self.create_transaction_for_esdt_token_transfer( @@ -109,11 +107,3 @@ def create_transaction_for_transfer(self, receiver=receiver, token_transfers=token_transfers ) - - def _create_native_transfer(self, - native_amount: Optional[int] = None) -> Union[TokenTransfer, None]: - if native_amount is None: - return None - - native_token = Token(EGLD_IDENTIFIER_FOR_MULTI_ESDTNFT_TRANSFER) - return TokenTransfer(native_token, native_amount)