Solidity 051 ContractWithoutModifier

本文介绍了一个不使用修饰符的Solidity智能合约,展示了如何直接在函数体内实现基本的访问控制,仅允许合同所有者执行某些操作,如双倍和十倍数据修改。
摘要由CSDN通过智能技术生成

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9; 

// This contract demonstrates basic access control without using modifiers.
// It restricts certain actions to only the owner of the contract, but does so
// directly in the function bodies instead of using a reusable modifier.
contract ContractWithoutModifier {
    // State variable to store the owner's address. The owner is set
    // to the address that deploys the contract.
    address owner;

    // Public state variable to store an integer value. This value can be
    // modified by the contract's functions, under certain conditions.
    int public mydata;
    
    // Constructor that runs once upon contract deployment.
    // It sets the deploying address as the owner of the contract.
    constructor() {
        owner = msg.sender;
    }
    
    // Function to double the input value and assign it to 'mydata',
    // but only if the caller is the owner of the contract.
    // This function demonstrates a pattern of inline access control,
    // checking the sender's address directly within the function body.
    function AssignDoubleValue(int _data) public {
        if(msg.sender == owner) {
            mydata = _data * 2;
        }
        // If the caller is not the owner, this function does nothing.
        // In a real-world scenario, it might be better to give feedback
        // (e.g., by reverting the transaction) when unauthorized callers attempt this action.
    }
    
    // Function to multiply the input value by ten and assign it to 'mydata',
    // with the same ownership check as in AssignDoubleValue.
    // This function further demonstrates direct access control without modifiers.
    function AssignTenerValue(int _data) public {
        if(msg.sender == owner) {
            mydata = _data * 10;
        }
        // Similar to AssignDoubleValue, this function also silently ignores calls
        // from non-owner addresses, which may not be the best practice for access control.
    }
}
//Deploy:

  • 28
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值