1

I have following struct on my contract :

 struct Token {
        address tokenAddress;
        uint256 stakedAmount;
        bool isUsed;
        uint256 lastTransactionDate;
    }

It is used on following mapping :

mapping(address => mapping(address => Token)) public _stakingUserBalance

Each time user is stacking, I update stakedAmount with an amount and lastTransactionDate with block.timestamp.

In order to compute rewards, I need to know if stacking is more than 1 day. But I'm not sure to understand correctly unix timestamp process.

Is it a way to convert lastTransactionDate to a readable date, and compare with the moment of computing reward ?

Thanks for your help.

1 Answer 1

1

I had a similar issue, solved it with a function that refreshes time limit to zero if conditions are met.

First you need to create your own constant variable DAY,

uint constant DAY = 60 * 60 * 24; // Number of seconds in a day

 /**
 * Refreshes the transfer limit of the given address if it has been more than 24h since it last sent funds.
 */
function refreshTimeLimit(address addr, address tokenAddress) public {
    if (block.timestamp - _stakingUserBalance[addr].Token[tokenAddress].transactionTime > DAY) {
        _stakingUserBalance[addr].Token[tokenAddress].lastTransactionDate = block.timestamp;
        _stakingUserBalance[addr].Token[tokenAddress].stakedAmount = 0;
    }
}

/**
 * Returns the time/date of the last transaction from this address.
 */
function transactionTime(address addr, address tokenAddress) public view returns (uint){
    return  _stakingUserBalance[addr].Token[tokenAddress].lastTransactionDate;
}

/**
 * Returns the amount that had been stacked from the address in the last 24h.
 */
function stakedSum(address addr, address tokenAddress) public view returns (uint){
    return _stakingUserBalance[addr].Token[tokenAddress].stakedAmount;
}
1
  • Note: my mapping had been different, and I didn't had time to fully edit the code. But the concept is there.
    – Sky
    Jan 20, 2022 at 11:34

Your Answer

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

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