Timestamp

如何檢查契約上的金額是否超過一天

  • January 20, 2022

我的契約上有以下​​結構:

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

它用於以下映射:

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

每次使用者堆疊時,我都會更新stakedAmount數量lastTransactionDateblock.timestamp.

為了計算獎勵,我需要知道堆疊是否超過 1 天。但我不確定是否能正確理解 unix 時間戳過程。

是否可以轉換lastTransactionDate為可讀日期,並與計算獎勵的時刻進行比較?

謝謝你的幫助。

我有一個類似的問題,如果滿足條件,可以通過將時間限制刷新為零的功能解決它。

首先,您需要創建自己的常量變數 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;
}

引用自:https://ethereum.stackexchange.com/questions/119438