Skip to content
Verified contractExactApache-2.0

WrappedPQOS

Address0xad55800116817e3a0884b514bab508afd9e95142
Balance

0 ETH

 

Transactions

3

 

Compiler

solc 0.8.24

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.24+commit.e11b9ed9
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
License
Apache-2.0
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,325,215
Creation transaction
0x506bca7a48…9ad00b9297

src/WrappedPQOS.sol

// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.24;

/**
 * WrappedPQOS — a GRIDCHAIN outpost satellite.
 *
 * Holds ONLY wrapped TIAMAT. Never the reserve. The core at unog.near is the
 * source of truth; this contract is a bounded mirror of it.
 *
 * Trust model: ATTESTED. An M-of-N quorum signs route tickets. There is no
 * owner mint, no pause, and no upgrade path. The supply cap is immutable and
 * is the hard limit on what a compromised quorum can create.
 *
 * Ticket state is the core's four-valued logic: only TRUE executes.
 */
contract WrappedPQOS {
    string public constant name = "Wrapped TIAMAT";
    string public constant symbol = "wPQOS";
    uint8 public constant decimals = 18;

    /// Tickets always carry NEAR base units (24 decimals). The outpost
    /// converts once, here, so no other layer has to remember the ratio.
    uint256 public constant SCALE = 1e6;

    /// The core's genesis tag, as exposed by status().chain_tag on unog.near.
    bytes32 public immutable chainTag;
    /// This outpost's route id.
    uint32 public immutable routeChainId;
    /// Hard cap in THIS outpost's base units (18 decimals). Immutable by design.
    uint256 public immutable supplyCap;

    uint256 public totalSupply;
    mapping(address => uint256) public balanceOf;
    mapping(address => mapping(address => uint256)) public allowance;

    /// The attestor set. Fixed at deployment; there is no setter.
    mapping(address => bool) public isAttestor;
    uint256 public immutable threshold;

    enum State { NEITHER, TRUE, FALSE, BOTH }

    mapping(bytes32 => State) public ticketState;
    mapping(bytes32 => uint256) public ticketSigners;
    mapping(bytes32 => mapping(address => bool)) public signedBy;
    mapping(bytes32 => bool) public executed;
    /// Highest executed nonce; a ticket at or below it is a replay.
    uint64 public highestNonce;

    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
    event Minted(bytes32 indexed routeId, address indexed to, uint256 amount, uint64 nonce);
    event Burned(bytes32 indexed routeId, address indexed from, uint256 amount, bytes32 nearRecipient, uint64 nonce);
    event Attested(bytes32 indexed routeId, address indexed attestor, uint256 signers, State state);
    event Refuted(bytes32 indexed routeId, address indexed attestor);
    event Contested(bytes32 indexed routeId);

    struct Ticket {
        uint8 version;
        uint8 direction;   // 0 = MINT, 1 = RELEASE
        uint32 chainId;
        uint64 nonce;
        uint128 amount;
        bytes32 recipient;
        uint64 expiry;
    }

    error BadVersion();
    error BadDirection();
    error BadChain(uint32 got);
    error Expired(uint64 expiry);
    error CapExceeded(uint256 wouldBe, uint256 cap);
    error NotTrue(State state);
    error AlreadyExecuted();
    error Replay(uint64 nonce, uint64 highest);
    error NotAttestor(address who);
    error BadSignatureCount();
    error InsufficientSigners(uint256 have, uint256 need);
    error ZeroAmount();
    error ZeroRecipient();

    constructor(bytes32 _chainTag, uint32 _routeChainId, uint256 _supplyCap, address[] memory _attestors, uint256 _threshold) {
        require(_attestors.length > 0, "no attestors");
        require(_threshold > 0 && _threshold <= _attestors.length, "bad threshold");
        chainTag = _chainTag;
        routeChainId = _routeChainId;
        supplyCap = _supplyCap;
        threshold = _threshold;
        for (uint256 i = 0; i < _attestors.length; i++) {
            require(_attestors[i] != address(0), "zero attestor");
            require(!isAttestor[_attestors[i]], "duplicate attestor");
            isAttestor[_attestors[i]] = true;
        }
    }

    // ---------------------------------------------------------------- ERC-20

    function transfer(address to, uint256 value) external returns (bool) {
        _move(msg.sender, to, value);
        return true;
    }

    function approve(address spender, uint256 value) external returns (bool) {
        allowance[msg.sender][spender] = value;
        emit Approval(msg.sender, spender, value);
        return true;
    }

    function transferFrom(address from, address to, uint256 value) external returns (bool) {
        uint256 a = allowance[from][msg.sender];
        require(a >= value, "allowance");
        if (a != type(uint256).max) allowance[from][msg.sender] = a - value;
        _move(from, to, value);
        return true;
    }

    function _move(address from, address to, uint256 value) internal {
        require(to != address(0), "zero to");
        uint256 b = balanceOf[from];
        require(b >= value, "balance");
        unchecked { balanceOf[from] = b - value; }
        balanceOf[to] += value;
        emit Transfer(from, to, value);
    }

    // --------------------------------------------------------------- routing

    function ticketDigest(bytes32 routeId) public view returns (bytes32) {
        return sha256(abi.encodePacked("GRP-SIG-v1", chainTag, routeId));
    }

    function encodeTicket(Ticket calldata t) public pure returns (bytes32) {
        return sha256(abi.encodePacked(
            "GRP-TICKET-v1", t.version, t.direction, t.chainId, t.nonce, t.amount, t.recipient, t.expiry
        ));
    }

    function _recover(bytes32 digest, bytes memory sig) internal pure returns (address) {
        if (sig.length != 65) return address(0);
        bytes32 r; bytes32 s; uint8 v;
        assembly {
            r := mload(add(sig, 32))
            s := mload(add(sig, 64))
            v := byte(0, mload(add(sig, 96)))
        }
        if (v < 27) v += 27;
        if (v != 27 && v != 28) return address(0);
        // reject malleable signatures
        if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) return address(0);
        return ecrecover(keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", digest)), v, r, s);
    }

    /// Submit a MINT ticket with its attestor signatures.
    function mint(Ticket calldata t, bytes[] calldata signatures) external returns (bytes32 routeId) {
        if (t.version != 1) revert BadVersion();
        if (t.direction != 0) revert BadDirection();
        if (t.chainId != routeChainId) revert BadChain(t.chainId);
        if (t.amount == 0) revert ZeroAmount();
        if (block.timestamp > t.expiry) revert Expired(t.expiry);
        routeId = encodeTicket(t);
        if (executed[routeId]) revert AlreadyExecuted();
        if (t.nonce <= highestNonce) revert Replay(t.nonce, highestNonce);

        State s = _collect(routeId, signatures);
        if (s != State.TRUE) revert NotTrue(s);

        uint256 scaled = t.amount / SCALE;
        if (scaled == 0) revert ZeroAmount();
        address to = address(uint160(uint256(t.recipient)));
        if (to == address(0)) revert ZeroRecipient();
        uint256 wouldBe = totalSupply + scaled;
        if (wouldBe > supplyCap) revert CapExceeded(wouldBe, supplyCap);

        executed[routeId] = true;
        highestNonce = t.nonce;
        totalSupply = wouldBe;
        balanceOf[to] += scaled;
        emit Transfer(address(0), to, scaled);
        emit Minted(routeId, to, scaled, t.nonce);
    }

    /// Burn wPQOS and emit the release ticket the core will consume.
    function burn(uint256 amount, bytes32 nearRecipient) external returns (bytes32 routeId) {
        if (amount == 0) revert ZeroAmount();
        require(amount <= type(uint128).max, "amount too large");
        uint256 b = balanceOf[msg.sender];
        require(b >= amount, "balance");
        unchecked { balanceOf[msg.sender] = b - amount; }
        totalSupply -= amount;
        uint64 nonce = highestNonce + 1;
        highestNonce = nonce;
        uint256 nearAmount = amount * SCALE; // back to the core's 24-decimal units
        routeId = sha256(abi.encodePacked(
            "GRP-TICKET-v1", uint8(1), uint8(1), routeChainId, nonce, uint128(nearAmount), nearRecipient, uint64(block.timestamp + 7 days)
        ));
        emit Transfer(msg.sender, address(0), amount);
        emit Burned(routeId, msg.sender, amount, nearRecipient, nonce);
    }

    /// An attestor refutes a ticket. A refutation after a quorum freezes it as BOTH.
    function refute(bytes32 routeId) external {
        if (!isAttestor[msg.sender]) revert NotAttestor(msg.sender);
        if (ticketState[routeId] == State.TRUE) {
            ticketState[routeId] = State.BOTH;
            emit Contested(routeId);
        } else {
            ticketState[routeId] = State.FALSE;
        }
        emit Refuted(routeId, msg.sender);
    }

    function _collect(bytes32 routeId, bytes[] calldata signatures) internal returns (State) {
        if (signatures.length == 0 || signatures.length > 32) revert BadSignatureCount();
        bytes32 digest = ticketDigest(routeId);
        uint256 n = ticketSigners[routeId];
        for (uint256 i = 0; i < signatures.length; i++) {
            address signer = _recover(digest, signatures[i]);
            if (signer == address(0) || !isAttestor[signer]) continue;
            if (signedBy[routeId][signer]) continue; // a repeat signer cannot add weight
            signedBy[routeId][signer] = true;
            n++;
        }
        ticketSigners[routeId] = n;
        if (n >= threshold) {
            if (ticketState[routeId] == State.BOTH) return State.BOTH; // frozen once contested
            ticketState[routeId] = State.TRUE;
        } else if (ticketState[routeId] != State.BOTH) {
            ticketState[routeId] = State.NEITHER;
        }
        emit Attested(routeId, msg.sender, n, ticketState[routeId]);
        return ticketState[routeId];
    }

    /// The price a route quotes, for arbitrage: base units of wPQOS per 1e18 wei of the native unit.
    function quote(uint256 nativeIn) external view returns (uint256) {
        // Purely informational on the outpost; the authoritative quote is the core's tiamat_quote.
        return nativeIn;
    }

    function metadata() external pure returns (string memory) {
        return "attested; not trustless; cap-bounded";
    }
}
Compiler settingsShow
{
  "viaIR": false,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "libraries": {},
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun"
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.