Solidity
https://docs.soliditylang.org/en/latest/index.html
实现智能合约的语言, 面向对象。
智能合约是运行在以太坊平台上的程序, 管理账户的行为和以太状态。
是一种花括号语言,类似 C++ PYTHON JAVASCRIPT.
静态类型,支持继承 库 和用户自定义的类型。
Solidity is an object-oriented, high-level language for implementing smart contracts. Smart contracts are programs which govern the behaviour of accounts within the Ethereum state.
Solidity is a curly-bracket language. It is influenced by C++, Python and JavaScript, and is designed to target the Ethereum Virtual Machine (EVM). You can find more details about which languages Solidity has been inspired by in the language influences section.
Solidity is statically typed, supports inheritance, libraries and complex user-defined types among other features.
Example
https://docs.soliditylang.org/en/latest/introduction-to-smart-contracts.html#a-simple-smart-contract
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.4.16 <0.9.0; contract SimpleStorage { uint storedData; function set(uint x) public { storedData = x; } function get() public view returns (uint) { return storedData; } }
// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.4; contract Coin { // The keyword "public" makes variables // accessible from other contracts address public minter; mapping (address => uint) public balances; // Events allow clients to react to specific // contract changes you declare event Sent(address from, address to, uint amount); // Constructor code is only run when the contract // is created constructor() { minter = msg.sender; } // Sends an amount of newly created coins to an address // Can only be called by the contract creator function mint(address receiver, uint amount) public { require(msg.sender == minter); balances[receiver] += amount; } // Errors allow you to provide information about // why an operation failed. They are returned // to the caller of the function. error InsufficientBalance(uint requested, uint available); // Sends an amount of existing coins // from any caller to an address function send(address receiver, uint amount) public { if (amount > balances[msg.sender]) revert InsufficientBalance({ requested: amount, available: balances[msg.sender] }); balances[msg.sender] -= amount; balances[receiver] += amount; emit Sent(msg.sender, receiver, amount); } }
EVM
https://docs.soliditylang.org/en/latest/introduction-to-smart-contracts.html#index-6
以太坊虚拟机是智能合约的运行环境, 不但具有沙盒性质,而且是完全独立的,
以太坊的程序, 无法访问网络,文件系统, 以及其其它的以太坊进程, 甚至限制访问其它的智能合约。
The Ethereum Virtual Machine or EVM is the runtime environment for smart contracts in Ethereum. It is not only sandboxed but actually completely isolated, which means that code running inside the EVM has no access to network, filesystem or other processes. Smart contracts even have limited access to other smart contracts.
Arrays and Maps in Solidity
https://medium.com/coinmonks/array-and-map-in-solidity-a579b311d74b
pragma solidity ^0.5.3; contract{ uint[] myArray; function manipulateArray() external { myArray.push(1); // add an element to the array myArray.push(3); // add another element to the array myArray[0]; // get the element at key 0 or first element in the array myArray[0] = 20; // update the first element in the array //we can also get the element in the array using the for loop for (uint j = 0; j < myArray.length; j++) { myArray[j]; } } }
Assign an array as a map value:
pragma solidity ^0.5.3; contract Mapping { mapping(address => uint[]) scores; function manipulateArrayMap() external { scores[msg.sender].push(1); //assign a value; scores[msg.sender].push(2); //assign another element scores[msg.sender][0]; //access the element in the map array scores[msg.sender][1] = 5; //update an element in the map array in index 1 delete scores[msg.sender][0]; //delete the element in the index 0 of the map array } }
Assign another map as a map value:
pragma solidity ^0.5.3; contract Mapping { mapping(address => uint) balances; mapping(address => mapping(address => bool)) approved; function manipulateMapOfMap(spender) external { approved[msg.sender][spender] = true //assign a value to the approved map approved[msg.sender][spender]; //get the value of the map delete approved[msg.sender][spender] //delete the reference } }
Creating dynamic memory arrays
https://www.tutorialspoint.com/solidity/solidity_arrays.htm
pragma solidity ^0.5.0; contract test { function testArray() public pure{ uint len = 7; //dynamic array uint[] memory a = new uint[](7); //bytes is same as byte[] bytes memory b = new bytes(len); assert(a.length == 7); assert(b.length == len); //access array variable a[6] = 8; //test array variable assert(a[6] == 8); //static array uint[3] memory c = [uint(1) , 2, 3]; assert(c.length == 3); } }
Array Example
https://solidity-by-example.org/array/
// SPDX-License-Identifier: MIT pragma solidity ^0.7.6; contract Array { // Several ways to initialize an array uint[] public arr; uint[] public arr2 = [1,2,3]; // Fixed sized array, all elements initialize to 0 uint[10] public myFixedSizeArr; function get(uint i) public view returns (uint) { return arr[i]; } // Solidity can return the entire array. // But this function should be avoided for // arrays that can grow indefinitely in length. function getArr() public view returns (uint[] memory) { return arr; } function push(uint i) public { // Append to array // This will increase the array length by 1. arr.push(i); } function pop() public { // Remove last element from array // This will decrease the array length by 1 arr.pop(); } function getLength() public view returns (uint) { return arr.length; } function remove(uint index) public { // Delete does not change the array length. // It resets the value at index to it's default value, // in this case 0 delete arr[index]; } } contract CompactArray { uint[] public arr; // Deleting an element creates a gap in the array. // One trick to keep the array compact is to // move the last element into the place to delete. function remove(uint index) public { // Move the last element into the place to delete arr[index] = arr[arr.length - 1]; // Remove the last element arr.pop(); } function test() public { arr.push(1); arr.push(2); arr.push(3); arr.push(4); // [1, 2, 3, 4] remove(1); // [1, 4, 3] remove(2); // [1, 4] } }
Solidity by Example
https://solidity-by-example.org/
- Hello World
- First App
- Primitive Data Types
- Variables
- Reading and Writing to a State Variable
- Ether and Wei
- Gas and Gas Price
- If / Else
- For and While Loop
- Mapping
- Array
- Enum
- Structs
- Data Locations - Storage, Memory and Calldata
- Function
- View and Pure Functions
- Error
- Function Modifier
- Events
- Constructor
- Inheritance
- Shadowing Inherited State Variables
- Calling Parent Contracts
- Visibility
- Interface
- Payable
- Sending Ether - Transfer, Send, and Call
- Fallback
- Call
- Delegatecall
- Function Selector
- Calling Other Contract
- Creating Contracts from a Contract
- Try / Catch
- Import
- Library
- Hashing with Keccak256
- Verifying Signature
综合例子
https://github.com/kamalkishorm/Blockchain_SupplyChain
区块链学习
https://github.com/llSourcell/Learn_Blockchain_in_2_months