-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example Ethereum lottery contract for migration guide to Archway (#…
…544)
- Loading branch information
Showing
1 changed file
with
175 additions
and
0 deletions.
There are no files selected for viewing
175 changes: 175 additions & 0 deletions
175
.../1.smart-contract-development/3.migrate-ethereum-contract-to-archway-with-ai.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
--- | ||
objectID: developers_guides_migrate-ethereum-contract-to-archway-with-ai | ||
title: AI-Assisted Migration of Smart Contracts from Ethereum to Archway | ||
description: How to use AI assistance to build dapps on Archway | ||
parentSection: Developers | ||
parentSectionPath: /developers | ||
--- | ||
|
||
# AI-Assisted Migration: ethereum to archway smart contract conversion | ||
|
||
This tutorial will guide you through the process of migrating an **Ethereum smart contract** to **Archway** using AI assistance from ChatGPT. The AI will help generate all the necessary code and guide you through key adjustments for compatibility with Archway's CosmWasm environment. | ||
|
||
Before starting, ensure you've reviewed the [setup guide](/developers/getting-started/install) to install the tools required for Archway development, including the [Archway Developer CLI](/developers/getting-started/install#archway-developer-cli). | ||
|
||
## Key tips for effective ChatGPT interaction | ||
|
||
- **Be Specific**: Clearly outline what your Ethereum smart contract does when prompting ChatGPT and share code if possible. | ||
- **Follow Up**: Ask for code explanations or adjustments if needed. | ||
- **Iterate and Test**: Use feedback from testing to ask ChatGPT for revisions. | ||
- **Request Clarifications**: Don't hesitate to ask questions if any part of the code or process is unclear. | ||
|
||
## Ethereum contract | ||
|
||
Here's a simple Ethereum Solidity smart contract for a basic lottery system that we'll use for this guide: | ||
|
||
```solidity | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
contract Lottery { | ||
address public manager; | ||
address[] public participants; | ||
constructor() { | ||
manager = msg.sender; | ||
} | ||
function enter() public payable { | ||
require(msg.value > 0.01 ether, "Minimum ETH required to enter is 0.01"); | ||
participants.push(msg.sender); | ||
} | ||
function getParticipants() public view returns (address[] memory) { | ||
return participants; | ||
} | ||
function pickWinner() public restricted { | ||
require(participants.length > 0, "No participants in the lottery"); | ||
uint256 index = random() % participants.length; | ||
address winner = participants[index]; | ||
// Transfer the contract balance to the winner | ||
payable(winner).transfer(address(this).balance); | ||
// Reset the lottery for the next round | ||
participants = new address } | ||
function random() private view returns (uint256) { | ||
return uint256(keccak256(abi.encodePacked(block.difficulty, block.timestamp, participants))); | ||
} | ||
modifier restricted() { | ||
require(msg.sender == manager, "Only the manager can call this function"); | ||
_; | ||
} | ||
} | ||
``` | ||
|
||
## Creating a blank archway project | ||
|
||
Start by setting up a blank project as a foundation for your contract migration using the [Archway Developer CLI](/developers/developer-tools/developer-cli). Refer to the [setup guide](/developers/getting-started/setup#creating-a-blank-project) to initialize your project. | ||
|
||
## 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](https://chatgpt.com/g/g-g9aIUiOOS-archway-smart-contract-engineer) and is the recommended GPT for building smart contracts on Archway. | ||
|
||
## Converting the contract structure | ||
|
||
Ethereum contracts use `Solidity`, while Archway contracts use `Rust`. Let's use ChatGPT to help rewrite the Ethereum contract above: | ||
|
||
### Step 1: Translate Data Structures | ||
|
||
Prompt: | ||
|
||
> Convert the following Solidity data structures for a lottery smart contract into equivalent Rust structures compatible with CosmWasm's `cw-storage-plus` and `serde` for serialization. This will be stored in the `state.rs` file. Explain each structure's purpose and usage." | ||
> | ||
> -- Add Code Here -- | ||
Review and adjust the output to match your contract's requirements. | ||
|
||
### Step 2: Migrate core logic | ||
|
||
Prompt: | ||
|
||
> Translate the core logic of my Ethereum lottery contract into the `contract.rs` file. Ensure to include initialization and other necessary entry points. | ||
## Creating message types | ||
|
||
The `msg.rs` file defines the messages the contract handles: | ||
|
||
Prompt: | ||
|
||
> Generate the `msg.rs` file for the migrated lottery contract, including `InstantiateMsg`, `ExecuteMsg`, and `QueryMsg` enums. Ensure all are `serde` serializable. | ||
## Custom error handling | ||
|
||
Define custom errors for better troubleshooting: | ||
|
||
Prompt: | ||
|
||
> Generate the `error.rs` file for my migrated lottery contract, ensuring it aligns with the errors used in the `contract.rs` file and follows CosmWasm's error handling best practices. | ||
## Rectify any issues | ||
|
||
Compile the contract using: | ||
|
||
```bash | ||
archway contracts build | ||
``` | ||
|
||
For any build errors, copy the error message and prompt ChatGPT: | ||
|
||
> Here's the error I encountered while building my contract: [error message]. Suggest code fixes to resolve it. | ||
## Deploying and instantiating the contract | ||
|
||
Once your contract is error-free and compiled: | ||
|
||
1. Store it on-chain: | ||
|
||
```bash | ||
archway contracts store <contract-name> | ||
``` | ||
|
||
2. Instantiate the contract: | ||
|
||
```bash | ||
archway contracts instantiate <contract-name> --args '{}' | ||
``` | ||
|
||
Refer to your `InstantiateMsg` structure in `msg.rs` for required fields. | ||
|
||
## Executing transactions and queries | ||
|
||
Use `ExecuteMsg` and `QueryMsg` to interact with your contract: | ||
|
||
### Execute entry and lottery draw | ||
|
||
- Enter the lottery: | ||
|
||
```bash | ||
archway contracts execute lottery --args '{"enter": {}}' | ||
``` | ||
|
||
- Draw the winner: | ||
|
||
```bash | ||
archway contracts execute lottery --args '{"draw": {}}' | ||
``` | ||
|
||
### Query participants | ||
|
||
```bash | ||
archway contracts query smart lottery --args '{"get_participants": {}}' | ||
``` | ||
|
||
## Iterate with ChatGPT | ||
|
||
For additional features or debugging: | ||
|
||
Prompt: | ||
|
||
> Help me add a function to distribute rewards proportionally in my lottery contract. Explain how it integrates with the existing code. |