Skip to content

Instantly share code, notes, and snippets.

@ryestew
Created October 12, 2020 03:24
Show Gist options
  • Save ryestew/5362adf2000afa4cbdd2c6d239e06bf5 to your computer and use it in GitHub Desktop.
Save ryestew/5362adf2000afa4cbdd2c6d239e06bf5 to your computer and use it in GitHub Desktop.
Remix Lecture Sample Solidity Files
// 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;
}
}
// 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;
}
// call function - is blue nonpayable orange payable red & demo paying
pragma solidity ^0.6.0;
contract SimpleContract {
uint 256 fur;
function checkBalance() external view returns (uint256) {
return address(this).balance;
}
function setFur(uint256 _fur) public {
fur = _fur;
}
function setFurBank(uint256 _fur) public payable {
fur = _fur;
}
}
// 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;
}
}
// 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);
}
// 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);
}
}
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;
}
}
// 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
}
// 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;
}
}
// 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;
}
}
// 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