Solidity:如何使用chatGPT创建/优化/审计智能合约

Balut Catalin热度: 16082

人工智能有可能大大改善智能合约的开发和执行。

原文作者 :Balut Catalin

原文来源 :Medium

原文标题:Solidity: How to use chatGPT to create/optimize/audit smart contracts!

编译:hiiro,SevenUpDAO开发者公会

我知道这是一篇很长的文章,但它会改变你的编码方式,所以慢慢来。

现在是凌晨 5 点,我花了 16 个小时在chatGPT 上。现在,我在这里写一篇文章,因为如果我不分享这个,我就无法入睡。

这是一个关于如何与chatGPT进行交互的概念验证,基于我的经验。我希望在阅读本文之后,越来越多的开发者开始撰写文章,探讨我们应该如何讨论人工智能。

智能合约

人工智能有可能大大改善智能合约的开发和执行。以下是应将AI用于此目的的几个原因:

AI可以帮助自动化创建智能合约的过程,减少人类干预的需求和潜在的错误风险。AI可以协助验证和确认智能合约,确保它们准确无误,满足必要的标准。

AI可以帮助识别和解决智能合约潜在的漏洞,提高其安全性,减少黑客或其他攻击的风险。

AI可以协助监控和执行智能合约,确保它们按照既定计划执行,并且任何争议都能够快速而高效地解决。

总的来说,使用AI来开发和执行智能合约具有提高它们准确性、安全性和效率的潜力,使它们成为一种更可靠和有效的工具,适用于各种应用场景。

(*此文本由AI自己生成)

现在,如果你问这个有趣的家伙,他会说,“你可能需要咨询一位软件开发人员……”但是等一下,chatGPT先生,我就是软件开发人员啊!这意味着我在问错问题了!

在询问了24,173,431个问题之后,我意识到了一个至关重要的问题。通过花时间了解AI“思考”的方式,你可以提高与它的沟通并得到更有用的回答。这可以节省您的时间、减少挫败感,并避免潜在的智能合约开发项目中出现的障碍。因此,不要犹豫,花点时间学习如何有效地“与chatGPT交流”,你将踏上成功之路。

智能合约

因此,通过提出正确的问题,它说“兄弟,我可以帮你”。

智能合约

这个AI家伙是神一样的。它可以为你编写代码,优化智能合约,审计智能合约,编写单元测试,编写脚本,前端,一切

旅程开始了...

我将介绍一些用例以及我如何与它讨论以解决我的技术问题:

从头开始生成智能合约

我的第一次尝试是创建一个带有非常特定组件的投票应用程序。我想要一个:

  • Solidity 智能合约
  • 使用 Pragma 版本0.8.0
  • 投票应用程序
  • 具有创建投票会话的所有者

智能合约

这是 AI 生成的代码:

pragma solidity 0.8.0;

contract Voting {

// The address of the contract owner address public owner;

// The name of the voting session string public votingSessionName;

// The total number of votes uint256 public totalVotes;

// The mapping of voters to their vote counts mapping (address => uint256) public votes;

// The constructor that sets the owner of the contract constructor() public { owner = msg.sender; }

// The function that allows the owner to create a new voting session function createVotingSession(string memory _votingSessionName) public { require(msg.sender == owner, "Only the owner can create a voting session."); votingSessionName = _votingSessionName; }

// The function that allows a voter to cast their vote function vote() public { votes[msg.sender]++; totalVotes++; }

}

这是来自AI的信息:

智能合约

现在我想要更多。我想使用Openzeppelin作为所有权,我想有签名进行投票。

智能合约

AI理解错误的签名验证,所以我不得不坚持一点。

以下是我们的讨论:

智能合约

以下是AI生成的代码:

pragma solidity 0.8.0;

import "https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/ownership/Ownable.sol";

contract Voting is Ownable {

// The name of the voting session string public votingSessionName;

// The total number of votes uint256 public totalVotes;

// The mapping of voters to their vote counts mapping (address => uint256) public votes;

// The function that allows the owner to create a new voting session function createVotingSession(string memory _votingSessionName) public onlyOwner { votingSessionName = _votingSessionName; }

// The function that allows a voter to cast their vote function vote(bytes32 _signature) public { require( _signature == keccak256(abi.encodePacked(votingSessionName)), "Invalid signature." ); votes[msg.sender]++; totalVotes++; }

}

以下是代码下方的文本:

智能合约

嗯,那关于一些元交易呢:

智能合约

但它有点失败,所以我以一种 AI 能够理解我要求的方式重新表述:

智能合约

*AI老哥说“你是对的”,我笑死。

这是代码:

pragma solidity 0.8.0;

import "https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/ownership/Ownable.sol";import "https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/GSN/Forwarder.sol";

contract Voting is Forwarder, Ownable {

// The name of the voting session string public votingSessionName;

// The total number of votes uint256 public totalVotes;

// The mapping of voters to their vote counts mapping (address => uint256) public votes;

// The constructor that sets the owner of the contract and the Forwarder contract address constructor(address _forwarder) public { owner = msg.sender; Forwarder(_forwarder); }

// The function that allows the owner to create a new voting session function createVotingSession(string memory _votingSessionName) public onlyOwner { votingSessionName = _votingSessionName; }

// The function that allows a voter to cast their vote function vote() public { require( forwarderCanCall(msg.sender), "This function can only be called through the Forwarder contract." ); votes[msg.sender]++; totalVotes++; }

}

以下是文字:

智能合约

等等...现在,这只是一个例子,证明如果你一点点告诉chatGPT正确的单词,你只需要写文本,而不是代码。我惊呆了。

优化智能合约

我拿了一个由我编写的智能合约,该合约正在进行中,其要求与上面生成的投票应用程序类似,我告诉chatGPT对其进行优化。

智能合约

*我写了“优化这个智能合约”+粘贴原始代码。

智能合约

我重置了线程,再次问他。回应:

智能合约

但是我遇到了这个错误,所以我无法发布代码(ChatGPT 中一个非常令人沮丧的错误):


智能合约

总之,如果你有正确的知识,人工智能可以给你优化代码的好主意。我还尝试了“删除冗余代码”和“重写注释”。我让你自己试试。

单元测试

智能合约

*文本+复制粘贴原始代码。

智能合约

我认为因为它必须编写大量代码,所以出现了神秘的“network error”。

第三次当我按下“重试”时,它给了我这个响应。我认为它某种程度上知道这是太多的代码,并向我提供了如何解决的指示。

智能合约

智能合约

智能合约

以此类推,AI写了所有的测试。

审计

智能合约

响应:

智能合约

哼!所以我仍然有用。谢谢聊天!. . . . .开玩笑我,我只是问错了!


智能合约

带有一些错误标志但仍然是响应的响应:

智能合约

让我们看看它对重入的看法:

智能合约

我又试了一次:

智能合约

很酷的信息。谢谢,人工智能伙计。

声明:本文为入驻“MarsBit 专栏”作者作品,不代表MarsBit官方立场。
转载请联系网页底部:内容合作栏目,邮件进行授权。授权后转载时请注明出处、作者和本文链接。未经许可擅自转载本站文章,将追究相关法律责任,侵权必究。
提示:投资有风险,入市须谨慎,本资讯不作为投资理财建议。
免责声明:本文不构成投资建议,用户应考虑本文中的任何意见、观点或结论是否符合其特定状况,及遵守所在国家和地区的相关法律法规。
关键字:智能合约