From 1be824f207dba5004896ca9656cec1ae78bf2021 Mon Sep 17 00:00:00 2001 From: Thoralf-M <46689931+Thoralf-M@users.noreply.github.com> Date: Wed, 30 Aug 2023 14:32:33 +0200 Subject: [PATCH] Add python send wrapper methods (#1104) * Add send wrapper methods * Fix example, update comment --- bindings/core/src/method/account.rs | 2 +- bindings/nodejs/lib/wallet/account.ts | 2 +- bindings/python/CHANGELOG.md | 1 + .../consolidate_outputs.py | 3 +- .../python/examples/how_tos/alias/create.py | 2 +- .../how_tos/alias_wallet/transaction.py | 3 +- .../examples/how_tos/native_tokens/create.py | 2 +- .../examples/how_tos/native_tokens/melt.py | 2 +- .../examples/how_tos/native_tokens/mint.py | 4 +- .../examples/how_tos/native_tokens/send.py | 2 +- .../nft_collection/00_mint_issuer_nft.py | 3 +- .../nft_collection/01_mint_collection_nft.py | 3 +- .../python/examples/how_tos/nfts/mint_nft.py | 2 +- .../python/examples/how_tos/nfts/send_nft.py | 2 +- .../python/examples/wallet/create_alias.py | 2 +- bindings/python/iota_sdk/wallet/account.py | 66 +++++++++++++++++++ .../high_level/burning_melting/mod.rs | 2 +- 17 files changed, 84 insertions(+), 19 deletions(-) diff --git a/bindings/core/src/method/account.rs b/bindings/core/src/method/account.rs index 65f7c2fcec..4a1ca46b2a 100644 --- a/bindings/core/src/method/account.rs +++ b/bindings/core/src/method/account.rs @@ -129,7 +129,7 @@ pub enum AccountMethod { /// Returns all pending transactions of the account /// Expected response: [`Transactions`](crate::Response::Transactions) PendingTransactions, - /// A generic `burn()` function that can be used to burn native tokens, nfts, foundries and aliases. + /// A generic function that can be used to burn native tokens, nfts, foundries and aliases. /// /// Note that burning **native tokens** doesn't require the foundry output which minted them, but will not /// increase the foundries `melted_tokens` field, which makes it impossible to destroy the foundry output. diff --git a/bindings/nodejs/lib/wallet/account.ts b/bindings/nodejs/lib/wallet/account.ts index 4e46bb7bba..8366c2f357 100644 --- a/bindings/nodejs/lib/wallet/account.ts +++ b/bindings/nodejs/lib/wallet/account.ts @@ -117,7 +117,7 @@ export class Account { } /** - * A generic `burn()` function that can be used to prepare to burn native tokens, nfts, foundries and aliases. + * A generic function that can be used to prepare to burn native tokens, nfts, foundries and aliases. * @param burn The outputs to burn * @param transactionOptions The options to define a `RemainderValueStrategy` * or custom inputs. diff --git a/bindings/python/CHANGELOG.md b/bindings/python/CHANGELOG.md index ef2ee4fb09..62f858f5fe 100644 --- a/bindings/python/CHANGELOG.md +++ b/bindings/python/CHANGELOG.md @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - `ConflictReason` display implementation with an explanation of the conflict; +- `Account::{burn(), consolidate_outputs(), create_alias_output(), create_native_token(), melt_native_token(), mint_native_token(), mint_nfts(), send_transaction(), send_native_tokens(), send_nft()}` methods; ## 1.0.1 - 2023-08-23 diff --git a/bindings/python/examples/how_tos/accounts_and_addresses/consolidate_outputs.py b/bindings/python/examples/how_tos/accounts_and_addresses/consolidate_outputs.py index cd0480a98c..674602e73a 100644 --- a/bindings/python/examples/how_tos/accounts_and_addresses/consolidate_outputs.py +++ b/bindings/python/examples/how_tos/accounts_and_addresses/consolidate_outputs.py @@ -47,8 +47,7 @@ # Consolidate unspent outputs and print the consolidation transaction ID # Set `force` to true to force the consolidation even though the # `output_threshold` isn't reached. -transaction = account.prepare_consolidate_outputs( - ConsolidationParams(force=True)).send() +transaction = account.consolidate_outputs(ConsolidationParams(force=True)) print('Transaction sent: ', transaction.transactionId) # Wait for the consolidation transaction to get confirmed diff --git a/bindings/python/examples/how_tos/alias/create.py b/bindings/python/examples/how_tos/alias/create.py index 599982ceb2..2f2838eef1 100644 --- a/bindings/python/examples/how_tos/alias/create.py +++ b/bindings/python/examples/how_tos/alias/create.py @@ -21,5 +21,5 @@ wallet.set_stronghold_password(os.environ["STRONGHOLD_PASSWORD"]) # Send transaction. -transaction = account.prepare_create_alias_output(None, None).send() +transaction = account.create_alias_output(None, None) print(f'Block sent: {os.environ["EXPLORER_URL"]}/block/{transaction.blockId}') diff --git a/bindings/python/examples/how_tos/alias_wallet/transaction.py b/bindings/python/examples/how_tos/alias_wallet/transaction.py index d0e6786511..53c27f086f 100644 --- a/bindings/python/examples/how_tos/alias_wallet/transaction.py +++ b/bindings/python/examples/how_tos/alias_wallet/transaction.py @@ -34,7 +34,8 @@ # Find first output unlockable by the alias address query_parameters = NodeIndexerAPI.QueryParameters(alias_address) -transaction_input = wallet.get_client().basic_output_ids(query_parameters).items[0] +transaction_input = wallet.get_client( +).basic_output_ids(query_parameters).items[0] params = [SendParams( address='rms1qpszqzadsym6wpppd6z037dvlejmjuke7s24hm95s9fg9vpua7vluaw60xu', diff --git a/bindings/python/examples/how_tos/native_tokens/create.py b/bindings/python/examples/how_tos/native_tokens/create.py index 4e3038616c..d04c55d07d 100644 --- a/bindings/python/examples/how_tos/native_tokens/create.py +++ b/bindings/python/examples/how_tos/native_tokens/create.py @@ -25,7 +25,7 @@ # existing one. if not balance.aliases: # If we don't have an alias, we need to create one - transaction = account.prepare_create_alias_output(None, None).send() + transaction = account.create_alias_output(None, None) print(f'Transaction sent: {transaction.transactionId}') # Wait for transaction to get included diff --git a/bindings/python/examples/how_tos/native_tokens/melt.py b/bindings/python/examples/how_tos/native_tokens/melt.py index 79d7365641..d7341e8fbb 100644 --- a/bindings/python/examples/how_tos/native_tokens/melt.py +++ b/bindings/python/examples/how_tos/native_tokens/melt.py @@ -30,7 +30,7 @@ melt_amount = 10 # Send transaction. -transaction = account.prepare_melt_native_token(token_id, melt_amount).send() +transaction = account.melt_native_token(token_id, melt_amount) print(f'Transaction sent: {transaction.transactionId}') # Wait for transaction to get included diff --git a/bindings/python/examples/how_tos/native_tokens/mint.py b/bindings/python/examples/how_tos/native_tokens/mint.py index 38624d241e..9e07d8889a 100644 --- a/bindings/python/examples/how_tos/native_tokens/mint.py +++ b/bindings/python/examples/how_tos/native_tokens/mint.py @@ -29,8 +29,8 @@ mint_amount = 10 -# Prepare and send transaction. -transaction = account.prepare_mint_native_token(token_id, mint_amount).send() +# Send transaction. +transaction = account.mint_native_token(token_id, mint_amount) print(f'Transaction sent: {transaction.transactionId}') # Wait for transaction to get included diff --git a/bindings/python/examples/how_tos/native_tokens/send.py b/bindings/python/examples/how_tos/native_tokens/send.py index 00d195cc07..ae0db2d8d9 100644 --- a/bindings/python/examples/how_tos/native_tokens/send.py +++ b/bindings/python/examples/how_tos/native_tokens/send.py @@ -32,7 +32,7 @@ )], )] -transaction = account.prepare_send_native_tokens(outputs, None).send() +transaction = account.send_native_tokens(outputs, None) print(f'Transaction sent: {transaction.transactionId}') # Wait for transaction to get included diff --git a/bindings/python/examples/how_tos/nft_collection/00_mint_issuer_nft.py b/bindings/python/examples/how_tos/nft_collection/00_mint_issuer_nft.py index 64d7bc1570..c89b105dab 100644 --- a/bindings/python/examples/how_tos/nft_collection/00_mint_issuer_nft.py +++ b/bindings/python/examples/how_tos/nft_collection/00_mint_issuer_nft.py @@ -28,8 +28,7 @@ ) -prepared = account.prepare_mint_nfts([params]) -transaction = prepared.send() +transaction = account.mint_nfts([params]) # Wait for transaction to get included block_id = account.retry_transaction_until_included(transaction.transactionId) diff --git a/bindings/python/examples/how_tos/nft_collection/01_mint_collection_nft.py b/bindings/python/examples/how_tos/nft_collection/01_mint_collection_nft.py index a21154736b..79e22b7008 100644 --- a/bindings/python/examples/how_tos/nft_collection/01_mint_collection_nft.py +++ b/bindings/python/examples/how_tos/nft_collection/01_mint_collection_nft.py @@ -63,8 +63,7 @@ def get_immutable_metadata(index: int, collection_id: str) -> str: chunk, nft_mint_params = nft_mint_params[:NUM_NFTS_MINTED_PER_TRANSACTION], nft_mint_params[NUM_NFTS_MINTED_PER_TRANSACTION:] print( f'Minting {len(chunk)} NFTs... ({NFT_COLLECTION_SIZE-len(nft_mint_params)}/{NFT_COLLECTION_SIZE})') - prepared = account.prepare_mint_nfts(chunk) - transaction = prepared.send() + transaction = account.mint_nfts(chunk) # Wait for transaction to get included block_id = account.retry_transaction_until_included( diff --git a/bindings/python/examples/how_tos/nfts/mint_nft.py b/bindings/python/examples/how_tos/nfts/mint_nft.py index 6b0042f587..a1a97f975c 100644 --- a/bindings/python/examples/how_tos/nfts/mint_nft.py +++ b/bindings/python/examples/how_tos/nfts/mint_nft.py @@ -24,5 +24,5 @@ immutableMetadata=utf8_to_hex("some immutable nft metadata"), )] -transaction = account.prepare_mint_nfts(outputs).send() +transaction = account.mint_nfts(outputs) print(f'Block sent: {os.environ["EXPLORER_URL"]}/block/{transaction.blockId}') diff --git a/bindings/python/examples/how_tos/nfts/send_nft.py b/bindings/python/examples/how_tos/nfts/send_nft.py index 7415082d2a..282682b58e 100644 --- a/bindings/python/examples/how_tos/nfts/send_nft.py +++ b/bindings/python/examples/how_tos/nfts/send_nft.py @@ -25,5 +25,5 @@ nftId=balance.nfts[0], )] -transaction = account.prepare_send_nft(outputs).send() +transaction = account.send_nft(outputs) print(f'Block sent: {os.environ["EXPLORER_URL"]}/block/{transaction.blockId}') diff --git a/bindings/python/examples/wallet/create_alias.py b/bindings/python/examples/wallet/create_alias.py index 4700e3ee62..73e4fb4a5f 100644 --- a/bindings/python/examples/wallet/create_alias.py +++ b/bindings/python/examples/wallet/create_alias.py @@ -21,5 +21,5 @@ wallet.set_stronghold_password(os.environ["STRONGHOLD_PASSWORD"]) # Send transaction. -transaction = account.prepare_create_alias_output(None, None).send() +transaction = account.create_alias_output(None, None) print(f'Block sent: {os.environ["EXPLORER_URL"]}/block/{transaction.blockId}') diff --git a/bindings/python/iota_sdk/wallet/account.py b/bindings/python/iota_sdk/wallet/account.py index e68b5797a5..864ab894fc 100644 --- a/bindings/python/iota_sdk/wallet/account.py +++ b/bindings/python/iota_sdk/wallet/account.py @@ -78,6 +78,12 @@ def get_metadata(self) -> AccountMetadata: return AccountMetadata( self.meta["alias"], self.meta["coinType"], self.meta["index"]) + def burn( + self, burn: Burn, options: Optional[TransactionOptions] = None) -> Transaction: + """A generic function that can be used to burn native tokens, nfts, foundries and aliases. + """ + return self.prepare_burn(burn, options).send() + def prepare_burn( self, burn: Burn, options: Optional[TransactionOptions] = None) -> PreparedTransaction: """A generic `prepare_burn()` function that can be used to prepare the burn of native tokens, nfts, foundries and aliases. @@ -119,6 +125,12 @@ def prepare_burn_nft(self, ) return PreparedTransaction(self, prepared) + def consolidate_outputs( + self, params: ConsolidationParams) -> Transaction: + """Consolidate outputs. + """ + return self.prepare_consolidate_outputs(params).send() + def prepare_consolidate_outputs( self, params: ConsolidationParams) -> PreparedTransaction: """Consolidate outputs. @@ -130,6 +142,13 @@ def prepare_consolidate_outputs( ) return PreparedTransaction(self, prepared) + def create_alias_output(self, + params: Optional[CreateAliasOutputParams] = None, + options: Optional[TransactionOptions] = None) -> Transaction: + """Create an alias output. + """ + return self.prepare_create_alias_output(params, options).send() + def prepare_create_alias_output(self, params: Optional[CreateAliasOutputParams] = None, options: Optional[TransactionOptions] = None) -> PreparedTransaction: @@ -272,6 +291,12 @@ def pending_transactions(self): ) return [Transaction.from_dict(tx) for tx in transactions] + def create_native_token(self, params: CreateNativeTokenParams, + options: Optional[TransactionOptions] = None) -> Transaction: + """Create native token. + """ + return self.prepare_create_native_token(params, options).send() + def prepare_create_native_token(self, params: CreateNativeTokenParams, options: Optional[TransactionOptions] = None) -> PreparedTransaction: """Create native token. @@ -285,6 +310,16 @@ def prepare_create_native_token(self, params: CreateNativeTokenParams, return PreparedCreateTokenTransaction( account=self, prepared_transaction_data=prepared) + def melt_native_token(self, + token_id: HexStr, + melt_amount: int, + options: Optional[TransactionOptions] = None) -> Transaction: + """Melt native tokens. This happens with the foundry output which minted them, by increasing it's + `melted_tokens` field. + """ + return self.prepare_melt_native_token( + token_id, melt_amount, options).send() + def prepare_melt_native_token(self, token_id: HexStr, melt_amount: int, @@ -301,6 +336,13 @@ def prepare_melt_native_token(self, ) return PreparedTransaction(self, prepared) + def mint_native_token(self, token_id: HexStr, mint_amount: int, + options: Optional[TransactionOptions] = None) -> Transaction: + """Mint additional native tokens. + """ + return self.prepare_mint_native_token( + token_id, mint_amount, options).send() + def prepare_mint_native_token(self, token_id: HexStr, mint_amount: int, options: Optional[TransactionOptions] = None) -> PreparedTransaction: """Mint additional native tokens. @@ -314,6 +356,12 @@ def prepare_mint_native_token(self, token_id: HexStr, mint_amount: int, ) return PreparedTransaction(self, prepared) + def mint_nfts(self, params: List[MintNftParams], + options: Optional[TransactionOptions] = None) -> Transaction: + """Mint NFTs. + """ + return self.prepare_mint_nfts(params, options).send() + def prepare_mint_nfts(self, params: List[MintNftParams], options: Optional[TransactionOptions] = None) -> PreparedTransaction: """Mint NFTs. @@ -361,6 +409,12 @@ def prepare_send(self, params: List[SendParams], ) return PreparedTransaction(self, prepared) + def send_transaction( + self, outputs: List[Output], options: Optional[TransactionOptions] = None) -> Transaction: + """Send a transaction. + """ + return self.prepare_transaction(outputs, options).send() + def prepare_transaction( self, outputs: List[Output], options: Optional[TransactionOptions] = None) -> PreparedTransaction: """Prepare transaction. @@ -420,6 +474,12 @@ def send_with_params( } )) + def send_native_tokens( + self, params: List[SendNativeTokensParams], options: Optional[TransactionOptions] = None) -> Transaction: + """Send native tokens. + """ + return self.prepare_send_native_tokens(params, options).send() + def prepare_send_native_tokens( self, params: List[SendNativeTokensParams], options: Optional[TransactionOptions] = None) -> PreparedTransaction: """Send native tokens. @@ -432,6 +492,12 @@ def prepare_send_native_tokens( ) return PreparedTransaction(self, prepared) + def send_nft(self, params: List[SendNftParams], + options: Optional[TransactionOptions] = None) -> Transaction: + """Send nft. + """ + return self.prepare_send_nft(params, options).send() + def prepare_send_nft(self, params: List[SendNftParams], options: Optional[TransactionOptions] = None) -> PreparedTransaction: """Send nft. diff --git a/sdk/src/wallet/account/operations/transaction/high_level/burning_melting/mod.rs b/sdk/src/wallet/account/operations/transaction/high_level/burning_melting/mod.rs index be0fbb61c1..7d16ccd177 100644 --- a/sdk/src/wallet/account/operations/transaction/high_level/burning_melting/mod.rs +++ b/sdk/src/wallet/account/operations/transaction/high_level/burning_melting/mod.rs @@ -12,7 +12,7 @@ use crate::{ pub(crate) mod melt_native_token; impl Account { - /// A generic `burn()` function that can be used to burn native tokens, nfts, foundries and aliases. + /// A generic function that can be used to burn native tokens, nfts, foundries and aliases. /// /// Note that burning **native tokens** doesn't require the foundry output which minted them, but will not increase /// the foundries `melted_tokens` field, which makes it impossible to destroy the foundry output. Therefore it's