solidity-essentials
所属分类:以太坊
开发工具:Solidity
文件大小:20415KB
下载次数:0
上传日期:2023-04-17 16:28:58
上 传 者:
sh-1993
说明: Solidity编程婴儿示例。。。
(Solidity programming baby examples...)
文件列表:
Create2.sol (1403, 2023-06-09)
ERC721.sol (6927, 2023-06-09)
EnglishAuction.sol (2294, 2023-06-09)
LICENSE (1069, 2023-06-09)
MyLedger.sol (329, 2023-06-09)
Solidity programming essentials a beginner’s guide to build smart contracts for Ethereum and blockchain (Ritesh Modi) (z-lib.org).pdf (24245957, 2023-06-09)
Weth.sol (721, 2023-06-09)
abacAccessControl.sol (2210, 2023-06-09)
# Solidity-Baby Steps...
The best path for earning Solidity is to approach examples.
Here are baby examples. Typing one-by-one is recommended.
References:
[MyBlog](https://dev.to/yongchanghe/tutorial-play-with-an-auction-demo-based-on-erc721-3kpb) explanation for using EnglishAuction and testing.
[examples](https://solidity-by-example.org/)
[videoResource](https://www.youtube.com/watch?v=6aQErpWPLbk&t=2s)
#### Example-1
updateBalance & getBalance
```solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract MyLedger {
mapping(address => uint) public balances;
function updateBalance(uint newBal) public {
balances[msg.sender] = newBal;
}
function getBalance() public view returns(uint) {
return balances[msg.sender];
}
}
```
#### Example-2
Inherited from Square
```solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import "@openzeppelin/contracts/utils/Strings.sol";
contract Shape {
uint height;
uint width;
constructor(uint _height, uint _width) {
height = _height;
width = _width;
}
}
contract Square is Shape {
constructor(uint h, uint w) Shape(h, w) {}
function getHeight() public view returns(uint) {
return height;
}
function getArea() public view returns(uint) {
return height * width;
}
}
```
> When using Remix, assigning height and width is needed before deployment.
#### Example-3
Owner & Purchase
```solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import "@openzeppelin/contracts/utils/Strings.sol";
contract Owner {
address owner;
constructor() {owner = msg.sender;}
modifier onlyOwner {
require(msg.sender == owner);
_;
}
}
contract Purchase is Owner {
mapping(address => bool) purchasers;
uint price;
constructor(uint _price) {
price = _price;
}
function purchase() public payable {
purchasers[msg.sender] = true;
}
function setPrice(uint _price) public onlyOwner {
price = _price;
}
function getPrice() public view returns(uint) {
return price;
}
}
```
#### Example-4
Simple Storage for unsigned integer and string data type
```solidity
// SPDX-License-Identifier:GPL-3.0
pragma solidity >=0.4.16 < 0.9.0;
contract SimpleStorage {
uint storedData;
function setData(uint x) public {
storedData = x;
}
function getData() public view returns (uint) {
return storedData;
}
}
contract StringStorage {
string storedString;
function setString(string memory x) public {
storedString = x;
}
function getString() public view returns (string memory) {
return storedString;
}
}
```
#### Example-5
Struct example
```solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Storage1{
struct People {
string name;
uint256 favoriteNumber;
}
People[] public people;
mapping(string => uint256) public nameToFavoriteNumber;
function appPerson(string memory _name, uint256 _favoriteNumber) public {
people.push(People(_name, _favoriteNumber));
nameToFavoriteNumber[_name] = _favoriteNumber;
}
}
```
#### Example-6
Smart Funding...
```solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.7;
contract CryptoKids {
// owner DAD
address owner;
event LogKidFundingReceived(address addr, uint amount, uint contractBalance);
constructor() {
owner = msg.sender;
}
// define Kid
struct Kid {
address payable walletAddress;
string firstName;
string lastName;
uint releaseTime;
uint amount;
bool canWithdraw;
}
// add kid to contract
Kid[] public kids;
modifier onlyOwner() {
require(msg.sender == owner, "Only the owner can add kids");
_;
}
function addKid(address payable walletAddress, string memory firstName, string memory lastName, uint releaseTime, uint amount, bool canWithdraw) public onlyOwner {
kids.push(Kid(
walletAddress,
firstName,
lastName,
releaseTime,
amount,
canWithdraw
));
}
function balanceOf() public view returns(uint) {
return address(this).balance;
}
// deposit funds to contract, especially to a kid's account
function deposit(address walletAddress) payable public {
addToKidsBalance(walletAddress);
}
function addToKidsBalance(address walletAddress) private {
for(uint i = 0; i < kids.length; i++) {
if(kids[i].walletAddress == walletAddress) {
kids[i].amount += msg.value;
emit LogKidFundingReceived(walletAddress, msg.value, balanceOf());
}
}
}
function getIndex(address walletAddress) view private returns(uint) {
for(uint i = 0; i < kids.length; i++) {
if (kids[i].walletAddress == walletAddress) {
return i;
}
}
return 999;
}
// kid checks if able to withdraw
function availableToWithdraw(address walletAddress) public returns(bool) {
uint i = getIndex(walletAddress);
if (block.timestamp > kids[i].releaseTime) {
kids[i].canWithdraw = true;
return true;
}
return false;
}
// withdraw the money
function withdraw(address walletAddress) payable public {
uint i = getIndex(walletAddress);
require(msg.sender == kids[i].walletAddress, "You must be the very kid to withdraw ETH");
require(kids[i].canWithdraw == true, "You are not be able to withdraw at this time");
kids[i].walletAddress.transfer(kids[i].amount);
}
}
```
#### Example-7
```solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.7;
contract Greeter {
address creator;
string greeting;
constructor(string memory _greeting) {
creator = msg.sender;
greeting = _greeting;
}
function greet() public view returns (string memory) {
return greeting;
}
function getBlockNumber() public view returns (uint) {
return block.number;
}
function setGreeting(string memory _newgreeting) public {
greeting = _newgreeting;
}
function getCreatorBalance() public view returns (uint) {
return creator.balance;
}
}
```
#### Example-8
basic messages
```solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract basicInfoGetter {
address creator;
constructor()
{
creator = msg.sender;
}
function minerAddress() public view returns (address) // get CURRENT block miner's address,
{ // not necessarily the address of the miner when this block was born
return block.coinbase;
}
function difficulty() public view returns (uint)
{
return block.difficulty;
}
function gaslimit() public view returns (uint) // the most gas that can be spent on any given transaction right now
{
return block.gaslimit;
}
function blockNumber() public view returns (uint)
{
return block.number;
}
function timestamp() public view returns (uint) // returns current block timestamp in SECONDS (not ms) from epoch
{
return block.timestamp; // also "now" == "block.timestamp", as in "return now;"
}
function msgData() public pure returns (bytes memory) // The data of a call to this function. Always returns "0xc8e7ca2e" for me.
{ // adding an input parameter would probably change it with each diff call?
return msg.data;
}
function msgSender() public view returns (address) // Returns the address of whomever made this call
{ // (i.e. not necessarily the creator of the contract)
return msg.sender;
}
function msgValue() public payable returns (uint) // returns amt of wei sent with this call
{
return msg.value;
}
// check balance of the contract
function balanceOf() public view returns(uint) {
return address(this).balance / 1e18;
}
/*** A note about gas and gasprice:
Every transaction must specify a quantity of "gas" that it is willing to consume (called startgas),
and the fee that it is willing to pay per unit gas (gasprice). At the start of execution,
startgas * gasprice ether are removed from the transaction sender's account.
Whatever is not used is immediately refunded.
*/
function msgGas() public view returns (uint)
{
return gasleft();
}
function txGasprice() public view returns (uint) // "gasprice" is the amount of gas the sender was *willing* to pay. 50000000 for me. (geth default)
{
return tx.gasprice;
}
function txOrigin() public view returns (address) // returns sender of the transaction
{ // What if there is a chain of calls? I think it returns the first sender, whoever provided the gas.
return tx.origin;
}
function contractAddress() public view returns (address)
{
return creator;
}
function contractBalance() public view returns (uint)
{
return creator.balance;
}
}
```
#### Example-9
> increment: transactions will be made when clicking 'increment'; result will be shown when clicking 'getIterration'.
```solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Increment {
address creator;
uint iteration;
constructor() {
creator = msg.sender;
iteration = 0;
}
function increment() public {
iteration += 1;
}
function getIteration() public view returns (uint) {
return iteration;
}
function ContractBal() public view returns (uint) {
return address(this).balance / 1e18;
}
function getBalance() public view returns (uint) {
return creator.balance / 1e18;
}
function valueToContract() public payable returns (uint) {
return msg.value;
}
}
```
#### Example-10
> increment2: increment (integer) number can be defined by user
```solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Increment2 {
address owner;
int iteration;
string whatHappened;
constructor() {
owner = msg.sender;
iteration = 0;
whatHappened = "constructor executed";
}
function increment(int howmuch) public {
if (howmuch == 0) {
iteration += 1;
whatHappened = "howmuch was 0. Incremented by 1.";
}
else {
iteration += howmuch;
whatHappened = "howmuch was not 0. Incremented by its value";
}
return;
}
function getWhatHappened() public view returns (string memory) {
return whatHappened;
}
function getIteration() public view returns (int) {
return iteration;
}
}
```
#### Example-11
Increment3
```solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Increment3 {
address creator;
int iteration;
string whatHappened;
int customValue;
constructor () {
creator = msg.sender;
iteration = 0;
whatHappened = "constructor initialized";
}
function increment(int incre, int _customValue) public {
customValue = _customValue;
if (incre == 0) {
iteration += 1;
whatHappened = "Increment was 0, Incremented by 1. Custom Value also set";
}
else {
iteration += incre;
whatHappened = "Increment was not 0, Incremented by its value. Custom Value also set";
}
return;
}
function getInfo() public view returns (string memory) {
return whatHappened;
}
function getTotalIter() public view returns (int) {
return iteration;
}
}
```
#### Example-12
```solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract msgExaminer {
address owner;
address miner;
constructor() {
owner = msg.sender;
miner = 0x8945A1288dc78A6D8952a92C77aEe6730B414778;
}
function getMsgData() public pure returns (bytes calldata) {
return msg.data;
}
function getMsgGas() public view returns (uint) {
return gasleft();
}
function getMsgVal() public payable returns (uint) {
return msg.value;
}
function txGasprice() public view returns (uint) // "gasprice" is the amount of gas the sender was *willing* to pay. 50000000 for me. (geth default)
{
return tx.gasprice;
}
function txOrigin() public view returns (address) // returns sender of the transaction
{ // What if there is a chain of calls? I think it returns the first sender, whoever provided the gas.
return tx.origin;
}
function minerAddress() public view returns (address) // get CURRENT block miner's address,
{ // not necessarily the address of the miner when this block was born
return block.coinbase;
}
}
```
#### Example-13
Add, update and delete student struct type
```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract StudentList {
struct Student{
string name;
uint age;
}
//
Student[] public students;
function create(string memory _name, uint _age) public {
students.push(Student({name:_name, age:_age}));
}
function updateAge(uint _index, uint _age) public {
Student storage studentUpdate = students[_index];
studentUpdate.age = _age;
}
function updateName(uint _index, string memory _name) public {
Student storage studentUpdate = students[_index];
studentUpdate.name = _name;
}
function getLength() public view returns (uint) {
return students.length;
}
function getStudent(uint _index) public view returns (string memory name, uint age) {
Student storage studentEvr = students[_index];
return (studentEvr.name, studentEvr.age);
}
// delete the last student
function delLast() public {
students.pop();
}
// delete student using index
function remove(uint _index) public {
require(_index < students.length, "index out of bound");
for (uint i = _index; i < students.length - 1; i++) {
students[i] = students[i + 1];
}
students.pop();
}
}
```
#### Example-14
Balance
```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract Account {
uint public balance;
uint public constant MAX_UINT = 2**256 - 1;
function deposit(uint _amount) public {
uint oldBalance = balance;
uint newBalance = balance + _amount;
// balance + _amount does not overflow if balance + _amount >= balance
require(newBalance >= oldBalance, "Overflow");
balance = newBalance;
assert(balance >= oldBalance);
}
function withdraw(uint _amount) public {
uint oldBalance = balance;
// balance - _amount does not underflow if balance >= _amount
require(balance >= _amount, "Underflow");
if (balance < _amount) {
revert("Underflow");
}
balance -= _amount;
assert(balance <= oldBalance);
}
}
```
#### Example-15
ETH transfer example
> Reference : [HERE](https://solidity-by-example.org/payable)
```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract Payable {
address payable public owner;
constructor() payable {
owner = payable(msg.sender);
}
// Function to deposit Ether into this contract.
// Call this function along with some Ether.
// The balance of this contract will be automatically updated.
function deposit() public payable {}
// check the balance of current contract
function balContract() public view returns (uint) {
return address(this).balance / 1e18;
}
function balOwner() public view returns (uint) {
return address(owner).balance / 1e18;
}
function withdraw() public {
// get the amount of Ether stored in this contract
uint amount = address(this).balance;
// send all Ether to owner
// Owner can receive Ether since the address of owner is payable
(bool success, ) = owner.call{value: amount}("");
require(success, "Failed to send Ether");
}
// Function to transfer Ether from this contract to address from input
function transfer(address payable _to, uint _amount) public {
// Note that "to" is declared as payable
(bool success, ) = _to.call{value: _amount * 1e18}("");
require(success, "Failed to send Ether");
}
}
```
#### Example-16
> Version2. slight difference...
```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract Payable {
address payable public owner;
constructor() payable {
owner = payable(msg.sender);
}
// Function to deposit Ether into this contract.
// Call this function along with some Ether.
// The balance of this contract will be automatically updated.
function deposit() public payable {
}
receive() external payable {}
fallback() external payable {}
function deposit2(uint _amount) public payable {
// Call returns a boolean value indicating success or failure.
// This is the current recommended method to use.
(bool sent, ) = (address(this)).call{value: _amount *1 * 1e18}("");
require(sent, "Failed to send Ether");
}
// check the balance of current contract
function balContract() public view returns (uint) {
return address(this).balance / 1e18;
}
function balOwner() public view returns (uint) {
return address(owner).balance / 1e18;
}
function withdraw() public {
// get the amount of Ether stored in this contract
uint amount = address(this).balance;
// send all Ether to owner
// Owner can receive Ether since the address of owner is payable
(bool success, ) = owner.call{value: amount}("");
require(success, "Failed to send Ether");
}
// Function to transfer Ether from this contract to address from input
function transfer(address payable _to, uint _amount) public {
// Note that "to" is declared as payable
(bool success, ) = _to.call{value: _amount * 1e18}("");
require(success, "Failed to send Ether");
}
function getConAddr() public view returns (address) {
return address(this);
}
}
```
#### Example-17
> Default values
```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract DefaultVals {
bool public b; // default is false;
... ...
近期下载者:
相关文件:
收藏者: