home / skills / multiversx / mx-ai-skills / mvx_sdk_py_contracts

mvx_sdk_py_contracts skill

/antigravity/skills/mvx_sdk_py_contracts

This skill helps you manage MultiversX smart contracts with ABI loading, deployment, calls, and queries from the Python SDK.

npx playbooks add skill multiversx/mx-ai-skills --skill mvx_sdk_py_contracts

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
2.7 KB
---
name: mvx_sdk_py_contracts
description: Smart contract operations for MultiversX Python SDK.
---

# MultiversX SDK-Py Smart Contracts

This skill covers ABI loading, deployments, calls, and queries.

## ABI Loading

```python
from multiversx_sdk import Abi
from pathlib import Path

# Load from file
abi = Abi.load("contract.abi.json")
```

## Smart Contract Controller

```python
# Create controller with loaded ABI
controller = entrypoint.create_smart_contract_controller(abi=abi)
```

## Deployment

```python
bytecode = Path("contract.wasm").read_bytes()

tx = controller.create_transaction_for_deploy(
    sender=account,
    nonce=account.get_nonce_then_increment(),
    bytecode=bytecode,
    gas_limit=60000000,
    arguments=[42]  # Typed automatically via ABI
)

tx_hash = entrypoint.send_transaction(tx)
parsed_outcome = controller.await_completed_deploy(tx_hash)
contract_address = parsed_outcome[0].contract_address
```

## Contract Calls

```python
# Call endpoint
tx = controller.create_transaction_for_execute(
    sender=account,
    nonce=account.get_nonce_then_increment(),
    contract=contract_address,
    function="add",
    arguments=[10],
    gas_limit=5000000
)

# Call with token transfer (Transfer & Execute)
tx = controller.create_transaction_for_execute(
    sender=account,
    nonce=account.get_nonce_then_increment(),
    contract=contract_address,
    function="deposit",
    gas_limit=10000000,
    native_amount=1000000000000000000
)
```

## VM Queries

Read-only calls to the contract state.

```python
# Using controller (parsed output)
result = controller.query_contract(
    contract=contract_address,
    function="getSum",
    arguments=[]
)
print(result[0])  # Typed output
```

## Without ABI (Manual Typing)

If ABI is not available, use manual type hinting (less recommended).

```python
from multiversx_sdk import SmartContractTransactionsFactory

factory = entrypoint.create_smart_contract_transactions_factory()

tx = factory.create_transaction_for_execute(
    sender=account.address,
    contract=contract_address,
    function="add",
    gas_limit=5000000,
    arguments=[42]  # Basic types inferred
)
```

## Upgrades

```python
new_bytecode = Path("contract_v2.wasm").read_bytes()

tx = controller.create_transaction_for_upgrade(
    sender=account,
    nonce=account.get_nonce_then_increment(),
    contract=contract_address,
    bytecode=new_bytecode,
    gas_limit=60000000,
    arguments=[]
)
```

## Best Practices

1. **Always use ABI**: Ensures correct argument encoding/decoding
2. **Query for reads**: Free (no gas), instant response
3. **Wait for completion**: Use controller methods to get parsed results
4. **Gas limit**: Ensure enough gas for computation + storage

Overview

This skill provides Python tools for working with MultiversX smart contracts: loading ABIs, preparing and sending deploys, upgrades, execute calls, and VM queries. It wraps low-level transaction construction with typed encoding when an ABI is available and offers fallbacks for manual typing. The skill focuses on reliable transaction lifecycle handling and parsed outcomes for easier integration.

How this skill works

Load a contract ABI to get a smart contract controller that builds typed transactions and decodes responses. Use controller methods to create deploy, execute, upgrade, or query requests, then send transactions and await parsed completion results. If no ABI is available, a transactions factory can build basic, manually typed calls but without automatic encoding/decoding.

When to use it

  • Deploy a new smart contract from Python with bytecode and ABI.
  • Execute state-changing functions with proper gas and optional native token transfer.
  • Perform read-only VM queries to return typed state without consuming gas.
  • Upgrade contract bytecode while preserving on-chain state and calling upgrade endpoints.
  • Build manual transactions when ABI is unavailable or for custom low-level flows.

Best practices

  • Always use the contract ABI to ensure correct argument encoding and typed decoding of results.
  • Prefer VM queries for reads: they are free and return immediate typed outputs.
  • Wait for transaction completion via controller helpers to get parsed outcomes and contract addresses.
  • Provision sufficient gas for computation plus storage changes; set conservative gas limits for complex operations.
  • Use nonces correctly (get and increment) to avoid transaction collisions.

Example use cases

  • Deploy a new token or dApp: load ABI, read wasm bytes, create deploy transaction, send and await parsed deploy outcome.
  • Call a payable contract function: create execute transaction with native_amount to transfer tokens and invoke logic in one step.
  • Read contract state: run query for getSum or other getters to obtain typed values without gas costs.
  • Upgrade contract implementation: prepare new bytecode, create upgrade transaction, send and verify parsed completion.
  • Create manual execute transactions when working with minimal ABI info or custom typed arguments.

FAQ

Do I need an ABI to interact with contracts?

No, but using an ABI is strongly recommended. ABI enables correct encoding/decoding and typed results; without it you must manually manage types.

Are VM queries free and safe to use?

Yes. VM queries are read-only calls that do not consume gas and return instant, typed results when using the controller.