Skip to content

Instantly share code, notes, and snippets.

@psvehla
Created March 1, 2020 00:57
Show Gist options
  • Save psvehla/803ba915ba19fbd52eb73ef05fcd00a4 to your computer and use it in GitHub Desktop.
Save psvehla/803ba915ba19fbd52eb73ef05fcd00a4 to your computer and use it in GitHub Desktop.
pragma solidity ^0.6.2;
contract SoapBox {
// Our 'dict' of addresses that are approved to share opinions
mapping (address => bool) approvedSoapboxer;
string opinion;
// Our event to announce an opinion on the blockchain
event OpinionBroadcast(address _soapboxer, string _opinion);
// This is a constructor function, so its name has to match the contract
constructor() public {
}
// Because this function is 'payable' it will be called when ether is sent to the contract address.
function pay() public payable {
// msg is a special variable that contains information about the transaction
if (msg.value > 20000000000000000) {
//if the value sent greater than 0.02 ether (in Wei)
// then add the sender's address to approvedSoapboxer
approvedSoapboxer[msg.sender] = true;
}
}
// Our read-only function that checks whether the specified address is approved to post opinions.
function isApproved(address _soapboxer) public view returns (bool approved) {
return approvedSoapboxer[_soapboxer];
}
// Read-only function that returns the current opinion
function getCurrentOpinion() public view returns(string memory) {
return opinion;
}
//Our function that modifies the state on the blockchain
function broadcastOpinion(string memory _opinion) public returns (bool success) {
// Looking up the address of the sender will return false if the sender isn't approved
if (approvedSoapboxer[msg.sender]) {
opinion = _opinion;
emit OpinionBroadcast(msg.sender, opinion);
return true;
}
else {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment