【问题标题】:Solidity storing timestamp/value data thats easily accessibleSolidity 存储易于访问的时间戳/值数据
【发布时间】:2022-08-24 05:07:43
【问题描述】:

我有一个有趣的用例,我似乎无法解决。

问题:代币每天获得 X 点。我想冻结 ERC721 代币(它们有 ID)一段时间。在此期间,他们每天获得 0 分。

我有以下计算点:

uint32 public constant SECONDS_IN_DAY = 1 days;
struct UserInfo {
  uint256 itemCount;
  uint256 pendingPoints;
  uint256 lastUpdate;
}

mapping(address => UserInfo) public userInfo;


function pending(address account) public view returns (uint256) {
  uint256 pendingPoints = userInfo[account].pendingPoints + (((block.timestamp - userInfo[account].lastUpdate) / SECONDS_IN_DAY) * (userInfo[account].itemCount));
  return pendingPoints;
}

modifier updatePoints(address account) {
  userInfo[account].pendingPoints = pending(account);
  userInfo[account].lastUpdate = block.timestamp;
  _;
}

我无法弄清楚的问题:

  1. 如何存储每个令牌冻结多长时间,以便我可以准确确定何时减少pending 函数中的点。
  2. 以省油的方式执行此操作。

    我已经考虑过添加一个映射,该映射包含一个时间戳和每天在UserInfo 结构中减少的数量,但是我将无法检索此信息。

    mapping(uint256 => uint256) perDayPointDeductions;
    

    接下来我可以尝试什么?

    标签: solidity


    【解决方案1】:

    也许像快照或/和chainlink keeper这样的东西可能是解决这个问题的可靠方法,也许你可以检查一些staking机制是如何工作的,因为你面临的问题是类似的staking

    【讨论】:

      【解决方案2】:

      我不确定我是否很好地理解了这个问题,但在这种情况下,我会将数据存储在链下,例如工具https://thegraph.com/en/

      我会在函数中发出事件,这些函数只会将我的数据存储在 TheGraph 上。然后我可以从那里读取这些数据并确定令牌会发生什么以及何时冻结它们。 (气体效率)

      但是,如果您需要直接在合同中执行此操作(因此避免了链下)。我会去https://docs.chain.link/docs/chainlink-keepers/introduction/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-12
        • 2015-01-09
        • 2011-01-08
        • 2022-06-22
        • 2011-01-07
        相关资源
        最近更新 更多