Skip to content

Instantly share code, notes, and snippets.

@hackervera
Created June 6, 2018 23:26
Show Gist options
  • Save hackervera/ebe28caf1db738fff1800f243cdb60a6 to your computer and use it in GitHub Desktop.
Save hackervera/ebe28caf1db738fff1800f243cdb60a6 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.4.24+commit.e67f0147.js&optimize=false&gist=
pragma solidity ^0.4.0;
contract Ballot {
struct Voter {
uint weight;
bool voted;
uint8 vote;
address delegate;
}
struct Proposal {
uint voteCount;
}
address chairperson;
mapping(address => Voter) voters;
Proposal[] proposals;
/// Create a new ballot with $(_numProposals) different proposals.
function Ballot(uint8 _numProposals) public {
chairperson = msg.sender;
voters[chairperson].weight = 1;
proposals.length = _numProposals;
}
/// Give $(toVoter) the right to vote on this ballot.
/// May only be called by $(chairperson).
function giveRightToVote(address toVoter) public {
if (msg.sender != chairperson || voters[toVoter].voted) return;
voters[toVoter].weight = 1;
}
/// Delegate your vote to the voter $(to).
function delegate(address to) public {
Voter storage sender = voters[msg.sender]; // assigns reference
if (sender.voted) return;
while (voters[to].delegate != address(0) && voters[to].delegate != msg.sender)
to = voters[to].delegate;
if (to == msg.sender) return;
sender.voted = true;
sender.delegate = to;
Voter storage delegateTo = voters[to];
if (delegateTo.voted)
proposals[delegateTo.vote].voteCount += sender.weight;
else
delegateTo.weight += sender.weight;
}
/// Give a single vote to proposal $(toProposal).
function vote(uint8 toProposal) public {
Voter storage sender = voters[msg.sender];
if (sender.voted || toProposal >= proposals.length) return;
sender.voted = true;
sender.vote = toProposal;
proposals[toProposal].voteCount += sender.weight;
}
function winningProposal() public constant returns (uint8 _winningProposal) {
uint256 winningVoteCount = 0;
for (uint8 prop = 0; prop < proposals.length; prop++)
if (proposals[prop].voteCount > winningVoteCount) {
winningVoteCount = proposals[prop].voteCount;
_winningProposal = prop;
}
}
}
pragma solidity ^0.4.18;
import 'https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC20/DetailedERC20.sol';
// import 'zeppelin-solidity/contracts/token/DetailedERC20.sol';
//import 'zeppelin-solidity/contracts/token/MintableToken.sol';
import 'https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC20/MintableToken.sol';
import './ServiceRegistry.sol';
import './RegulatorService.sol';
/// @notice An ERC-20 token that has the ability to check for trade validity
contract RegulatedToken is DetailedERC20, MintableToken {
mapping (address => bool) public whitelist;
/**
* @notice R-Token decimals setting (used when constructing DetailedERC20)
*/
uint8 constant public RTOKEN_DECIMALS = 18;
/**
* @notice Triggered when regulator checks pass or fail
*/
event CheckStatus(uint8 reason, address indexed spender, address indexed from, address indexed to, uint256 value);
/**
* @notice Address of the `ServiceRegistry` that has the location of the
* `RegulatorService` contract responsible for checking trade
* permissions.
*/
ServiceRegistry public registry;
/**
* @notice Constructor
*
* @param _name Name of the token: See DetailedERC20
* @param _symbol Symbol of the token: See DetailedERC20
*/
// function RegulatedToken(ServiceRegistry _registry, string _name, string _symbol) public
function RegulatedToken(string _name, string _symbol) public
DetailedERC20(_name, _symbol, RTOKEN_DECIMALS)
{
// require(_registry != address(0));
// registry = _registry;
}
// Update whitelist
function updateWhitelist(address[] _whitelist){
uint256 i=0;
for(i;i<_whitelist.length;i++){
address a = _whitelist[i];
whitelist[a] = true;
}
}
/**
* @notice ERC-20 overridden function that include logic to check for trade validity.
*
* @param _to The address of the receiver
* @param _value The number of tokens to transfer
*
* @return `true` if successful and `false` if unsuccessful
*/
function transfer(address _to, uint256 _value) public returns (bool) {
// if (_check(msg.sender, _to, _value)) {
if (whitelist[_to]){
emit CheckStatus(0, msg.sender, msg.sender, _to, _value);
return super.transfer(_to, _value);
} else {
emit CheckStatus(1, msg.sender, msg.sender, _to, _value);
return false;
}
}
/**
* @notice ERC-20 overridden function that include logic to check for trade validity.
*
* @param _from The address of the sender
* @param _to The address of the receiver
* @param _value The number of tokens to transfer
*
* @return `true` if successful and `false` if unsuccessful
*/
function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
if (_check(_from, _to, _value)) {
return super.transferFrom(_from, _to, _value);
} else {
return false;
}
}
/**
* @notice Performs the regulator check
*
* @dev This method raises a CheckStatus event indicating success or failure of the check
*
* @param _from The address of the sender
* @param _to The address of the receiver
* @param _value The number of tokens to transfer
*
* @return `true` if the check was successful and `false` if unsuccessful
*/
function _check(address _from, address _to, uint256 _value) private returns (bool) {
var reason = _service().check(this, msg.sender, _from, _to, _value);
CheckStatus(reason, msg.sender, _from, _to, _value);
return reason == 0;
}
/**
* @notice Retreives the address of the `RegulatorService` that manages this token.
*
* @dev This function *MUST NOT* memoize the `RegulatorService` address. This would
* break the ability to upgrade the `RegulatorService`.
*
* @return The `RegulatorService` that manages this token.
*/
function _service() constant public returns (RegulatorService) {
return RegulatorService(registry.service());
}
}
pragma solidity ^0.4.18;
/// @notice Standard interface for `RegulatorService`s
contract RegulatorService {
/*
* @notice This method *MUST* be called by `RegulatedToken`s during `transfer()` and `transferFrom()`.
* The implementation *SHOULD* check whether or not a transfer can be approved.
*
* @dev This method *MAY* call back to the token contract specified by `_token` for
* more information needed to enforce trade approval.
*
* @param _token The address of the token to be transfered
* @param _spender The address of the spender of the token
* @param _from The address of the sender account
* @param _to The address of the receiver account
* @param _amount The quantity of the token to trade
*
* @return uint8 The reason code: 0 means success. Non-zero values are left to the implementation
* to assign meaning.
*/
function check(address _token, address _spender, address _from, address _to, uint256 _amount) public returns (uint8);
}
pragma solidity ^0.4.18;
import './RegulatorService.sol';
import 'https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/ownership/Ownable.sol';
/// @notice A service that points to a `RegulatorService`
contract ServiceRegistry is Ownable {
address public service;
/**
* @notice Triggered when service address is replaced
*/
event ReplaceService(address oldService, address newService);
/**
* @dev Validate contract address
* Credit: https://github.com/Dexaran/ERC223-token-standard/blob/Recommended/ERC223_Token.sol#L107-L114
*
* @param _addr The address of a smart contract
*/
modifier withContract(address _addr) {
uint length;
assembly { length := extcodesize(_addr) }
require(length > 0);
_;
}
/**
* @notice Constructor
*
* @param _service The address of the `RegulatorService`
*
*/
function ServiceRegistry(address _service) public {
service = _service;
}
/**
* @notice Replaces the address pointer to the `RegulatorService`
*
* @dev This method is only callable by the contract's owner
*
* @param _service The address of the new `RegulatorService`
*/
function replaceService(address _service) onlyOwner withContract(_service) public {
address oldService = service;
service = _service;
ReplaceService(oldService, service);
}
}
pragma solidity ^0.4.0;
contract Moneh {
string message;
function getMessage() public view returns (string _message){
_message = message;
}
function setMessage(string m) public{
message = m;
}
}
pragma solidity ^0.4.18;
import './RegulatedToken.sol';
/**
* TestRegulatedToken is a RegulatedToken meant for testing purposes.
* Developers can mint an unlimited number of TestRegulatedTokens.
* TestRegulatedToken is meant to be instantiated with a ServiceRegistry
* that points to an instance of TestRegulatorService.
*/
contract TestRegulatedToken is RegulatedToken {
function TestRegulatedToken( string _name, string _symbol) public
RegulatedToken( _name, _symbol)
{
}
/**
* Override zeppelin.MintableToken.mint() without onlyOwner or canMint modifiers.
*/
function mint(address _to, uint256 _amount) public returns (bool) {
balances[_to] = balances[_to].add(_amount);
Mint(_to, _amount);
Transfer(address(0), _to, _amount);
return true;
}
}
pragma solidity ^0.4.18;
import './RegulatorService.sol';
/**
* TestRegulatorService is a RegulatorService meant for testing purposes.
* It returns check() reason codes based on the amount of the transaction.
*/
contract TestRegulatorService is RegulatorService {
uint8 constant private SUCCESS = 0; /* 0 <= AMOUNT < 10 */
uint8 constant private ELOCKED = 1; /* 10 <= AMOUNT < 20 */
uint8 constant private EDIVIS = 2; /* 20 <= AMOUNT < 30 */
uint8 constant private ESEND = 3; /* 30 <= AMOUNT < 40 */
uint8 constant private ERECV = 4; /* 40 <= AMOUNT < 50 */
/**
* @notice Checks whether or not a trade should be approved by checking the trade amount
*
* @param _token The address of the token to be transfered
* @param _spender The address of the spender of the token (unused in this implementation)
* @param _from The address of the sender account
* @param _to The address of the receiver account
* @param _amount The quantity of the token to trade
*
* @return `true` if the trade should be approved and `false` if the trade should not be approved
*/
function check(address _token, address _spender, address _from, address _to, uint256 _amount) public returns (uint8) {
if (_amount >= 10 && _amount < 20) {
return ELOCKED;
}
else if (_amount >= 20 && _amount < 30) {
return EDIVIS;
}
else if (_amount >= 30 && _amount < 40) {
return ESEND;
}
else if (_amount >= 40 && _amount < 50) {
return ERECV;
}
return SUCCESS;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment