# Token Interaction

The AI Agent SDK offers powerful token interaction capabilities on the Cronos blockchain. It supports retrieving token balances for both native tokens and ERC20 tokens, performing token transfers, as well as wrapping and swapping tokens.

This comprehensive feature set makes it easy for developers to build AI agents that smoothly interact with tokens.

To learn more about the underlying Developer Platform functionalities, please visit [Developer Platform Client SDK Token Module](https://ai-agent-sdk-docs.crypto.com/crypto.com-developer-platform/on-chain-developer-platform-client-sdk/token-module).

#### 2.1 Get native balance

Query the native token balance for the specified blockchain address using the Crypto.com developer platform.

{% tabs %}
{% tab title="Query" %}

* Arguments

  ```python
  address (str): The blockchain address to query.
  ```
* Example Query

  ```
  "Get native balance of <example-address>"
  ```

{% endtab %}

{% tab title="Response" %}

* Returns

  ```python
  str: A formatted string containing the native token balance for the address.
  ```
* Example Response

  ```
  The native balance for address <example-address> is <example-amount> CRO (CRO being the native token).
  ```

{% endtab %}
{% endtabs %}

Example Code

```python
from crypto_com_agent_client import Agent

agent = Agent.init(
    llm_config={
        "provider": "OpenAI",
        "model": "gpt-4o-mini",
        "temperature": 1,
        "provider-api-key": "sk-proj-example-key",
        "transfer-limit": -1,  # -1 means no limit (unlimited), 0 disables transfers completely, any positive number (e.g. 5) allows exactly that transfer amount
    },
    blockchain_config={
        "api-key": "your-crypto.com-developer-platform-api-key",
        "private-key": "your-private-key",
        "sso-wallet-url": "your-sso-wallet-url",
        "timeout": "timeout-in-seconds-for-API-calls-default-20s")
    },
)
response = agent.interact("Get native balance of <example-address>")
print(response)
```

#### 2.2 Get ERC20 Token Balance

Query the ERC20 token balance for the specified blockchain address and ERC20 contract address.

{% tabs %}
{% tab title="Query" %}

* Arguments

  ```python
  address (str): The blockchain address to query.
  contract_address (str): The contract address of the ERC20 token.
  ```
* Example Query

  ```
  "Get ERC20 balance of <example-address> in contract <example-contract>"
  ```

{% endtab %}

{% tab title="Response" %}

* Returns

  ```python
  str: A formatted string containing the ERC20 token balance for the address.
  ```
* Example Response

  ```
  The ERC20 balance for address <example-address> in the contract <example-contract> is <example-amount> tokens.
  ```

{% endtab %}
{% endtabs %}

Example Code

```python
from crypto_com_agent_client import Agent

agent = Agent.init(
    llm_config={
        "provider": "OpenAI",
        "model": "gpt-4o-mini",
        "temperature": 1,
        "provider-api-key": "sk-proj-example-key",
        "transfer-limit": -1,  # -1 means no limit (unlimited), 0 disables transfers completely, any positive number (e.g. 5) allows exactly that transfer amount
    },
    blockchain_config={
        "api-key": "your-crypto.com-developer-platform-api-key",
        "private-key": "your-private-key",
        "sso-wallet-url": "your-sso-wallet-url",
        "timeout": "timeout-in-seconds-for-API-calls-default-20s")
    },
)
response = agent.interact("Get ERC20 balance of <example-address> in contract <example-contract>")
print(response)
```

#### 2.3 Transfer native token

Transfer native tokens (cro, CRO, tcro, tCRO, zkcro, zkCRO, zktcro, zkTCRO) to a specified address using the Crypto.com developer platform.

{% tabs %}
{% tab title="Query" %}

* Arguments

  ```python
  state (dict): The current state of the workflow.
  to (str): The recipient's blockchain address. 
  amount (float): The amount of native tokens to transfer.
  ```
* Example Query

  ```
  "Transfer <example-amount> CRO to <example-to>"
  ```

{% endtab %}

{% tab title="Response" %}

* Returns

  ```python
  str: A formatted string confirming the success of the token transfer.
  ```
* Example Response

  ```
  The transfer of <example-amount> CRO to <example-to> was successful. 
  You can view the transaction details [here](<example-transaction-explorer-link>).
  ```

{% endtab %}
{% endtabs %}

Example Code

```python
from crypto_com_agent_client import Agent

agent = Agent.init(
    llm_config={
        "provider": "OpenAI",
        "model": "gpt-4o-mini",
        "temperature": 1,
        "provider-api-key": "sk-proj-example-key",
        "transfer-limit": -1,  # -1 means no limit (unlimited), 0 disables transfers completely, any positive number (e.g. 5) allows exactly that transfer amount
    },
    blockchain_config={
        "api-key": "your-crypto.com-developer-platform-api-key",
        "private-key": "your-private-key",
        "sso-wallet-url": "your-sso-wallet-url",
        "timeout": "timeout-in-seconds-for-API-calls-default-20s")
    },
)
response = agent.interact("Transfer <example-amount> CRO to <example-to>")
print(response)
```

#### 2.4 Transfer ERC20 token

Transfer ERC20 tokens to a specified address.&#x20;

{% tabs %}
{% tab title="Query" %}

* Arguments

  ```python
  state (dict): The current state of the workflow.
  to (str): The recipient's blockchain address. 
  amount (float): The amount of tokens to transfer. 
  token_symbol_or_address (str): The ERC20 token symbol or contract address.
  ```
* Example Query

  ```
  "Transfer <exmaple-amount> to <example-to> of contract <example-token>"
  ```

{% endtab %}

{% tab title="Response" %}

* Returns

  ```python
  str: A formatted string confirming the success of the token transfer.
  ```
* Example Response

  ```
  The transfer of <exmaple-amount> to <example-to> of contract <example-token> was successful. 

  You can view the transaction details [here](<example-transaction-explorer-link>).
  ```

{% endtab %}
{% endtabs %}

Example Code

```python
from crypto_com_agent_client import Agent

agent = Agent.init(
    llm_config={
        "provider": "OpenAI",
        "model": "gpt-4o-mini",
        "temperature": 1,
        "provider-api-key": "sk-proj-example-key",
        "transfer-limit": -1,  # -1 means no limit (unlimited), 0 disables transfers completely, any positive number (e.g. 5) allows exactly that transfer amount
    },
    blockchain_config={
        "api-key": "your-crypto.com-developer-platform-api-key",
        "private-key": "your-private-key",
        "sso-wallet-url": "your-sso-wallet-url",
        "timeout": "timeout-in-seconds-for-API-calls-default-20s")
    },
)
response = agent.interact("Transfer <exmaple-amount> to <example-to> of contract <example-token>")
print(response)

```

#### 2.5 Wrap token

Wrap native tokens into wrapped tokens using the Crypto.com developer platform.

{% tabs %}
{% tab title="Query" %}

* Arguments

  ```python
  state (dict): The current state of the workflow.
  amount (float): The amount of native tokens to wrap.
  ```
* Example Query

  ```
  "Wrap <example-amount> CRO token"
  ```

{% endtab %}

{% tab title="Response" %}

* Returns

  ```python
  str: A formatted string confirming the success of the wrapping operation.
  ```
* Example Response

  ```
  The CRO token wrapping was successful. 
  - Transaction Hash: <example-transaction-hash>
  - Transaction Link: [View Transaction](<example-transaction-explorer-link>)
  ```

{% endtab %}
{% endtabs %}

Example Code

```python
from crypto_com_agent_client import Agent

agent = Agent.init(
    llm_config={
        "provider": "OpenAI",
        "model": "gpt-4o-mini",
        "temperature": 1,
        "provider-api-key": "sk-proj-example-key",
        "transfer-limit": -1,  # -1 means no limit (unlimited), 0 disables transfers completely, any positive number (e.g. 5) allows exactly that transfer amount
    },
    blockchain_config={
        "api-key": "your-crypto.com-developer-platform-api-key",
        "private-key": "your-private-key",
        "sso-wallet-url": "your-sso-wallet-url",
        "timeout": "timeout-in-seconds-for-API-calls-default-20s")
    },
)
response = agent.interact("Wrap <example-amount> CRO token")
print(response)
```

#### 2.6 Swap token

Swap tokens between two different tokens.

{% tabs %}
{% tab title="Query" %}

* Arguments

  ```python
  state: The current state of the workflow.
  from_token_symbol_or_address (str): The symbol or contract address of the token to swap from.
  to_token_symbol_or_address (str): The symbol or contract address of the token to swap to.
  amount (float): The amount of tokens to swap.
  ```
* Example Query

  ```
  "Swap <example-amount> <example-from-token> for <example-to-token>"
  ```

{% endtab %}

{% tab title="Response" %}

* Returns

  ```python
  str: A formatted string confirming the success of the token swap.
  ```
* Example Response

  ```
  The token swap of <example-amount> <example-from-token> for <example-to-token> was successful. 

  - Transaction Hash: [<example-transaction-hash>](<example-explorer-link>) 

  You can view the transaction details by clicking on the link.
  ```

{% endtab %}
{% endtabs %}

Example Code

```python
from crypto_com_agent_client import Agent

agent = Agent.init(
    llm_config={
        "provider": "OpenAI",
        "model": "gpt-4o-mini",
        "temperature": 1,
        "provider-api-key": "sk-proj-example-key",
        "transfer-limit": -1,  # -1 means no limit (unlimited), 0 disables transfers completely, any positive number (e.g. 5) allows exactly that transfer amount
    },
    blockchain_config={
        "api-key": "your-crypto.com-developer-platform-api-key",
        "private-key": "your-private-key",
        "sso-wallet-url": "your-sso-wallet-url",
        "timeout": "timeout-in-seconds-for-API-calls-default-20s")
    },
)
response = agent.interact("Swap <example-amount> <example-from-token> for <example-to-token>")
print(response)
```
