Skip to content
Verified contractExactMIT

FlucVotes

Address0xf4d94881a13a5fbc6a9d3dba1fbdf01302e22d37
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.39

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.39+commit.ce47f2c8
Language
solidity
Optimization
Yes, 1,024 runs
EVM version
osaka
Via IR
yes
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,459,917
Creation transaction
0xe5d2ec2f4a…0ecaeb9218

src/FlucVotes.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.39;

interface IERC20 {
    function transfer(address to, uint256 amount) external returns (bool);
    function transferFrom(
        address from,
        address to,
        uint256 amount
    ) external returns (bool);
}

contract FlucVotes {
    struct Vote {
        address opener;
        uint64 votingStart;
        uint64 votingEnd;
        uint32 boxCount;
        bool opened;
    }

    IERC20 public immutable fluc;

    mapping(bytes32 => Vote) public votes;
    mapping(bytes32 => mapping(uint32 => uint128)) public boxVotes;
    mapping(bytes32 => mapping(address => uint128)) public funds;
    mapping(bytes32 => mapping(address => uint128)) public token;
    mapping(bytes32 => mapping(address => uint128)) public allocated;
    mapping(bytes32 => mapping(address => uint128)) public freed;

    event VoteOpened(
        bytes32 indexed voteId,
        address indexed opener,
        uint64 votingStart,
        uint64 votingEnd,
        uint32 boxCount,
        uint256 nonce,
        bytes32 contentHash
    );
    event Deposited(address indexed participant, bytes32 indexed voteId, uint256 amount);
    event Withdrawn(
        address indexed participant,
        bytes32 indexed voteId,
        uint256 amount,
        bool allTokensBurned
    );
    event VoteTokenTransferred(
        bytes32 indexed voteId,
        address indexed from,
        address indexed to,
        uint256 amount
    );
    event FundsMoved(
        address indexed account,
        bytes32 indexed fromVoteId,
        bytes32 indexed toVoteId,
        uint256 amount,
        bool allTokensBurned
    );
    event Voted(bytes32 indexed voteId, address indexed voter, uint32 box, uint256 amount);

    error ZeroAmount();
    error InvalidSchedule();
    error InvalidVoteId();
    error AlreadyOpened();
    error DepositClosed();
    error VotingNotStarted();
    error VotingEnded();
    error InvalidBoxCount();
    error InvalidBox();
    error InvalidRecipient();
    error InvalidMove();
    error AmountTooLarge();
    error InsufficientFunds();
    error AllocatedTokens();
    error TransferLocked();
    error TransferFailed();

    constructor(address flucToken) {
        if (flucToken == address(0)) revert InvalidRecipient();
        fluc = IERC20(flucToken);
    }

    function deposit(bytes32 voteId, uint256 amount) external {
        if (amount == 0) revert ZeroAmount();
        if (amount > type(uint128).max) revert AmountTooLarge();

        if (votes[voteId].opened) revert DepositClosed();

        uint128 amount128 = uint128(amount);
        funds[voteId][msg.sender] += amount128;
        token[voteId][msg.sender] += amount128;

        if (!fluc.transferFrom(msg.sender, address(this), amount)) {
            revert TransferFailed();
        }

        emit Deposited(msg.sender, voteId, amount);
    }

    function openVote(
        bytes32 voteId,
        uint64 votingStart,
        uint64 votingEnd,
        uint32 boxCount,
        uint256 nonce,
        bytes32 contentHash
    ) external {
        if (votingStart <= block.timestamp || votingStart >= votingEnd) {
            revert InvalidSchedule();
        }
        if (boxCount < 2) revert InvalidBoxCount();

        if (votes[voteId].opened) revert AlreadyOpened();
        if (voteId != _computeVoteId(msg.sender, nonce, contentHash)) {
            revert InvalidVoteId();
        }

        _writeVote(voteId, msg.sender, votingStart, votingEnd, boxCount);

        emit VoteOpened(
            voteId,
            msg.sender,
            votingStart,
            votingEnd,
            boxCount,
            nonce,
            contentHash
        );
    }

    function withdraw(bytes32 voteId, uint256 amount) external {
        if (amount == 0) revert ZeroAmount();
        if (amount > type(uint128).max) revert AmountTooLarge();

        Vote storage voteRecord = votes[voteId];
        uint128 amount128 = uint128(amount);
        uint128 currentFunds = funds[voteId][msg.sender];
        if (amount128 > currentFunds) revert InsufficientFunds();

        bool ended = voteRecord.opened && block.timestamp >= voteRecord.votingEnd;
        if (ended) {
            _clearPosition(voteId, msg.sender);
        } else {
            _release(voteId, msg.sender, amount128);
        }

        funds[voteId][msg.sender] = currentFunds - amount128;

        if (!fluc.transfer(msg.sender, amount)) revert TransferFailed();

        emit Withdrawn(msg.sender, voteId, amount, ended);
    }

    function transferVoteToken(
        bytes32 voteId,
        address to,
        uint256 amount
    ) external {
        if (to == address(0) || to == msg.sender) revert InvalidRecipient();
        if (amount == 0) revert ZeroAmount();
        if (amount > type(uint128).max) revert AmountTooLarge();

        Vote storage voteRecord = votes[voteId];
        if (!voteRecord.opened || block.timestamp >= voteRecord.votingStart) {
            revert TransferLocked();
        }

        uint128 amount128 = uint128(amount);
        uint128 available = token[voteId][msg.sender] - allocated[voteId][msg.sender];
        if (amount128 > available) revert AllocatedTokens();

        _decreaseToken(voteId, msg.sender, amount128);
        token[voteId][to] += amount128;

        emit VoteTokenTransferred(voteId, msg.sender, to, amount);
    }

    function moveFunds(
        bytes32 fromVoteId,
        bytes32 toVoteId,
        uint256 amount
    ) external {
        if (fromVoteId == toVoteId) revert InvalidMove();
        if (amount == 0) revert ZeroAmount();
        if (amount > type(uint128).max) revert AmountTooLarge();

        if (votes[toVoteId].opened) revert DepositClosed();

        Vote storage source = votes[fromVoteId];
        uint128 amount128 = uint128(amount);
        uint128 currentFunds = funds[fromVoteId][msg.sender];
        if (amount128 > currentFunds) revert InsufficientFunds();

        bool ended = source.opened && block.timestamp >= source.votingEnd;
        if (ended) {
            _clearPosition(fromVoteId, msg.sender);
        } else {
            _release(fromVoteId, msg.sender, amount128);
        }

        funds[fromVoteId][msg.sender] = currentFunds - amount128;
        funds[toVoteId][msg.sender] += amount128;
        token[toVoteId][msg.sender] += amount128;

        emit FundsMoved(msg.sender, fromVoteId, toVoteId, amount, ended);
    }

    function vote(
        bytes32 voteId,
        uint32 box,
        uint256 amount
    ) external {
        uint32 boxCount = _requireVoting(voteId);
        if (box >= boxCount) revert InvalidBox();
        if (amount == 0) revert ZeroAmount();
        if (amount > type(uint128).max) revert AmountTooLarge();

        uint128 amount128 = uint128(amount);
        uint128 available = token[voteId][msg.sender] - allocated[voteId][msg.sender];
        if (amount128 > available) revert AllocatedTokens();

        allocated[voteId][msg.sender] += amount128;
        _recordVote(voteId, box, amount128);
    }

    function voteTokenUnallocated(bytes32 voteId, address account) external view returns (uint128) {
        Vote storage voteRecord = votes[voteId];
        if (voteRecord.opened && block.timestamp >= voteRecord.votingEnd) return 0;
        return token[voteId][account] - allocated[voteId][account];
    }

    function withdrawable(bytes32 voteId, address account) external view returns (uint128) {
        uint128 currentFunds = funds[voteId][account];
        Vote storage voteRecord = votes[voteId];
        if (voteRecord.opened && block.timestamp >= voteRecord.votingEnd) {
            return currentFunds;
        }

        uint128 cap = token[voteId][account] - freed[voteId][account];
        return currentFunds < cap ? currentFunds : cap;
    }

    function _release(
        bytes32 voteId,
        address account,
        uint128 amount
    ) private {
        uint128 fromFreed = allocated[voteId][account] - freed[voteId][account];
        if (fromFreed > amount) fromFreed = amount;
        freed[voteId][account] += fromFreed;

        uint128 remainder = amount - fromFreed;
        if (remainder > token[voteId][account] - allocated[voteId][account]) {
            revert AllocatedTokens();
        }
        _decreaseToken(voteId, account, remainder);
    }

    function _decreaseToken(bytes32 voteId, address account, uint128 amount) private {
        token[voteId][account] -= amount;
    }

    function _clearPosition(bytes32 voteId, address account) private {
        _decreaseToken(voteId, account, token[voteId][account]);
        allocated[voteId][account] = 0;
        freed[voteId][account] = 0;
    }

    function _computeVoteId(
        address opener,
        uint256 nonce,
        bytes32 contentHash
    ) private view returns (bytes32) {
        return keccak256(abi.encode(address(this), opener, nonce, contentHash));
    }

    function _writeVote(
        bytes32 voteId,
        address opener,
        uint64 votingStart,
        uint64 votingEnd,
        uint32 boxCount
    ) private {
        Vote storage voteRecord = votes[voteId];
        voteRecord.opened = true;
        voteRecord.opener = opener;
        voteRecord.votingStart = votingStart;
        voteRecord.votingEnd = votingEnd;
        voteRecord.boxCount = boxCount;
    }

    function _requireVoting(bytes32 voteId) private view returns (uint32 boxCount) {
        Vote storage voteRecord = votes[voteId];
        if (!voteRecord.opened) revert VotingNotStarted();
        if (block.timestamp < voteRecord.votingStart) revert VotingNotStarted();
        if (block.timestamp >= voteRecord.votingEnd) revert VotingEnded();
        return voteRecord.boxCount;
    }

    function _recordVote(
        bytes32 voteId,
        uint32 box,
        uint128 amount
    ) private {
        uint256 next = uint256(boxVotes[voteId][box]) + amount;
        if (next > type(uint128).max) revert AmountTooLarge();
        boxVotes[voteId][box] = uint128(next);
        emit Voted(voteId, msg.sender, box, amount);
    }
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "libraries": {},
  "optimizer": {
    "runs": 1024,
    "enabled": true
  },
  "viaSSACFG": false,
  "evmVersion": "osaka",
  "experimental": false
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.