Skip to content

Instantly share code, notes, and snippets.

@tuckcodes
Created January 1, 2019 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuckcodes/15909c37d113af91891f20bee9560fd8 to your computer and use it in GitHub Desktop.
Save tuckcodes/15909c37d113af91891f20bee9560fd8 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.5.1+commit.c8a2cb62.js&optimize=false&gist=
pragma solidity 0.5.1;
contract DogContract {
struct Dog {
string name;
uint age;
}
mapping(address => Dog) owner;
modifier onlyNewOwners(){
require(owner[msg.sender].age == 0, "Already owned!");
_;
}
function addDog(string memory _name, uint _age) public onlyNewOwners {
owner[msg.sender] = Dog(_name, _age);
}
function getDog() public view returns (string memory){
return owner[msg.sender].name;
}
}
pragma solidity 0.5.1;
contract DogContract {
struct Dog {
string name;
uint age;
}
mapping(address => Dog) owner;
// modifier allows us to create our own function modifier, such as view or pure, and scale it to many functions
modifier onlyNewOwners(){
// 'Already owned!' can be viewed in the debug section
require(owner[msg.sender].age == 0, "Already owned!");
_;
}
// add the modifer to the function declaration. makes it more simple to read as well
function addDog(string memory _name, uint _age) public onlyNewOwners {
owner[msg.sender] = Dog(_name, _age);
}
function getDog() public view returns (string memory){
return owner[msg.sender].name;
}
}
pragma solidity 0.5.1;
contract DogContract {
struct Dog {
string name;
uint age;
}
address owner;
mapping(address => Dog) dogOwner;
modifier onlyNewOwners(){
require(dogOwner[msg.sender].age == 0, "Already owned!");
_;
}
// _adddress is supplied within the function, then compared against msg.sender to verify the dog owner is the requester
modifier onlyOwner(address _address){
require(_address == msg.sender, "You are not the owner!");
_;
}
// constructor sets the contract owner immediatly - later used for validation
constructor() public {
owner = msg.sender;
}
// use onlyOwner to make sure that only the dog owner can delete the dog. Feed _address from function paramater to mofifier paramater
function deleteDog(address _address) public onlyOwner(_address) {
delete dogOwner[_address];
}
function addDog(string memory _name, uint _age) public onlyNewOwners {
dogOwner[msg.sender] = Dog(_name, _age);
}
function getDog(address _address) public view returns (string memory){
return dogOwner[_address].name;
}
}
pragma solidity 0.5.1;
/*
STORAGE TYPES
- storage (expensive)
persistant storage throughout the contract
for state variables (global variables)
- memory (normal cost)
for temporary varaibles
example, internal function variable that is not needed throughout the contract
- STACK (cheapest)
for local variables of simple types
- *simple TYPES
int, bool, etc, are 'simple types' and are small enough to fit within 256 bits, so do not need declared
*/
contract MemoryAndStorage {
mapping(uint => User) users;
struct User{
uint id;
uint balance;
}
function addUser(uint id, uint balance) public {
users[id] = User(id, balance);
}
function updateBalance(uint id, uint balance) public {
// is set to storage instead of memory so that the global variable is mutated
User storage user = users[id];
user.balance = balance;
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
}
pragma solidity 0.5.1;
contract DogContract {
/*
public
- can be called from outside and inside the contract (anyone can call)
external
- can only be called from the outside, but not the internal contract (api?)
internal
- can only be executed from inside the contract, or any contract that inherits from this contract
- for example, internal will make a usable function unusable on remix, such as addDog
private
- only available to the host contract, not even inherited contracts can use it
state varialbes can have public, internal and private (eg: 'address public owner;'). Public will make the variable 'clickable' on remix without building a function
*/
struct Dog {
string name;
uint age;
}
address owner;
mapping(address => Dog) dogOwner;
modifier onlyNewOwners(){
require(dogOwner[msg.sender].age == 0, "Already owned!");
_;
}
modifier onlyOwner(address _address){
require(_address == msg.sender, "You are not the owner!");
_;
}
constructor() public {
owner = msg.sender;
}
function deleteDog(address _address) public onlyOwner(_address) {
delete dogOwner[_address];
}
function addDog(string memory _name, uint _age) public onlyNewOwners {
dogOwner[msg.sender] = Dog(_name, _age);
}
function getDog(address _address) public view returns (string memory){
return dogOwner[_address].name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment