Skip to content

Instantly share code, notes, and snippets.

@heathdrobertson
Last active May 26, 2020 16:15
Show Gist options
  • Save heathdrobertson/8404a38d0d2d27d85f379fa8bf33ecc6 to your computer and use it in GitHub Desktop.
Save heathdrobertson/8404a38d0d2d27d85f379fa8bf33ecc6 to your computer and use it in GitHub Desktop.
Basic Solidity Contract Layout
pragma solidity >=0.4.21 <0.7.0;
// It's important to avoid vulnerabilities due to numeric overflow bugs
// OpenZeppelin's SafeMath library, when used correctly, protects agains such bugs
// More info: https://www.nccgroup.trust/us/about-us/newsroom-and-events/blog/2018/\
// november/smart-contract-insecurity-bad-arithmetic/
import "@openzeppelin/contracts/math/SafeMath.sol";
import "@Openzeppelin/contracts/access/Ownable.sol";
/* FlightSurety Smart Contract *********************************************************/
contract SampleContract {
using SafeMath for uint256;
/* DATA VARIABLES ******************************************************************/
string public data;
struct Data {
uint8 status;
uint256 timestamp;
address owner;
string name;
}
mapping(bytes32 => Data) private data;
/* EVENT DEFINITIONS ***************************************************************/
event DataSaved(string data, string _message);
/* FUNCTION MODIFIERS **************************************************************/
modifier requireOwner() {
require(msg.sender == Owner, "Caller is not owner");
_;
}
/* CONSTRUCTOR *********************************************************************/
constructor(address payable dataContractAddress) public
{
contractOwner = msg.sender;
dataContractAddress = dataContractAddress;
data = "Seed Data";
}
/* UTILITY FUNCTIONS ***************************************************************/
function getData() view external returns(string memory) {
return data;
}
function setData(string calldata _data) external {
data = _data;
emit DataSaved(_data, "Data has been saved");
}
/* SMART CONTRACT FUNCTIONS ********************************************************/
function normalThing() public {
// anyone can call this normalThing()
}
function specialThing() public onlyOwner {
// only the owner can call specialThing()!
}
}
/** DATA CONTRACT INTERFACE ************************************************************/
contract DataContract{
function getThingState(address thing) external returns(uint);
function createThing(address thingAddress, uint8 state, string calldata name)
external;
function getPassengerBalance(address passenger) external pure returns (uint256);
function payPassenger(address passenger) external;
}
/* FlightSurety Smart Contract *********************************************************/
/* DATA VARIABLES ******************************************************************/
/* EVENT DEFINITIONS ***************************************************************/
/* FUNCTION MODIFIERS **************************************************************/
/* CONSTRUCTOR *********************************************************************/
/* UTILITY FUNCTIONS ***************************************************************/
/* SMART CONTRACT FUNCTIONS ********************************************************/
/** DATA CONTRACT INTERFACE ************************************************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment