Skip to main content

Interact with a Contract

The main purpose of a smart contract is to be used by external applications or other contracts. As a developer user, you will find that interacting with a deployed smart contract can be achieved in several ways: from the aurad CLI, with libraries such as CosmJS, through other smart contracts, or even through Aurascan (for verified contracts).

In this section

We will show you how to interact with the contract we deployed in the previous section via the aurad CLI.

Entry Points

If you remember, the main entry points for a smart contract, meaning how we can communicate with it, are defined by messages. These messages define the structure of what we can tell the contract to execute or what information to give us.

For this example, we defined the InstantiateMsg, QueryMsg and ExecuteMsg. We already once interacted with our contract at instantiation. We specified to the contract the creation of a new flower item so that the store wouldn't be empty on creation.

The arguments we used is what we defined as InstantiateMsg, we only declared the values of the variables included in the data structure of the message. Here it is again below in a pretty JSON format:

{
"name": "init-flower",
"amount": 1,
"price": 1
}

Now let's see how we can interact more with our contract.

Executing

The ExecuteMsg we declared for creating a new flower item to sell in our store is very similar to the one we used in the InstantiateMsg, however, we must call the tx in a different manner.

Initially we used aurad tx wasm instantiate to instantiate the contract, but now we must use a different type of transaction. For all execute methods, we must use aurad tx wasm execute to call any transactions defined as ExecuteMsg, otherwise, it will not be handled correctly and just not work.

The proper way of calling our transaction now would be like this:

aurad tx wasm execute [contract_addr_bech32] [json_encoded_send_args] --amount [coins,optional] [flags]

First, let's get our contract's address. We can get it from the CLI by using aurad query wasm with our contract's CODE ID like this:

CONTRACT=$(aurad query wasm list-contract-by-code $CODE_ID --output json | jq -r '.contracts[-1]')

So, then let's construct the execute transaction to create a new flower item. Let's say we want to create a new item for a tulip flower, and we want to sell 100 of them at a price of 2 each. But now we also have to specify the name of the ExecuteMsg to match which is add_new. Now let's write it as the flat string JSON form.

ADD_NEW='{"add_new":{"id":"tulip1","name":"tulip","amount":100,"price":2}}'

Our transaction to add the new flower tulip to the store would look like this:

aurad tx wasm execute $CONTRACT $ADD_NEW --amount 1000ueaura --from wallet --gas 200000 --fees 25000ueaura 

Awesome! Now we have created a new flower item in our Flower Store. We can see all the transaction details in Aurascan and it should look like the picture below.

aurascan-new-flower

We can see the structure of the add_new execute message we sent, with a bit more information on the funds and what the message is.

But now, how can we make sure that the new flower is indeed in our Flower Store? On the next section we will query the state of the Flower Store contract to verify.

Querying

Querying a contract in Aura requires yet another type of transaction. In this case, we will use the command aurad query wasm. We can use this to query all contracts.

Now let's verify that the flower we just added to the store is indeed in the contract state. For the QueryMsg get_flower we defined in the contract, we need to provide the flower id we set on creation of the flower item, which in this case is tulip1.

The query structure we defined should look like this in the flat string JSON form:

QUERY='{"get_flower":{"id":"tulip1"}}'

As you can see, same as the ExecuteMsg we just used, we need to specify the name of the message we intend to use and of course, follow the structure we defined in the contract's src/msg.rs file.

So, let's query the contract to confirm. The final call should be aurad query wasm contract-state [flags], so if we run the command, we should get the result we're looking for:

$ aurad query wasm contract-state smart $CONTRACT "$QUERY" --output json

{"data":{"flower":{"id":"tulip1","name":"tulip","amount":100,"price":2}}}

Perfect! Our flower is there. All we have to do now is wait for people to buy them and financial freedom is ours once and for all.

Now it's up to you to explore and play around with some other contracts. Have fun and happy hacking!


Join us

If you have any questions related to Aura or simply would like to chat with us, come join us in the Aura Discord server or any of our official channels. We love dev chatter!

Telegram
Telegram
@AuraNetwork