This site requires Javascript to be enabled.

AI assisted smart contract development with ChatGPT

In this tutorial, you'll create a Lottery smart contract using the Archway Developer CLI but assisted by ChatGPT. The AI will generate all the code necessary to have a fully functional contract.

Please make sure you've gone through the following guide to install all the tools required for developing on Archway especially the section on installing the Archway Developer CLI.

The following are the requirements for the lottery contract:

  • An account enters the lottery by paying a small fee
  • A winner is selected to end the lottery and will receive the total collected fees

Key tips for interacting with ChatGPT

To successfully create and deploy the Lottery smart contract using the Archway Developer CLI, you will interact with the ChatGPT AI to get clear, concise, and accurate instructions.

  • Be Specific: Clearly state what you need help with. The more specific your question, the better the response.
  • Follow Instructions: Ensure you follow the instructions provided by ChatGPT closely to avoid errors.
  • Ask for Clarification: If any part of the response is unclear, ask for further clarification.
  • Check Outputs: After running commands, check the outputs and return any errors or messages to ChatGPT for further assistance.

Create a blank smart contract

For this project, we’ll be utilizing the Archway Developer CLI.

The Developer CLI follows a specific structure and configuration, so we need to begin by creating a blank smart contract as the foundation for our dapp. Once your environment is ready, refer to this setup guide to generate a blank smart contract project.

Building the smart contract: working with key files

Now that we’ve set up the foundation of our project, it's time to begin working on the key files that will make up the smart contract. Each file plays a critical role in defining how the contract operates, manages data, and allows for user interactions. We’ll go through each file step by step, focusing on how they contribute to the overall functionality of the contract.

The main files we’ll be working on include:

  • state.rs: Manages the contract’s state and data storage.
  • msg.rs: Defines the messages and parameters that users can send to interact with the contract.
  • contract.rs: Contains the core logic and execution flow of the smart contract.
  • error.rs: Handles error management and reporting in the contract.

In each section, we will walk through what needs to be done in the file, providing guidance and instructions for building out the lottery smart contract and the prompts that will be submited to ChatGPT for it to generate the necessary code.

Archway custom GPT

We've created a custom GPT configured with a knowledge set that should help with building smart contracts on Archway. The custom GPT can be accessed here and is the recommended GPT for building smart contracts on Archway.

Create contract data structure

In a CosmWasm smart contract project, the state.rs file is typically used to define and manage the data structures that represent the state of the smart contract. This includes defining the types and their serialization for storage, as well as the methods for interacting with the state.

Execute the following prompt:

Generate the required data structures in the state.rs file for a lottery smart contract that should allow users to enter the lottery by paying a small fee, and a winner is selected to end the lottery who will receive the total collected fees. The winner selection will be deterministic, based on the block height, as randomness is not supported in CosmWasm smart contracts. Please ensure that the structures are serializable using serde and ready to be stored with cw-storage-plus. Also include comments explaining the purpose of each data structure.

Contract logic

The main entry point for defining the core logic of the smart contract is stored in the contract.rs file. This file typically contains the primary functions that handle the execution of the contract's actions and queries. It orchestrates how the contract processes messages, manages state, and interacts with other contracts.

The following would be the prompt:

Generate the contract.rs file for the lottery smart contract. Please make sure to include a method for initializing the contract and all the necessary entry points are created. Use CosmWasm's standard response and error handling patterns, ensuring that each function interacts with the state using the structures defined in state.rs.

Create messages

In an Archway smart contract project, the msg.rs file is typically used to define the messages that the contract can handle. These messages represent the various commands or queries that can be sent to the smart contract by users or other contracts.

The following would be the prompt to get this file with the required messages created:

Generate the msg.rs file for the lottery smart contract based on the contract.rs and state.rs files. It should include:

  • An InstantiateMsg struct for initializing the contract.
  • An ExecuteMsg enum with variants executing required transactions
  • A QueryMsg enum with variant for querying poll results.

Ensure that all structs and enums are serializable using serde.

Error handling

The error.rs file is used to define custom error types for the contract. These custom error types help to provide more meaningful error messages and better error handling within the contract. By using specific error types, developers can more easily diagnose and handle issues that arise during the execution of the contract.

The following would be the prompt:

Generate the error.rs file based on the relevant errors used within the contract.rs file. It should also follow CosmWasm's best practices for error handling.

Rectify any errors

There may be errors in the contract.rs and other files. To address these, hover over each error to see the details of the error and copy the details into ChatGPT and following its instructions to rectify the errors which might require some code changes. Once there are no more errors you can move to the next section to start the process of deploying and creating a new instance of your contract on chain.

Build and optimize the contract

Once the contract has been fully generated, the next step is to build and optimize your smart contract for deployment on-chain. This process compiles your code and generates the necessary WASM executable.

From within your project folder, run the following command to build the contract:

archway contracts build

This command will generate a .wasm file, which is the executable version of your smart contract. Once compiled, you can deploy this contract on-chain and create instances to begin using your smart contract. Make sure to review any warnings or errors that arise during the build process which can then be fed to ChatGPT to get those errors corrected.

Storing the contract on-chain

With your optimized .wasm file, you’re now ready to deploy the contract on-chain. Before proceeding, ensure that you have tokens in your account to cover the transaction fees.

  • For the Constantine testnet, you will need CONST tokens. These can be obtained for free through our testnet faucet available on Discord.
  • For Mainnet, you will need to acquire ARCH tokens, which are available through the Archway Connect platform.

To store the contract on-chain, execute the following command from the root of your project:

archway contracts store <contract-name>

Arguments:

  • <contract-name> (required): The name of the contract.

Create an instance of the contract

Once the contract is stored on-chain, you can instantiate it using the following command. Replace <contract-name> with the name of the contract you want to instantiate:

archway contracts instantiate <contract-name> --args '{"key":"my_value"}'

To determine the required fields for instantiation, refer to the msg.rs file and review the InstantiateMsg struct. Each field corresponds to a key, and you will need to set the appropriate value for each field.

If the InstantiateMsg struct for the lottery contract has no fields, you can execute the instantiation with an empty args JSON object.

For example, if the contract name is lottery, the following command would work:

archway contracts instantiate lottery --args '{}'

Execute transactions

This smart contract should allow for entering the lottery and also to execute a lottery draw. In the msg.rs file, you will find the ExecuteMsg enum, which defines the messages that can be called. Each message has associated fields that need to be provided when executing the transaction.

Entering the lottery

To send a transaction to enter the lottery, you would use the following command and replace the placeholder values with your data:

archway contracts execute lottery --args '{"enter": {}}'

In this example:

  • enter is set to message to be executed,
  • {} No parameters are required as this message should simply add your address to the list of participants

Draw lottery winner

To select a lottery winner you would use the Draw function like this:

archway contracts execute lottery --args '{"draw": {}}'

Query the contract

In addition to executing transactions, this contract should allow you to query data such as the selected winner or just the list of participants. The QueryMsg enum in the msg.rs file defines the available query messages.

Querying participants

To query the list of participants for example, use the following command:

archway contracts query smart lottery --args '{"get_participants": {}}'

The query will return a JSON object containing the list of all participants taking part in the lottery.