Created
October 12, 2020 03:24
-
-
Save ryestew/5362adf2000afa4cbdd2c6d239e06bf5 to your computer and use it in GitHub Desktop.
Remix Lecture Sample Solidity Files
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
// 2 contracts pick one | |
pragma solidity >=0.4.22 <0.7.0; | |
contract Puppy { function retrieve() public view returns (uint256) { | |
return 1 + 3; | |
} | |
} | |
contract PerfectPuppy { | |
function retrieve() public pure returns (uint256) { | |
return 1 + 3; | |
} | |
} |
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
// show the automatic compiler switch | |
pragma solidity >=0.6.0 <0.7.0; | |
contract SimpleContract { | |
uint256 a = 100; | |
uint256 b = 50; | |
uint256 public c = a + b; | |
} |
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
// Payable function and mapping | |
pragma solidity ^0.6.0; | |
contract SimpleContract { | |
mapping(address => uint256) public contributors; | |
function checkBalance() external view returns (uint256) { | |
return address(this).balance; | |
} | |
function contribute() external payable { | |
contributors[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
// importing through https safemath - then show how to import with remix.loadurl() | |
pragma solidity >=0.6.0 <0.7.0; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/SafeMath.sol"; | |
contract SimpleContract { | |
using SafeMath for uint256; | |
uint256 a = 2**256 - 1; | |
uint256 b = 1; | |
uint256 public c = a.add(b); | |
} |
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
// minting example then sending | |
pragma solidity ^0.6.0; | |
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.1.0/contracts/token/ERC20/ERC20.sol'; | |
contract PuppyCoin is ERC20 { | |
constructor() public ERC20('Puppy Coin', 'PYC') { | |
_mint(address(this), 1000000000000000000000000); | |
} | |
} |
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.4.22 <0.7.0; contract Users { | |
struct participant { address participantID; bool canBuy; | |
bool canSell; | |
uint8 exists; | |
} | |
mapping(address => participant) public participants; | |
function register(bool prosumer, bool consumer ) public { | |
require(participants[msg.sender].exists == 0, "already registerd"); | |
participant memory newParticipant; | |
newParticipant.participantID = msg.sender; newParticipant.canSell = prosumer; | |
newParticipant.canBuy = consumer; newParticipant.exists = 1; participants[msg.sender] = newParticipant; | |
} | |
} |
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
// intro to the overflow | |
pragma solidity >=0.6.0 <0.7.0; | |
contract SimpleContract { | |
uint8 a = 255; //max value is 2^8 - 1 (255) | |
uint8 b = 1; | |
uint8 public c = a + b; //Overflow - number is equal to 0 even though it is increased | |
} |
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
// display warning about pure | |
pragma solidity >=0.4.22 <0.7.0; | |
contract Puppy { | |
function retrieve() public view returns (uint256) { //shows warning (pure visibility) | |
return 1 + 3; | |
} | |
} |
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
// declaring var in return statement | |
pragma solidity >=0.6.0 <0.7.0; | |
contract SimpleContract { | |
function retrieve() public pure returns (uint256 dogsCount) { //declare variable in return | |
dogsCount = 1 + 3; | |
} | |
} |
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
// intro to the underflow | |
pragma solidity >=0.6.0 <0.7.0; | |
contract SimpleContract { | |
uint8 a = 0; //min value is 0 | |
uint8 b = 1; | |
uint8 public c = a - b; //Underflow - number is equal to 255 even though it is decreased | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment