本文已参与「新人创作礼」活动,一起开启掘金创作之路。
solidity-MyToken.sol
使用erc20生成属于自己的代币。
pragma solidity >0.8.0;
import "http://47.99.87.207:8080/token/ERC20/ERC20.sol"; //引入erc20
contract MyToken is ERC20{
constructor (string memory name,string memory symbol)ERC20(name,symbol){
_mint(msg.sender,100*10**uint(decimals()));
}
}
solidity-TestContract.sol
一般使用这个合约用来做测试
pragma solidity ^0.8.3;
contract TestContract{
//公开声明变量i
uint pubilc i;
//每使用函数一次,i加j一次
function callMe(uint j)public{
i+=j;
}
function getData()public pure returns(bytes memory){
return abi.encodeWithSignature("callMe(uint256)",123);
}
function call() public {
(bool success, )=address(this).call( getData());
}
}
solidity-mapping.sol
智能合约map中的使用
pragma solidity ^0.8.3;
contract Mapping{
//映射
mapping(address =>uint)pubic myMap;
function get(address _addr)pubic view returns(uint){
return myMap[_addr];
}
function set(address _addr,uint _i)pubic{
myMap[_addr]=-i;
}
function remove(address _addr)pubic{
delete myMap[_addr];
}
}
contract NestedMapping{
mapping(address =>mapping(uint =>bool))pubic nested;
function get()address )addr1,uint _i}public view returns(bool){
return nested[_adddr1][_i];
}
function set(
address _addr1,
uint _i,
bool _boo
)public{
nested[_addr1][_i]=_boo;
}
function remove(address _addr1,uint _i)public{
delete nestedp[addr1][i];
}
}
solidity- Loop.sol
pragma solidity ^0.8.3;
contract Loop{
function loop()pubic{
for(uint i=0;i<10;i++){
if(i===3){
coutinue;
}
if(i==5){
break;
}
}
uint j;
while(j<10){
j++;
}
}
}
solidity-Enum.sol
合约中的枚举,和其他语言不大一样。一般返回的是数字。
pragma solidity ^0.8.3;
contract Enum{
enum Status{
Pengding,
Shipped,
Accepted,
Rejected,
Canceled
}
Status pubilc status;
function get()pubilc view returns (Status){
return staus;
}
function set(Status _status)pubic{
status =_status;
}
function cancel()pubic{
status=Status.Canceled;
}
function reset() poubic{
delete status;
}
}
solidity-Array.sol
合约中数组的使用
pragma solidity ^0.8.3
contract Array{
uint[] pubilc arr;
uint[] public arr2=[1,2,3];
uint[10] pubilc myFixedSizeArr;
function get(uint i) piblic view returns(uinnt){
return arr[i];
}
function getArr() pubic view returns (uint[] memory){
return arr;
}
function push(uint i) pubic {
arr.push(i);
}
funciton pop() pubic{
arr.pop();
}
function grtLength() pubic view returns (uint){
return arr.length;
}
function remove(uint index)pubic{
delete arr[index];
}
}
contract CompactArray{
uint[] pubic arr;
function test()pubic{
arr.push(1);
arr.push(2);
arr.push(3);
arr.push(4);
remove(1);
remove(2);
}
}
solidity-ifElse.sol
合约的判断流程
pragma solidity ^0.8.3;
contract IfElse{
function foo(uint x) public pure returns(uint){
if(x<10){
return 0;
}else if(x<20){
return 1;
}else{
return 2;
}
}
}
//简单的公开竞价合约
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;
contract SimpleAuction {
// Parameters of the auction. Times are either
// absolute unix timestamps (seconds since 1970-01-01)
// or time periods in seconds.
address payable public beneficiary;
uint public auctionEndTime;
// Current state of the auction.
address public highestBidder;
uint public highestBid;
// Allowed withdrawals of previous bids
mapping(address => uint) pendingReturns;
// Set to true at the end, disallows any change.
// By default initialized to `false`.
bool ended;
// Events that will be emitted on changes.
event HighestBidIncreased(address bidder, uint amount);
event AuctionEnded(address winner, uint amount);
// Errors that describe failures.
// The triple-slash comments are so-called natspec
// comments. They will be shown when the user
// is asked to confirm a transaction or
// when an error is displayed.
/// The auction has already ended.
error AuctionAlreadyEnded();
/// There is already a higher or equal bid.
error BidNotHighEnough(uint highestBid);
/// The auction has not ended yet.
error AuctionNotYetEnded();
/// The function auctionEnd has already been called.
error AuctionEndAlreadyCalled();
/// Create a simple auction with `biddingTime`
/// seconds bidding time on behalf of the
/// beneficiary address `beneficiaryAddress`.
constructor(
uint biddingTime,
address payable beneficiaryAddress
) {
beneficiary = beneficiaryAddress;
auctionEndTime = block.timestamp + biddingTime;
}
/// Bid on the auction with the value sent
/// together with this transaction.
/// The value will only be refunded if the
/// auction is not won.
function bid() external payable {
// No arguments are necessary, all
// information is already part of
// the transaction. The keyword payable
// is required for the function to
// be able to receive Ether.
// Revert the call if the bidding
// period is over.
if (block.timestamp > auctionEndTime)
revert AuctionAlreadyEnded();
// If the bid is not higher, send the
// money back (the revert statement
// will revert all changes in this
// function execution including
// it having received the money).
if (msg.value <= highestBid)
revert BidNotHighEnough(highestBid);
if (highestBid != 0) {
// Sending back the money by simply using
// highestBidder.send(highestBid) is a security risk
// because it could execute an untrusted contract.
// It is always safer to let the recipients
// withdraw their money themselves.
pendingReturns[highestBidder] += highestBid;
}
highestBidder = msg.sender;
highestBid = msg.value;
emit HighestBidIncreased(msg.sender, msg.value);
}
/// Withdraw a bid that was overbid.
function withdraw() external returns (bool) {
uint amount = pendingReturns[msg.sender];
if (amount > 0) {
// It is important to set this to zero because the recipient
// can call this function again as part of the receiving call
// before `send` returns.
pendingReturns[msg.sender] = 0;
if (!payable(msg.sender).send(amount)) {
// No need to call throw here, just reset the amount owing
pendingReturns[msg.sender] = amount;
return false;
}
}
return true;
}
/// End the auction and send the highest bid
/// to the beneficiary.
function auctionEnd() external {
// It is a good guideline to structure functions that interact
// with other contracts (i.e. they call functions or send Ether)
// into three phases:
// 1. checking conditions
// 2. performing actions (potentially changing conditions)
// 3. interacting with other contracts
// If these phases are mixed up, the other contract could call
// back into the current contract and modify the state or cause
// effects (ether payout) to be performed multiple times.
// If functions called internally include interaction with external
// contracts, they also have to be considered interaction with
// external contracts.
// 1. Conditions
if (block.timestamp < auctionEndTime)
revert AuctionNotYetEnded();
if (ended)
revert AuctionEndAlreadyCalled();
// 2. Effects
ended = true;
emit AuctionEnded(highestBidder, highestBid);
// 3. Interaction
beneficiary.transfer(highestBid);
}
}