#Contract call failing

59 messages · Page 1 of 1 (latest)

limpid orbit
#

Hi guys,
tried to call a contract method from a py script but im getting shown error, code

from algosdk.v2client import algod
from algosdk.atomic_transaction_composer import (
    AtomicTransactionComposer,
    AccountTransactionSigner,
)

atc = AtomicTransactionComposer()

# Connect LocalNet client
algod_client = algod.AlgodClient("a" * 64, "http://localhost:4001")

# Load contract ABI
with open(
    "../projects/DML-contracts/smart_contracts/artifacts/moderator/Moderator.arc32.json"
) as f:
    js = f.read()
contract = abi.Contract.from_json(js)


private_key, address = account.generate_account()
signer = AccountTransactionSigner(private_key)
print(f"address: {address}")
print(f"private key: {private_key}")

sp = algod_client.suggested_params()
app_id = 1001

atc.add_method_call(
    app_id,
    contract.get_method_by_name("hash"),
    address,
    sp,
    signer,
    method_args=[address],
)

wanton ridge
#

The abi.Contract.from_json() expects an ARC4 structure.
I recommend using algokit utils for a contract call, here you can find examples on how to do this: https://github.com/algorandfoundation/algokit-utils-py/blob/main/tests/test_app_client_call.py
However, a manual (not too clean, nor recommended) solution would be moving the "name" and "methods" fields in your ARC32 file to the json root level.

GitHub

Contribute to algorandfoundation/algokit-utils-py development by creating an account on GitHub.

limpid orbit
#

basically i want to call the contract method from an outside script @wanton ridge

limpid orbit
#

@dawn hollow is it possible to get some insight from you regarding this? (sorry about that)

dawn hollow
limpid orbit
#

SDK directly in a py script

#

thats what kapa recommended

dawn hollow
#

Right, and we're recommending that you use algokit-utils because it makes things much easier

limpid orbit
#

@dawn hollow is there a documentation i could refer to

dawn hollow
#

Have you checked out the example linked above?

#

Also, are you working within an AlgoKit project template, or a hand-crafted project?

limpid orbit
#

ill give u a full overview here @dawn hollow maybe you might have a better approach

dawn hollow
#

If you use the algokit init to create an AlgoKit project, it is pre-configured with everything showing how to call the contract method, etc.

limpid orbit
#

i want to pass in a ipfs hash through a model code block so i was trying to make a script to call within the model code block and pass it into the contract and start contract excecution.

dawn hollow
#

Sounds cool

#

Let's try it with AlgoKit 🙂

limpid orbit
#

im utilizing the algo kit currently

#

if could refer to a solid implementation out of this test file, i might be able to grasp a way

dawn hollow
#

I think the easiest way is to look at a fresh Python AlgoKit project

#

In deploy_config.py there is

#
    app_client = HelloWorldClient(
        algod_client,
        creator=deployer,
        indexer_client=indexer_client,
    )

    app_client.deploy(
        on_schema_break=algokit_utils.OnSchemaBreak.AppendApp,
        on_update=algokit_utils.OnUpdate.AppendApp,
    )
    name = "world"
    response = app_client.hello(name=name)
    logger.info(
        f"Called hello on {app_spec.contract.name} ({app_client.app_id}) "
        f"with name={name}, received: {response.return_value}"
    )```
#

Here you can see the creation of an app_client and then app_client.hello(name=name)

limpid orbit
#

this is nice, is there a documentation about this deploy functionality @dawn hollow , im thinking how do i call this within a model code block

dawn hollow
#

What is a model code block? Not sure if you're referring to something specific here

limpid orbit
#

to pass the has into the deployer from here and start smart contract execution

dawn hollow
#

Smart contract execution is only ever invoked when the application is called, although it is possible to have logic baked right into an application create, too

limpid orbit
#

this is a simple code block within jupyter

dawn hollow
#

So the mental model here is that you write a smart contract in Puya (Algorand Python) and then deploy it to the chain, where it can be called through an app call transaction

#

The deploy function in AlgoKit Utils is a helper function that provides some neat functionality wrapped around creating a smart contract

limpid orbit
#

🤔 what if i want to create and deploy smart contract right after the above ipfs storage step

dawn hollow
#

You have to decouple these things

#

Smart contracts cannot directly interact with ipfs

#

If you want these actions to happen in sequence, you can use your business logic to coordinate that

limpid orbit
#

yeah currently ipfs storage is not the issue but passing the hash into the contract is the issue right after a file has been uploaded

dawn hollow
#

I think I need more context about what you're trying to achieve in business terms

#

Passing a hash bytes into a smart contract, either as a method arg or even as a template variable, is straightforward to do in isolation

limpid orbit
#

is it possible to show me an example?

dawn hollow
#

I showed you an example above

#

This line

response = app_client.hello(name=name)``` is issuing an application call transaction that calls the `hello` method and provides an argument `name`
#

In this case name is a string, but that could just as easily be the 32 bytes of a hash

limpid orbit
#

yep i got the logic there but how do i pass it within the same flow utilizing jupyter for now

#

for ex i want the flow to be like this model execution -> model storage -> generate ipfs hash -> pass ipfs hash and start smart contract execution

dawn hollow
#

replace start smart contract execution with sign and send a transaction that calls the smart contract which already exists on the chain

limpid orbit
#

🤔 how do i invoke a transaction call within jupyter?

dawn hollow
#

Jupyter is just a way to run Python code

#

It has no bearing on the code you run

#

I would recommending using a full IDE such as VSCode + and AlgoKit project template

limpid orbit
#

is it possible to give more context of the flow you'd suggest?

dawn hollow
#

The 1-minute Algorand Python series will cover everything you need to know about Algorand Python, the high-level smart contract language on Algorand that allows you to write smart contracts with Python.

Resources:
Algorand Python Documentation: https://algorandfoundation.github.io/puya/#
Algorand Python Github: https://github.com/algorandfounda...

▶ Play video
limpid orbit
#

im sorry but ive gone through all the tuts and vids

dawn hollow
#

Have you followed any of the longer videos that go into more detail?

limpid orbit
#

yup all of em

#

from bootcamp to these short vids

#

I would recommending using a full IDE such as VSCode + and AlgoKit project template <- is there a feature i missed here that could mediate my usecase

dawn hollow
limpid orbit
#

yup

dawn hollow
#

So you were able to deploy the contract to localnet and call the application method through a transaction?