Skip to content

Instantly share code, notes, and snippets.

@Juan-cc
Created December 12, 2018 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Juan-cc/c5f0ab4decc1934957d96f8fc36a247b to your computer and use it in GitHub Desktop.
Save Juan-cc/c5f0ab4decc1934957d96f8fc36a247b 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 "./Owned.sol";
contract BC is Owned {
mapping (address => bool) public admins;
string public name;
string public phone;
string public url;
string private did;
address private uPortAddress;
mapping (address => bool) tokens;
constructor(address _owner, string _companyName, string _phone, string _url, string _did, address _uPortAddress)
public
{
owner = _owner; // Probably redundant if parent constructor executes.
name = _companyName;
phone = _phone;
url = _url;
did = _did;
uPortAddress = _uPortAddress;
}
}
/**
* We Won't be using BCFactory for now as we can't send strings on DELEGATECALL
* */
contract BCFactory is Owned {
/* Copy of KMP storage to allow DELEGATECALL*/
mapping (address => string) public companies;
address private bcFactory;
address private tokenFactory;
//mapping (address => BC) companies; I dont need to store anything in the factory, I need it in the KMPlatform
event BCCompanyCreated(address companyAddress, string _companyName, string _phone, string _url);
function createBCCompany(address _owner, string _companyName, string _phone, string _url, string _did, address _uPortAddress)
public
// ownerOnly(msg.sender) // Who calls me should be KMP owner only.
returns (BC)
{
BC newCompany = new BC(_owner, _companyName, _phone, _url, _did, _uPortAddress);
companies[newCompany] = _companyName;
emit BCCompanyCreated(newCompany, _companyName, _phone, _url);
return newCompany;
}
}
pragma solidity ^0.4.21;
contract A{
string v1;
string v2;
string v3;
event AEvent(string val1, string val2, string val3);
function aPirulo(string valor1, string valor2, string valor3){
B b = new B();
bool resp = b.delegatecall(bytes4(keccak256("bPirulo(string,string,string)")), valor1, valor2, valor3);
resp = b.delegatecall(bytes4(keccak256("bPirulo2(uint256,uint256,string,address,bytes)")), 54321, 12345, bytes32("hola"), "0xB279182D99E65703F0076E4812653aaB85FCA0f0", "aaa");
if (resp){
emit AEvent(valor1, valor2, valor3);
}
}
}
contract B{
string v1;
string v2;
string v3;
event BEvent(string val1, string val2, string val3);
event BEvent2(string val3,uint256 val1, uint256 val2, address addre, bytes something);
constructor() public {
}
function bPirulo(string valor1, string valor2, string valor3){
emit BEvent(valor1, valor2, valor3);
}
function bPirulo2(uint256 valor1, uint256 valor2, string valor3, address addr, bytes some){
emit BEvent2(valor3, valor1, valor2, addr, some);
}
}
/*
Implements EIP20 token standard: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md
.*/
pragma solidity ^0.4.21;
import "./EIP20Interface.sol";
contract EIP20 is EIP20Interface {
uint256 constant private MAX_UINT256 = 2**256 - 1;
mapping (address => uint256) public balances;
mapping (address => mapping (address => uint256)) public allowed;
/*
NOTE:
The following variables are OPTIONAL vanities. One does not have to include them.
They allow one to customise the token contract & in no way influences the core functionality.
Some wallets/interfaces might not even bother to look at this information.
*/
string public name; //fancy name: eg Simon Bucks
uint8 public decimals; //How many decimals to show.
string public symbol; //An identifier: eg SBX
function EIP20(
uint256 _initialAmount,
string _tokenName,
uint8 _decimalUnits,
string _tokenSymbol
) public {
balances[msg.sender] = _initialAmount; // Give the creator all initial tokens
totalSupply = _initialAmount; // Update total supply
name = _tokenName; // Set the name for display purposes
decimals = _decimalUnits; // Amount of decimals for display purposes
symbol = _tokenSymbol; // Set the symbol for display purposes
}
function transfer(address _to, uint256 _value) public returns (bool success) {
require(balances[msg.sender] >= _value);
balances[msg.sender] -= _value;
balances[_to] += _value;
emit Transfer(msg.sender, _to, _value); //solhint-disable-line indent, no-unused-vars
return true;
}
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
uint256 allowance = allowed[_from][msg.sender];
require(balances[_from] >= _value && allowance >= _value);
balances[_to] += _value;
balances[_from] -= _value;
if (allowance < MAX_UINT256) {
allowed[_from][msg.sender] -= _value;
}
emit Transfer(_from, _to, _value); //solhint-disable-line indent, no-unused-vars
return true;
}
function balanceOf(address _owner) public view returns (uint256 balance) {
return balances[_owner];
}
function approve(address _spender, uint256 _value) public returns (bool success) {
allowed[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value); //solhint-disable-line indent, no-unused-vars
return true;
}
function allowance(address _owner, address _spender) public view returns (uint256 remaining) {
return allowed[_owner][_spender];
}
}
import "./EIP20.sol";
pragma solidity ^0.4.21;
contract EIP20Factory {
mapping(address => address[]) public created;
mapping(address => bool) public isEIP20; //verify without having to do a bytecode check.
bytes public EIP20ByteCode; // solhint-disable-line var-name-mixedcase
function EIP20Factory() public {
//upon creation of the factory, deploy a EIP20 (parameters are meaningless) and store the bytecode provably.
address verifiedToken = createEIP20(10000, "Verify Token", 3, "VTX");
EIP20ByteCode = codeAt(verifiedToken);
}
//verifies if a contract that has been deployed is a Human Standard Token.
//NOTE: This is a very expensive function, and should only be used in an eth_call. ~800k gas
function verifyEIP20(address _tokenContract) public view returns (bool) {
bytes memory fetchedTokenByteCode = codeAt(_tokenContract);
if (fetchedTokenByteCode.length != EIP20ByteCode.length) {
return false; //clear mismatch
}
//starting iterating through it if lengths match
for (uint i = 0; i < fetchedTokenByteCode.length; i++) {
if (fetchedTokenByteCode[i] != EIP20ByteCode[i]) {
return false;
}
}
return true;
}
function createEIP20(uint256 _initialAmount, string _name, uint8 _decimals, string _symbol)
public
returns (address) {
EIP20 newToken = (new EIP20(_initialAmount, _name, _decimals, _symbol));
created[msg.sender].push(address(newToken));
isEIP20[address(newToken)] = true;
//the factory will own the created tokens. You must transfer them.
newToken.transfer(msg.sender, _initialAmount);
return address(newToken);
}
//for now, keeping this internal. Ideally there should also be a live version of this that
// any contract can use, lib-style.
//retrieves the bytecode at a specific address.
function codeAt(address _addr) internal view returns (bytes outputCode) {
assembly { // solhint-disable-line no-inline-assembly
// retrieve the size of the code, this needs assembly
let size := extcodesize(_addr)
// allocate output byte array - this could also be done without assembly
// by using outputCode = new bytes(size)
outputCode := mload(0x40)
// new "memory end" including padding
mstore(0x40, add(outputCode, and(add(add(size, 0x20), 0x1f), not(0x1f))))
// store length in memory
mstore(outputCode, size)
// actually retrieve the code, this needs assembly
extcodecopy(_addr, add(outputCode, 0x20), 0, size)
}
}
}
// Abstract contract for the full ERC 20 Token standard
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md
pragma solidity ^0.4.21;
contract EIP20Interface {
/* This is a slight change to the ERC20 base standard.
function totalSupply() constant returns (uint256 supply);
is replaced with:
uint256 public totalSupply;
This automatically creates a getter function for the totalSupply.
This is moved to the base contract since public getter functions are not
currently recognised as an implementation of the matching abstract
function by the compiler.
*/
/// total amount of tokens
uint256 public totalSupply;
/// @param _owner The address from which the balance will be retrieved
/// @return The balance
function balanceOf(address _owner) public view returns (uint256 balance);
/// @notice send `_value` token to `_to` from `msg.sender`
/// @param _to The address of the recipient
/// @param _value The amount of token to be transferred
/// @return Whether the transfer was successful or not
function transfer(address _to, uint256 _value) public returns (bool success);
/// @notice send `_value` token to `_to` from `_from` on the condition it is approved by `_from`
/// @param _from The address of the sender
/// @param _to The address of the recipient
/// @param _value The amount of token to be transferred
/// @return Whether the transfer was successful or not
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success);
/// @notice `msg.sender` approves `_spender` to spend `_value` tokens
/// @param _spender The address of the account able to transfer the tokens
/// @param _value The amount of tokens to be approved for transfer
/// @return Whether the approval was successful or not
function approve(address _spender, uint256 _value) public returns (bool success);
/// @param _owner The address of the account owning tokens
/// @param _spender The address of the account able to transfer the tokens
/// @return Amount of remaining tokens allowed to spent
function allowance(address _owner, address _spender) public view returns (uint256 remaining);
// solhint-disable-next-line no-simple-event-func-name
event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}
pragma solidity ^0.4.24;
import "./IERC20.sol";
import "../../math/SafeMath.sol";
/**
* @title Standard ERC20 token
*
* @dev Implementation of the basic standard token.
* https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md
* Originally based on code by FirstBlood: https://github.com/Firstbloodio/token/blob/master/smart_contract/FirstBloodToken.sol
*/
contract ERC20 is IERC20 {
using SafeMath for uint256;
mapping (address => uint256) private _balances;
mapping (address => mapping (address => uint256)) private _allowed;
uint256 private _totalSupply;
/**
* @dev Total number of tokens in existence
*/
function totalSupply() public view returns (uint256) {
return _totalSupply;
}
/**
* @dev Gets the balance of the specified address.
* @param owner The address to query the balance of.
* @return An uint256 representing the amount owned by the passed address.
*/
function balanceOf(address owner) public view returns (uint256) {
return _balances[owner];
}
/**
* @dev Function to check the amount of tokens that an owner allowed to a spender.
* @param owner address The address which owns the funds.
* @param spender address The address which will spend the funds.
* @return A uint256 specifying the amount of tokens still available for the spender.
*/
function allowance(
address owner,
address spender
)
public
view
returns (uint256)
{
return _allowed[owner][spender];
}
/**
* @dev Transfer token for a specified address
* @param to The address to transfer to.
* @param value The amount to be transferred.
*/
function transfer(address to, uint256 value) public returns (bool) {
_transfer(msg.sender, to, value);
return true;
}
/**
* @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
* Beware that changing an allowance with this method brings the risk that someone may use both the old
* and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this
* race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
* @param spender The address which will spend the funds.
* @param value The amount of tokens to be spent.
*/
function approve(address spender, uint256 value) public returns (bool) {
require(spender != address(0));
_allowed[msg.sender][spender] = value;
emit Approval(msg.sender, spender, value);
return true;
}
/**
* @dev Transfer tokens from one address to another
* @param from address The address which you want to send tokens from
* @param to address The address which you want to transfer to
* @param value uint256 the amount of tokens to be transferred
*/
function transferFrom(
address from,
address to,
uint256 value
)
public
returns (bool)
{
require(value <= _allowed[from][msg.sender]);
_allowed[from][msg.sender] = _allowed[from][msg.sender].sub(value);
_transfer(from, to, value);
return true;
}
/**
* @dev Increase the amount of tokens that an owner allowed to a spender.
* approve should be called when allowed_[_spender] == 0. To increment
* allowed value is better to use this function to avoid 2 calls (and wait until
* the first transaction is mined)
* From MonolithDAO Token.sol
* @param spender The address which will spend the funds.
* @param addedValue The amount of tokens to increase the allowance by.
*/
function increaseAllowance(
address spender,
uint256 addedValue
)
public
returns (bool)
{
require(spender != address(0));
_allowed[msg.sender][spender] = (
_allowed[msg.sender][spender].add(addedValue));
emit Approval(msg.sender, spender, _allowed[msg.sender][spender]);
return true;
}
/**
* @dev Decrease the amount of tokens that an owner allowed to a spender.
* approve should be called when allowed_[_spender] == 0. To decrement
* allowed value is better to use this function to avoid 2 calls (and wait until
* the first transaction is mined)
* From MonolithDAO Token.sol
* @param spender The address which will spend the funds.
* @param subtractedValue The amount of tokens to decrease the allowance by.
*/
function decreaseAllowance(
address spender,
uint256 subtractedValue
)
public
returns (bool)
{
require(spender != address(0));
_allowed[msg.sender][spender] = (
_allowed[msg.sender][spender].sub(subtractedValue));
emit Approval(msg.sender, spender, _allowed[msg.sender][spender]);
return true;
}
/**
* @dev Transfer token for a specified addresses
* @param from The address to transfer from.
* @param to The address to transfer to.
* @param value The amount to be transferred.
*/
function _transfer(address from, address to, uint256 value) internal {
require(value <= _balances[from]);
require(to != address(0));
_balances[from] = _balances[from].sub(value);
_balances[to] = _balances[to].add(value);
emit Transfer(from, to, value);
}
/**
* @dev Internal function that mints an amount of the token and assigns it to
* an account. This encapsulates the modification of balances such that the
* proper events are emitted.
* @param account The account that will receive the created tokens.
* @param value The amount that will be created.
*/
function _mint(address account, uint256 value) internal {
require(account != 0);
_totalSupply = _totalSupply.add(value);
_balances[account] = _balances[account].add(value);
emit Transfer(address(0), account, value);
}
/**
* @dev Internal function that burns an amount of the token of a given
* account.
* @param account The account whose tokens will be burnt.
* @param value The amount that will be burnt.
*/
function _burn(address account, uint256 value) internal {
require(account != 0);
require(value <= _balances[account]);
_totalSupply = _totalSupply.sub(value);
_balances[account] = _balances[account].sub(value);
emit Transfer(account, address(0), value);
}
/**
* @dev Internal function that burns an amount of the token of a given
* account, deducting from the sender's allowance for said account. Uses the
* internal burn function.
* @param account The account whose tokens will be burnt.
* @param value The amount that will be burnt.
*/
function _burnFrom(address account, uint256 value) internal {
require(value <= _allowed[account][msg.sender]);
// Should https://github.com/OpenZeppelin/zeppelin-solidity/issues/707 be accepted,
// this function needs to emit an event with the updated approval.
_allowed[account][msg.sender] = _allowed[account][msg.sender].sub(
value);
_burn(account, value);
}
}
pragma solidity ^0.4.24;
/*import "EIP20.sol";
import "EIP20Interface.sol";
import "github.com/ConsenSys/Tokens/contracts/eip20/EIP20Factory.sol";
*/
import "./KMToken.sol";
import "./Owned.sol";
import "./BC.sol";
contract KMP is Owned {
mapping (address => BC) public companies;
address private bcFactory;
address private tokenFactory;
event KMPCompanyCreated(address companyAddress, string _companyName, string _phone, string _url);
event KMPCompanyFound(address owner, address companyAddress, string _companyName, string _phone, string _url);
constructor() public {
//tokenFactory = new EIP20Factory();
//bcFactory = new BCFactory();
}
function createBCCompany(string _companyName, string _phone, string _url, string _did, address _uPortAddress)
public
// ownerOnly(msg.sender) I want anybody to be able to create a new BC on my platform.
returns(BC){
BC newCompany = new BC(msg.sender, _companyName, _phone, _url, _did, _uPortAddress);
companies[newCompany] = newCompany;
emit KMPCompanyCreated(newCompany, _companyName, _phone, _url);
return newCompany;
}
function findBC(address company)
public
view
ownerOnly(msg.sender)
returns (address)
{
BC aCompany = companies[company];
//emit KMPCompanyFound(aCompany.owner(), aCompany, aCompany.name(), aCompany.phone(), aCompany.url());
return aCompany.owner();
}
function createTokenForBCCompany(address bcCompany, uint256 _initialAmount, string _name, string _symbol)
public
//ownerOnly(msg.sender)
returns(address){
//return EIP20Factory(tokenFactory).createEIP20( _initialAmount, _name, _decimals, _symbol);
// TODO: EL QUE TIENE QUE LLAMAR LA CREACION DEL TOKEN ES BC, NO KMP ASI QUE
// HAY QUE USAR DELEGATECALL
KMToken token = new KMToken(_name, _symbol, _initialAmount);
}
}
pragma solidity ^0.4.24;
import "github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
import "github.com/OpenZeppelin/openzeppelin-solidity/contracts/math/SafeMath.sol";
import "github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
contract KMToken is ERC20 {
using SafeMath for uint256;
mapping (address => uint256) private _balances;
mapping (address => mapping (address => uint256)) private _allowed;
uint256 private _totalSupply;
address bcOwner;
string public symbol;
string public name;
uint8 public decimals = 0; // We wont work with fractions of a token.
constructor(string tokenName, string tokenSymbol, uint256 initialSupply) public {
bcOwner = msg.sender;
_balances[bcOwner] = initialSupply;
_totalSupply = initialSupply;
symbol = tokenSymbol;
name = tokenName;
}
/**
* @dev Total number of tokens in existence
*/
function totalSupply() public view returns (uint256) {
return _totalSupply;
}
/**
* @dev Gets the balance of the specified address.
* @param owner The address to query the balance of.
* @return An uint256 representing the amount owned by the passed address.
*/
function balanceOf(address owner) public view returns (uint256) {
return _balances[owner];
}
/**
* @dev Function to check the amount of tokens that an owner allowed to a spender.
* @param owner address The address which owns the funds.
* @param spender address The address which will spend the funds.
* @return A uint256 specifying the amount of tokens still available for the spender.
*/
function allowance(address owner, address spender) public view returns (uint256) {
return _allowed[owner][spender];
}
/**
* @dev Transfer token for a specified address
* @param to The address to transfer to.
* @param value The amount to be transferred.
*/
function transfer(address to, uint256 value) public returns (bool) {
_transfer(msg.sender, to, value);
return true;
}
/**
* @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
* Beware that changing an allowance with this method brings the risk that someone may use both the old
* and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this
* race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
* @param spender The address which will spend the funds.
* @param value The amount of tokens to be spent.
*/
function approve(address spender, uint256 value) public returns (bool) {
require(spender != address(0));
_allowed[msg.sender][spender] = value;
emit Approval(msg.sender, spender, value);
return true;
}
/**
* @dev Transfer tokens from one address to another.
* Note that while this function emits an Approval event, this is not required as per the specification,
* and other compliant implementations may not emit the event.
* @param from address The address which you want to send tokens from
* @param to address The address which you want to transfer to
* @param value uint256 the amount of tokens to be transferred
*/
function transferFrom(address from, address to, uint256 value) public returns (bool) {
_allowed[from][msg.sender] = _allowed[from][msg.sender].sub(value);
_transfer(from, to, value);
emit Approval(from, msg.sender, _allowed[from][msg.sender]);
return true;
}
/**
* @dev Increase the amount of tokens that an owner allowed to a spender.
* approve should be called when allowed_[_spender] == 0. To increment
* allowed value is better to use this function to avoid 2 calls (and wait until
* the first transaction is mined)
* From MonolithDAO Token.sol
* Emits an Approval event.
* @param spender The address which will spend the funds.
* @param addedValue The amount of tokens to increase the allowance by.
*/
function increaseAllowance(address spender, uint256 addedValue) public returns (bool) {
require(spender != address(0));
_allowed[msg.sender][spender] = _allowed[msg.sender][spender].add(addedValue);
emit Approval(msg.sender, spender, _allowed[msg.sender][spender]);
return true;
}
/**
* @dev Decrease the amount of tokens that an owner allowed to a spender.
* approve should be called when allowed_[_spender] == 0. To decrement
* allowed value is better to use this function to avoid 2 calls (and wait until
* the first transaction is mined)
* From MonolithDAO Token.sol
* Emits an Approval event.
* @param spender The address which will spend the funds.
* @param subtractedValue The amount of tokens to decrease the allowance by.
*/
function decreaseAllowance(address spender, uint256 subtractedValue) public returns (bool) {
require(spender != address(0));
_allowed[msg.sender][spender] = _allowed[msg.sender][spender].sub(subtractedValue);
emit Approval(msg.sender, spender, _allowed[msg.sender][spender]);
return true;
}
/**
* @dev Transfer token for a specified addresses
* @param from The address to transfer from.
* @param to The address to transfer to.
* @param value The amount to be transferred.
*/
function _transfer(address from, address to, uint256 value) internal {
require(to != address(0));
_balances[from] = _balances[from].sub(value);
_balances[to] = _balances[to].add(value);
emit Transfer(from, to, value);
}
/**
* @dev Internal function that mints an amount of the token and assigns it to
* an account. This encapsulates the modification of balances such that the
* proper events are emitted.
* @param account The account that will receive the created tokens.
* @param value The amount that will be created.
*/
function _mint(address account, uint256 value) internal {
require(account != address(0));
_totalSupply = _totalSupply.add(value);
_balances[account] = _balances[account].add(value);
emit Transfer(address(0), account, value);
}
/**
* @dev Internal function that burns an amount of the token of a given
* account.
* @param account The account whose tokens will be burnt.
* @param value The amount that will be burnt.
*/
function _burn(address account, uint256 value) internal {
require(account != address(0));
_totalSupply = _totalSupply.sub(value);
_balances[account] = _balances[account].sub(value);
emit Transfer(account, address(0), value);
}
/**
* @dev Internal function that burns an amount of the token of a given
* account, deducting from the sender's allowance for said account. Uses the
* internal burn function.
* Emits an Approval event (reflecting the reduced allowance).
* @param account The account whose tokens will be burnt.
* @param value The amount that will be burnt.
*/
function _burnFrom(address account, uint256 value) internal {
_allowed[account][msg.sender] = _allowed[account][msg.sender].sub(value);
_burn(account, value);
emit Approval(account, msg.sender, _allowed[account][msg.sender]);
}
}
pragma solidity ^0.4.24;
contract Owned{
/* TODO: Can be changed for Ownable from zepellin*/
address public owner;
constructor() public{
owner = msg.sender;
}
modifier ownerOnly(address _owner){
require(owner == _owner, "Access denied. Company owner only.");
_;
}
function modifyOwner(address _owner)
public
ownerOnly(msg.sender)
{
owner = _owner;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment