diff --git a/CallFromContract.sol b/CallFromContract.sol new file mode 100644 index 0000000..15e2ca2 --- /dev/null +++ b/CallFromContract.sol @@ -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; + } +} \ No newline at end of file diff --git a/CarFactory.sol b/CarFactory.sol new file mode 100644 index 0000000..cddb6d5 --- /dev/null +++ b/CarFactory.sol @@ -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); + } +} \ No newline at end of file diff --git a/CryptoLeekNFT.sol b/CryptoLeekNFT.sol new file mode 100644 index 0000000..477689a --- /dev/null +++ b/CryptoLeekNFT.sol @@ -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); + } +} \ No newline at end of file diff --git a/ERC1155.sol b/ERC1155.sol new file mode 100644 index 0000000..fc019be --- /dev/null +++ b/ERC1155.sol @@ -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, ""); + } +} \ No newline at end of file diff --git a/ERC20.sol b/ERC20.sol new file mode 100644 index 0000000..d3e6166 --- /dev/null +++ b/ERC20.sol @@ -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())); + } +} \ No newline at end of file diff --git a/Error.sol b/Error.sol new file mode 100644 index 0000000..c870de9 --- /dev/null +++ b/Error.sol @@ -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!"); + } + } +} \ No newline at end of file diff --git a/EtherUnits.sol b/EtherUnits.sol new file mode 100644 index 0000000..a2afc0d --- /dev/null +++ b/EtherUnits.sol @@ -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; + } +} \ No newline at end of file diff --git a/Event.sol b/Event.sol new file mode 100644 index 0000000..fc6efd8 --- /dev/null +++ b/Event.sol @@ -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!"); + } +} \ No newline at end of file diff --git a/Fallback.sol b/Fallback.sol new file mode 100644 index 0000000..053678c --- /dev/null +++ b/Fallback.sol @@ -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); + } +} \ No newline at end of file diff --git a/FunctionVisibility.sol b/FunctionVisibility.sol new file mode 100644 index 0000000..97cc2ec --- /dev/null +++ b/FunctionVisibility.sol @@ -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(); + } +} \ No newline at end of file diff --git a/GlobalVariables.sol b/GlobalVariables.sol new file mode 100644 index 0000000..cabfff5 --- /dev/null +++ b/GlobalVariables.sol @@ -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); + } +} \ No newline at end of file diff --git a/Import.sol b/Import.sol new file mode 100644 index 0000000..b58d29d --- /dev/null +++ b/Import.sol @@ -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; + } +} \ No newline at end of file diff --git a/Inheritance.sol b/Inheritance.sol new file mode 100644 index 0000000..18b0436 --- /dev/null +++ b/Inheritance.sol @@ -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; + } +} \ No newline at end of file diff --git a/Interface.sol b/Interface.sol new file mode 100644 index 0000000..13ef290 --- /dev/null +++ b/Interface.sol @@ -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(); + } +} \ No newline at end of file diff --git a/Library.sol b/Library.sol new file mode 100644 index 0000000..e0936ad --- /dev/null +++ b/Library.sol @@ -0,0 +1,51 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.4; + +library SafeMath { + function add(uint x, uint y) internal pure returns(uint) { + uint result = x + y; + require(result >= x, "Overflow!"); + + return result; + } +} + +contract TestSafeMath { + using SafeMath for uint; + + function testAdd(uint x, uint y) public pure returns (uint) { + uint result = x.add(y); + //SafeMath.add(x, y); + + return result; + } +} + +library Array { + function remove(uint[] storage arr, uint index) public { + arr[index] = arr[arr.length -1]; + arr.pop(); + } +} + +contract TestArray { + using Array for uint[]; + + uint[] public testArr; + + function testArrayRemove() public { + testArr.push(1); + testArr.push(2); + testArr.push(3); + + // [1,2,3] + + testArr.remove(1); + + // [1,3] + + assert(testArr.length == 2); + assert(testArr[0] == 1); + assert(testArr[1] == 3); + } +} \ No newline at end of file diff --git a/Mapping.sol b/Mapping.sol new file mode 100644 index 0000000..cd6cd13 --- /dev/null +++ b/Mapping.sol @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.4; + +contract Mapping { + event LOG(address, uint); + + mapping(address => uint) public myMap; + address[] public myMappingAddr; + + function setMapping(address _myAddr, uint _number) public { + myMap[_myAddr] = _number; + myMappingAddr.push(_myAddr); + } + + function getMapping(address _myAddr) public view returns (uint) { + return myMap[_myAddr]; + } + + function deleteMapping(address _myAddr) public { + delete myMap[_myAddr]; + } + + function getTotal() public view returns (uint) { + require(myMappingAddr.length > 0, "The mapping has no values!"); + + uint sum = 0; + for (uint i = 0; i < myMappingAddr.length; i++) { + address key = myMappingAddr[i]; + uint value = myMap[key]; + sum += value; + } + + return sum; + } + + function test () public { + setMapping(0x5B38Da6a701c568545dCfcB03FcB875f56beddC4, 10); + setMapping(0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2, 20); + setMapping(0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db, 30); + + uint sum = getTotal(); + + assert(sum == 60); + } + +} \ No newline at end of file diff --git a/Modifer.sol b/Modifer.sol new file mode 100644 index 0000000..bdab05e --- /dev/null +++ b/Modifer.sol @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.4; + +contract Modifer { + + address public owner; + + constructor() { + owner = msg.sender; + } + + modifier onlyOwner() { + require(owner == msg.sender, "Not Owner"); + _; + } + + function onlyCallByOwner() public onlyOwner pure returns (bool) { + return true; + } + + function adminOperation() onlyOwner { + + } + + function withdraw() onlyOwner { + + } +} diff --git a/MultiInheritance.sol b/MultiInheritance.sol new file mode 100644 index 0000000..a02b07e --- /dev/null +++ b/MultiInheritance.sol @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.4; + +contract A { + + string public name; + string public name2; + + constructor(string memory _name, string memory _name2) { + name = _name; + name = _name2; + } + + function getContractName() public view virtual returns (string memory) { + return name; + } +} + +contract B { + string public helloworld; + + constructor(string memory _helloworld) { + helloworld = _helloworld; + } + + function getHelloworld() public view virtual returns (string memory) { + return helloworld; + } +} + +contract C is A("A1", "A2"), B("HelloWorld") { + +} + +contract D is A, B { + // constructor() A("A1", "A2") B("HelloWorld") { + + // } + constructor(string memory _name, string memory _helloworld) A(_name, _helloworld) B(_helloworld) { + + } +} \ No newline at end of file diff --git a/StringUtils.sol b/StringUtils.sol new file mode 100644 index 0000000..14151b6 --- /dev/null +++ b/StringUtils.sol @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.4; + +library StringUtils { + + function equal(string memory a, string memory b) internal pure returns (bool) { + return keccak256(abi.encode(a)) == keccak256(abi.encode(b)); + } + + function concat(string memory a, string memory b) internal pure returns (string memory) { + return string(abi.encodePacked(a, b)); + } + + function length(string memory str) internal pure returns (uint) { + return bytes(str).length; + } +} + +contract StringUtilsTest { + using StringUtils for string; + + function testEqual(string memory a, string memory b) public pure returns (bool) { + return a.equal(b); + } + + function testConcat(string memory a, string memory b) public pure returns (string memory) { + return a.concat(b); + } + + function testLength(string memory str) public pure returns (uint) { + return str.length(); + } +} \ No newline at end of file diff --git a/Struct.sol b/Struct.sol new file mode 100644 index 0000000..721a8da --- /dev/null +++ b/Struct.sol @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.4; + +contract Struct { + // what you need to do? task? + // completed? true or false + + struct Todo { + string task; + bool completed; + } + + Todo[] public todoList; + + function create(string memory _task) public { + // Todo memory todo = Todo(_task, false); + + // Todo memory todo = Todo({ + // completed: false, + // task: _task, + // }); + + Todo memory todo; + todo.task = _task; + todo.completed = false; + + todoList.push(todo); + } + + function get(uint _index) public view returns (Todo memory) { + return todoList[_index]; + } + + function getDetails(uint _index) public view returns (string memory task, bool completed) { + Todo storage todo = todoList[_index]; + return (todo.task, todo.completed); + } + + function setTask(uint _index, string memory _task) public { + Todo storage todo = todoList[_index]; + todo.task = _task; + } + + function toggleCompleted(uint _index) public { + Todo storage todo = todoList[_index]; + todo.completed = !todo.completed; + } +} \ No newline at end of file diff --git a/UniswapExample.sol b/UniswapExample.sol new file mode 100644 index 0000000..921dcd6 --- /dev/null +++ b/UniswapExample.sol @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.4; + +// Uniswap example +interface UniswapV2Factory { + function getPair(address tokenA, address tokenB) external view returns (address pair); +} + +interface UniswapV2Pair { + function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast); +} + +contract UniswapExample { + address private factory = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f; + address private dai = 0x6B175474E89094C44Da98b954EedeAC495271d0F; + address private weth = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2; + + function getTokenReserves() external view returns (uint, uint) { + address pair = UniswapV2Factory(factory).getPair(dai, weth); + (uint reserve0, uint reserve1, ) = UniswapV2Pair(pair).getReserves(); + return (reserve0, reserve1); + } +} \ No newline at end of file diff --git a/Wallet.sol b/Wallet.sol new file mode 100644 index 0000000..8a1f011 --- /dev/null +++ b/Wallet.sol @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.4; + +contract Wallet { + address payable public owner; + + event Deposit(address sender, uint amount, uint balance); + event Withdraw(uint amount, uint balance); + event Transfer(address to, uint amount, uint balance); + + modifier onlyOwner() { + require (msg.sender == owner, "Not Owner"); + _; + } + + constructor() payable { + owner = payable(msg.sender); + } + + function deposit() public payable { + emit Deposit(msg.sender, msg.value, address(this).balance); + } + + function getBalance() public view returns (uint) { + return address(this).balance; + } + + function withdraw(uint amount) public onlyOwner { + payable(msg.sender).transfer(amount); + emit Withdraw(amount, address(this).balance); + } + + function transferTo(address payable _to, uint amount) public onlyOwner { + _to.transfer(amount); + emit Transfer(_to, amount, address(this).balance); + } +} \ No newline at end of file diff --git a/myLibs/Car.sol b/myLibs/Car.sol new file mode 100644 index 0000000..9d501f3 --- /dev/null +++ b/myLibs/Car.sol @@ -0,0 +1,14 @@ +// 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; + } +} \ No newline at end of file