Skip to content

Commit

Permalink
some more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
popenta committed Jul 1, 2024
1 parent 42a8656 commit b0ad9d6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
8 changes: 7 additions & 1 deletion multiversx_sdk/core/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
10 changes: 9 additions & 1 deletion multiversx_sdk/core/tokens_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from multiversx_sdk.core.errors import BadUsageError
from multiversx_sdk.core.tokens import (Token, TokenComputer,
TokenIdentifierParts)
TokenIdentifierParts, TokenTransfer)


class TestTokenComputer:
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -101,19 +99,11 @@ 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(
sender=sender,
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)

0 comments on commit b0ad9d6

Please sign in to comment.