Solidity学习笔记-记录cryptozombies学习
2024-11-20
| 2024-11-21
0  |  阅读时长 0 分钟
type
status
date
slug
summary
tags
category
icon
password
😀
本文用来作为solidity学习过程中的知识点记录,以作备忘;知识记录于cryptozombies学习过程,链接:https://cryptozombies.io/
 
 

📝 知识点

算数运算符

solidity不像js是3个等于号,他只能==, !=

Solidity的风格指南推荐使用双引号而非单引号来表示字符串

import "./zombiefactory.sol";

Internal and External

In addition to public and private, Solidity has two more types of visibility for functions: internal and external.
internal is the same as private, except that it's also accessible to contracts that inherit from this contract.
external is similar to public, except that these functions can ONLY be called outside the contract — they can't be called by other functions inside that contract. We'll talk about why you might want to use external vs public later.

view修饰符

使用 view 修饰符的函数可以被称为 只读函数,它们在调用时不会消耗任何Gas,因为它们不涉及状态的改变。提高了代码的安全性和可预测性。

主要特征

  • 只读操作view 函数可以访问合约的存储数据,但不能修改它们。例如,可以读取一个状态变量的值,但不能更新它。
  • 编译器检查:如果在 view 函数中尝试修改状态变量或执行其他会改变状态的操作(如发送以太币、创建新合约等),编译器会发出警告。
  • 默认getter:所有自动生成的getter函数都是 view 函数。例如,对于一个公开的状态变量,Solidity会自动生成一个 view 函数来返回该变量的值。

msg.sender

用于表示调用当前合约函数的地址

主要特征

  • 动态性msg.sender 是动态的,意味着在不同的函数调用中,它的值可能会改变。例如,当一个合约(合约 A)调用另一个合约(合约 B)时,在合约 B 中的 msg.sender 将是合约 A 的地址,而不是最初发起交易的用户地址12
  • 用途:开发者使用 msg.sender 来跟踪交易的来源,确保只有特定地址能够执行某些操作。例如,可以通过检查 msg.sender 来限制某些功能仅由合约创建者或特定用户调用

interface

可以和写普通contract一样,只不过里面的function没有body; 虽然solidity支持合约形式的接口,但使用 interface 关键字来定义接口是更为标准和清晰的方法。这样不仅提高了代码的可读性,也确保了更严格的编译器检查,有助于避免潜在错误。

uint is uint256

uint is an alias for uint256 — they're the same thing.

Ownable Contracts

 make contracts Ownable — meaning they have an owner (you) who has special privileges copy一个基类,然后其他合约继承即可;
Warning: Dapp不代表就去中心化,不代表安全。使用之前务必审计源代码!!!
Note: Giving the owner special powers over the contract like this is often necessary, but it could also be used maliciously. For example, the owner could add a backdoor function that would allow him to transfer anyone's zombies to himself!
So it's important to remember that just because a DApp is on Ethereum does not automatically mean it's decentralized — you have to actually read the full source code to make sure it's free of special controls by the owner that you need to potentially worry about. There's a careful balance as a developer between maintaining control over a DApp such that you can fix potential bugs, and building an owner-less platform that your users can trust to secure their data.

Immutability of Contracts

 after you deploy a contract to Ethereum, it’s immutable, which means that it can never be modified or updated again.

Struct packing to save gas

 If you have multiple uints inside a struct, using a smaller-sized uint when possible will allow Solidity to pack these variables together to take up less storage. For example:

Time units

  now, secondsminuteshoursdaysweeks and years.

Modifier

 注意语法

Saving Gas With 'View' Functions

view functions don't cost any gas when they're called externally by a user.
This is because view functions don't actually change anything on the blockchain – they only read the data. So marking a function with view tells web3.js that it only needs to query your local Ethereum node to run the function, and it doesn't actually have to create a transaction on the blockchain (which would need to be run on every single node, and cost gas).
We'll cover setting up web3.js with your own node later. But for now the big takeaway is that you can optimize your DApp's gas usage for your users by using read-only external view functions wherever possible.
Note: If a view function is called internally from another function in the same contract that is not a view function, it will still cost gas. This is because the other function creates a transaction on Ethereum, and will still need to be verified from every node. So view functions are only free when they're called externally.

函数声明的详细解释

  1. function
      • 这是一个关键字,用于定义一个函数。在Solidity中,所有函数都以这个关键字开始。
  1. getZombiesByOwner
      • 这是函数的名称。函数名应具有描述性,以便清楚地表明该函数的作用。在这个例子中,getZombiesByOwner 表示该函数用于获取特定地址(所有者)的僵尸(Zombies)。
  1. (address _owner)
      • 这个部分是函数的参数列表。在括号内定义了该函数需要接受的输入参数。
      • address:这是参数的类型,表示该参数将接收一个以太坊地址。
      • _owner:这是参数的名称。这里使用下划线开头是一种常见的命名约定,用于区分参数和其他变量。
  1. external
      • 这是一个可见性修饰符,指示该函数只能被合约外部调用(如其他合约或外部账户)。这意味着不能在合约内部直接调用此函数。
      • 使用 external 可以节省Gas,因为在调用时不需要将参数复制到内存中。
  1. view
      • 这是一个状态修饰符,表示该函数不会修改合约的状态变量。view 函数可以读取状态变量,但不能更改它们。
      • 使用 view 修饰符可以提高代码的可读性,并且在调用此类函数时不会消耗Gas(如果是从外部调用)。
  1. returns(uint[] memory)
      • 这个部分指定了函数的返回类型。
      • uint[]:表示该函数将返回一个无符号整数数组(uint 是无符号整数类型)。
      • memory:这是数据存储位置修饰符,表示返回的数据将存储在内存中。Solidity有三种数据位置:storagememory 和 calldata。在这里,使用 memory 表示返回的数据是临时的,仅在当前调用上下文中有效

private vs public vs internal vs external

 private means it's only callable from other functions inside the contract; internal is like private but can also be called by contracts that inherit from this one; external can only be called outside the contract; and finally public can be called anywhere, both internally and externally.

view vs pure

 view tells us that by running the function, no data will be saved/changed. pure tells us that not only does the function not save any data to the blockchain, but it also doesn't read any data from the blockchain. Both of these don't cost any gas to call if they're called externally from outside the contract (but they do cost gas if called internally by another function).

Immutability of Contracts

 after you deploy a contract to Ethereum, it’s immutable, which means that it can never be modified or updated again.

Immutability of Contracts

 after you deploy a contract to Ethereum, it’s immutable, which means that it can never be modified or updated again.

Immutability of Contracts

 after you deploy a contract to Ethereum, it’s immutable, which means that it can never be modified or updated again.

🤗 总结归纳

总结文章的内容

📎 参考文章

  • 一些引用
  • 引用文章
 
💡
有关Notion安装或者使用上的问题,欢迎您在底部评论区留言,一起交流~

Immutability of Contracts

 after you deploy a contract to Ethereum, it’s immutable, which means that it can never be modified or updated again.
  • solidity
  • 使用Cloudflare作为域名解析monorepo下子包如何读取根目录的env环境变量
    Loading...