Created
January 22, 2022 09:51
-
-
Save escaper01/11bab26319f26f498936bcfc204d9a68 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.0+commit.c7dfd78e.js&optimize=false&runs=200&gist=
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
pragma solidity ^0.6.0; | |
contract MyContract { | |
// state variable | |
/* | |
//number | |
int public myInt = 1; | |
uint public myUint = 1; | |
int256 public myInt256 = 1; | |
int8 public myInt8 = 10; | |
// string | |
string public myString = "hello escaper"; | |
bytes32 public myBytes32 = "Heyyyy"; | |
//address | |
address public myAdress = 0xf9a4b6d6E700D88Bd21D38a7Da28b2865dE30c0E; | |
//struct | |
struct MyStruct { | |
uint age; | |
string name; | |
} | |
MyStruct public myStructInstance = MyStruct(23, 'escaper'); | |
//array | |
uint[] public uintArray = [1,2,3]; | |
string[] public stringArray = ["ali","escaper"]; | |
string[] public values; | |
uint[][] public array2D = [[4,5,6],[1,2,3]]; | |
function addValue(string memory _value) public { | |
values.push(_value); | |
} | |
function getLength() public view returns(uint) { | |
return values.length; | |
} | |
// local variables | |
function getValue() public pure returns(uint) { | |
uint myval = 1998; | |
return myval; | |
} | |
//mapping | |
mapping(uint => string) public names; | |
constructor() public{ | |
names[1] = "escaper"; | |
names[2] = "ali"; | |
names[3] = "omar"; | |
} | |
struct Book { | |
string title; | |
string author; | |
} | |
mapping(uint => Book) public books; | |
function addBook(uint _id, string memory _title, string memory _author) public { | |
books[_id] = Book(_title, _author); | |
} | |
mapping(uint => Book) public books; | |
mapping(address => mapping(uint => Book)) public myBooks; | |
struct Book { | |
string title; | |
string author; | |
} | |
function addBook(uint _id, string memory _title, string memory _author) public { | |
books[_id] = Book(_title, _author); | |
} | |
function addMyBook(uint _id, string memory _title, string memory _author) public { | |
myBooks[msg.sender][_id] = Book(_title, _author); | |
} | |
//conditionals and loops | |
uint[] public numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | |
function countEvenNumbers() public view returns (uint) { | |
uint count = 0; | |
for(uint i = 0; i < numbers.length; i++) { | |
if(isEvenNumber(numbers[i])) { | |
count ++; | |
} | |
} | |
return count; | |
} | |
function isEvenNumber(uint _number) public pure returns(bool) { | |
if(_number % 2 == 0) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
*/ | |
} |
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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.3; | |
contract HelloWorld { | |
string public greeting = "Hi escaper"; | |
} |
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
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity ^0.8.0; | |
contract Bank { | |
address public bankOwner; | |
string public bankName; | |
mapping(address => uint256) public customerBalance; | |
constructor() { | |
bankOwner = msg.sender; | |
} | |
function depositMoney() public payable { | |
require(msg.value != 0, "You need to deposit some amount of money!"); | |
customerBalance[msg.sender] += msg.value; | |
} | |
function setBankName(string memory _name) external { | |
require( | |
msg.sender == bankOwner, | |
"You must be the owner to set the name of the bank" | |
); | |
bankName = _name; | |
} | |
function withdrawMoney(address payable _to, uint256 _total) public { | |
require( | |
_total <= customerBalance[msg.sender], | |
"You have insuffient funds to withdraw" | |
); | |
customerBalance[msg.sender] -= _total; | |
_to.transfer(_total); | |
} | |
function getCustomerBalance() external view returns (uint256) { | |
return customerBalance[msg.sender]; | |
} | |
function getBankBalance() public view returns (uint256) { | |
require( | |
msg.sender == bankOwner, | |
"You must be the owner of the bank to see all balances." | |
); | |
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
pragma solidity ^0.6.0; | |
contract HotelRoom { | |
enum Statues { Vacant, Occupied } | |
Statues currentStatues; | |
event Occupy(address _occupant, uint _value); | |
address payable public owner; | |
constructor () public { | |
owner = msg.sender; | |
currentStatues = Statues.Vacant; | |
} | |
modifier onlyWhileVacant { | |
//check status | |
require(currentStatues == Statues.Vacant, "Currently occupied"); | |
_; | |
} | |
modifier costs(uint _amount) { | |
//check the price | |
require(msg.value >= _amount, "Not enough Ether provided"); | |
_; | |
} | |
receive() external payable onlyWhileVacant costs(2 ether) { | |
currentStatues = Statues.Occupied; | |
owner.transfer(msg.value); | |
emit Occupy(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
pragma solidity ^0.6.0; | |
contract Ownable { | |
address owner; | |
constructor() public { | |
owner = msg.sender; | |
} | |
modifier onlyOwner() { | |
require(msg.sender == owner, 'must be owner'); | |
_; | |
} | |
} | |
contract SecretVault { | |
constructor(string memory _secret) public { | |
secret = _secret; | |
} | |
function getSecret() public view returns(string memory) { | |
return secret; | |
} | |
} | |
contract MyContract is Ownable { | |
string secret; | |
constructor(string memory _secret) public { | |
SecretVault _secretVault = new SecretVault(_secret); | |
secret = address(_secretVault); | |
super; | |
} | |
function getSecret() public view returns(string memory) { | |
SecretVault _secretVault = SecretVault(_secret); | |
return _secretVault.getSecret(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment