Interacting with Our NFTs
Tags: cpp, wax, blockchain, tutorial
Published: 2023-08-06 22:29:07

## Introduction

In this section, we are going to interact with AtomicAsssets via the Smart Contract we wrote in the last section. We will deploy the contract, and then use [Bloks.io](http://Bloks.io) to view the contract, and show an example using Anchor to interact with the contract to purchase an NFT!

## Deploying the Smart Contract

Let’s deploy our contract. Since we are using the same account name as our previous tutorials, this will overwrite the contract that is on the blockchain – but any old tables will continue to exist on the chain. This is important since it means even if we change the contract for our account, all of the data is immutable.

Let’s unlock our wallet:

```sh
cd /wax
cleos wallet open -n testwallet
cleos wallet unlock -n testwallet --password="$(cat ./secrets)"
```

No we will deploy our contract:

```sh
cleos -v -u https://testnet.wax.pink.gg set contract waxcourse123 ./babychicks/build/babychicks -p waxcourse123@active
```

This command may give you an error:

```sh
Reading WASM from /wax/babychicks/build/babychicks/guestbook.wasm...
Publishing contract...
Error 3080001: Account using more than allotted RAM usage
Error Details:
account waxcourse123 has insufficient ram; needs 81572 bytes has 9592 bytes
Stack Trace:
resource_limits.cpp:235 verify_account_ram_usage
```

In this case, we need to buy RAM for our account. This is a useful command to have bookmarked:

```sh
# Remember to change waxcourse123 to your account name
cleos -v -u https://testnet.wax.pink.gg \
  system buyram waxcourse123 waxcourse123 "500" --kbytes \
  -p waxcourse123@active
```

If everything succeeded, you will see similar output to the following:

![Example response](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hgedi4z4aymxmvuaq9bv.png)

In the next section, we will interact with our contract using the Bloks.io interface.

## Setting Permissions

Before we interact with our contract, we need to set a permission setting for our account, otherwise we will see the following error when trying to call actions:

```markdown
Provided keys, permissions, and delays do not satisfy declared authorizations
```

To remedy this, we need to run the following line after unlocking our wallet:

```sh
cleos --url="https://testnet.wax.pink.gg" set account permission waxcourse123 active --add-code
```

Now we can interact with out contract.

## Interacting via Bloks.io

If we go to https://wax-test.bloks.io/account/waxcourse123 and then click on the Contract tab, we will be able to see the tables and actions our contract provides. Right now it leverages AtomicAssets to mint and handle the lifecycle of NFTs, so we won’t see any tables.

If we go to the Actions tab, we can see our airdropegg action that we defined.

![waxcouse123 on bloks](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/81p7tlb0uaavnabwzt4e.png)

You can type in your account name to mint an egg NFT. This will mint an egg just like we did during the **Manually Minting NFTs** section.

<aside>
💡 Typically, this works on the production Wax Chain. Currently while writing, using the [bloks.io](http://bloks.io) interface was not working due to CORS issues.
</aside>

## Interacting via Anchor

An alternative way to work with the Smart Contract is through Anchor. Simply open Anchor, then go to Tools→Smart Contracts. Type your account name, in our case `waxcourse123`**.**

Select the `airdropegg` action and type in your account name:

![Contract information](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hooaf2xcaqrs0lc31o98.png)

You’ll see a Transaction submitted message when the transaction goes through:

![Executing an action on bloks](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3lh48pwa0bbcm84iyrjq.png)

Some common errors:

```markdown
assertion failure with message: No collection with this name exists ()
```

You did not use the correct collection name from the one you originally created.

## Purchasing an NFT

In the previous section, we used our admin functionality to mint an NFT egg. Most of our users will not be able to purchase an egg this way. Instead they will need to send WAX to our contract.

If you are using Anchor, you can transfer tokens to the wallet in order to mint an egg:


![Transfer tokens](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tmnbkelj872bg19tdr4m.png)


If you send the incorrect amount of WAX (say 200 instead of 2000), the contract will reject the transfer:


![An expected error](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j2974ir9lucs7zr1tc1p.png)


This helps users interact with our contract correctly without any need for manual refunds.

## Dark Emblem

The mechanisms we used in this example Smart Contract and the way user’s can interact with the contract are all part of the [Dark Emblem NFT](https://darkemblem.com) Smart Contract. The Dark Emblem contract is deployed to the WAX mainnet, demonstrating how all of the above listed functionality can come together to create an interactive Smart Contract experience.

## Conclusion

In this section, we went over how to purchase an NFT from our deployed Smart Contract. This is a huge step into creating awesome WAX blockchain experiences using NFTs. From here, we will be able to continue to build up new functionality for our BabyChick NFTs!

Next post: [Using AtomicHub](https://dev.to/idmontie/using-atomichub-20nj)

## E-book

Get this entire WAX tutorial as an [e-book on Amazon](https://amzn.to/3ZjwaK7).

## Additional links

- Github: https://github.com/CapsuleCat/wax-nft-tutorial/tree/main/babychicks
- Photo by [Brian McGowan](https://unsplash.com/@sushioutlaw?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/photos/I0fDR8xtApA?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)