SmartCogent Experience
Smart Contract Code Generation
- Create Project.
If you need to start by creating a Foundry project, you can proceed with the creation using the following prompts. Please specify the project path and name. Cogent will create a Foundry project in the specified directory. Ensure the path exists and there is no project with the same name. If you already have a project or do not need a Foundry project, skip this step.Create a contract project, with the path as the current project's root directory, and the name as test_cline_project.
- Generate Contract.
- Specify the contract file name and storage path.
- Describe the contract functionality. The more detailed, precise, and unambiguous the description, the higher the quality of the generated code.
Cogent will perform the following tasks: task planning, pre-generation, compilation, and auditing, until generating code that can be compiled and passed the audit. Each step is an asynchronous task, and you can view the task status in Cline.Generate the English Auction smart contract code, requirements as follows: 1. The contract is placed in the test_cline_project/src/ directory under the project root directory, with the contract name being EnglishAuction.sol. 2. Description of the English Auction smart contract code: - Seller deploys contract with an IERC721 NFT and sets optional minimum starting bid. Auction duration is configurable (default 10 days), with a "last-minute bid extension" rule: any bid within last 15 minutes extends auction by 15 minutes. - Bid mechanism: * Bidders must send ETH exceeding current highest bid by at least 5%. * Each bid creates a Bid struct containing bidder address, amount, and timestamp. * Failed bids are immediately refunded. - Withdrawal rules: * Non-winning bidders can withdraw funds anytime during/after auction. * Current highest bidder cannot withdraw until outbid. - Auction conclusion: * After final bid, seller must manually finalize within 24hrs to trigger NFT transfer. * 98% of winning bid goes to seller, 2% as protocol fee. * If no bids after auction period, seller can reclaim NFT. - Emergency features: * Auction can be canceled by seller before any bids. * All funds return to bidders if auction fails. - Events emitted for critical actions: BidPlaced, Withdrawal, AuctionExtended, Finalized.
Note:
The current limitations and our upcoming plans for the developer trial version:
- Currently, all users share a common resource pool which may cause task queuing and extended processing times. In the future, we will release a professional edition featuring dedicated private resource pools to guarantee enhanced performance and service stability.
- The current implementation only supports single-file generation. We are actively developing multi-file generation capabilities to handle complex engineering projects.
- Precise requirement specifications are crucial — ambiguous descriptions will generate unclear code. In future releases, we will implement interactive conversational AI capabilities to help developers iteratively refine requirements through dynamic dialogue.
Here are screenshots of each step during execution, for your reference:
- Task Decomposition: Cogent will call the
generate_contract_planner
tool to generate a plan.
- Pre-generation: Cogent will call the
pre_generate_contract
tool to pre-generate the code and save it to a specified path.
The task will be completed asynchronously, and task_query will be called to check if the task has been completed.
- Compilation: Cogent will call the
compile_generate_contract
tool to compile the code.
- Based on whether the compilation is successful or not, Cogent will call different tools.
- If the compilation is successful, Cogent will call the
contract_audit_planner
,audit_file
, andcontract_auto_repair
tools to audit the generated code.
- If the compilation fails, Cogent will call the
repair_contract
tool to fix the compilation error information.
- If the compilation is successful, Cogent will call the
Smart Contract Code Audit
Step 1: Contract Audit Plan Generation
First, you need to prepare a smart contract that requires a security audit. For example, you can create a file named test_contract.sol
, copy the following contract code into the file, and save it.
contract Preservation {
address public timeZone1Library;
address public timeZone2Library;
address public owner;
uint storedTime;
bytes4 constant setTimeSignature = bytes4(keccak256("setTime(uint256)"));
constructor(address _timeZone1LibraryAddress, address _timeZone2LibraryAddress) public {
timeZone1Library = _timeZone1LibraryAddress;
timeZone2Library = _timeZone2LibraryAddress;
owner = msg.sender;
}
function setFirstTime(uint _timeStamp) public {
timeZone1Library.delegatecall(abi.encodePacked(setTimeSignature, _timeStamp));
}
function setSecondTime(uint _timeStamp) public {
timeZone2Library.delegatecall(abi.encodePacked(setTimeSignature, _timeStamp));
}
}
contract LibraryContract {
uint storedTime;
function setTime(uint _time) public {
storedTime = _time;
}
}
We can use a very brief prompt to directly ask the language model to audit the contract file. For example, you can input audit @/test_contract.sol
directly in the cline.
First, the language model will extract the basic information of the contract, then prepare the parameters required by the audit planner tool, and call the MCP tool accordingly.
Here, we can click Approve to authorize the language model to make this tool call.
After the call, a contract audit plan task will be created. Our language model will receive a task ID. With this task ID, the language model can query the task, invoke the tool, and check the progress of the task.
Note:
Please continue by clicking the Approve button below.
Now, we can see that the language model has obtained the results returned by the audit plan tool.
Step 2: In-depth Contract Audit
Next, it can provide the audit plan and contract to the audit tool for an in-depth audit.
Note:
Please continue by clicking the Approve button below.
Once the in-depth audit is completed, you will receive a SARIF audit report. You can find and view the SARIF audit report in the same directory as the smart contract code.
Step 3: Contract Code Repair
Finally, the language model will automatically pass the audit report and contract file as parameters to the contract automatic repair tool.
Note:
Please continue by clicking the Approve button below.
After the repair task is completed, you can compare the code before and after the repair.
Compilation and Deployment
Prompt Words
SmartCogent supports both individual contract and Foundry contract project compilation and deployment. Query operation for Blockchain transactions is also supported. The following are the prompt words:
-
Compilation Prompts
# compile a new contract (1) compile contract xxx.sol # recompile a contract (2) recompile contract xxx.sol # compile a new foundry project (3) compile project [project name] # recompile a foundry project (4) recompile project [project name]
-
Deployment, Call and Query Prompts
# deploy specifized contract (1) deploy contract xxx.sol # deploy all contracts in project xxx (2) deploy all contracts in project [project name] # query transaction (3) query transtaction 0x1234... # call contract (4) call contract [contract address] [method name]
Contract Demo
Here we provide a test solidity file named preservation.sol
which contains two contracts: Preservation
and LibraryContract
for compilation and deployment.
contract Preservation {
address public timeZone1Library;
address public timeZone2Library;
address public owner;
uint256 storedTime;
bytes4 constant setTimeSignature = bytes4(keccak256("setTime(uint256)"));
constructor(address _timeZone1LibraryAddress, address _timeZone2LibraryAddress) public {
timeZone1Library = _timeZone1LibraryAddress;
timeZone2Library = _timeZone2LibraryAddress;
owner = msg.sender;
}
function setFirstTime(uint256 _timeStamp) public {
timeZone1Library.delegatecall(abi.encodePacked(setTimeSignature, _timeStamp));
}
function setSecondTime(uint256 _timeStamp) public {
timeZone2Library.delegatecall(abi.encodePacked(setTimeSignature, _timeStamp));
}
}
contract LibraryContract {
uint256 storedTime;
function setTime(uint256 _time) public {
storedTime = _time;
}
}
Contract Compilation and Deployment (signed by SmartCogent)
We take preservation.sol
as an example to show how to compile a single solidity file by SmartCogent.
- Contract Preparement.
Create a new contract file named preservation.sol and open the directory containing the contract file in VSCode.
- Start Contract Compilation.
Open Cline plugin, enter compile contract preservationin the dialog,then enter.
- View the compilation output.
Compile successfully. You can see four files generated, corresponding to the two contracts defined in the Solidity file.
- Configuration Preparement.
Make sure you have configue the blockchain RPC_URL and PRIVATE_KEY correctly. We promise your private key wont't be discloured.
- Start deploy two contracts.
Deploy two contracts Preservation and LibraryContract compiled before. We firstly deploy LibraryContract.- Open Cline plugin, enterdeploy contract LibraryContractin the dialog,then enter. When deploy successfully, copy the contract address. It will be taken as the Preservation constuctor input param.
- Open Cline plugin, enterdeploy contract Preservationin the dialog,then enter.
- Since the constructor of the Preservation contract requires two address-type parameters, copy the contract addresses generated earlier here, separated by a space.
- Preservation contract is successfully deployed.
- Open Cline plugin, enterdeploy contract LibraryContractin the dialog,then enter. When deploy successfully, copy the contract address. It will be taken as the Preservation constuctor input param.
- Query Transaction.
Open Cline plugin, enterquery tx 0x23f6f8b138da1aeaebed15f44c4e295145cb04876ac47162c853ab46b5e1c3d4
in the dialog,then enter.
Contract Compilation and Deployment (signed by MetaMask)
- Configuration Preparation:
The configuration file has set the key custody mode to MetaMask and configured the correct account address (refer to Sign By MetaMask). For MetaMask configuration, please refer to the MetaMask Configuration below. - Contract Preparation:
Prepare the test contract in advance and open vscode. The contract fileStorage.sol
is provided in the demonstration.
- Start Compilation and Deployment:
Open the cline plugin, start a new conversation, and enter in the dialog box: deploy contract Storage. The tool will automatically search the context of the directory currently opened in vscode for contract files named Storage. Of course, you can accurately locate the contract you need to compile and deploy by using the @ symbol.
- Execute Compilation:
Although I only mentioned deployment, when the model recognizes that there are noabi
andwasm
files for the Storage contract in the working directory, it will automatically trigger the compilation process and output the directory and files of the compilation output.
- Deployment Execution:
After compilation, the deployment process will be executed automatically. - Input Contract Constructor Parameters:
The tool will automatically read the parameters required by the contract constructor and prompt you to input (although it provides options for you to input, you can still customize the input). If there are multiple parameters in the constructor, you can input them together, separated by spaces.
- Construct Transaction:
SmartCogent organizes the bytecode and parameters required to deploy the contract, and calls the contract signature deployment tool. Since MetaMask is configured in the configuration file, SmartCogent will automatically open the default browser and wake up MetaMask at this time. (Note: MetaMask needs to be installed in the default browser).
- Wait for MetaMask Contract Deployment:
Before signing, MetaMask will pop up a window to prompt you to check and confirm the transaction.
- Confirm Deployment:
After confirming that there are no errors, click the Confirm button to complete the contract signing and deployment.
- Contract Successfully Deployed:
The interface will appear as follows after confirmation.
- Return to VS Code for More Information:
You can directly inputquery contract address
to get the contract address.
Updated about 19 hours ago