Created
February 24, 2024 13:27
-
-
Save Mexidense/479b74045981a1268d22d5fee2703da1 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.20+commit.a1b79de6.js&optimize=false&runs=200&gist=
This file contains hidden or 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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.20; | |
import { ERC721 } from "@openzeppelin/contracts/token/ERC721/ERC721.sol"; | |
contract FirstContract is ERC721 { | |
address owner; | |
constructor(string memory _name, string memory _symbol) ERC721(_name, _symbol) { | |
owner = msg.sender; | |
} | |
} |
This file contains hidden or 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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.20; | |
contract VariablesAndModifiers { | |
uint256 a; | |
uint8 public b = 3; | |
int256 c; | |
int8 public d = -32; | |
int e = 65; | |
string str; | |
string public str_public = 'This is a public variable'; | |
string private str_private = 'This is a private variable'; | |
bool boolean; | |
bool public boolean_public = true; | |
bool private boolean_private = false; | |
bytes32 first_bytes; | |
bytes4 second_bytes; | |
bytes1 bytes_1; | |
bytes32 public hashing_keccak256 = keccak256(abi.encodePacked('Salva', uint(10), 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2)); | |
bytes32 public hashing_sha256 = sha256(abi.encodePacked('Salva')); | |
bytes20 public hashing_ripemd160 = ripemd160(abi.encodePacked('Salva')); | |
address my_address; | |
address public address1 = 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2; | |
address public address2 = msg.sender; | |
enum options { | |
ON, | |
OFF | |
} | |
options state; | |
options constant defaultChoise = options.OFF; | |
function turnOn() public { | |
state = options.ON; | |
} | |
function turnOff() public { | |
state = options.OFF; | |
} | |
function displayState() public view returns (options) { | |
return state; | |
} | |
} |
This file contains hidden or 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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.20; | |
contract DataStructures { | |
struct Customer { | |
uint256 id; | |
string name; | |
string email; | |
} | |
Customer customerOne = Customer(1, "Salva", "[email protected]"); | |
uint256 [5] public fixedListUInts = [1, 2, 3, 4, 5]; | |
uint256 [] dynamicListUInts; | |
Customer [] public dynamicCustomerList; | |
function modifierCustomerList (uint256 _id, string memory _name, string memory _email) public { | |
Customer memory randomCustomer = Customer(_id, _name, _email); | |
dynamicCustomerList.push(randomCustomer); | |
} | |
mapping (address => uint256) public addressUInt; | |
mapping (string => uint256 []) public stringUIntList; | |
mapping (address => Customer) public addressCustomer; | |
function assignNumber (uint256 _number) public { | |
addressUInt[msg.sender] = _number; | |
} | |
function assignList (string memory _name, uint256 _number) public { | |
stringUIntList[_name].push(_number); | |
} | |
function assignAddressCustomer(uint256 _id, string memory _name, string memory _email) public { | |
addressCustomer[msg.sender] = Customer(_id, _name, _email); | |
} | |
} |
This file contains hidden or 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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.20; | |
contract Functions { | |
function getName() public pure returns (string memory) { | |
return 'Salva'; | |
} | |
uint256 x = 100; | |
function getNumber() public view returns (uint256) { | |
return x*2; | |
} | |
} |
This file contains hidden or 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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.20; | |
contract EthSend { | |
constructor() payable {} | |
receive() external payable { } | |
event sendStatus(bool); | |
event callStatus(bool, bytes); | |
function sendViaTransfer(address payable _to) public payable { | |
_to.transfer(1 ether); | |
} | |
function sendViaSend(address payable _to) public payable { | |
bool sent = _to.send(1 ether); | |
emit sendStatus(sent); | |
require(sent == true, 'Shipment has failed'); | |
} | |
function sendViaCall(address payable _to) public payable { | |
(bool success, bytes memory data) = _to.call{value: 1 ether}(''); | |
emit callStatus(success, data); | |
require(success == true, 'Shipment has failed'); | |
} | |
} | |
contract EthReceiver { | |
event log(uint amount, uint gas); | |
receive() external payable { | |
emit log(address(this).balance, gasleft()); | |
} | |
} |
This file contains hidden or 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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.20; | |
contract FallbackReceive { | |
event log(string _name, address _sender, uint _amount, bytes _data); | |
fallback() external payable { | |
emit log('Fallback', msg.sender, msg.value, msg.data); | |
} | |
/* | |
receive() external payable { | |
emit log('Receive', msg.sender, msg.value, ''); | |
} | |
*/ | |
} |
This file contains hidden or 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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.20; | |
contract Math { | |
function sum(uint a, uint b) public pure returns (uint) { | |
return a+b; | |
} | |
function rest(uint a, uint b) public pure returns (uint) { | |
return a-b; | |
} | |
function multipler(uint a, uint b) public pure returns (uint) { | |
return a*b; | |
} | |
function divider(uint a, uint b) public pure returns (uint) { | |
return a/b; | |
} | |
function exponential(uint a, uint b) public pure returns (uint) { | |
return a**b; | |
} | |
function module(uint a, uint b) public pure returns (uint) { | |
return a%b; | |
} | |
} |
This file contains hidden or 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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.20; | |
contract LoopsCondtionals { | |
function sum(uint _number) public pure returns (uint) { | |
uint temporalSum = 0; | |
for (uint i = 0; i < _number; i++) { | |
temporalSum += i; | |
} | |
return temporalSum; | |
} | |
function sumOdd() public pure returns (uint) { | |
uint temporalSum = 0; | |
uint counter = 0; | |
uint counterOdd = 0; | |
while (counterOdd < 10) { | |
if (counter % 2 != 0) { | |
temporalSum += counter; | |
counterOdd++; | |
} | |
counter++; | |
} | |
return temporalSum; | |
} | |
function sumOrRest(string memory operation, uint a, uint b) public pure returns (uint) { | |
bytes32 hashedOperation = keccak256(abi.encodePacked(operation)); | |
bytes32 sumHashedOperation = keccak256(abi.encodePacked('suma')); | |
if (hashedOperation == sumHashedOperation) { | |
return a+b; | |
} | |
return a-b; | |
} | |
} |
This file contains hidden or 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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.20; | |
contract Food { | |
struct DinnerDish { | |
string name; | |
string ingredients; | |
} | |
DinnerDish [] public menu; | |
function newMenu(string memory _name, string memory _ingredients) internal { | |
menu.push(DinnerDish(_name, _ingredients)); | |
} | |
} | |
contract Hamburger is Food { | |
address public owner; | |
constructor () { | |
owner = msg.sender; | |
} | |
function cookHamburger(string memory _ingredients, uint __units) external { | |
require(__units <= 5, "Ops, you cannot order 5 or more hamburgers"); | |
newMenu('Hamburger', _ingredients); | |
} | |
modifier onlyOwner () { | |
require(owner == msg.sender, "You do not have permissions to execute this function"); | |
_; | |
} | |
function hasPrivateNumber(uint _number) public view onlyOwner returns (bytes32) { | |
return keccak256(abi.encodePacked(_number)); | |
} | |
} |
This file contains hidden or 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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.20; | |
contract ContractFactory { | |
mapping (address => address) public personalContract; | |
function factory() public { | |
address newContract = address( | |
new SingleContract( | |
msg.sender, | |
address(this) | |
) | |
); | |
personalContract[msg.sender] = newContract; | |
} | |
} | |
contract SingleContract { | |
Owner public owner; | |
struct Owner { | |
address _owner; | |
address _smartContractFactory; | |
} | |
constructor(address _account, address _contractAddress) { | |
owner._owner = _account; | |
owner._smartContractFactory = _contractAddress; | |
} | |
} |
This file contains hidden or 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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.20; | |
// https://docs.openzeppelin.com/contracts/4.x/erc20 | |
import "@openzeppelin/[email protected]/token/ERC20/ERC20.sol"; | |
contract GLDToken is ERC20 { | |
constructor(uint256 initialSupply) ERC20("Gold", "GLD") { | |
_mint(msg.sender, initialSupply); | |
} | |
} |
This file contains hidden or 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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.20; | |
interface IERC20 { | |
function totalSupply() external view returns (uint256); | |
function balanceOf(address account) external view returns (uint256); | |
function transfer(address to, uint256 amount) external returns (bool); | |
function allowance(address owner, address spender) external view returns (uint256); | |
function approve(address spender, uint256) external returns (bool); | |
function transferFrom( | |
address from, | |
address to, | |
uint256 amount | |
) external returns (bool); | |
event Transfer( | |
address indexed from, | |
address indexed to, | |
uint256 value | |
); | |
event Approval( | |
address indexed owner, | |
address indexed spender, | |
uint256 value | |
); | |
} | |
contract ERC20 is IERC20 { | |
mapping (address => uint256) private _balances; | |
mapping (address => mapping (address => uint256)) private _allowances; | |
uint256 private _totalSupply; | |
string private _name; | |
string private _symbol; | |
constructor(string memory name_, string memory symbol_) { | |
_name = name_; | |
_symbol = symbol_; | |
} | |
function name() public view virtual returns (string memory) { | |
return _name; | |
} | |
function symbol() public view virtual returns (string memory) { | |
return _symbol; | |
} | |
function decimals() public view virtual returns (uint8) { | |
return 18; | |
} | |
function totalSupply() public view virtual override returns (uint256) { | |
return _totalSupply; | |
} | |
function balanceOf(address account) public view virtual override returns (uint256) { | |
return _balances[account]; | |
} | |
function transfer(address to, uint256 amount) public virtual override returns (bool) { | |
address owner = msg.sender; | |
_transfer( | |
owner, | |
to, | |
amount | |
); | |
return true; | |
} | |
function allowance(address owner, address spender) public view virtual override returns (uint256) { | |
return _allowances[owner][spender]; | |
} | |
function approve(address spender, uint256 amount) public virtual override returns (bool) { | |
address owner = msg.sender; | |
_approve( | |
owner, | |
spender, | |
amount | |
); | |
return true; | |
} | |
function transferFrom( | |
address from, | |
address to, | |
uint256 amount | |
) public virtual override returns (bool) { | |
address spender = msg.sender; | |
_spendAllowance( | |
from, | |
spender, | |
amount | |
); | |
_transfer( | |
from, | |
to, | |
amount | |
); | |
return true; | |
} | |
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) { | |
address owner = msg.sender; | |
_approve(owner, spender, _allowances[owner][spender] + addedValue); | |
return true; | |
} | |
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) { | |
address owner = msg.sender; | |
uint256 currentAllowance = _allowances[owner][spender]; | |
require(currentAllowance >= subtractedValue, "[ERC20] Decrease allowance below zero"); | |
unchecked { | |
_approve( | |
owner, | |
spender, | |
currentAllowance - subtractedValue | |
); | |
} | |
return true; | |
} | |
function _transfer( | |
address from, | |
address to, | |
uint256 amount | |
) internal virtual { | |
require(from != address(0), "[ERC20] Transfer from the zero address"); | |
require(to != address(0), "[ERC20] Transfer to the zero address"); | |
_beforeTokenTransfer(from, to, amount); | |
uint256 fromBalance = _balances[from]; | |
require(fromBalance >= amount, "[ERC20] Transfer amount exceeds balance"); | |
unchecked { | |
_balances[from] = fromBalance - amount; | |
} | |
_balances[to] += amount; | |
emit Transfer(from, to, amount); | |
_afterTokenTransfer(from, to, amount); | |
} | |
function _mint(address account, uint amount) internal virtual { | |
require(account != address(0), "[ERC20] Mint to the zero address"); | |
_beforeTokenTransfer(address(0), account, amount); | |
_totalSupply += amount; | |
_balances[account] += amount; | |
emit Transfer(address(0), account, amount); | |
_afterTokenTransfer(address(0), account, amount); | |
} | |
function _burn(address account, uint256 amount) internal virtual { | |
require(account != address(0), "[ERC20] Burn from the zero address"); | |
_beforeTokenTransfer(account, address(0), amount); | |
uint256 accountBalance = _balances[account]; | |
require(accountBalance >= amount, "[ERC20] Burn amount exceeds balance"); | |
unchecked { | |
_balances[account] = accountBalance - amount; | |
} | |
emit Transfer(account, address(0), amount); | |
_afterTokenTransfer(account, address(0), amount); | |
} | |
function _approve( | |
address owner, | |
address spender, | |
uint256 amount | |
) internal virtual { | |
require(owner != address(0), "[ERC20] Approve from the zero address"); | |
require(spender != address(0), "[ERC20] Approve to the zero address"); | |
_allowances[owner][spender] = amount; | |
emit Approval(owner, spender, amount); | |
} | |
function _spendAllowance( | |
address owner, | |
address spender, | |
uint256 amount | |
) internal virtual { | |
uint256 currentAllowance = allowance(owner, spender); | |
if (currentAllowance != type(uint256).max) { | |
require(currentAllowance >= amount, "[ERC] insufficient allowance"); | |
unchecked { | |
_approve(owner, spender, amount); | |
} | |
} | |
} | |
function _beforeTokenTransfer( | |
address from, | |
address to, | |
uint256 amount | |
) internal virtual {} | |
function _afterTokenTransfer( | |
address from, | |
address to, | |
uint256 amount | |
) internal virtual {} | |
} |
This file contains hidden or 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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.20; | |
import './012_create_a_contract_erc20.sol'; | |
contract CustomERC20 is ERC20 { | |
constructor() ERC20("Salva", "SBR") {} | |
function createTokens() public { | |
_mint(msg.sender, 1000); | |
} | |
} |
This file contains hidden or 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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.20; | |
import "@openzeppelin/[email protected]/token/ERC721/ERC721.sol"; | |
import "@openzeppelin/[email protected]/utils/Counters.sol"; | |
contract CustomERC721 is ERC721 { | |
using Counters for Counters.Counter; | |
Counters.Counter private _tokenIds; | |
constructor(string memory _name, string memory _symbol) ERC721(_name, _symbol) {} | |
function sendNFT(address _account) public { | |
_tokenIds.increment(); | |
uint256 newItemId = _tokenIds.current(); | |
_safeMint(_account, newItemId); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment