Run contract functions in Python

I will try to test this endpoint https://docs.moralis.io/web3-data-api/evm/how-to-run-contract-functions
My contract data to test is 0x1A92f7381B9F03921564a437210bB9396471050C like this page
The function is getPrice
This function has not input parameters

    abi = [ 
        {
        "inputs":[],
        "name":"getPrice",
        "outputs":[
            {
                "internalType":"uint256",
                "name":"",
                "type":"uint256"
            }
        ],
        "stateMutability":"view",
        "type":"function"
        }
    ]
    api_key = "XXXXXXXXXXXXXXXXXXXXXxxxx" # Moralis
    params = {
        "address": "0x1A92f7381B9F03921564a437210bB9396471050C", 
        "function_name": "getPrice", 
        "chain": "eth", 
    }
    
    body = {}
    body['abi'] = abi
    body['params'] = {}
    
    result = evm_api.utils.run_contract_function(
        api_key=api_key,
        params=params,
        body=body,
    )

    print(result)

The error code is: openapi_evm_api.exceptions.ApiTypeError: Invalid type. Required value type is frozendict and passed type was tuple at [β€˜args[0]’][β€˜abi’]

Do you have any idea to run this function in this contract?

With this function get abi complete using api rest etherscan

def ObtenerABI(contrato, red='Principal'):
    '''Dado un contrato y una red obtnedo un json con el ABI del contrato
    '''
    API_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXx'
    if red == 'Principal':
        ABI_ENDPOINT = 'https://api.etherscan.io/api?module=contract&action=getabi&address='
    else:
        ABI_ENDPOINT = 'https://api-goerli.etherscan.io/api?module=contract&action=getabi&address='

    response = requests.get('%s%s&apikey=%s'%(ABI_ENDPOINT, contrato, API_KEY))
    if response.status_code == 200:
        response_json = response.json()
        if response_json['status'] == '1':            
            abi_json = response_json['result']
        else:
            abi_json = {}    
    else:
        abi_json = {}
    return abi_json

this works for me:

from moralis import evm_api


abi = {
    "inputs": [],
    "name": "getRound",
    "outputs": [
        {
            "internalType": "int256",
            "name": "",
            "type": "int256",
        },
    ],
    "stateMutability": "view",
    "type": "function",
}

params = {
    "address": "0xD91212683F8F7e3010dAaa9E29031A518453ebd9",
    "function_name": "getRound",
    "chain": "0x13881",
}
body = {"abi": [abi], "params": {}, "functionName": "getRound"}

result = evm_api.utils.run_contract_function(
    api_key="API_KEY_HERE",
    params=params,
    body=body,
)

print(result)

are you using latest version of the SDK?

pip freeze | grep moralis
moralis==0.1.10

This is my version

[jrios@MSI-JRA TestMoralis]$ pip freeze | grep moralis
moralis==0.1.10
pip install --upgrade moralis
[jrios@MSI-JRA TestMoralis]$ pip freeze | grep moralis
moralis==0.1.22

and runs the script

everything works as expected now?

Yes. Only I need to upgrade the APi.

2 Likes

Amazing, do you need any assistance with choosing a plan?