-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement account transactions factory
- Loading branch information
Showing
5 changed files
with
248 additions
and
8 deletions.
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
multiversx_sdk/core/transactions_factories/account_transactions_factory.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
from typing import Dict, List, Protocol | ||
|
||
from multiversx_sdk.core.interfaces import IAddress | ||
from multiversx_sdk.core.transaction import Transaction | ||
from multiversx_sdk.core.transactions_factories.transaction_builder import \ | ||
TransactionBuilder | ||
|
||
|
||
class IConfig(Protocol): | ||
chain_id: str | ||
min_gas_limit: int | ||
gas_limit_per_byte: int | ||
gas_limit_save_key_value: int | ||
gas_limit_persist_per_byte: int | ||
gas_limit_store_per_byte: int | ||
gas_limit_set_guardian: int | ||
gas_limit_guard_account: int | ||
gas_limit_unguard_account: int | ||
extra_gas_limit_for_guarded_transaction: int | ||
|
||
|
||
class AccountTransactionsFactory: | ||
def __init__(self, config: IConfig) -> None: | ||
self.config = config | ||
|
||
def create_transaction_for_saving_key_value( | ||
self, | ||
sender: IAddress, | ||
key_value_pairs: Dict[bytes, bytes] | ||
) -> Transaction: | ||
function = "SaveKeyValue" | ||
|
||
extra_gas = self._compute_extra_gas_for_saving_key_value(key_value_pairs) | ||
data_parts = self._compute_data_parts_for_saving_key_value(key_value_pairs) | ||
|
||
data_parts.insert(0, function) | ||
|
||
return TransactionBuilder( | ||
config=self.config, | ||
sender=sender, | ||
receiver=sender, | ||
data_parts=data_parts, | ||
gas_limit=extra_gas, | ||
add_data_movement_gas=True | ||
).build() | ||
|
||
def create_transaction_for_setting_guardian( | ||
self, | ||
sender: IAddress, | ||
guardian_address: IAddress, | ||
service_id: str | ||
) -> Transaction: | ||
data_parts = [ | ||
"SetGuardian", | ||
guardian_address.to_hex(), | ||
service_id.encode().hex() | ||
] | ||
|
||
gas_limit = self.config.gas_limit_set_guardian + self.config.extra_gas_limit_for_guarded_transaction | ||
|
||
return TransactionBuilder( | ||
config=self.config, | ||
sender=sender, | ||
receiver=sender, | ||
data_parts=data_parts, | ||
gas_limit=gas_limit, | ||
add_data_movement_gas=True | ||
).build() | ||
|
||
def create_transaction_for_guarding_account(self, sender: IAddress) -> Transaction: | ||
data_parts = ["GuardAccount"] | ||
|
||
gas_limit = self.config.gas_limit_guard_account + self.config.extra_gas_limit_for_guarded_transaction | ||
|
||
return TransactionBuilder( | ||
config=self.config, | ||
sender=sender, | ||
receiver=sender, | ||
data_parts=data_parts, | ||
gas_limit=gas_limit, | ||
add_data_movement_gas=True | ||
).build() | ||
|
||
def create_transaction_for_unguarding_account(self, sender: IAddress) -> Transaction: | ||
data_parts = ["UnGuardAccount"] | ||
|
||
gas_limit = self.config.gas_limit_unguard_account + self.config.extra_gas_limit_for_guarded_transaction | ||
|
||
transaction = TransactionBuilder( | ||
config=self.config, | ||
sender=sender, | ||
receiver=sender, | ||
data_parts=data_parts, | ||
gas_limit=gas_limit, | ||
add_data_movement_gas=True | ||
).build() | ||
transaction.options = 2 | ||
|
||
return transaction | ||
|
||
def _compute_data_parts_for_saving_key_value(self, key_value_pairs: Dict[bytes, bytes]) -> List[str]: | ||
data_parts: List[str] = [] | ||
|
||
for key, value in key_value_pairs.items(): | ||
data_parts.extend([key.hex(), value.hex()]) | ||
|
||
return data_parts | ||
|
||
def _compute_extra_gas_for_saving_key_value(self, key_value_pairs: Dict[bytes, bytes]) -> int: | ||
extra_gas = 0 | ||
|
||
for key, value in key_value_pairs.items(): | ||
extra_gas += self.config.gas_limit_persist_per_byte * (len(key) + len(value)) + self.config.gas_limit_store_per_byte * len(value) | ||
|
||
return extra_gas + self.config.gas_limit_save_key_value |
63 changes: 63 additions & 0 deletions
63
multiversx_sdk/core/transactions_factories/account_transactions_factory_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from typing import Dict | ||
|
||
from multiversx_sdk.core.address import Address | ||
from multiversx_sdk.core.transactions_factories.account_transactions_factory import \ | ||
AccountTransactionsFactory | ||
from multiversx_sdk.core.transactions_factories.transactions_factory_config import \ | ||
TransactionsFactoryConfig | ||
|
||
|
||
class TestAccountTransactionsFactory: | ||
config = TransactionsFactoryConfig("D") | ||
factory = AccountTransactionsFactory(config) | ||
|
||
def test_save_key_value(self): | ||
sender = Address.new_from_bech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th") | ||
pairs: Dict[bytes, bytes] = {} | ||
key = bytes.fromhex("6b657930") | ||
value = bytes.fromhex("76616c756530") | ||
pairs[key] = value | ||
|
||
tx = self.factory.create_transaction_for_saving_key_value( | ||
sender=sender, | ||
key_value_pairs=pairs | ||
) | ||
|
||
assert tx.sender == "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th" | ||
assert tx.receiver == "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th" | ||
assert tx.data.decode() == "SaveKeyValue@6b657930@76616c756530" | ||
assert tx.gas_limit == 271000 | ||
|
||
def test_set_guardian(self): | ||
sender = Address.new_from_bech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th") | ||
guardian = Address.new_from_bech32("erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx") | ||
service_id = "MultiversXTCSService" | ||
|
||
tx = self.factory.create_transaction_for_setting_guardian( | ||
sender=sender, | ||
guardian_address=guardian, | ||
service_id=service_id | ||
) | ||
|
||
assert tx.sender == "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th" | ||
assert tx.receiver == "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th" | ||
assert tx.data.decode() == "SetGuardian@8049d639e5a6980d1cd2392abcce41029cda74a1563523a202f09641cc2618f8@4d756c7469766572735854435353657276696365" | ||
assert tx.gas_limit == 525500 | ||
|
||
def test_guard_account(self): | ||
sender = Address.new_from_bech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th") | ||
tx = self.factory.create_transaction_for_guarding_account(sender) | ||
|
||
assert tx.sender == "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th" | ||
assert tx.receiver == "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th" | ||
assert tx.data.decode() == "GuardAccount" | ||
assert tx.gas_limit == 368000 | ||
|
||
def test_unguard_account(self): | ||
sender = Address.new_from_bech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th") | ||
tx = self.factory.create_transaction_for_unguarding_account(sender) | ||
|
||
assert tx.sender == "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th" | ||
assert tx.receiver == "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th" | ||
assert tx.data.decode() == "UnGuardAccount" | ||
assert tx.gas_limit == 371000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters