library Checkpoints:Transform openzeppelin

打印 上一主题 下一主题

主题 567|帖子 567|积分 1701

使用时间戳替换区块号,check数字支持uint256。
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. library Checkpoints {
  4.         struct data{
  5.         uint32 fromTime;
  6.         uint256 quantity;
  7.         }
  8.     struct Checkpoint {
  9.                 mapping (address => mapping (uint32 => data)) checkpoints;
  10.                 mapping (address => uint32) numCheckpoints;
  11.     }
  12.     // --------------checkpoint-----------
  13.         function _getCurrentQuantity(Checkpoint storage checkpoint,address account) internal view returns (uint256) {
  14.         uint32 nCheckpoints = checkpoint.numCheckpoints[account];
  15.         return nCheckpoints > 0 ? checkpoint.checkpoints[account][nCheckpoints - 1].quantity : 0;
  16.     }
  17.         function getCurrentQuantity(Checkpoint storage checkpoint,address account) external view returns (uint256) {
  18.         return _getCurrentQuantity(checkpoint,account);
  19.     }
  20.     function getPriorQuantity(Checkpoint storage checkpoint,address account, uint blockTimestamp) external view returns (uint256) {
  21.         return _getPriorQuantity(checkpoint,account,blockTimestamp);
  22.     }
  23.     function _getPriorQuantity(Checkpoint storage checkpoint,address account, uint blockTimestamp) internal view returns (uint256) {
  24.         require(blockTimestamp < block.timestamp, "ShipSell::getPriorQuantity: not yet determined");
  25.         uint32 nCheckpoints = checkpoint.numCheckpoints[account];
  26.         if (nCheckpoints == 0) {
  27.             return 0;
  28.         }
  29.         // First check most recent balance
  30.         if (checkpoint.checkpoints[account][nCheckpoints - 1].fromTime <= blockTimestamp) {
  31.             return checkpoint.checkpoints[account][nCheckpoints - 1].quantity;
  32.         }
  33.         // Next check implicit zero balance
  34.         if (checkpoint.checkpoints[account][0].fromTime > blockTimestamp) {
  35.             return 0;
  36.         }
  37.         uint32 lower = 0;
  38.         uint32 upper = nCheckpoints - 1;
  39.         while (upper > lower) {
  40.             uint32 center = upper - (upper - lower) / 2; // ceil, avoiding overflow
  41.             data memory cp = checkpoint.checkpoints[account][center];
  42.             if (cp.fromTime == blockTimestamp) {
  43.                 return cp.quantity;
  44.             } else if (cp.fromTime < blockTimestamp) {
  45.                 lower = center;
  46.             } else {
  47.                 upper = center - 1;
  48.             }
  49.         }
  50.         return checkpoint.checkpoints[account][lower].quantity;
  51.     }
  52.     function _writeCheckpoint(Checkpoint storage checkpoint,address account, uint256 newQuantity) internal {
  53.       uint32 blockTimestamp = uint32(block.timestamp);
  54.       uint32 nCheckpoints = checkpoint.numCheckpoints[msg.sender];
  55.       if (nCheckpoints > 0 && checkpoint.checkpoints[account][nCheckpoints - 1].fromTime == blockTimestamp) {
  56.           checkpoint.checkpoints[account][nCheckpoints - 1].quantity = newQuantity;
  57.       } else {
  58.           checkpoint.checkpoints[account][nCheckpoints] = data(blockTimestamp, newQuantity);
  59.           checkpoint.numCheckpoints[account] = nCheckpoints + 1;
  60.       }
  61.     }
  62.     function WriteCheckpoint(Checkpoint storage checkpoint,address account, uint256 newQuantity) external {
  63.         _writeCheckpoint(checkpoint,account,newQuantity);
  64.     }
  65. }
复制代码
mock:
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity 0.8.7;
  3. import "./Checkpoints.sol";
  4. contract CheckpointsMock{
  5.     using Checkpoints for Checkpoints.Checkpoint;
  6.     Checkpoints.Checkpoint example;
  7.     function write(uint256 amount)external{
  8.         uint256 current = Checkpoints.getCurrentQuantity(example,msg.sender);
  9.         Checkpoints.WriteCheckpoint(example,msg.sender,current+amount);
  10.     }
  11.     function readCurrent()public view returns (uint256){
  12.         return Checkpoints.getCurrentQuantity(example,msg.sender);
  13.     }
  14.     function readPrior(uint256 fromTime)public view returns (uint256){
  15.         return Checkpoints.getPriorQuantity(example,msg.sender,fromTime);
  16.     }
  17. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

十念

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表