I am quite new to solidity but am taking on work for a client to build a small dApp.

One thing I am struggling with is building a 'leasing' contract where:

Token holder leases token A to customer B for X ETH.

There is an agreed time frame for the length of the 'lease'.

Token A is returned to token holder after set timeframe expires.

Is this possible at all ? If not is there any other recommended approach?


With Tokens in Solidity the contract controls all the tokens regardless of the users. All the tokens are usually kept within a map commonly called "balanceOf" or a function with mimics that functionality where an address would would have a balance against it.

You could do something like:

pragma solidity ^0.4.0;

 * Basic expiry example using the expire cache on modify/read concept
 * based on block times (consider having a trigger method for metropolis)
contract MyToken {
     * Minimal viable token, ERC20, ERC223, etc code here

     mapping (address => uint) public _balanceOf;
     mapping (address => uint) public _expiryOf;

     uint leasePeriod = 100;
     uint price = 1 Ether;

      * Expire the leased tokens if the expiry time has passed and attach to 
      * all the functions which interact with balances
     modifier expiry(address _addr) {
         if (_balanceOf[_addr] > 0 && _expiryOf[_addr] < block.timestamp) {
             _expiryOf[_addr] = 0;
             _balanceOf[_addr] = 0;

     function lease(uint _amount) 
        returns (bool) {
        require(1 Ether == msg.value); // Assume we can only buy 1 at a time
        require(0 == _balanceOf[msg.sender]); // Only permit leasing 1 value at a time
        _expiryOf[msg.sender] = block.timestamp + leasePeriod;
        _balanceOf[msg.sender] += 1; // use safe math for this

    function balanceOf(address _addr) 
        returns (uint) {
        return _balanceOf[_addr];

This is just a crude concept based off block timestamps and the on read/modify expiry to keep things simple.

  • Out of curiosity - if I have already developed the initial smart contract can I extend methods on it later? My coin is already minted :( – Krishan Patel Oct 10 '17 at 10:39
  • Nope, you cannot modify an existing contract as all contracts are immutable. You will run into a few issues as if you want the user's address to be the holder of the coin they can still fully transact with the original contract. You could delegate a set amount of the token to contract and have the contract manage that on the user's behalf, but that's the best I can suggest. – James Lockhart Oct 10 '17 at 12:27

You can set the timeframe in the smart contract and have the token holder periodically check if it is expired.

What you cannot do is have the smart contract trigger this event by itself.

I.E. You can use now to get the unix timestamp of the last block, add your timeframe and check against that.

  • So essentially Token Holder would loan Token A to Customer B for 2 days. After 2 days, Token Holder would request Token A back to his account by sending gas to a smart contract - and it would then check if the time had expired before giving back the token to Token Holder? I guess I could automate a call to the smart contract after an expired time myself but it would be so much better if the smart contract could automate such a thing ! – Krishan Patel Oct 10 '17 at 9:42
  • Yes that's exactly what happens. – simonDos Oct 10 '17 at 11:24
  • 1
    Well, a blockchain is a database after all. It only ever does anything if you send transactions to it, so automatic event triggers based on expired time or smth will and should never be possible :) – simonDos Oct 10 '17 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.