forked from cryptoleek-team/learn-smartcontracts-yt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
learn smart contracts with cryptoleek on youtube
- Loading branch information
1 parent
8ad0fb8
commit 63d8651
Showing
23 changed files
with
682 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.4; | ||
|
||
contract ContractA { | ||
uint public x; | ||
uint public value; | ||
|
||
function setX(uint _x) public returns (uint) { | ||
x = _x; | ||
return x; | ||
} | ||
|
||
function setXandSendEther(uint _x) public payable returns (uint, uint) { | ||
x = _x; | ||
value = msg.value; | ||
return (x, value); | ||
} | ||
|
||
function getBalance() public view returns (uint) { | ||
return address(this).balance; | ||
} | ||
} | ||
|
||
// ContractB -> ContractA | ||
contract ContractB { | ||
|
||
function callSetX(ContractA _contractA, uint _x) public { | ||
_contractA.setX(_x); | ||
} | ||
|
||
function callSetXFromAddress(address _contractAAddres, uint _x) public { | ||
ContractA _contractA = ContractA(_contractAAddres); | ||
_contractA.setX(_x); | ||
} | ||
|
||
function callSetXandSendEther(ContractA _contractA, uint _x) public payable { | ||
_contractA.setXandSendEther{value: msg.value}(_x); | ||
} | ||
|
||
function getBalance() public view returns (uint) { | ||
return address(this).balance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.4; | ||
|
||
contract Car { | ||
string public model; | ||
address public owner; | ||
uint public cost; | ||
|
||
constructor(string memory _model, address _owner) payable { | ||
model = _model; | ||
owner = _owner; | ||
cost = msg.value; | ||
} | ||
|
||
|
||
} | ||
|
||
contract CarFactory { | ||
Car[] public cars; | ||
|
||
// function create(string memory _model) public { | ||
// Car car = new Car(_model, address(this)); | ||
// cars.push(car); | ||
// } | ||
|
||
// 0x447Ec763df0A9806e33130d9695a5c0a5DAe9e76 | ||
// 0x978a01431F9bF1d7750DE1b0b0Bd48445E8184F1 | ||
|
||
function createWithMoney(string memory _model) public payable { | ||
|
||
require(msg.value >= 1 ether, "Not enough money"); | ||
|
||
Car car = new Car{value: msg.value}(_model, address(this)); | ||
cars.push(car); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.4; | ||
|
||
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; | ||
|
||
contract CryptoLeekNFT is ERC721URIStorage { | ||
uint public counter; | ||
|
||
constructor() ERC721("CryptoLeekNFT", "CLN") { | ||
counter = 0; | ||
} | ||
|
||
function createNFTs (string memory tokenURI) public returns (uint) { | ||
uint tokenId = counter; | ||
|
||
_safeMint(msg.sender, tokenId); | ||
_setTokenURI(tokenId, tokenURI); | ||
|
||
counter ++; | ||
|
||
return tokenId; | ||
} | ||
|
||
function burn(uint tokenId) public virtual { | ||
require(_isApprovedOrOwner(msg.sender, tokenId), "You are not the owner or not approved@"); | ||
super._burn(tokenId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.4; | ||
|
||
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC1155/ERC1155.sol"; | ||
|
||
contract GameItems is ERC1155 { | ||
uint256 public constant GOLD = 0; | ||
uint256 public constant SILVER = 1; | ||
uint256 public constant THORS_HAMMER = 2; | ||
uint256 public constant SWORD = 3; | ||
uint256 public constant SHIELD = 4; | ||
|
||
constructor() public ERC1155("https://gateway.pinata.cloud/ipfs/QmaGvW4ynPfSUNg949mXdVezsaym9nb9e1QjvQ6EAr7x8L/{id}.json") { | ||
_mint(msg.sender, GOLD, 10**18, ""); | ||
_mint(msg.sender, SILVER, 10**27, ""); | ||
_mint(msg.sender, THORS_HAMMER, 1, ""); | ||
_mint(msg.sender, SWORD, 10**9, ""); | ||
_mint(msg.sender, SHIELD, 10**9, ""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.4; | ||
|
||
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol"; | ||
|
||
contract MyToken is ERC20 { | ||
constructor(string memory name, string memory symbol) ERC20(name, symbol) { | ||
_mint(msg.sender, 100 * 10 ** uint(decimals())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
// require | ||
// assert | ||
// revert | ||
|
||
pragma solidity ^0.8.4; | ||
|
||
contract Error { | ||
int public balance; | ||
|
||
function deposit(int _amount) public { | ||
//require(_amount > 0, "Deposited amount must be greater than zero"); | ||
int oldBalance = balance; | ||
balance += _amount; | ||
if (balance < oldBalance) { | ||
revert("Impossible!"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.4; | ||
|
||
contract EtherUnits { | ||
uint public oneWei = 1 wei; | ||
uint public oneGwei = 1 gwei; // 1 * 10 ** 9 | ||
uint public oneEther = 1 ether; | ||
|
||
function testOneWei() public view returns(bool) { | ||
return oneWei == 1; | ||
} | ||
|
||
function testOneGwei() public view returns(bool) { | ||
return oneGwei == 1 * 10 ** 9 wei; | ||
} | ||
|
||
function testOneEther() public view returns(bool) { | ||
return oneEther == 1 * 10 ** 18 wei; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.4; | ||
|
||
contract Event { | ||
event Log(address sender, string message); | ||
|
||
function transfer() public { | ||
// xxxxx | ||
emit Log(msg.sender, "I send 1 ether to you!"); | ||
emit Log(msg.sender, "I send 2 ether to you!"); | ||
emit Log(msg.sender, "I send 3 ether to you!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.4; | ||
|
||
contract Fallback { | ||
event Log(uint gas); | ||
|
||
|
||
fallback() external payable { | ||
emit Log(gasleft()); | ||
} | ||
|
||
function getBalance() public view returns (uint) { | ||
return address(this).balance; | ||
} | ||
} | ||
|
||
contract SendEther { | ||
|
||
function send(address payable _to) public payable { | ||
_to.transfer(msg.value); | ||
} | ||
|
||
function call(address payable _to) public payable { | ||
(bool sent,) = _to.call{value:msg.value}("hello world!"); | ||
require(sent); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// SPDX-License-Identifier: MIT | ||
// private, internal, external, public | ||
|
||
pragma solidity ^0.8.4; | ||
|
||
contract FunctionVisibility { | ||
uint value; | ||
|
||
function getValue() external view returns (uint) { | ||
return _getValuePrivate(); | ||
} | ||
|
||
function setValue(uint _value) public { | ||
value = _value; | ||
} | ||
|
||
function _getValueInternal() internal view returns (uint) { | ||
return value; | ||
} | ||
|
||
function _getValuePrivate() private view returns (uint) { | ||
return value; | ||
} | ||
} | ||
|
||
contract SubFunctionVisibility is FunctionVisibility{ | ||
function getValueFromParent() public view returns(uint) { | ||
return _getValueInternal(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.4; | ||
|
||
contract GlobalVariables { | ||
event LOG(address, uint); | ||
|
||
function getGasInfo() public view returns (uint, uint) { | ||
return (tx.gasprice, block.gaslimit); | ||
} | ||
|
||
function getBlockInfo() public view returns (uint, address, uint, uint, uint, uint) { | ||
return (block.chainid, // current chain id | ||
block.coinbase, // current block miner’s address | ||
block.difficulty, // (uint): current block difficulty | ||
block.gaslimit, // (uint): current block gaslimit | ||
block.number, // (uint): current block number | ||
block.timestamp); // (uint) | ||
} | ||
|
||
function getMessageInfo() public payable { | ||
emit LOG(msg.sender, msg.value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.4; | ||
|
||
|
||
import "./myLibs/Car.sol"; | ||
|
||
contract CarFactory2 { | ||
Car[] public cars; | ||
|
||
// function create(string memory _model) public { | ||
// Car car = new Car(_model, address(this)); | ||
// cars.push(car); | ||
// } | ||
|
||
// 0x447Ec763df0A9806e33130d9695a5c0a5DAe9e76 | ||
// 0x978a01431F9bF1d7750DE1b0b0Bd48445E8184F1 | ||
|
||
function createWithMoney(string memory _model) public payable { | ||
|
||
require(msg.value >= 1 ether, "Not enough money"); | ||
|
||
Car car = new Car{value: msg.value}(_model, address(this)); | ||
cars.push(car); | ||
} | ||
} | ||
|
||
|
||
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol"; | ||
contract MyToken is ERC20 { | ||
// constructor() ERC20("cryptoleek.finance", "LEEK") { | ||
|
||
// } | ||
|
||
constructor(string memory name, string memory symbol) ERC20(name, symbol) { | ||
|
||
} | ||
|
||
function decimals() public pure override returns (uint8) { | ||
return 9; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.4; | ||
|
||
contract A { | ||
|
||
string public name; | ||
|
||
constructor(string memory _name) { | ||
name = _name; | ||
} | ||
|
||
function getContractName() public view virtual returns (string memory) { | ||
return name; | ||
} | ||
} | ||
|
||
contract B is A { | ||
string public helloworld; | ||
|
||
constructor(string memory _name, string memory _helloworld) A(_name) { | ||
helloworld = _helloworld; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.4; | ||
|
||
contract Counter { | ||
uint public count; | ||
|
||
function increment() external { | ||
count ++; | ||
} | ||
} | ||
|
||
interface ICounter { | ||
function increment() external; | ||
} | ||
|
||
contract MyContract { | ||
|
||
function incrementCounter(address _counter) external { | ||
ICounter(_counter).increment(); | ||
} | ||
} |
Oops, something went wrong.