diff --git a/content/2.developers/3.guides/15.ai-assisted-development/1.smart-contract-development/2.build-smart-contracts-cursor-ai.md b/content/2.developers/3.guides/15.ai-assisted-development/1.smart-contract-development/2.build-smart-contracts-cursor-ai.md index c32265b3..87db1e25 100644 --- a/content/2.developers/3.guides/15.ai-assisted-development/1.smart-contract-development/2.build-smart-contracts-cursor-ai.md +++ b/content/2.developers/3.guides/15.ai-assisted-development/1.smart-contract-development/2.build-smart-contracts-cursor-ai.md @@ -6,9 +6,9 @@ parentSection: Developers parentSectionPath: /developers --- -# Overview +# Build smart contracts with Cursor AI -This guide will take you through the process of building a "Simple Voting" smart contract with the Assistance of [Cursor AI](https://www.cursor.com). Cursor AI is an AI-powered code editor designed to enhance the software development process by leveraging artificial intelligence to improve coding productivity, reduce development time, and assist with code generation, error detection, and real-time collaboration. Go to https://www.cursor.com to download and install the application. +This guide will take you through the process of building a "Simple Voting" smart contract with the Assistance of [Cursor AI](https://www.cursor.com). Cursor AI is an AI-powered code editor designed to enhance the software development process by leveraging artificial intelligence to improve coding productivity, reduce development time, and assist with code generation, error detection and optimization. Go to https://www.cursor.com to download and install the application. ## Features of the voting smart contract @@ -33,7 +33,7 @@ The main files we’ll be working on include: - **`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 voting smart contract and the prompts that will be submited to Cursor AI for it to generate the necessary code. +In each section, we will walk through what needs to be done in the file, providing guidance and instructions for building out the voting smart contract and the prompts that will be submited to `Cursor AI` for it to generate the necessary code. ## State @@ -52,7 +52,7 @@ In this section, we'll set up the state management for our voting smart contract > Generate the data structures for a voting smart contract. The contract should include: > > - A structure to represent a poll, including fields for the poll ID, the question, a list of options, and a corresponding list to track votes. -> - A mapping of all polls, where the poll ID serves as the key. +> - A mapping of all polls. > - Ensure that the structures are serializable using `serde` and ready to be stored with `cw-storage-plus`. > - Include comments explaining the purpose of each data structure. @@ -73,18 +73,18 @@ Once the `state.rs` file has been generated, the next step is to have Cursor AI > Generate the `contract.rs` file for a voting smart contract. The contract should include: > -> - An `instantiate` function to initialize the contract. -> - An `execute_create_poll` function to create a new poll, accepting a poll ID, a question, and a list of options. -> - An `execute_vote` function to allow users to vote on a poll by specifying the poll ID and their chosen option. -> - A `query_poll_results` function that allows anyone to query the current results of a poll. +> - A function to initialize the contract. +> - A function to create a new poll, accepting a poll ID, a question, and a list of options. +> - A function to allow users to vote on a poll by specifying the poll ID and their chosen option. +> - A function that allows anyone to query the current results of a poll. > > Use CosmWasm's standard response and error handling patterns, ensuring that each function interacts with the state using the structures defined in `state.rs`. -Click the `generate` button: +Click the `generate` button and you should see something like the following: ![](/images/docs/cursor_ai/contract/prompt.png) -Click the `Accept` button and save the file. You may encounter some errors because the necessary messages haven't been created yet. We will address these errors in the next step. +Click the `Accept` button and save the file. You may encounter some errors in the file because the necessary messages haven't been created. We will address these errors in the next step. ## Messages @@ -151,7 +151,7 @@ There may be errors in the `contract.rs` and other files. To address these, hove ![](/images/docs/cursor_ai/errors/ai-fix-in-chat.png) -For the solution in the chat on the right click the `Apply` button: +For the solution, in the chat on the right click the `Apply` button: ![](/images/docs/cursor_ai/errors/apply.png) @@ -159,10 +159,108 @@ You will then need to click the `Accept` button to apply the changes to the file ![](/images/docs/cursor_ai/errors/accept.png) -## Summary +## Build and optimize the contract -In this tutorial, we walked through the process of building a simple voting smart contract using Cursor AI and the Archway Developer CLI. We covered key steps such as generating the `state.rs` file to manage contract data, creating the core contract logic in `contract.rs`, defining messages in `msg.rs`, and handling custom errors in `error.rs`. Throughout the process, we utilized AI to assist with code generation, fixing potential errors, and ensuring smooth development. +Once all errors have been resolved, 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. -By the end of this guide, you should have a fully functional voting smart contract that allows users to create polls, vote, and query results. This contract can be a foundation for more complex dapps, expanding on the principles we covered. +From within your project folder, run the following command to build the contract: -With your contract complete, you’re now ready to deploy and test it on the Archway blockchain or continue developing additional features. \ No newline at end of file +```sh +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 to ensure the contract is properly optimized for on-chain performance. + +## 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](https://discord.gg/archwayhq). +- For **Mainnet**, you will need to acquire `ARCH` tokens, which are available through the [Archway Connect](https://connect.archway.io/get-arch) platform. + +To store the contract on-chain, execute the following command from the root of your project: + +```bash +archway contracts store CONTRACT +``` + +**Arguments:** + +- `CONTRACT` (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 **\** with the name of the contract you want to instantiate: + +```bash +archway contracts instantiate --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. + +In this particular case, since the `InstantiateMsg` struct for the voting contract is most likely empty, you can simply instantiate it with an empty JSON object. + +For example, if the contract name is `voting`, the following command would work: + +```bash +archway contracts instantiate voting --args '{}' +``` + +## Execute transactions + +This smart contract allows for executing two main transactions: creating a poll and voting on a poll. 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. + +To see the required fields for each transaction, refer to the `ExecuteMsg` enum in `msg.rs`. For example: + +- The `CreatePoll` message might require the following fields: + - `poll_id`: A unique identifier for the poll. + - `question`: The question or prompt for the poll. + - `options`: A list of options that users can vote on. + +- The `Vote` message: + - `poll_id`: The unique identifier of the poll. + - `vote_option`: The index (position) of the chosen option in the list of options. + +### Creating a poll + +To send a transaction to create a poll, you would use the following command and replace the placeholder values with your data: + +```bash +archway contracts execute voting --args '{"create_poll": {"poll_id": "poll_1", "question": "Your favorite programming language?", "options": ["Rust", "Go", "Solidity"]}}' +``` + +In this example: +- `poll_id` is set to `"poll_1"`, +- `question` is `"Your favorite programming language?"`, and +- `options` are `["Rust", "Go", "Solidity"]`. + +### Voting on a poll + +To send a vote on an existing poll, use the `Vote` function like this: + +```bash +archway contracts execute voting --args '{"vote": {"poll_id": "poll_1", "vote_option": 0}}' +``` + +In this example: +- `poll_id` is `"poll_1"`, and +- `vote_option` is `0`, which corresponds to the first option, `"Rust"`. + +## Query the contract + +In addition to executing transactions, this contract allows you to query data such as poll results. The `QueryMsg` enum in the `msg.rs` file defines the available query messages. For this contract, the primary query is for retrieving poll results. + +To find the required fields for querying, refer to the `QueryMsg` enum in `msg.rs`. In this case, the `PollResults` query requires: +- `poll_id`: The unique identifier of the poll you want to query. + +### Querying poll results + +To query the results of a specific poll, use the following command, replacing the placeholder `poll_id` with the ID of the poll you want to retrieve results for: + +```bash +archway contracts query voting --args '{"poll_results": {"poll_id": "poll_1"}}' +``` + +In this example, the query fetches the results for the poll with `poll_id` set to `"poll_1"`. + +The query will return a JSON object containing the current vote counts for each option in the specified poll. \ No newline at end of file