Last active
November 10, 2022 05:46
-
-
Save shriomtri/eef750a3f4b40964d27c85af474489b8 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=
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.7; | |
contract Enum { | |
enum Status { | |
Pending, | |
Shipped, | |
Accepted, | |
Rejected, | |
Canceled | |
} | |
Status public status; | |
function get() public view returns (Status) { | |
return status; | |
} | |
function set(Status _status) public { | |
status = _status; | |
} | |
function cancel() public { | |
status = Status.Canceled; | |
} | |
} |
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.10; | |
contract Events { | |
event TestCalled(address sender, string message); | |
function test() public { | |
emit TestCalled(msg.sender, "Someone called test()"); | |
} | |
} |
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.7; | |
contract Mapping { | |
// Mapping from address to uint | |
mapping(address => uint) public myMap; | |
function get(address _addr) public view returns (uint) { | |
return myMap[_addr]; | |
} | |
function set(address _addr, uint _i) public { | |
myMap[_addr] = _i; | |
} | |
function remove(address _addr) public { | |
delete myMap[_addr]; | |
} | |
} |
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.10; | |
contract Modifiers { | |
address public owner; | |
constructor() { | |
// Set the contract deployer as the owner of the contract | |
owner = msg.sender; | |
} | |
// Create a modifier that only allows a function to be called by the owner | |
modifier onlyOwner() { | |
require(msg.sender == owner, "You are not the owner"); | |
// Underscore is a special character used inside modifiers | |
// Which tells Solidity to execute the function the modifier is used on | |
// at this point | |
// Therefore, this modifier will first perform the above check | |
// Then run the rest of the code | |
_; | |
} | |
// Create a function and apply the onlyOwner modifier on it | |
function changeOwner(address _newOwner) public onlyOwner { | |
// We will only reach this point if the modifier succeeds with its checks | |
// So the caller of this transaction must be the current owner | |
owner = _newOwner; | |
} | |
} |
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
contract SendEther { | |
function sendEth(address payable _to) public payable { | |
// Just forward the ETH received in this payable function | |
// to the given address | |
uint amountToSend = msg.value; | |
// call returns a bool value specifying success or failure | |
(bool success, bytes memory data) = _to.call{value: msg.value}(""); | |
require(success == true, "Failed to send ETH"); | |
} | |
} |
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.7; | |
contract TodoList { | |
struct Todo { | |
string text; | |
bool completed; | |
} | |
Todo[] public todos; | |
function createTodo(string memory _text) public { | |
todos.push(Todo(_text, false)); | |
todos.push(Todo({text: _text, completed: false })); | |
Todo memory todo; | |
todo.text = _text; | |
todo.completed = false; | |
todos.push(todo); | |
} | |
function update(uint _index, string memory _text) public { | |
todos[_index].text = _text; | |
} | |
function toggleCompleted(uint _index) public { | |
todos[_index].completed = !todos[_index].completed; | |
} | |
} |
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.13; | |
contract ViewAndPure { | |
uint public x = 1; | |
// Promise not to modify the state. | |
function addToX(uint y) public view returns (uint) { | |
return x + y; | |
} | |
// Promise not to modify or read from the state. | |
function add(uint i, uint j) public pure returns (uint) { | |
return i + j; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment