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

Transfer and execute #367

Merged
merged 3 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions multiversx_sdk_cli/cli_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def setup_parser(args: List[str], subparsers: Any) -> Any:
cli_shared.add_tx_args(args, sub, with_receiver=False, with_data=False, with_guardian=True)
_add_function_arg(sub)
_add_arguments_arg(sub)
_add_token_transfers_args(sub)
sub.add_argument("--wait-result", action="store_true", default=False,
help="signal to wait for the transaction result - only valid if --send is set")
sub.add_argument("--timeout", default=100, help="max num of seconds to wait for result"
Expand Down Expand Up @@ -234,6 +235,12 @@ def _add_arguments_arg(sub: Any):
"boolean] or hex-encoded. E.g. --arguments 42 0x64 1000 0xabba str:TOK-a1c2ef true erd1[..]")


def _add_token_transfers_args(sub: Any):
sub.add_argument("--token-transfers", nargs='+',
help="token transfers for transfer & execute, as [token, amount] "
"E.g. --token-transfers NFT-123456-0a 1 ESDT-987654 100000000")


def _add_metadata_arg(sub: Any):
sub.add_argument("--metadata-not-upgradeable", dest="metadata_upgradeable", action="store_false",
help="‼ mark the contract as NOT upgradeable (default: upgradeable)")
Expand Down
26 changes: 23 additions & 3 deletions multiversx_sdk_cli/contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from pathlib import Path
from typing import Any, List, Optional, Protocol, Sequence

from multiversx_sdk_core import TokenComputer, Transaction, TransactionPayload
from multiversx_sdk_core import (Token, TokenComputer, TokenTransfer,
Transaction, TransactionPayload)
from multiversx_sdk_core.address import Address
from multiversx_sdk_core.transaction_factories import \
SmartContractTransactionsFactory
Expand Down Expand Up @@ -102,14 +103,18 @@ def prepare_execute_transaction(self, caller: Account, args: Any) -> Transaction
arguments = args.arguments or []
arguments = prepare_args_for_factory(arguments)

value = int(args.value)
transfers = args.token_transfers
token_transfers = self._prepare_token_transfers(transfers) if transfers else []

tx = self._factory.create_transaction_for_execute(
sender=caller.address,
contract=contract_address,
function=args.function,
gas_limit=int(args.gas_limit),
arguments=arguments,
native_transfer_amount=int(args.value),
token_transfers=[]
native_transfer_amount=value,
token_transfers=token_transfers
)
tx.nonce = int(args.nonce)
tx.version = int(args.version)
Expand Down Expand Up @@ -144,6 +149,21 @@ def prepare_upgrade_transaction(self, owner: Account, args: Any):

return tx

def _prepare_token_transfers(self, transfers: List[str]) -> List[TokenTransfer]:
token_computer = TokenComputer()
token_transfers: List[TokenTransfer] = []

for i in range(0, len(transfers) - 1, 2):
identifier = transfers[i]
amount = int(transfers[i + 1])
nonce = token_computer.extract_nonce_from_extended_identifier(identifier)

token = Token(identifier, nonce)
transfer = TokenTransfer(token, amount)
token_transfers.append(transfer)

return token_transfers


def query_contract(
contract_address: IAddress,
Expand Down
43 changes: 43 additions & 0 deletions multiversx_sdk_cli/tests/test_cli_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,43 @@ def test_contract_call():
assert Path.is_file(output_file) == True


def test_contract_transfer_and_execute(capsys: Any):
contract_address = "erd1qqqqqqqqqqqqqpgqv7sl6ws5dgwe5m04xtg0dvqyu2efz5a6d8ssxn4k9q"
first_token = "NFT-123456-02"
second_token = "ESDT-987654"

main([
"contract", "call", contract_address,
"--pem", f"{parent}/testdata/testUser.pem",
"--proxy", "https://devnet-api.multiversx.com",
"--chain", "D",
"--recall-nonce",
"--gas-limit", "5000000",
"--function", "add",
"--arguments", "5",
"--token-transfers", first_token, "1"
])
data = get_transaction_data(capsys)
assert data == "ESDTNFTTransfer@4e46542d313233343536@02@01@0000000000000000050067a1fd3a146a1d9a6df532d0f6b004e2b29153ba69e1@616464@05"

# Clear the captured content
capsys.readouterr()

main([
"contract", "call", contract_address,
"--pem", f"{parent}/testdata/testUser.pem",
"--proxy", "https://devnet-api.multiversx.com",
"--chain", "D",
"--recall-nonce",
"--gas-limit", "5000000",
"--function", "add",
"--arguments", "5",
"--token-transfers", first_token, "1", second_token, "100"
])
data = get_transaction_data(capsys)
assert data == "MultiESDTNFTTransfer@0000000000000000050067a1fd3a146a1d9a6df532d0f6b004e2b29153ba69e1@02@4e46542d313233343536@02@01@455344542d393837363534@@64@616464@05"


def test_contract_flow(capsys: Any):
alice = f"{parent}/testdata/alice.pem"
adder = f"{parent}/testdata/adder.wasm"
Expand Down Expand Up @@ -268,3 +305,9 @@ def get_contract_address(capsys: Any):
def get_query_response(capsys: Any):
out = _read_stdout(capsys).replace("\n", "").replace(" ", "")
return json.loads(out)[0]


def get_transaction_data(capsys: Any):
out = _read_stdout(capsys)
output = json.loads(out)
return output["emittedTransactionData"]
Loading