# How to add a Time lock token function on several addresses?

Hi, just learning about smart contract recently for fun. However, I stumbled on some problems. I create a crowdsale contract that works fine, but I want to add some function. I do not understand how it works, the feature that I want is the token lock on several addresses. For example, I transfer the token to 100 different address, and I want the token on those addresses locked for one year, how do I do that?

This is my contract, I’m using remix as the compiler;

``````pragma solidity ^0.4.23;
/**
* @title SafeMath
* @dev Math operations with safety checks that throw on error
*/
library SafeMath {
/**
* @dev Multiplies two numbers, throws on overflow.
**/
function mul(uint256 a, uint256 b) internal pure returns (uint256 c) {
if (a == 0) {
return 0;
}
c = a * b;
assert(c / a == b);
return c;
}

/**
* @dev Integer division of two numbers, truncating the quotient.
**/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
// uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return a / b;
}

/**
* @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
**/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a);
return a - b;
}

/**
* @dev Adds two numbers, throws on overflow.
**/
function add(uint256 a, uint256 b) internal pure returns (uint256 c) {
c = a + b;
assert(c >= a);
return c;
}
}
/**
* @title Ownable
* @dev The Ownable contract has an owner address, and provides basic authorization control
* functions, this simplifies the implementation of "user permissions".
**/

contract Ownable {
/**
* @dev The Ownable constructor sets the original `owner` of the contract to the sender account.
**/
constructor() public {
owner = msg.sender;
}

/**
* @dev Throws if called by any account other than the owner.
**/
modifier onlyOwner() {
require(msg.sender == owner);
_;
}

/**
* @dev Allows the current owner to transfer control of the contract to a newOwner.
* @param newOwner The address to transfer ownership to.
**/
function transferOwnership(address newOwner) public onlyOwner {
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
}
}
/**
* @title ERC20Basic interface
* @dev Basic ERC20 interface
**/
contract ERC20Basic {
function totalSupply() public view returns (uint256);
function balanceOf(address who) public view returns (uint256);
function transfer(address to, uint256 value) public returns (bool);
}
/**
* @title ERC20 interface
* @dev see https://github.com/ethereum/EIPs/issues/20
**/
contract ERC20 is ERC20Basic {
function approve(address spender, uint256 value) public returns (bool);
}
/**
* @title Basic token
* @dev Basic version of StandardToken, with no allowances.
**/
contract BasicToken is ERC20Basic {
using SafeMath for uint256;
uint256 totalSupply_;

/**
* @dev total number of tokens in existence
**/
function totalSupply() public view returns (uint256) {
}

/**
* @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) {
require(_value <= balances[msg.sender]);

balances[msg.sender] = balances[msg.sender].sub(_value);
emit Transfer(msg.sender, _to, _value);
return true;
}

/**
* @dev Gets the balance of the specified address.
* @param _owner The address to query the 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];
}
}
contract StandardToken is ERC20, BasicToken {
/**
* @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
**/
require(_value <= balances[_from]);
require(_value <= allowed[_from][msg.sender]);

balances[_from] = balances[_from].sub(_value);
allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);

emit Transfer(_from, _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) {
allowed[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
return true;
}

/**
* @dev Function to check the amount of tokens that an owner allowed to a spender.
* @return A uint256 specifying the amount of tokens still available for the spender.
**/
return allowed[_owner][_spender];
}

