Tutorial #2 - Interacting with smart contracts

In this tutorial, we will learn how to deploy and interact with smart contracts supported by the Stratis blockchain. Let’s get started!

Prerequisite

For this tutorial, you need to fully-setup the StratisUnrealManager instance, as described in Tutorial #1.

Transaction-level API

At first, let’s see how we can interact with smart contracts using low-level (i.e. transaction-level) API.

Deploying a smart contract

To deploy smart contract, we should use method sendCreateContractTransaction:

Deploy contract

where:

  • contractCode - hex-encoded bytecode of the contract. You can compile your contract using sct tool, or you can use one of the whitelisted contracts from UWhitelistedSmartContracts class.
  • parameters - serialized arguments passed to contract’s constructor. You can encode parameters using USmartContractsParametersEncoder.
  • money - the number of satoshis to deposit on the contract’s balance.
  • delegate, error delegate - return delegates, returns either transactionID of contract creation or error.

Example:

Example contract deployment

Calling contract’s methods

To call contract’s methods, we need to use sendCallContractTransaction method:

Call contract

where:

  • contractAddress - transaction ID of contract deployment transaction

  • methodName - name of the method we want to call.

  • parameters - serialized parameters list. See more in Deploying smart contract section.

  • money - amount of satoshis to send to contract.

  • delegate, error delegate - return delegates, returns either transactionID of contract call or error.

    Example:

Call contract example

Making a local call

Sometimes, we want to get some information from the smart contract, but we don’t want to post any updates to the blockchain. In this case, we can use a **local call** functionality.

To make a local call, we need to use the makeLocalCall method:

Local call

where:

  • data - structure containing all necessary information to resolve smart contract’s method call.
  • delegate, error delegate - return delegates, returns either string-encoded return value or error.

Example:

Local call example

Smart contract wrappers

Although, we can use any method of any of smart contracts with 3 methods we discussed above, this requires a lot of boilerplate code for each call.

That’s why we have wrappers for some of the white-listed contracts, such as the NFT contract. These wrappers encapsulate all of the necessary boilerplate, giving you a simple and powerful interface.

Let’s see how we can use UNFTWrapper to work with the NFT contract.

Creating an instance of UNFTWrapper

To create instance of UNFTWrapper, we can use createInstance or createDefaultInstance methods:

Create wrapper instance

where:

  • contractAddress - address of deployed contract. For createDefaultInstance method, canonical NFT contract address will be used.
  • manager - valid pointer to UStratisUnrealManager instance.
  • outer - “parent” object for our new instance.

Example:

Create wrapper instance example

Deploying a smart contract

To deploy NFT contract, we need to use deployNFTContract method:

Deploy NFT contract

where:

  • name, symbol, tokenURIFormat, ownerOnlyMinting - parameters passed to constructor of the NFT contract
  • delegate, error delegate - return delegates, returns either transactionID of contract deployment transaction or error.

Example:

Deploy NFT contract example

Calling some methods

Now, let’s try to call some of the wrapper’s methods:

Get symbol of NFT:

Get symbol

Mint NFT to current address:

Mint

Conclusion

In this tutorial, we’ve learned how to interact with smart contracts using low-level and high-level APIs.

If you found a problem, you can open an issue on the project’s Github page. If you still have questions, feel free to ask them in our Discord channel.

Stay tuned!