Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save deadelus/533fab9b20aa8afc19ff71ef9bcef028 to your computer and use it in GitHub Desktop.
Save deadelus/533fab9b20aa8afc19ff71ef9bcef028 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.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract test10 {
uint nombre;
function setNombre(uint _n) public {
if (_n == 10) {
revert('Nombre can be 10');
}
nombre = _n;
}
function setNombre2(uint _n) public {
require(_n != 10, 'Nombre can be 10');
nombre = _n;
}
function getNombre() public view returns (uint) {
return nombre;
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract Owner {
address owner;
bool paused;
uint nombre;
constructor() {
owner = msg.sender;
}
function setPaused(bool _paused) public {
require(msg.sender == owner, 'Not Owner');
paused = _paused;
}
function setNombre(uint _n) public {
require(paused == false, 'Paused Contract');
require(msg.sender == owner, 'Not owner');
nombre = _n;
}
function getNombre() public view returns (uint) {
require(paused == false, 'Paused Contract');
return nombre;
}
function destroy(address payable _to) public {
require(msg.sender == owner, 'Not owner');
selfdestruct(_to);
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract StudentBook {
address owner;
struct Grades {
string subject;
uint grade;
}
struct Student {
string firstname;
string lastname;
uint gradesCount;
mapping(uint => Grades) grades;
}
mapping(address => Student) Students;
constructor() {
owner = msg.sender;
}
function addStudent(address _address, string memory _fname, string memory _lname) public {
require(msg.sender == owner, 'Not Owner');
bytes memory firstnameOfAddress = bytes(Students[_address].firstname);
require(firstnameOfAddress.length == 0, 'Student Already Exists');
Students[_address].firstname = _fname;
Students[_address].lastname = _lname;
}
function addGrade(address _address, uint _grade, string memory _suject) public {
require(msg.sender == owner, 'Not Owner');
bytes memory firstnameOfAddress = bytes(Students[_address].firstname);
require(firstnameOfAddress.length > 0, 'Student Not Exists');
uint index = Students[_address].gradesCount;
Students[_address].grades[index].subject = _suject;
Students[_address].grades[index].grade = _grade;
Students[_address].gradesCount++;
}
function getGrades(address _address) public view returns (uint[] memory) {
require(msg.sender == owner, 'Not Owner');
bytes memory firstnameOfAddress = bytes(Students[_address].firstname);
require(firstnameOfAddress.length > 0, 'Student Not Exists');
uint nbOfGrades = Students[_address].gradesCount;
uint[] memory grades = new uint[](nbOfGrades);
for(uint i = 0; i < nbOfGrades; i++) {
grades[i] = (Students[_address].grades[i].grade);
}
return grades;
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract test13 {
address owner;
uint nombre;
constructor() {
owner = msg.sender;
}
modifier isOwner() {
require(msg.sender == owner, 'Not Owner');
_;
}
function setNombre(uint _n) public isOwner {
nombre = _n;
}
function getNombre() public view returns(uint){
return nombre;
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract Owner {
address owner;
constructor() {
owner = msg.sender;
}
modifier isOwner() {
require(msg.sender == owner, 'Not Owner');
_;
}
}
contract test14 is Owner {
uint nombre;
function setNombre(uint _n) public isOwner {
nombre = _n;
}
function getNombre() public view returns(uint){
return nombre;
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
import './14_Extends.sol';
contract test15 is Owner {
uint nombre;
function setNombre(uint _n) public isOwner {
nombre = _n;
}
function getNombre() public view returns(uint){
return nombre;
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
import './14_Extends.sol';
contract LandlordHouses is Owner {
enum typeOf {house, flat, land}
uint counter;
struct House {
uint id;
string name;
uint price;
typeOf _typeOf;
}
mapping(address => House[]) Houses;
function addHouse(address _landlord, string memory _name, uint _price, typeOf _typeOf) public isOwner {
require(_price > 1000, 'Price > 1000 WEI');
require(uint(_typeOf) >= 0, 'Type entre 0 et 2');
require(uint(_typeOf) <= 0, 'Type entre 0 et 2');
counter++;
Houses[_landlord].push(House(counter, _name, _price, _typeOf));
}
function getHouses(address _landlord) public view isOwner returns(House[] memory) {
return Houses[_landlord];
}
function nbHouses(address _landlord) public view isOwner returns(uint) {
return Houses[_landlord].length;
}
function getMyHouses() public view returns(House[] memory) {
return Houses[msg.sender];
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract Nombre {
uint internal nombre;
function getNombre() internal view returns(uint) {
return nombre;
}
function setNombre(uint _nombre) internal {
nombre = _nombre;
}
}
contract Nombre2 is Nombre {
function getNombreX2() external view returns(uint) {
return getNombre();
}
function setNombreX2(uint _nombre) external {
setNombre(multiple(_nombre, 2));
}
function multiple(uint _n, uint _multiplicator) private pure returns(uint) {
return _n * _multiplicator;
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract Nombre {
uint[] public arr; // storage
uint nombre;
function doStuffMemory() external {
arr.push(3);
arr.push(8);
uint[] memory arr2 = arr; // in memory
arr2[0] = 0; // arr[0] = 3
}
function doStuffStorage() external {
arr.push(3);
arr.push(8);
uint[] storage arr2 = arr; // pointeur
arr2[0] = 0; // arr[0] = 0
}
function doStuffCalldata(uint[] calldata _users) external { // external => calldata
nombre = _users[0];
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract Events {
uint[] numbers;
event numberAdded(address by, uint number);
function addNumber(uint _nb) external {
numbers.push(_nb);
emit numberAdded(msg.sender, _nb);
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract test {
uint nombre;
function getNombre() public view returns(uint) {
return nombre;
}
function setNombre(uint _nombre) public {
nombre = _nombre;
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
import "@openzeppelin/contracts/utils/Strings.sol";
contract Libs {
function concat(string memory _str, uint _a, uint _b) external pure returns(string memory) {
string memory res = string(abi.encodePacked(_str, Strings.toString(_a), Strings.toString(_b)));
return res;
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
interface contractBinterface {
function getNombre() external view returns(uint);
function setNombre(uint _nombre) external;
}
contract contractA {
address addressB;
function setAddressB(address _addressB) external {
addressB = _addressB;
}
function callGetNb() external view returns(uint) {
contractBinterface b = contractBinterface(addressB);
return b.getNombre();
}
function callSetNb(uint _nb) external {
contractBinterface b = contractBinterface(addressB);
return b.setNombre(_nb);
}
}
contract contractB {
uint nombre;
function getNombre() external view returns(uint) {
return nombre;
}
function setNombre(uint _nombre) external {
nombre = _nombre;
}
function setNombreX2(uint _nombre) external {
nombre = _nombre * 2;
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract factoryNumber {
Number[] numbersContracts;
function createNumberContract() external returns(address){
Number n = new Number(100);
numbersContracts.push(n);
return address(n);
}
function getNumberByContract(address _Contract) external view returns(uint) {
Number n = Number(_Contract);
return n.getNombre();
}
function getAllContracts() external view returns(Number[] memory) {
return numbersContracts;
}
function getAllContractsValues() external view returns(uint[] memory) {
uint[] memory values = new uint[](numbersContracts.length);
for(uint i; i < numbersContracts.length; i++) {
Number n = numbersContracts[i];
values[i] = n.getNombre();
}
return values;
}
function setNumberByContract(uint _id, uint _nb) external {
Number n = numbersContracts[_id];
return n.setNombre(_nb);
}
}
contract Number {
uint nombre;
constructor(uint _nombre) {
nombre = _nombre;
}
function getNombre() external view returns(uint) {
return nombre;
}
function setNombre(uint _nombre) external {
nombre = _nombre;
}
function setNombreX2(uint _nombre) external {
nombre = _nombre * 2;
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract test2 {
uint nombre;
string phrase;
address uneAddress;
function getNombre() public view returns(uint) {
return nombre;
}
function setNombre(uint _nombre) public {
nombre = _nombre;
}
function getPhrase() public view returns(string memory) {
return phrase;
}
function setPhrase(string memory _phrase) public {
phrase = _phrase;
}
function getAddress() public view returns(address) {
return uneAddress;
}
function setAddress(address _address) public {
uneAddress = _address;
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract test3 {
address lastPerson;
uint balance;
function getLastPerson() public view returns(address) {
return lastPerson;
}
function getBalance() public view returns(uint) {
return balance;
}
receive() external payable {
lastPerson = msg.sender;
balance = balance + msg.value;
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract test4 {
mapping(address => uint) Balances;
function getBalance(address _address) public view returns(uint) {
return Balances[_address];
}
receive() external payable {
Balances[msg.sender] = msg.value;
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract test5 {
struct balance {
uint money;
uint numPayments;
}
mapping(address => balance) Balances;
function getBalance() public view returns(uint) {
return Balances[msg.sender].money;
}
function getnumPayments() public view returns(uint) {
return Balances[msg.sender].numPayments;
}
receive() external payable {
Balances[msg.sender].money += msg.value;
Balances[msg.sender].numPayments += 1;
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract test6 {
struct wallet {
uint balance;
uint numPayments;
}
mapping(address => wallet) Wallets;
function getTotalBalance() public view returns(uint) {
return address(this).balance;
}
function getBalance() public view returns(uint) {
return Wallets[msg.sender].balance;
}
function withdrawAllMoney(address payable _to) public {
uint _amount = Wallets[msg.sender].balance;
Wallets[msg.sender].balance = 0;
_to.transfer(_amount);
}
receive() external payable {
Wallets[msg.sender].balance += msg.value;
Wallets[msg.sender].numPayments += 1;
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract test7 {
uint[] nombre;
function addValue(uint _val) public {
nombre.push(_val);
}
function updateValue(uint _index, uint _val) public {
nombre[_index] = _val;
}
function deleteValue(uint _index) public {
delete nombre[_index];
}
function getValue(uint _index) public view returns(uint) {
return nombre[_index];
}
function getNombreX2() public view returns(uint[] memory) {
uint size = nombre.length;
uint[] memory nombreX2 = new uint[](size);
for (uint i = 0; i < size; i++) {
nombreX2[i] = _multiple(nombre[i], 2);
}
return nombreX2;
}
function _multiple(uint _n, uint _multiplicator) private pure returns(uint) {
return _n * _multiplicator;
}
function sum(uint[] memory tab) public pure returns(uint[] memory) {
return tab;
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract test8 {
struct eleve {
string name;
uint[] notes;
}
mapping (address => eleve) Eleves;
function addNote(address _eleve, uint _note) public {
Eleves[_eleve].notes.push(_note);
}
function addName(address _eleve, string memory _name) public {
Eleves[_eleve].name = _name;
}
function getNotes(address _eleve) public view returns (uint[] memory) {
return Eleves[_eleve].notes;
}
function getName(address _eleve) public view returns (string memory) {
return Eleves[_eleve].name;
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract test9 {
enum state {ordered, shipped, delivered}
struct product {
string SKU;
test9.state _state;
}
mapping(address => product) Orders;
function order(address _address, string memory _SKU) public {
product memory p = product(_SKU, state.ordered);
Orders[_address] = p;
}
function ship(address _address) public {
Orders[_address]._state = state.shipped;
}
function getSKU(address _address) public view returns (string memory) {
return Orders[_address].SKU;
}
function getState(address _address) public view returns (state) {
return Orders[_address]._state;
}
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* @title Storage
* @dev Store & retrieve value in a variable
* @custom:dev-run-script ./scripts/deploy_with_ethers.ts
*/
contract Storage {
uint256 number;
/**
* @dev Store value in variable
* @param num value to store
*/
function store(uint256 num) public {
number = num;
}
/**
* @dev Return value
* @return value of 'number'
*/
function retrieve() public view returns (uint256){
return number;
}
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import "hardhat/console.sol";
/**
* @title Owner
* @dev Set & change owner
*/
contract Owner {
address private owner;
// event for EVM logging
event OwnerSet(address indexed oldOwner, address indexed newOwner);
// modifier to check if caller is owner
modifier isOwner() {
// If the first argument of 'require' evaluates to 'false', execution terminates and all
// changes to the state and to Ether balances are reverted.
// This used to consume all gas in old EVM versions, but not anymore.
// It is often a good idea to use 'require' to check if functions are called correctly.
// As a second argument, you can also provide an explanation about what went wrong.
require(msg.sender == owner, "Caller is not owner");
_;
}
/**
* @dev Set contract deployer as owner
*/
constructor() {
console.log("Owner contract deployed by:", msg.sender);
owner = msg.sender; // 'msg.sender' is sender of current call, contract deployer for a constructor
emit OwnerSet(address(0), owner);
}
/**
* @dev Change owner
* @param newOwner address of new owner
*/
function changeOwner(address newOwner) public isOwner {
emit OwnerSet(owner, newOwner);
owner = newOwner;
}
/**
* @dev Return owner address
* @return address of owner
*/
function getOwner() external view returns (address) {
return owner;
}
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* @title Ballot
* @dev Implements voting process along with vote delegation
*/
contract Ballot {
struct Voter {
uint weight; // weight is accumulated by delegation
bool voted; // if true, that person already voted
address delegate; // person delegated to
uint vote; // index of the voted proposal
}
struct Proposal {
// If you can limit the length to a certain number of bytes,
// always use one of bytes1 to bytes32 because they are much cheaper
bytes32 name; // short name (up to 32 bytes)
uint voteCount; // number of accumulated votes
}
address public chairperson;
mapping(address => Voter) public voters;
Proposal[] public proposals;
/**
* @dev Create a new ballot to choose one of 'proposalNames'.
* @param proposalNames names of proposals
*/
constructor(bytes32[] memory proposalNames) {
chairperson = msg.sender;
voters[chairperson].weight = 1;
for (uint i = 0; i < proposalNames.length; i++) {
// 'Proposal({...})' creates a temporary
// Proposal object and 'proposals.push(...)'
// appends it to the end of 'proposals'.
proposals.push(Proposal({
name: proposalNames[i],
voteCount: 0
}));
}
}
/**
* @dev Give 'voter' the right to vote on this ballot. May only be called by 'chairperson'.
* @param voter address of voter
*/
function giveRightToVote(address voter) public {
require(
msg.sender == chairperson,
"Only chairperson can give right to vote."
);
require(
!voters[voter].voted,
"The voter already voted."
);
require(voters[voter].weight == 0);
voters[voter].weight = 1;
}
/**
* @dev Delegate your vote to the voter 'to'.
* @param to address to which vote is delegated
*/
function delegate(address to) public {
Voter storage sender = voters[msg.sender];
require(!sender.voted, "You already voted.");
require(to != msg.sender, "Self-delegation is disallowed.");
while (voters[to].delegate != address(0)) {
to = voters[to].delegate;
// We found a loop in the delegation, not allowed.
require(to != msg.sender, "Found loop in delegation.");
}
sender.voted = true;
sender.delegate = to;
Voter storage delegate_ = voters[to];
if (delegate_.voted) {
// If the delegate already voted,
// directly add to the number of votes
proposals[delegate_.vote].voteCount += sender.weight;
} else {
// If the delegate did not vote yet,
// add to her weight.
delegate_.weight += sender.weight;
}
}
/**
* @dev Give your vote (including votes delegated to you) to proposal 'proposals[proposal].name'.
* @param proposal index of proposal in the proposals array
*/
function vote(uint proposal) public {
Voter storage sender = voters[msg.sender];
require(sender.weight != 0, "Has no right to vote");
require(!sender.voted, "Already voted.");
sender.voted = true;
sender.vote = proposal;
// If 'proposal' is out of the range of the array,
// this will throw automatically and revert all
// changes.
proposals[proposal].voteCount += sender.weight;
}
/**
* @dev Computes the winning proposal taking all previous votes into account.
* @return winningProposal_ index of winning proposal in the proposals array
*/
function winningProposal() public view
returns (uint winningProposal_)
{
uint winningVoteCount = 0;
for (uint p = 0; p < proposals.length; p++) {
if (proposals[p].voteCount > winningVoteCount) {
winningVoteCount = proposals[p].voteCount;
winningProposal_ = p;
}
}
}
/**
* @dev Calls winningProposal() function to get the index of the winner contained in the proposals array and then
* @return winnerName_ the name of the winner
*/
function winnerName() public view
returns (bytes32 winnerName_)
{
winnerName_ = proposals[winningProposal()].name;
}
}
// SPDX-License-Identifier: MIT
pragma solidity >= 0.6.0 < 0.9.0;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/SafeMath.sol";
contract FundMe {
// to check for uint overflows
using SafeMath for uint256;
mapping(address => uint256) public addressToAmountFunded;
address[] public funders;
address public owner;
constructor() {
owner = msg.sender;
}
// Especially with ETH
function fund() public payable {
// min val 50$
// GWEI terms so we multiply by ten with 18 decimal;
uint256 minimumUSDValue = 50 * 10 ** 18;
require(getConversiontRate(msg.value) >= minimumUSDValue, "NO ENOUGHT ETH !");
// keywords on every contract transaction
addressToAmountFunded[msg.sender] += msg.value;
// ETH -> USD converstion ?
}
// MODIFER is used to add custom behavion before or afer code
modifier onlyOwner {
// _; if we wand to exec code before
require(msg.sender == owner, "YOU DON'T HAVE THE OWNER PERMISSIONS.");
_;
}
function withdraw() payable onlyOwner public {
payable(msg.sender).transfer(address(this).balance);
for(uint256 fundersIndex=0; fundersIndex < funders.length; fundersIndex++) {
address funder = funders[fundersIndex];
addressToAmountFunded[funder] = 0;
}
funders = new address[](0);
}
function getVersion() public view returns(uint256) {
// Use the contract's address of chainlink for ETH / USD conversion
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
return priceFeed.version();
}
function getPrice() public view returns(uint256) {
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
(
/*uint80 roundID*/,
int price,
/*uint startedAt*/,
/*uint timeStamp*/,
/*uint80 answeredInRound*/
) = priceFeed.latestRoundData();
return uint256(price * 10000000000); // to match WEI values
}
// 1000000000
function getConversiontRate(uint256 ethAmout) public view returns(uint256) {
uint256 ethPrice = getPrice();
// 1622533090090.000000000000000000
uint256 ethAmountInUSD = (ethPrice * ethAmout) / 1000000000000000000;
// 1622533090090 in cents
return ethAmountInUSD;
}
}
// SPDX-License-Identifier: MIT
pragma solidity >= 0.6.0 < 0.9.0;
contract SimpleStorage {
uint256 public favoriteNumber = 5;
int256 favoriteInt = -5;
bool favoriteBool = false;
string favoriteString = "Hello";
address favoriteAddress = 0xa34e9c2Ad2de0a98397F7154f45e91eAc6f8B74E;
bytes32 favoriteBytes = "Toto";
struct People {
uint256 favoriteNumber;
string name;
}
People public person = People({favoriteNumber: 2, name: "Patrick"});
People[] public people;
// A key val map
mapping(string => uint256) public nameToFavNumber;
function store(uint256 _favoriteNumber) public {
favoriteNumber = _favoriteNumber;
}
// view, pure are RO - non state changing
// pure is just for Math purpose
function retrieve() public view returns(uint256) {
return favoriteNumber;
}
function additionFunc(int256 number) public pure {
number + number;
}
// memory only store data for execution of the function or contract
// storage keyword the data can be processed after the execution
function addPerson(string memory _name, uint256 _nb) public {
people.push(People(_nb,_name));
nameToFavNumber[_name] = _nb;
}
}
// SPDX-License-Identifier: MIT
pragma solidity >= 0.6.0 < 0.9.0;
import "./SimpleStorage.sol";
contract StorageFactory is SimpleStorage{
SimpleStorage[] public stArray;
function createSimpleStorageContract() public {
SimpleStorage st = new SimpleStorage();
stArray.push(st);
}
function sfStore(uint256 _simpleStorageIndex, uint256 _simpleStorageNumber) public {
// Address
// ABI
SimpleStorage st = SimpleStorage(address(stArray[_simpleStorageIndex]));
st.store(_simpleStorageNumber);
}
function stGet(uint256 _simpleStorageIndex) public view returns(uint256) {
SimpleStorage st = SimpleStorage(address(stArray[_simpleStorageIndex]));
return st.retrieve();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment