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

Transaction Converter & refactoring and reorganizing #17

Merged
merged 3 commits into from
Apr 3, 2024

Conversation

popenta
Copy link
Collaborator

@popenta popenta commented Mar 15, 2024

Implemented the TransactionConverter class according to the sdk-specs. Also moved the adapters package outside of the core package and the TransactionComputer class out of the transaction module in the transaction_computer module.

@popenta popenta self-assigned this Mar 15, 2024
@@ -314,7 +314,7 @@
],
"source": [
"from multiversx_sdk.core import TokenComputer\n",
"from multiversx_sdk.core.transaction_factories import \\\n",
"from multiversx_sdk.core.transactions_factories import \\\n",
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor, easy to welcome breaking change.

"\n",
"provider = ProxyNetworkProvider(\"https://devnet-gateway.multiversx.com\")\n",
"provider = ProxyNetworkProvider(\"https://devnet-gateway.multiversx.com\", TransactionConverter())\n",
Copy link
Contributor

Choose a reason for hiding this comment

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

Additional breaking change. Very good from a design point of view. Perhaps less welcomed by some developers.

Let's brainstorm an eventual facade package - first, in the specs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Perhaps we can provide also the default value?

def __init__(
self,
url: str,
transaction_converter: ITransactionConverter,
Copy link
Contributor

Choose a reason for hiding this comment

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

As mentioned above, it's a breaking change.

In the specs, we use the plural variant: https://github.com/multiversx/mx-sdk-specs/blob/main/converters/transactionsConverter.md.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Renamed to ITransactionsConverter.

ITransaction, TransactionOnNetwork)


class ITransactionConverter(Protocol):
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be plural, I think.

transaction.options = transaction.options | TRANSACTION_OPTIONS_TX_GUARDED
transaction.guardian = guardian

def _to_dictionary(self, transaction: ITransaction) -> Dict[str, Any]:
Copy link
Contributor

Choose a reason for hiding this comment

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

Correct, this should not use the new converter 👍

(it has other reasons for change / wrt. SRP).

def __init__(
self,
url: str,
transaction_converter: ITransactionConverter,
Copy link
Contributor

Choose a reason for hiding this comment

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

It's a breaking change (we can postpone it via some workarounds, but perhaps we should not).

Also, can use plural variant.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Using the plural variant

def __init__(self, transaction: Transaction) -> None:
self.expected = transaction

def __eq__(self, actual: object) -> bool:
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting. Maybe we can also promote this as a core functionality (just a question)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Well, I think this is more for testing purposes, but we can think about it.

return Transaction(
nonce=dictionary.get("nonce", None),
value=int(dictionary.get("value", None)),
receiver=dictionary["receiver"],
Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed, sender, receiver, gas limit, chain ID are required - but if the input dictionary is missing them, the resulting exception will not be very intelligible (JS implementation would suffer from the same thing, but there we have the legacy IPlainTransaction interface).

Maybe validate the input dictionary around the required fields and throw exceptions with explicit messages?

Maybe can also stay as it is.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added a method to validate that the fields sender, receiver, chainID and gasLimit are not missing

def _value_to_b64_or_empty(self, value: str | bytes) -> str:
value_as_bytes = value.encode() if isinstance(value, str) else value

if value and len(value):
Copy link
Contributor

Choose a reason for hiding this comment

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

Using len(value) only would be all right? Below as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Indeed. Changed.

return b""

def _bytes_from_hex(self, value: str) -> bytes:
print(value)
Copy link
Contributor

Choose a reason for hiding this comment

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

Rogue print statement.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

deleted

from multiversx_sdk.core.transaction import Transaction


class TransactionsConverter:
Copy link
Contributor

Choose a reason for hiding this comment

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

in the specs there is also transaction_on_network_to_outcome(transaction: TransactionOnNetwork): TransactionOutcome; do we implement this one also? or not yet?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That has been added later. Will be implemented in a future PR.

@popenta popenta merged commit 1ada8c1 into main Apr 3, 2024
3 checks passed
@popenta popenta deleted the add-converter-and-small-refactoring branch April 3, 2024 13:15
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