/**
* @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.
**/
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 decreaseApproval(address _spender, uint _subtractedValue) public returns (bool) {
uint oldValue = allowed[msg.sender][_spender];
if (_subtractedValue > oldValue) {
allowed[msg.sender][_spender] = 0;
} else {
allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
}
emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
return true;
}
}
/**
* @title Configurable
* @dev Configurable varriables of the contract
**/
contract Configurable {
uint256 public constant cap = 21000000*10**18;
uint256 public constant basePrice = 100000*10**18; // tokens per 1 ether
uint256 public tokensSold = 0;

uint256 public constant tokenReserve = 1000000*10**18;
uint256 public remainingTokens = 0;
}
/**
* @title CrowdsaleToken
* @dev Contract to preform crowd sale with token
**/
contract CrowdsaleToken is StandardToken, Configurable, Ownable {
/**
* @dev enum of current crowd sale state
**/
enum Stages {
none,
icoStart,
icoEnd
}

Stages currentStage;

/**
* @dev constructor of CrowdsaleToken
**/
constructor() public {
currentStage = Stages.none;
remainingTokens = cap;
}

/**
* @dev fallback function to send ether to for Crowd sale
**/
function () public payable {
require(currentStage == Stages.icoStart);
require(msg.value > 0);
require(remainingTokens > 0);

uint256 weiAmount = msg.value; // Calculate tokens to sell
uint256 tokens = weiAmount.mul(basePrice).div(1 ether);
uint256 returnWei = 0;

uint256 newTokens = cap.sub(tokensSold);
uint256 newWei = newTokens.div(basePrice).mul(1 ether);
returnWei = weiAmount.sub(newWei);
weiAmount = newWei;
tokens = newTokens;
}

tokensSold = tokensSold.add(tokens); // Increment raised amount
remainingTokens = cap.sub(tokensSold);
if(returnWei > 0){
msg.sender.transfer(returnWei);
}

owner.transfer(weiAmount);// Send money to owner
}
/**
* @dev startIco starts the public ICO
**/
function startIco() public onlyOwner {
require(currentStage != Stages.icoEnd);
currentStage = Stages.icoStart;
}
/**
* @dev endIco closes down the ICO
**/
function endIco() internal {
currentStage = Stages.icoEnd;
// Transfer any remaining tokens
if(remainingTokens > 0)
// transfer any remaining ETH balance in the contract to the owner
}
/**
* @dev finalizeIco closes down the ICO and sets needed varriables
**/
function finalizeIco() public onlyOwner {
require(currentStage != Stages.icoEnd);
endIco();
}

}
/**
* @title Test Learning Token
* @dev Contract to create the Test Learning Token
**/
contract testlearingToken is CrowdsaleToken {
string public constant name = "Test Learning Token";
string public constant symbol = "TLT";
uint32 public constant decimals = 18;
}``````
1 Like

Hi @zam,

Welcome to the community.

If you are creating an ERC20 I suggest looking at Points to consider when creating a fungible token (ERC20, ERC777)

OpenZeppelin Contracts include Crowdsales in v2.x (Crowdsales were removed in v3.x)

You could look at extending/modifying a Crowdsale along the lines of PostDeliveryCrowdsale

Alternatively you could create a TokenTimelock for each beneficiary, though you would likely want to create minimal proxies to reduce the gas cost of deploying multiple contracts.

Hi @abcoathup,
Thanks for the reference link you gave me it help part of my plan. I want to ask another question, is it possible to make that no token transferable beside the owner transfer it until a specific time, but all token minted from crowdsales can be transferred freely anytime to anywhere. For example, the token reserved on my address, I want it to become a private sale, I move it manually to particular addresses, and I want that token to be locked for 1 year on that address not on other smart contract, and one month later I start the crowdsales function and token minted from crowdsales doesn’t affect by the lock period

1 Like

Hi @zam,

If I understand you correctly, you want to do a private sale, lock those tokens for 1 year but tokens purchased in the crowd sale shouldn’t have any restrictions.

Whilst you could put locking functionality in the token itself (something like a deny list on transfers until a certain date), my personal preference is that the token doesn’t include any functionality that isn’t required for the life of the token. So instead I would look at locking the tokens in a contract (such as a token timelock) that the private sale purchasers can withdraw when they can be released.

I recommend reading and implementing a prevention method to protect against Bypassing Smart Contract Timelocks.

As always, I recommend looking at Points to consider when creating a fungible token (ERC20, ERC777) including seeking appropriate advice on regulatory compliance.

Thanks @abcoathup,
Would you mind give me some more reference to this?

I cant find some kind of documentation for it.

1 Like

Hi @zam,

My preference is to not include functionality in the token not required for the life of the token. I think you may be better off using some form of time lock contract.

If you wanted to build a deny list, you could use access control
OpenZeppelin Contracts 3.x: https://docs.openzeppelin.com/contracts/3.x/access-control#using-access-control
OpenZeppelin Contracts 2.x: https://docs.openzeppelin.com/contracts/2.x/access-control#using-roles

You can see this concept in:
https://docs.openzeppelin.com/contracts/2.x/api/crowdsale#WhitelistCrowdsale