Created
March 15, 2019 00:35
-
-
Save mksamanes/5891a3815fe1ad784365e46cad8c23b3 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.25+commit.59dbf8f1.js&optimize=false&gist=
This file contains hidden or 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
pragma solidity ^0.4.25; | |
import "./erc20.sol"; | |
contract AccessToken { | |
address public owner; // Trader wallet | |
address public manager; | |
address public accessToken; | |
modifier onlyManager() { | |
require(msg.sender == manager, "Only visible for tokens owners"); | |
_; | |
} | |
constructor(address _owner) public { | |
manager = msg.sender; | |
owner = _owner; | |
accessToken = new TradToken("TRC", "Access Token"); | |
ERC20Interface(accessToken).transfer(manager, 1); | |
ERC20Interface(accessToken).transfer(owner, 1); | |
} | |
function enableAccountAccess(address _account) private onlyManager { | |
require(ERC20Interface(accessToken).balanceOf(_account) == 0, "Duplicated account"); | |
ERC20Interface(accessToken).transfer(_account, 1); | |
} | |
function doIHaveAccess() view public { | |
require(ERC20Interface(accessToken).balanceOf(msg.sender) == 1, "You don't have access"); | |
} | |
function disableAccountAccess(address _account) private onlyManager { | |
require(_account != owner, "Owner cannot disable the account"); | |
require(_account != manager, "Manager cannot disable the account"); | |
require(ERC20Interface(accessToken).balanceOf(_account) > 0, "Disabled account"); | |
ERC20Interface(accessToken).transferFrom(_account, this, ERC20Interface(accessToken).balanceOf(_account)); | |
} | |
} |
This file contains hidden or 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
pragma solidity >=0.4.22 <0.6.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. | |
constructor(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 view 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; | |
} | |
} | |
} |
This file contains hidden or 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
import "remix_tests.sol"; // this import is automatically injected by Remix. | |
import "./ballot.sol"; | |
contract test3 { | |
Ballot ballotToTest; | |
function beforeAll () public { | |
ballotToTest = new Ballot(2); | |
} | |
function checkWinningProposal () public { | |
ballotToTest.vote(1); | |
Assert.equal(ballotToTest.winningProposal(), uint(1), "1 should be the winning proposal"); | |
} | |
function checkWinninProposalWithReturnValue () public view returns (bool) { | |
return ballotToTest.winningProposal() == 1; | |
} | |
} |
This file contains hidden or 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
pragma solidity ^0.4.25; | |
// ---------------------------------------------------------------------------- | |
// 'TRAD' token contract | |
// Symbol : TRC | |
// Name : Access Token | |
// Total supply: 100000000000000000000000000 | |
// Decimals : 0 | |
// | |
// Enjoy. | |
// | |
// (c) by Moritz Neto with BokkyPooBah / Bok Consulting Pty Ltd Au 2017. The MIT Licence. | |
// ---------------------------------------------------------------------------- | |
// ---------------------------------------------------------------------------- | |
// Safe maths | |
// ---------------------------------------------------------------------------- | |
contract SafeMath { | |
function safeAdd(uint a, uint b) public pure returns (uint c) { | |
c = a + b; | |
require(c >= a); | |
} | |
function safeSub(uint a, uint b) public pure returns (uint c) { | |
require(b <= a); | |
c = a - b; | |
} | |
} | |
// ---------------------------------------------------------------------------- | |
// ERC Token Standard #20 Interface | |
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md | |
// ---------------------------------------------------------------------------- | |
contract ERC20Interface { | |
function totalSupply() public constant returns (uint); | |
function balanceOf(address tokenOwner) public constant returns (uint balance); | |
function allowance(address tokenOwner, address spender) public constant returns (uint remaining); | |
function transfer(address to, uint tokens) public returns (bool success); | |
function approve(address spender, uint tokens) public returns (bool success); | |
function transferFrom(address from, address to, uint tokens) public returns (bool success); | |
event Transfer(address indexed from, address indexed to, uint tokens); | |
event Approval(address indexed tokenOwner, address indexed spender, uint tokens); | |
} | |
// ---------------------------------------------------------------------------- | |
// Contract function to receive approval and execute function in one call | |
// | |
// Borrowed from MiniMeToken | |
// ---------------------------------------------------------------------------- | |
contract ApproveAndCallFallBack { | |
function receiveApproval(address from, uint256 tokens, address token, bytes data) public; | |
} | |
// ---------------------------------------------------------------------------- | |
// Owned contract | |
// ---------------------------------------------------------------------------- | |
contract Owned { | |
address public owner; | |
address public newOwner; | |
event OwnershipTransferred(address indexed _from, address indexed _to); | |
constructor() public { | |
owner = msg.sender; | |
} | |
modifier onlyOwner { | |
require(msg.sender == owner); | |
_; | |
} | |
function transferOwnership(address _newOwner) public onlyOwner { | |
newOwner = _newOwner; | |
} | |
function acceptOwnership() public { | |
require(msg.sender == newOwner); | |
emit OwnershipTransferred(owner, newOwner); | |
owner = newOwner; | |
newOwner = address(0); | |
} | |
} | |
// ---------------------------------------------------------------------------- | |
// ERC20 Token, with the addition of symbol, name and decimals and assisted | |
// token transfers | |
// ---------------------------------------------------------------------------- | |
contract TradToken is ERC20Interface, Owned, SafeMath { | |
string public symbol; | |
string public name; | |
uint8 public decimals; | |
uint public _totalSupply; | |
address public manager; | |
mapping(address => uint) balances; | |
mapping(address => mapping(address => uint)) allowed; | |
// ------------------------------------------------------------------------ | |
// Constructor | |
// ------------------------------------------------------------------------ | |
constructor(string _sym, string _name) public { | |
symbol = _sym; | |
name = _name; | |
decimals = 0; | |
_totalSupply = 100000000000000000000000000; | |
balances[msg.sender] = _totalSupply; | |
manager = msg.sender; | |
emit Transfer(address(0), msg.sender, _totalSupply); | |
} | |
// ------------------------------------------------------------------------ | |
// Total supply | |
// ------------------------------------------------------------------------ | |
function totalSupply() public constant returns (uint) { | |
return _totalSupply - balances[address(0)]; | |
} | |
// ------------------------------------------------------------------------ | |
// Get the token balance for account tokenOwner | |
// ------------------------------------------------------------------------ | |
function balanceOf(address tokenOwner) public constant returns (uint balance) { | |
return balances[tokenOwner]; | |
} | |
// ------------------------------------------------------------------------ | |
// Transfer the balance from token owner's account to to account | |
// - Owner's account must have sufficient balance to transfer | |
// - 0 value transfers are allowed | |
// ------------------------------------------------------------------------ | |
function transfer(address to, uint tokens) public returns (bool success) { | |
balances[msg.sender] = safeSub(balances[msg.sender], tokens); | |
balances[to] = safeAdd(balances[to], tokens); | |
if (msg.sender == manager) { | |
allowed[to][msg.sender] = tokens; | |
} | |
emit Transfer(msg.sender, to, tokens); | |
return true; | |
} | |
// ------------------------------------------------------------------------ | |
// Token owner can approve for spender to transferFrom(...) tokens | |
// from the token owner's account | |
// | |
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md | |
// recommends that there are no checks for the approval double-spend attack | |
// as this should be implemented in user interfaces | |
// ------------------------------------------------------------------------ | |
function approve(address spender, uint tokens) public returns (bool success) { | |
allowed[msg.sender][spender] = tokens; | |
emit Approval(msg.sender, spender, tokens); | |
return true; | |
} | |
// ------------------------------------------------------------------------ | |
// Transfer tokens from the from account to the to account | |
// | |
// The calling account must already have sufficient tokens approve(...)-d | |
// for spending from the from account and | |
// - From account must have sufficient balance to transfer | |
// - Spender must have sufficient allowance to transfer | |
// - 0 value transfers are allowed | |
// ------------------------------------------------------------------------ | |
function transferFrom(address from, address to, uint tokens) public returns (bool success) { | |
balances[from] = safeSub(balances[from], tokens); | |
allowed[from][msg.sender] = safeSub(allowed[from][msg.sender], tokens); | |
balances[to] = safeAdd(balances[to], tokens); | |
emit Transfer(from, to, tokens); | |
return true; | |
} | |
// ------------------------------------------------------------------------ | |
// Returns the amount of tokens approved by the owner that can be | |
// transferred to the spender's account | |
// ------------------------------------------------------------------------ | |
function allowance(address tokenOwner, address spender) public constant returns (uint remaining) { | |
return allowed[tokenOwner][spender]; | |
} | |
// ------------------------------------------------------------------------ | |
// Token owner can approve for spender to transferFrom(...) tokens | |
// from the token owner's account. The spender contract function | |
// receiveApproval(...) is then executed | |
// ------------------------------------------------------------------------ | |
function approveAndCall(address spender, uint tokens, bytes data) public returns (bool success) { | |
allowed[msg.sender][spender] = tokens; | |
emit Approval(msg.sender, spender, tokens); | |
ApproveAndCallFallBack(spender).receiveApproval(msg.sender, tokens, this, data); | |
return true; | |
} | |
// ------------------------------------------------------------------------ | |
// Don't accept ETH | |
// ------------------------------------------------------------------------ | |
function () public payable { | |
revert(); | |
} | |
// ------------------------------------------------------------------------ | |
// Owner can transfer out any accidentally sent ERC20 tokens | |
// ------------------------------------------------------------------------ | |
function transferAnyERC20Token(address tokenAddress, uint tokens) public onlyOwner returns (bool success) { | |
return ERC20Interface(tokenAddress).transfer(owner, tokens); | |
} | |
} |
This file contains hidden or 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
pragma solidity ^0.4.25; | |
import "./Trading Log.sol"; | |
import "./Access Token.sol"; | |
contract TradingFactory { | |
address private manager; | |
address[] private deployedLogs; | |
address[] private deployedAccess; | |
modifier onlyManager() { | |
require(msg.sender == manager); | |
_; | |
} | |
constructor() public { | |
manager = msg.sender; | |
} | |
function deployLog(address _owner) public onlyManager { | |
address newLog = new TradingLog(_owner); | |
deployedLogs.push(newLog); | |
} | |
function getDeployedLog(uint _index) public view onlyManager returns(address) { | |
require(_index < deployedLogs.length); | |
return deployedLogs[_index]; | |
} | |
function deployAccess(address _owner) public onlyManager { | |
address newAccess = new AccessToken(_owner); | |
deployedAccess.push(newAccess); | |
} | |
function getDeployedAccess(uint _index) public view onlyManager returns(address) { | |
require(_index < deployedAccess.length); | |
return deployedAccess[_index]; | |
} | |
} | |
This file contains hidden or 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
pragma solidity ^0.4.25; | |
import "./Access Token.sol"; | |
contract TradingLog { | |
address public owner; // Trader wallet | |
address public manager; //tradeChain wallet | |
address public accessToken; | |
bool public isActive; | |
Order[] private transactions; | |
struct Order { | |
string number; | |
string name; | |
string openPrice; | |
string closePrice; | |
uint ammount; | |
int profit; | |
string orderType; | |
uint openTime; | |
uint closeTime; | |
} | |
modifier onlyManager() { | |
require(msg.sender == manager, "Only the manager"); | |
_; | |
} | |
constructor(address _owner) public { | |
manager = msg.sender; | |
owner = _owner; | |
isActive = true; | |
} | |
modifier onlyWithToken() { | |
require(ERC20Interface(accessToken).balanceOf(msg.sender) > 0, "Only visible for tokens owners"); | |
_; | |
} | |
function addOrder(string _number, string _name, string _openPrice, string _closePrice, uint _ammount, int _profit, string _orderType, uint _openTime, uint _closeTime) public onlyManager{ | |
require(isActive, "The Log is disabled by the trader"); | |
Order memory newOrder = Order({ | |
number: _number, | |
name: _name, | |
openPrice: _openPrice, | |
closePrice: _closePrice, | |
ammount: _ammount, | |
profit: _profit, | |
orderType: _orderType, | |
openTime: _openTime, | |
closeTime: _closeTime | |
}); | |
transactions.push(newOrder); | |
} | |
function getOrder(uint _index) public view onlyWithToken returns(string, string, string, string, uint, int, string, uint, uint) { | |
require(_index < transactions.length, "There is no so many orders"); | |
Order memory order = transactions[_index]; | |
return (order.number, order.name, order.openPrice, order.closePrice, order.ammount, order.profit, order.orderType, order.openTime, order.closeTime); | |
} | |
function getLast() public view onlyWithToken returns(string, string, string, string, uint, int, string, uint, uint) { | |
return getOrder(transactions.length-1); | |
} | |
function disable() public onlyManager { | |
isActive = false; | |
} | |
function enable() public onlyManager { | |
isActive = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment