Table of contents
问题:Warning: SPDX license identifier not provided in source file
To become a smart contract that can run on Ethereum, the Solidity source code needs to go through the following steps
- The source code of the smart contract written in Solidity needs to be compiled into bytecode (Bytecode) with a compiler first , and the binary interface specification (Application Binary Interface, ABI for short) of the smart contract will be generated at the same time during the compilation process ;
- Deploy the bytecode to the Ethereum network through a transaction (Transaction) , and each successful deployment will generate a new smart contract account
- DApps written in Javascript usually use web3.js + ABI to call functions in smart contracts to read and modify data
Solidity compiler
Remix
- Remix is a web browser based Solidity IDE; available online without installing anything
- http://remix.ethereum.org
Solcs
- solc is one of the build targets of the Solidity source code library, it is Solidity's command-line compiler
- The Solidity compiler solicjs can be easily installed using npm
npm install -g solc
# -g是全局安装,也可以不加自己选择目的文件夹
问题:Warning: SPDX license identifier not provided in source file
问题:Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: <SPDX-License>" to each source file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more information.
Cause: The SPDX license was introduced from Solidity ^0.6.8. So you need to use SPDX-License-Identifier in your code
Solution: Add the first sentence of the .sol file
// SPDX-License-Identifier: MIT
问题:TypeError: Data location must be "memory" or "calldata" for parameter in function, but none was give
Reason: This is caused by the update of solidity version 0.5.0. You only need toadd memory after the string is used.
Solution:
Case number one
// SPDX-License-Identifier: MIT
// pragma solidity >0.4.22 <0.5.0;
pragma solidity ^0.8.3;
contract SimpleStorage{
uint myData;
function setData(uint newData) public{
myData = newData;
}
// 返回值类型需要加上 returns + 返回值类型, 例如 returns(uint)
// view 表示只读不写
function getData() public view returns(uint){
return myData;
}
// 返回两个数
function pureAdd(uint a,uint b) public pure returns(uint sum, uint _a){
return (a + b, a);
}
}
case two
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.3;
contract Car{
string brand; // 车牌
uint public price; // 价格
// 构造函数,合约创建时自动调用
constructor(string memory initBrand,uint initPrice){
brand = initBrand;
price = initPrice;
}
function setBrand(string memory newBrand) public {
brand = newBrand;
}
function getBrand() public view returns(string memory){
return brand;
}
function setPrice(uint newPrice) public {
price = newPrice;
}
}
case three
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.3;
contract Coin{
address public minter; // 铸币者
mapping(address => uint) public balances; // 所有账号
event Sent(address from,address to,uint amount);
/**
* 功能: 初始化
*/
constructor(){
minter = msg.sender;
}
/**
* 功能: 铸币
* @param receiver
* @param amount
*/
function mint(address receiver, uint amount) public{
// 调用铸币的人必须是规定的铸币人
require(msg.sender == minter);
// 铸币人的 账户余额 += amount
balances[receiver] += amount;
}
/**
* 功能: 发币
* @param receiver
* @param amount
*/
function send(address receiver, uint amount) public{
// msg.sender 代表调用这个函数的人
// 发币人的账户要大于等于 amount
require(balances[msg.sender] >= amount);
// 发币人的 账户余额 -= amount
balances[msg.sender] -= amount;
// 接收人的 账户余额 += amount
balances[receiver] += amount;
emit Sent(msg.sender, receiver, amount);
}
}
case four
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.3;
contract Try{
uint public a;
uint public b;
uint[] public data;
function f() public{
uint[] storage x = data;
x.push(2);
data = x;
}
}
contract FakeHoneyPot{
uint public luckyNum = 12;
struct Guess{
address player;
uint number;
}
Guess[] guessHistory;
address owner = msg.sender;
function guess(uint _num) public payable{
guessHistory.push(Guess({
player:msg.sender,
number:_num
}));
if(_num == luckyNum){
payable(msg.sender).transfer(msg.value * 2);
}
}
}