• XSS.stack #1 – первый литературный журнал от юзеров форума

Статья ChatGPT & Solidity VS понимание кода и непринуждённый анализ ошибок

вавилонец

CPU register
Пользователь
Регистрация
17.06.2021
Сообщения
1 116
Реакции
1 265
Насмотревшись на хайп этого ChatGPT думал я а дай-ка я его солидити покормлю. Уснул. Проснулся, пишет знакомый. Говорит: "код посмотри, скам вроде скамом а $ снять не могу. Привет учусь понемногу по твоей роадмап👍Нашел один Скам контракт,пытаюсь запустить не идёт и не понимаю в чём Скам,можешь помочь?"
Код:
pragma solidity ^0.8.0;

contract RestrictedWithdrawal {
   address payable onlyWithdrawAddress; // address that is allowed to withdraw
   mapping(address => uint) public balances; // map to keep track of balances

   constructor() public {
       // constructor function to set the initial value of onlyWithdrawAddress
       onlyWithdrawAddress = msg.sender; // msg.sender is the address that deploys the contract
   }

   function deposit() public payable {
       // deposit function to accept TRX
       balances[msg.sender] += msg.value; // add the deposit amount to the sender's balance
   }

   function setWithdrawAddress(address payable newAddress) public {
       // function to set the address that is allowed to withdraw
       require(msg.sender == onlyWithdrawAddress, "Only the current withdraw address can change the withdraw address.");
       onlyWithdrawAddress = newAddress;
   }

   function withdraw(uint256 amount) public {
       // function to withdraw TRX
       require(msg.sender == onlyWithdrawAddress, "Only the withdraw address can withdraw TRX.");
       require(amount <= balances[onlyWithdrawAddress], "Insufficient balance.");
       onlyWithdrawAddress.transfer(amount); // transfer the requested amount to the withdraw address
       balances[onlyWithdrawAddress] -= amount; // update the balance
   }
}

Одним глазом глядя в код, другим в кружку кофе, не придав значения вижу что функции вывода не payable, а депозит payable думаю вот и решение так и написал. Проходит некоторое время:

"вообщем добавил в фунции что ты написал payable всеравно не работает транса "

Говорю, давай сюда

Код:
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.7;
contract RestrictedWithdrawal {
    address payable onlyWithdrawAddress; // address that is allowed to withdraw
    mapping(address => uint) public balances; // map to keep track of TRX token balances
    address payable owner;
    constructor()  {
        // constructor function to set the initial value of onlyWithdrawAddress
        owner = payable (msg.sender);
        onlyWithdrawAddress = payable(msg.sender); // msg.sender is the address that deploys the contract
    }
    function deposit() public payable {
        // deposit function to accept TRX token
        require(msg.value > 0, "Deposit amount must be greater than 0.");
        require(address(this).balance >= msg.value, "Insufficient TRX token balance in contract.");
        balances[msg.sender] += msg.value; // add the deposit amount to the sender's balance
        emit Deposit(msg.sender, msg.value);
    }
    function setWithdrawAddress(address payable newAddress) public {
        // function to set the address that is allowed to withdraw
        require(msg.sender == owner || msg.sender == onlyWithdrawAddress, "Only the current withdraw address or contract owner can change the withdraw address.");
        onlyWithdrawAddress = newAddress;
        emit WithdrawAddressChanged(newAddress);
    }
    function withdraw(uint256 amount) public payable {
        // function to withdraw TRX token
        require(msg.sender == onlyWithdrawAddress, "Only the withdraw address can withdraw TRX token.");
        require(amount <= balances[onlyWithdrawAddress], "Insufficient TRX token balance.");
        require(address(this).balance >= amount, "Insufficient TRX token balance in contract.");
        onlyWithdrawAddress.transfer(amount); // transfer the requested amount to the withdraw address
        balances[onlyWithdrawAddress] -= amount; // update the balance
        emit Withdrawal(msg.sender, amount);
    }
    function emergencyStop() public {
        require(msg.sender == owner, "Only the contract owner can perform this action.");
        emit EmergencyStop();
    }
    function autoWithdraw() public payable {
        require(msg.sender == owner, "Only the contract owner can perform this action.");
        uint256 amount = balances[msg.sender];
        require(amount > 0, "Insufficient TRX token balance.");
        require(address(this).balance >= amount, "Insufficient TRX token balance in contract.");
        payable(msg.sender).transfer(amount);
        balances[msg.sender] = 0;
        emit AutoWithdrawal(msg.sender, amount);
    }

    event Deposit(address indexed depositor, uint256 value);
    event Withdrawal(address indexed withdrawer, uint256 value);
    event WithdrawAddressChanged(address newAddress);
    event EmergencyStop();
    event AutoWithdrawal(address indexed withdrawer, uint256 value);
}

А я уже проснулся. И вижу что код полная чепуха. Каждая функци. ChatGPT не умеет походу не только скам писать но и payable правильно вставлять, хотя коментарии пишет, как надо) Возможно если научить его и выдет толк, но пока лучше сначала понть что происходит в коде, а потом учить ИИ))

2023-01-22 14_04_09-1screen.png


2023-01-22 14_04_55-Window-2screen.png


2023-01-22 14_05_36-3screen.png


Дай думаю закину в ремикс, что скажет:

chatGpt.png


То что он задеплоился - говорит о том что с документацией солидити то его уже познакомили. Но логика то совсем никуда, в 44 строках кода (без пустых строк и } поднял ) 12 ошибок.
НО, настроение то улучшилось)
 


Напишите ответ...
  • Вставить:
Прикрепить файлы
Верх