Skip to content

Instantly share code, notes, and snippets.

@Juan-cc
Created December 20, 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/b0cc7693cf9eb0a300b70a97892abe3f to your computer and use it in GitHub Desktop.
Save Juan-cc/b0cc7693cf9eb0a300b70a97892abe3f 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.5.1+commit.c8a2cb62.js&optimize=true&gist=
pragma solidity ^0.5.1;
import "./Owned.sol";
import "./KMToken.sol";
import "./TokenFactory.sol";
import "./Storage.sol";
contract BCFactory is Owned, Storage {
event BCFactoryMsgSender(address indexed msgSender);
event BCFactoryCompanyCreated(address companyAddress, string _companyName, string _phone, string _url);
event BCFactoryPosition(uint8 position);
constructor(address deployer)
public
{
owner = deployer;
}
function createBCCompany(string memory _companyName, string memory _phone, string memory _url, string memory _did, address _uPortAddress)
public
// ownerOnly(msg.sender) // I won't control WHO because I want anybody to create companies.
returns (BC)
{
emit BCFactoryMsgSender(msg.sender);
uint8 position = nextCompanyAvailablePosition();
require(MAX_OWNER_COMPANIES > position, "You can't create a new company. Max limit reached (5 companies).");
BC newCompany = new BC(msg.sender, _companyName, _phone, _url, _did, _uPortAddress);
someCompany = address(newCompany);
companies[msg.sender][position] = address(newCompany);
emit BCFactoryCompanyCreated(address(newCompany), _companyName, _phone, _url);
return newCompany;
}
function nextCompanyAvailablePosition()
internal
returns (uint8)
{
address[MAX_OWNER_COMPANIES] memory ownerCompanies = companies[msg.sender];
for (uint8 i = 0; i < MAX_OWNER_COMPANIES; i++) {
if (ownerCompanies[i] == EMPTY_ADDRESS){
emit BCFactoryPosition(i);
return i; // This is the first available position.
}
}
return MAX_OWNER_COMPANIES; // No empty spot available.
}
}
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 => address[]) public tokens;
event BCCreated(address companyAddress, string _companyName, string _phone, string _url);
event BCTokenAdded(address indexed tokenAdded);
event BCMsgSender(address indexed msgSender);
constructor(address _owner, string memory _companyName, string memory _phone, string memory _url, string memory _did, address _uPortAddress)
public
{
owner = _owner;
emit BCMsgSender(msg.sender);
name = _companyName;
phone = _phone;
url = _url;
did = _did;
uPortAddress = _uPortAddress;
admins[owner] = true;
emit BCCreated(address(this), _companyName, _phone, _url);
}
/*
function addToken(address token)
public
ownerOnly(msg.sender)
{
require (0 != token && !tokens[token], "Token already exists or is null.");
tokens[token] = true;
emit BCTokenAdded(token);
}
function getToken(address token)
public
ownerOnly(msg.sender)
returns (KMToken)
{
require (0 != token && tokens[token], "Token not found.");
return KMToken(token);
}
*/
}
pragma solidity ^0.5.1;
import "./IERC20_510.sol";
import "./SafeMath_510.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
*
* This implementation emits additional Approval events, allowing applications to reconstruct the allowance status for
* all accounts just by listening to said events. Note that this isn't required by the specification, and other
* compliant implementations may not do it.
*/
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.
* 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.5.1;
/**
* @title ERC20 interface
* @dev see https://github.com/ethereum/EIPs/issues/20
*/
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address who) external view returns (uint256);
function allowance(address owner, address spender) external view returns (uint256);
function transfer(address to, uint256 value) external returns (bool);
function approve(address spender, uint256 value) external returns (bool);
function transferFrom(address from, address to, uint256 value) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
pragma solidity ^0.5.1;
import "./KMToken.sol";
import "./Owned.sol";
import "./BC.sol";
import "./TokenFactory.sol";
import "./Storage.sol";
contract KMP is Owned, Storage {
// Events
event KMPCompanyCreated(string _companyName, string _phone, string _url);
event KMPTokenCreated(address _company, string _name, string _symbol, uint256 _initialAmount);
event KMMsgSender(address indexed msgSender);
event KMUserTokenBalance(address indexed user, uint256 balance);
event KMReturnedToken(KMToken returnToken);
event KMReturnedBC(BC returnBC);
constructor() public {
bcFactory = new BCFactory(msg.sender);
tkFactory = new TokenFactory(msg.sender);
}
function factoryOwnerUtil()
public
view
returns (address)
{
return bcFactory.owner();
}
function testCreateBC() public {
address anAddress = 0xdA35deE8EDDeAA556e4c26268463e26FB91ff74f;
createBCCompany("companyName", "123456789", "www.google.com", "did:eth:0x2f3fcf4c3", anAddress);
}
function testCreateToken() public {
createTokenForBCCompany(someCompany, "Tokenzito", "TKZT", 100);
}
function testCreateToken(address anAddress) public{
createTokenForBCCompany(anAddress, "Tokenzito", "TKZT", 100);
}
function testAssignTokenToUser(address company, address token, address user, uint256 amount) public {
assignTokenToUser(company, token, user, 10);
}
function assignTokenToUser(address _company, address _token, address _user, uint256 _amount)
public
returns (bool tokenTransfered)
{
require(msg.sender == findBCowner(_company), "Only company owner can transfer tokens.");
address tokenFound = findBCToken(_company, _token);
if (EMPTY_ADDRESS != tokenFound){
bytes memory payload = abi.encodeWithSignature("transfer(address,uint256)", _user, _amount);
(bool tokenTransfered, bytes memory returnData) = address(tokenFound).delegatecall(payload);
(KMToken newToken) = abi.decode(returnData, (KMToken));
emit KMReturnedToken(newToken);
}
return tokenTransfered;
}
function createBCCompany(string memory _companyName, string memory _phone, string memory _url, string memory _did, address _uPortAddress)
public
// ownerOnly(msg.sender) I want anybody to be able to create a new BC on my platform.
returns(bool){
(bool companyCreated, bytes memory returnData) = address(bcFactory).delegatecall(
abi.encodeWithSignature("createBCCompany(string,string,string,string,address)",
_companyName, _phone, _url, _did, _uPortAddress));
(BC newCompany) = abi.decode(returnData, (BC));
emit KMReturnedBC(newCompany);
if (companyCreated){
emit KMPCompanyCreated(_companyName, _phone, _url);
return companyCreated;
}
revert("Unfortunately your company was not created correctly. Please contact KMP Support. Reverting state changes.");
}
function findBCownerUtil(address company) // Util methods are for development purposes only.
public
view
//ownerOnly(msg.sender)
returns (address)
{
return findBCowner(company);
}
function findBCowner(address aCompany)
internal
view
returns (address)
{
address[MAX_OWNER_COMPANIES] memory ownerCompanies = companies[msg.sender];
for (uint8 i = 0; i < MAX_OWNER_COMPANIES; i++) {
if (ownerCompanies[i] == aCompany){
return BC(ownerCompanies[i]).owner();
}
}
revert("Company address not found.");
}
function tokenInBC(address aCompany, address aToken)
public
returns (bool)
{
require(msg.sender == findBCowner(aCompany), "Only company owner can search for tokens.");
address[MAX_COMPANY_TOKENS] memory companyTokens = tokens[aCompany];
for (uint8 i = 0; i < MAX_COMPANY_TOKENS; i++) {
if (companyTokens[i] == aToken){
return true; // Token found.
}
}
return false; // Token not found.
}
function findBCToken(address aCompany, address aToken)
public
returns (address)
{
if (tokenInBC(aCompany, aToken)){
return aToken;
}
return EMPTY_ADDRESS;
}
/* function companiesLimitReached()
internal
view
returns (bool)
{
address anOwner = msg.sender; // Check if msg.sender gets here or if I need param.
address[MAX_OWNER_COMPANIES] memory ownersCompanies = companies[anOwner];
for (uint8 i = 0; i < MAX_OWNER_COMPANIES; i++) {
if (ownersCompanies[i] == 0){
return true; // There is an empty spot available.
}
}
return false; // No empty spot available.
}*/
function createTokenForBCCompany(address _bcCompany, string memory _name, string memory _symbol, uint256 _initialAmount)
public
//ownerOnly(companies[bcCompany].owner()) // Only Company owner can create tokens.
returns(bool){
require (msg.sender == findBCowner(_bcCompany), "Only company owner can create tokens.");
(bool tokenCreated, bytes memory returnData) = address(tkFactory).delegatecall(
abi.encodeWithSignature("createTokenForBCCompany(address,string,string,uint256)",
_bcCompany, _name, _symbol, _initialAmount));
(KMToken newToken) = abi.decode(returnData, (KMToken));
emit KMReturnedToken(newToken);
if (tokenCreated){
emit KMPTokenCreated(_bcCompany, _name, _symbol, _initialAmount);
return tokenCreated;
}
revert("Unfortunately your token was not created correctly. Please contact KMP Support. Reverting state changes.");
}
function kmpOwner()
public
view
returns (address)
{
return owner;
}
}
pragma solidity ^0.5.1;
//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";
import "./SafeMath_510.sol";
import "./ERC20_510.sol";
import "./IERC20_510.sol";
import "./Owned.sol";
contract KMToken is ERC20, Owned {
using SafeMath for uint256;
mapping (address => uint256) private _balances;
mapping (address => mapping (address => uint256)) private _allowed;
uint256 private _totalSupply;
address kmp;
string public symbol;
string public name;
uint8 public decimals = 0; // We wont work with fractions of a token.
event TokenMsgSender (address msgSender); // Mio
constructor(address _owner, string memory tokenName, string memory tokenSymbol, uint256 initialSupply) public {
kmp = msg.sender;
owner = _owner;
_balances[owner] = 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) {
emit TokenMsgSender(msg.sender);
_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.5.1;
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;
}
}
pragma solidity ^0.5.1;
/**
* @title SafeMath
* @dev Math operations with safety checks that revert on error
*/
library SafeMath {
int256 constant private INT256_MIN = -2**255;
/**
* @dev Multiplies two unsigned integers, reverts on overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b);
return c;
}
/**
* @dev Multiplies two signed integers, reverts on overflow.
*/
function mul(int256 a, int256 b) internal pure returns (int256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
if (a == 0) {
return 0;
}
require(!(a == -1 && b == INT256_MIN)); // This is the only case of overflow not detected by the check below
int256 c = a * b;
require(c / a == b);
return c;
}
/**
* @dev Integer division of two unsigned integers truncating the quotient, reverts on division by zero.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
// Solidity only automatically asserts when dividing by 0
require(b > 0);
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
/**
* @dev Integer division of two signed integers truncating the quotient, reverts on division by zero.
*/
function div(int256 a, int256 b) internal pure returns (int256) {
require(b != 0); // Solidity only automatically asserts when dividing by 0
require(!(b == -1 && a == INT256_MIN)); // This is the only case of overflow
int256 c = a / b;
return c;
}
/**
* @dev Subtracts two unsigned integers, reverts on overflow (i.e. if subtrahend is greater than minuend).
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a);
uint256 c = a - b;
return c;
}
/**
* @dev Subtracts two signed integers, reverts on overflow.
*/
function sub(int256 a, int256 b) internal pure returns (int256) {
int256 c = a - b;
require((b >= 0 && c <= a) || (b < 0 && c > a));
return c;
}
/**
* @dev Adds two unsigned integers, reverts on overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a);
return c;
}
/**
* @dev Adds two signed integers, reverts on overflow.
*/
function add(int256 a, int256 b) internal pure returns (int256) {
int256 c = a + b;
require((b >= 0 && c >= a) || (b < 0 && c < a));
return c;
}
/**
* @dev Divides two unsigned integers and returns the remainder (unsigned integer modulo),
* reverts when dividing by zero.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
require(b != 0);
return a % b;
}
}
pragma solidity ^0.5.1;
import "./BC.sol";
import "./TokenFactory.sol";
contract Storage {
// Shared storage
uint8 constant internal MAX_OWNER_COMPANIES = 5; // 1 owner could register up to 5 companies.
uint8 constant internal MAX_COMPANY_TOKENS = 10; // 1 company could register up to 10 tokens.
address constant internal EMPTY_ADDRESS = address(0);
mapping (address => address[MAX_OWNER_COMPANIES]) internal companies; // (owner => companies[5])
mapping (address => address[10]) internal tokens; // (company => token[]))
BCFactory internal bcFactory;
TokenFactory internal tkFactory;
address someCompany; // Delete - only for testing.
}
pragma solidity ^0.5.1;
import "./Owned.sol";
import "./BC.sol";
import "./KMToken.sol";
import "./Storage.sol";
contract TokenFactory is Owned, Storage {
event TokenFactoryMsgSender(address msgSender);
event TokenFactoryTokenCreated(address _bcCompany, address _token, string _name, string _symbol, uint256 _initialAmount);
event TokenFactoryPositionAvailable(uint8 position);
constructor(address deployer)
public
{
owner = deployer;
}
function createTokenForBCCompany(address _bcCompany, string memory _name, string memory _symbol, uint256 _initialAmount)
public
returns (KMToken)
{
emit TokenFactoryMsgSender(msg.sender);
require(msg.sender == findBCowner(_bcCompany)); // 2nd time checking correct ownership.
KMToken newToken = new KMToken(_bcCompany, _name, _symbol, _initialAmount);
tokens[_bcCompany][1] = address(newToken);
emit TokenFactoryTokenCreated(_bcCompany, address(newToken), _name, _symbol, _initialAmount);
return newToken;
}
function findBCowner(address aCompany)
internal
view
returns (address)
{
address[MAX_OWNER_COMPANIES] memory ownerCompanies = companies[msg.sender];
for (uint8 i = 0; i < MAX_OWNER_COMPANIES; i++) {
if (ownerCompanies[i] == aCompany){
return BC(ownerCompanies[i]).owner();
}
}
revert("Company address not found.");
}
function nextTokenAvailablePosition(address aCompany)
internal
returns (uint8)
{
require(msg.sender == findBCowner(aCompany), "Only company owner can search for tokens.");
address[MAX_COMPANY_TOKENS] memory companyTokens = tokens[aCompany];
for (uint8 i = 0; i < MAX_COMPANY_TOKENS; i++) {
if (companyTokens[i] == EMPTY_ADDRESS){
emit TokenFactoryPositionAvailable(i);
return i; // This is the first available position.
}
}
return MAX_COMPANY_TOKENS; // No empty spot available.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment