0

This Contract for locked token in "slots??" with a term. What is term here? and what his mean with slots. And if mint locked tokens how can unlock it?

contract LockSlots is ERC20Token {

using SafeMath for uint;

uint public constant LOCK_SLOTS = 5;
mapping(address => uint[LOCK_SLOTS]) public lockTerm;
mapping(address => uint[LOCK_SLOTS]) public lockAmnt;
mapping(address => bool) public mayHaveLockedTokens;

event RegisteredLockedTokens(address indexed account, uint indexed idx, uint tokens, uint **term**);

function registerLockedTokens(address _account, uint _tokens, uint _term) internal returns (uint idx) {
    require(_term > now, "lock term must be in the future");

    // find a slot (clean up while doing this)
    // use either the existing slot with the exact same term,
    // of which there can be at most one, or the first empty slot
    idx = 9999;
    uint[LOCK_SLOTS] storage term = lockTerm[_account];
    uint[LOCK_SLOTS] storage amnt = lockAmnt[_account];
    for (uint i; i < LOCK_SLOTS; i++) {
        if (term[i] < now) {
            term[i] = 0;
            amnt[i] = 0;
            if (idx == 9999) idx = i;
        }
        if (term[i] == _term) idx = i;
    }

    // fail if no slot was found
    require(idx != 9999, "registerLockedTokens: no available slot found");

    // register locked tokens
    if (term[idx] == 0) term[idx] = _term;
    amnt[idx] = amnt[idx].add(_tokens);
    mayHaveLockedTokens[_account] = true;
    emit RegisteredLockedTokens(_account, idx, _tokens, _term);
}

// public view functions

function lockedTokens(address _account) public view returns (uint) {
    if (!mayHaveLockedTokens[_account]) return 0;
    return pNumberOfLockedTokens(_account);
}

function unlockedTokens(address _account) public view returns (uint) {
    return balances[_account].sub(lockedTokens(_account));
}

function isAvailableLockSlot(address _account, uint _term) public view returns (bool) {
    if (!mayHaveLockedTokens[_account]) return true;
    if (_term < now) return true;
    uint[LOCK_SLOTS] storage term = lockTerm[_account];
    for (uint i; i < LOCK_SLOTS; i++) {
        if (term[i] < now || term[i] == _term) return true;
    }
    return false;
}

// internal and private functions

function unlockedTokensInternal(address _account) internal returns (uint) {
    // updates mayHaveLockedTokens if necessary
    if (!mayHaveLockedTokens[_account]) return balances[_account];
    uint locked = pNumberOfLockedTokens(_account);
    if (locked == 0) mayHaveLockedTokens[_account] = false;
    return balances[_account].sub(locked);
}

function pNumberOfLockedTokens(address _account) private view returns (uint locked) {
    uint[LOCK_SLOTS] storage term = lockTerm[_account];
    uint[LOCK_SLOTS] storage amnt = lockAmnt[_account];
    for (uint i; i < LOCK_SLOTS; i++) {
        if (term[i] >= now) locked = locked.add(amnt[i]);
    }
}

}

1 Answer 1

2

It appears that term here means "length of time from now".

The contract is locking up tokens for any given term, and it appears that the number of different "lockings" a single account can achieve is limed to 5 slots.

There would seem to be no way to unlock tokens, you just have to wait for the term to expire. Once that has happened, you may withdraw tokens the same way you would with any other ERC20 token (whose contract is inherited by this one, but not shown).

The mechanism for locking tokens here is simply to allocate a portion of your tokens as locked (when you lock them of course), and subtract that amount from your balance in the ERC20 token when you query it:

// ...
return balances[_account].sub(lockedTokens(_account));
// ...

Meaning that for all intents and purposes, you can no longer move the "locked" portion of your tokens, hence they are locked. When the term has expired, they are no longer subtracted via this line, and so can be moved as you wish and are ∴ "unlocked".

 


 

Edit: Re the type of term

In the contract it's a uint, and is directly compared to solidity's now, and so we can surmise that it's an UTC timestamp, aka: Unix Time or Epoch time.

7
  • Thanks for your answer, can you give me the type of term is it "names like "day , week", or unix time"?
    – TahaBA
    Dec 29, 2018 at 22:46
  • 1
    Have edited the above to answer your question, please accept the answer if it solves you query - thanks!
    – gskapka
    Dec 29, 2018 at 22:51
  • I will, when I add Unix Time the Tx got fail !!!?
    – TahaBA
    Dec 29, 2018 at 23:00
  • 1
    Did you get an error message for the failure? Where you using a unix time that's far enough into the future? Was your unix time in milliseconds or seconds?
    – gskapka
    Dec 29, 2018 at 23:12
  • 1
    Wohoo - glad it worked! No problem - happy coding!
    – gskapka
    Dec 29, 2018 at 23:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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