SushiToken的投票实现

该博客介绍了SushiToken如何基于ERC20标准扩展实现投票功能。内容包括代理投票的数据结构、投票转移、投票代理、投票查询及mint函数。文章指出SushiToken在转移和销毁代币时投票权处理的潜在问题。
摘要由CSDN通过智能技术生成

SushiToken继承标准的ERC20,在此基础上,又实现了代币的投票功能。

1、数据结构

/// @notice A record of each accounts delegate
    mapping (address => address) internal _delegates;

    /// @notice A checkpoint for marking number of votes from a given block
    struct Checkpoint {
        uint32 fromBlock;
        uint256 votes;
    }

    /// @notice A record of votes checkpoints for each account, by index
    mapping (address => mapping (uint32 => Checkpoint)) public checkpoints;

    /// @notice The number of checkpoints for each account
    mapping (address => uint32) public numCheckpoints;

    /// @notice The EIP-712 typehash for the contract's domain
    bytes32 public constant DOMAIN_TYPEHASH = keccak256("EIP712Domain(string name,uint256 chainId,address verifyingContract)");

    /// @notice The EIP-712 typehash for the delegation struct used by the contract
    bytes32 public constant DELEGATION_TYPEHASH = keccak256("Delegation(address delegatee,uint256 nonce,uint256 expiry)");

    /// @notice A record of states for signing / validating signatures
    mapping (address => uint) public nonces;

_delegates表示代理,A->B的映射,表示B可以代替A行使投票。

Checkpoint是记录投票的数据结构,类似于一个快照,里面记录了用户在某个区块持有的投票数。

numCheckpoints记录的是快照的ID,每次有新的快照,这个ID都会自增。

checkpoints是用户所有的投票快照记录。

DOMAIN_TYPEHASH、DELEGATION_TYPEHASH和nonces都和EIP-712有关,这个EIP旨在提高链下消息签名对链上的可用性,具体可以看https://eips.ethereum.org/EIPS/eip-712

2、转移投票

function _moveDelegates(address srcRep, address dstRep, uint256 amount) internal {
        if (srcRep != dstRep && amount > 0) {
            if (srcRep != address(0)) {
                // decrease old representative
                uint32 s
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值