solidity
datatype
solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2 <0.9.0;
contract ArrayType {
uint8[3] data;
uint8[] ddata;
// function testStaticArray1() internal view returns (uint8[3] storage) {
// return data;
// }
// function testStaticArray2() public view returns (uint8[3] memory) {
// // data[0] = 25;
// return data;
// }
function testStaticArray() public returns (uint8[3] memory) {
data[0] = 25;
return data;
}
function testReadDynamicStorageArray()
public
view
returns (uint8[] memory)
{
return ddata;
}
function testWriteDynamicStorageArray() public {
ddata.push(32);
ddata.pop();
ddata.push(60);
}
function testMemoryDynamicArra(
uint8 size
) public pure returns (uint8[] memory) {
uint8[] memory mdata = new uint8[](size + 1);
// for (uint8 i = 0; i < size; i++) {
// mdata[i] = i;
// }
// mdata.push(67);
return mdata;
}
}
solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2 <0.9.0;
contract BasicType {
function testInt() public pure returns (uint256) {
uint8 i8_0 = 12;
uint8 i8_1 = 0;
uint8 i8_2 = 8;
// uint8 i8_3 = -1;
uint8 i8_4 = 255;
// uint8 i8_5 = 256;
// i8_4++;
uint8 x = 8;
uint16 y = 9;
// x = y; // error
// 强制类型转换
// x = uint8(y);
y = x;
uint256 i256_1 = 255;
i256_1++;
uint256 max = type(uint256).max;
return max;
// 115792089237316195423570985008687907853269984665640564039457584007913129639935
}
enum OrderState {
layorder,
paymen
}
function enumTest() public view returns (OrderState) {
OrderState state = OrderState.layorder;
// OrderState state = OrderState.paymen;
return state;
}
}
solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2 <0.9.0;
contract BytesAndString {
// 初始化方式 new
string name = "BytesAndString"; // new string(5);
bytes name1 = "BytesAndString1"; // new bytes(5);
// bytes memory name1 = bytes("BytesAndString1");
// bytes memory name1 = bytes(abi.encodePacked("BytesAndString1"));
// string 是不能修改的, bytes 是可以修改的
function testStringAndBytes() public view returns (string memory) {
// 初始化方式 new
// 为什么用 new ?
// struct 初始化为什么不用 new ?
string memory data = "xyz";
bytes memory data1 = "cd";
// 不同 location 的 memory 拷贝
data = name;
data1 = name1;
// 类型转换
data1 = bytes(data);
data = string(data1);
// 下标访问
bytes1 b = data1[0];
data1[0] = 0x88;
// bytes(data)[0] = 0x88;
// name1.push(0x00);
// data1.push(0x00);
return data;
}
// bytes 与 byte1[] 的区别
}
solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2 <0.9.0;
contract ComplexValueType {
function testAddress() public view returns (address) {
address addr = msg.sender;
// 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4
// 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4
return addr;
}
function testMyAddress() public view returns (address) {
address addr = address(this);
// 0x5FD6eB55D12E759a21C09eF703fe0CBa1DC9d88D
// 0x5FD6eB55D12E759a21C09eF703fe0CBa1DC9d88D
return addr;
}
// Contract Type
// function testContract() public view {
// ComplexValueType myContract = this;
// }
function testFixedByteArray() public pure returns (bytes3) {
// default: 0x000000
bytes3 data = 0x111111;
return data;
}
function testFixedByteArray1() public pure returns (bytes1) {
// default: 0x000000
bytes3 data = 0x111111;
// 0x11
bytes1 first = data[0];
return first;
}
}
solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2 <0.9.0;
contract MappingType {
mapping(string => uint8) public ages;
mapping(string => mapping(string => uint8)) public ages1;
function getAge(string memory name) public view returns (uint8) {
return ages[name];
}
function setAge(string memory name, uint8 age) public {
// mapping(string => uint8) storage myages = ages;
ages[name] = age;
}
// 一般规则: public menory calldata internal private 可以是 storage
// mapping: 只能是 storage
// => public 函数参数或者返回值不可能出现 mapping 类型
// 写一个 internal 或者 private 函数,传递 storage 的 mapping
// function setAge1(
// mapping(string => uint8) storage myages,
// string memory name,
// uint8 age
// ) internal {
// myages[name] = age;
// }
}
solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2 <0.9.0;
contract StructType {
struct Person {
string name;
uint8 age;
Home home;
// Person bestFriend;
// 测试递归
}
Person master;
struct Home {
string country;
string province;
string city;
}
// 测试 struct 作为参数
function readPerson() public view returns (Person memory) {
return master;
}
function writePerson(Person memory p) public {
master = p;
}
function writePersonName(string memory name) public {
master.name = name;
}
// 测试内存 struct. 对 struct 来说,memory 与 storage 并没有大的影响
function testMemoryStruct() public pure returns (Person memory) {
// Person memory p = Person(
// "zhangsan",
// 18,
// Home("China", "Shanghai", "Shanghai")
// );
// return p;
Person memory p;
p.name = "cd";
p.age = 28;
return p;
}
// 测试内存 location 为 storage 的局部变量
function testStorageLocalStruct() public view returns (Person memory) {
Person storage p = master;
// p.name = "dd";
// p.age = 29;
return p;
}
}
contracts
solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.2 <0.9.0;
contract BanlanceManager {
mapping(address => uint256) public balanceOf;
string public name = "MYDOLLAR";
string public symbol = "$";
uint8 public decimals = 4;
constructor(uint256 totalBalance) {
balanceOf[msg.sender] = totalBalance;
}
function transfer(
// address from,
address to,
uint256 amount
) public {
address from = msg.sender;
uint256 fb = balanceOf[from];
uint256 tb = balanceOf[to];
require(amount <= fb, "Insufficient balance");
fb -= amount;
tb += amount;
balanceOf[from] = fb;
balanceOf[to] = tb;
}
}
// 0x3261f5Fe789eB2780062B8cc3a98e947ac9F801B
// 0x380d495A265ACd2D1D6c506d28CD70FA7610f8B2
// 0x6Ad1B2dD054eB74F4CeBbC6D5681062A250189C5