Skip to content

Instantly share code, notes, and snippets.

@youfoundron
Created July 27, 2018 19:09
Show Gist options
  • Save youfoundron/264272677547fe32d1c2eb2fd8294315 to your computer and use it in GitHub Desktop.
Save youfoundron/264272677547fe32d1c2eb2fd8294315 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.24;
import './SimpleRestrictedToken.sol';
contract MyRestrictedToken is SimpleRestrictedToken {
string public name;
string public symbol;
uint public decimals;
uint public totalSupply;
constructor() public {
// Map restriction codes to human-readable messages
restrictions[1] = 'ILLEGAL_TRANSFER_TO_ZERO_ADDRESS';
restrictions[2] = 'ILLEGAL_TRANSFER_TO_OWN_TOKEN_CONTRACT';
// Set token details and initial balances
name = 'MyRestrictedToken';
symbol = 'MRT';
decimals = 18;
totalSupply = 100 * (10 ** decimals);
balances[msg.sender] = totalSupply;
}
// Detect restricted transfers and return appropriate codes
function detectTransferRestriction (address to, address from, uint value)
public
constant
returns (uint restrictionCode)
{
if (to == 0x0) {
// illegal transfer to zero address
restrictionCode = 1;
} else if (to == address(this)) {
// illegal transfer to own token contract
restrictionCode = 2;
} else {
// successful transfer
restrictionCode = 0;
}
}
}
pragma solidity 0.4.24;
import 'https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol';
contract SimpleRestrictedToken is StandardToken {
mapping (uint => string) public restrictions;
constructor () public {
restrictions[0] = 'SUCCESS';
}
function detectTransferRestriction (address to, address from, uint value)
public
constant
returns (uint restrictionCode)
{
restrictionCode = 0;
}
function transfer (address to, uint value)
public
returns (bool success)
{
require(detectTransferRestriction(to, msg.sender, value) == 0);
success = super.transfer(to, value);
}
function transferFrom (address to, address from, uint value)
public
returns (bool success)
{
require(detectTransferRestriction(to, from, value) == 0);
success = super.transferFrom(to, from, value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment