Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace transaction class with the one from sdk-core #278

Merged
merged 9 commits into from
Jul 13, 2023

Conversation

popenta
Copy link
Contributor

@popenta popenta commented Jun 23, 2023

In this PR the class BunchOfTransactions has also been removed, as it was not used within mxpy.
As some people were referencing mxpy as a package just to use this class, the following code is an alternative implementation to that class, that can be copied into your own project and used directly.

from typing import Any, Dict, List, Optional, Protocol, Tuple

from multiversx_sdk_core import Transaction, TransactionPayload
from multiversx_sdk_wallet import UserSigner


class IAddress(Protocol):
    def bech32(self) -> str:
        ...


class ITransaction(Protocol):
    def to_dictionary(self) -> Dict[str, Any]:
        ...


class INetworkProvider(Protocol):
    def send_transactions(self, transactions: List[ITransaction]) -> Tuple[int, str]:
        ...


class BunchOfTransactions:
    def __init__(self) -> None:
        self.transactions: List[Transaction] = []

    def add_prepared(self, transaction: Transaction) -> None:
        self.transactions.append(transaction)

    def add(self,
            sender: UserSigner,
            receiver_address: IAddress,
            chain: str,
            gas_limit: int,
            nonce: int,
            value: int,
            data: str,
            gas_price: Optional[int] = None,
            version: Optional[int] = None,
            options: Optional[int] = None) -> None:
        tx = Transaction(
            chain_id=chain,
            sender=sender.get_pubkey().to_address("erd"),
            receiver=receiver_address,
            gas_limit=gas_limit,
            gas_price=gas_price,
            nonce=nonce,
            value=value,
            data=TransactionPayload.from_str(data),
            version=version,
            options=options
        )

        tx.signature = sender.sign(tx)
        self.transactions.append(tx)

    def send(self, proxy: INetworkProvider) -> Tuple[int, str]:
        logger.info(f"BunchOfTransactions.send: {len(self.transactions)} transactions")
        num_sent, hashes = proxy.send_transactions(self.transactions)
        return num_sent, hashes

@popenta popenta self-assigned this Jun 23, 2023
@popenta popenta marked this pull request as draft July 7, 2023 10:59
Base automatically changed from use-address-from-core to feat/next July 10, 2023 09:56
@popenta popenta marked this pull request as ready for review July 11, 2023 08:03
@bogdan-rosianu bogdan-rosianu self-requested a review July 12, 2023 10:27
@@ -9,6 +9,6 @@ semver
requests-cache
rich==13.3.4

multiversx-sdk-core>=0.5.0
multiversx-sdk-core==0.6.0b1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good, we reference the release from feat/*, for now.

@@ -62,7 +62,7 @@ class IContractQueryResponse(Protocol):


class SmartContract:
def __init__(self, address: Optional[Address] = None, bytecode=None, metadata=None):
def __init__(self, address: Optional[IAddress] = EmptyAddress(), bytecode=None, metadata=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think SmartContract is the next in line, to be removed (replaced).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I don't know if we should do it now or wait for the sdk to be compliant with the specs.

transaction.signature = alice.sign_transaction(transaction)

assert "0e69f27e24aba2f3b7a8842dc7e7c085a0bfb5b29112b258318eed73de9c8809889756f8afaa74c7b3c7ce20a028b68ba90466a249aaf999a1a78dcf7f4eb40c" == transaction.signature
transaction = Transaction(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these tests can be removed (the one with signing a transaction), since we have them (most of the flow, at least) in sdk-core, as well. Can stay for now, but we can remove them in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, indeed. Will be removed in the future.

fields = json.loads(data_json).get("tx") or json.loads(data_json).get("emittedTransaction")

instance = JSONTransaction()
instance.__dict__.update(fields)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we could have used Transaction.from_dictionary?

https://github.com/multiversx/mx-sdk-py-core/blob/main/multiversx_sdk_core/transaction.py#L105

Then, we can remove JSONTransaction as well, I think.

(for a future PR).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. I forgot we have that in sdk-core. Will be replaced in a future PR.

@popenta popenta merged commit 514022f into feat/next Jul 13, 2023
9 checks passed
@popenta popenta deleted the replace-transaction-class branch July 13, 2023 09:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants