简体   繁体   English

在solidity中声明为视图错误的函数

[英]Function declared as view error in solidity

pragma solidity >=0.4.16 <0.9.0;

contract Wallet {

    uint balance = 0;
    
    bool a = true;
    
    
    function deposit(uint dep_amt) public {
        balance += dep_amt;
    }

    function withdraw (uint wdraw_amt) public view returns(string memory error){
        if(wdraw_amt<=balance){
         balance -= wdraw_amt;
        }
        
        else{
        error = "Insufficient Balance";
        return error;
        }

    }

    function getBalnce() public view returns (uint) {
       
        return balance;
    }
    

}

I'm very new to solidity and I'm trying to code a simple bank system which shows the balance and updates the balance according to the deposits and withdraws.我对solidity非常陌生,我正在尝试编写一个简单的银行系统,该系统显示余额并根据存款和取款更新余额。 I want to display an error in withdraw function when the amount to be withdrawn is greater than the balance but it shows an error saying:当要提取的金额大于余额时,我想在提取功能中显示错误,但它显示一条错误消息:

TypeError: Function declared as view, but this expression (potentially) modifies the state and thus requires non-payable (the default) or payable.

Is there a way to possibly show the error from the same function?有没有办法可能显示来自同一函数的错误?

If not, Please let me know an alternative.如果没有,请让我知道替代方案。

Thanks in advance!!.提前致谢!!。

A view function promises not to modify the contract state - such as not to modify storage variables. view函数承诺不修改合约状态——例如不修改存储变量。 See the docs for more info.有关更多信息,请参阅文档

But your code modifies the balance variable, which is a storage variable.但是您的代码修改了balance变量,这是一个存储变量。

function withdraw (uint wdraw_amt) public view returns(string memory error){
    if(wdraw_amt<=balance){
     balance -= wdraw_amt;    // <-- here
    }

and updates the balance according to the deposits and withdraws并根据存取款更新余额

Since one of your requirements is to actually update the storage variable, you need to remove the view modifier in order to be able to do that.由于您的要求之一是实际更新存储变量,因此您需要删除view修饰符才能做到这一点。

function withdraw (uint wdraw_amt) public returns(string memory error){

Users will then need to send a transaction (not a call) to execute the withdraw() function.然后用户需要发送交易(而不是调用)来执行withdraw()函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 证明你在 Solidity 中执行了视图 function - Prove that you executed view function in Solidity 为什么 memory 关键字在视图 function 中被提及 - Why memory keyword is mentioned in view function in solidity Solidity - 是否有 function 可以查看持有者地址列表以及它拥有的每个地址附加令牌 - Solidity - Is there function to view list of holders addresses and each address attached tokens that it owns 在另一个合约中调用 function - Solidity - Call a function in another contract - Solidity 可靠性:调用另一个合约的函数时出错。 错误:发送值时应向构造函数付款 - Solidity: Error when calling a function of another contract. Error: The constructor should be payable when you send value javascript函数正在返回promise,并且solidity函数中使用了promise,该函数会给出无效参数的错误 - The javascript function is returning a promise and the promise is used in the solidity function which gives the error of invalid arguments 传递函数无法如预期般可靠地工作 - Transfer function not working as expected in solidity Solidity setName 函数编程 (BitDegree) - Programming on Solidity setName function (BitDegree) 如何在solidity中使用传递函数 - How to use transfer function in solidity 关于 Solidity 的问题致电 function - Questions about Solidity call function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM