Created
March 2, 2022 17:44
-
-
Save erin-at-work/26e94aa3a2dbd7c59400e631bd36116b 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 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 AdvanceStorage { | |
mapping(address => uint8) public ages; | |
// register -> anybody can register by providing their address & age | |
function register(uint8 age) external returns(bool) { | |
// [address1 - 8] | |
ages[msg.sender] = age; | |
return true; | |
} | |
} |
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.7; | |
contract Functions { | |
// data | |
// setData | |
// setComputedData | |
// setMultiplier | |
uint256 public data; | |
uint256 public multiplier; | |
address public owner; | |
constructor(uint256 initialValue, uint256 initialMultiplier) { | |
data = initialValue; | |
multiplier = initialMultiplier; | |
owner = msg.sender; | |
} | |
function setMultiplier(uint256 newMultiplier) external onlyOwner returns(bool) { | |
multiplier = newMultiplier; | |
return true; | |
} | |
function setData(uint256 _newData) public onlyOwner returns(bool) { | |
data = _newData; | |
return true; | |
} | |
function setComputedData(uint256 _newData) external onlyOwner returns(bool) { | |
uint256 dataComputed = computeData(_newData); | |
return setData(dataComputed); | |
} | |
function computeData(uint256 someData) internal view returns(uint256){ | |
return someData * multiplier; | |
} | |
modifier onlyOwner { | |
require(msg.sender == owner, "Invalid owner"); | |
_; | |
} | |
} |
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.7; | |
contract Greeting { | |
function sayHello() external pure returns (string memory) { | |
return "hello world"; | |
} | |
} |
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.7; | |
abstract contract ContractD { | |
function whoAreYou() public virtual returns(string memory); | |
} | |
contract ContractC { | |
function whoAmI() public virtual view returns(string memory) { | |
return "contract C"; | |
} | |
} | |
contract ContractB { | |
function whoAmI() public virtual view returns(string memory) { | |
return "contract B"; | |
} | |
function whoAmIInternal() internal pure returns(string memory) { | |
return "contract B"; | |
} | |
} | |
contract ContractA is ContractB, ContractC, ContractD { | |
enum Type { None, ContractBType, ContractCType } | |
Type contractType; | |
constructor(Type initialType) { | |
contractType = initialType; | |
} | |
function whoAmI() override(ContractC, ContractB) public view returns (string memory) { | |
if(contractType == Type.ContractBType) { | |
return ContractB.whoAmI(); | |
} | |
if(contractType == Type.ContractCType) { | |
return ContractC.whoAmI(); | |
} | |
return "Contract A"; | |
} | |
function changeType(Type newType) external { | |
contractType = newType; | |
} | |
function whoAmIExternal() external pure returns(string memory){ | |
return whoAmIInternal(); | |
} | |
function whoAreYou() public pure override returns(string memory) { | |
return "a person"; | |
} | |
} |
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.7; | |
contract SimpleStorage { | |
// age of a person? | |
// uint8 -> 0-255 | |
// uint256 -> 0-2^256-1 | |
uint8 public age; | |
constructor() { | |
age = 10; | |
} | |
} | |
// Acccount -> Storage (how do we write to the storage) |
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.7; | |
contract Structs { | |
struct User { | |
uint8 age; | |
string name; | |
} | |
mapping(address => User) public users; | |
function register(uint8 age, string memory name) external returns(bool) { | |
users[msg.sender] = User(age, name); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment