Skip to content
Verified contractPartialGPL-3.0

TokenDelegationLib

Address0x52d24426b844737fc5fdf704bf68346cbb4f45be
Balance

0 ETH

 

Transactions

0

 

Compiler

solc 0.8.30

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.30+commit.73712a01
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
License
GPL-3.0
Match
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,434,342
Creation transaction
0x5bc71916c2…ae5987384c
Libraries
dependencies/trex-develop/contracts/token/TokenGuardsLib.sol:TokenGuardsLib0xc8e4…27bf58dependencies/trex-develop/contracts/token/TokenLedgerLib.sol:TokenLedgerLib0x543f…dbbf0bdependencies/trex-develop/contracts/token/TokenRecoveryLib.sol:TokenRecoveryLib0x5231…fbf9b4dependencies/trex-develop/contracts/interop/TREXMessagingLib.sol:TREXMessagingLib0x4142…75c5c9dependencies/trex-develop/contracts/token/TokenDelegationLib.sol:TokenDelegationLib0x52d2…4f45bedependencies/trex-develop/contracts/compliance/modular/ModuleSetLib.sol:ModuleSetLib0xf674…14900bdependencies/trex-develop/contracts/compliance/modular/TransferValidationLib.sol:TransferValidationLib0xc492…2078d0

41 files

The source code is large: one file is shown at a time. Choose a file in the list.

dependencies/trex-develop/contracts/token/TokenDelegationLib.sol

// SPDX-License-Identifier: GPL-3.0
//
//                                             :+#####%%%%%%%%%%%%%%+
//                                         .-*@@@%+.:+%@@@@@%%#***%@@%=
//                                     :=*%@@@#=.      :#@@%       *@@@%=
//                       .-+*%@%*-.:+%@@@@@@+.     -*+:  .=#.       :%@@@%-
//                   :=*@@@@%%@@@@@@@@@%@@@-   .=#@@@%@%=             =@@@@#.
//             -=+#%@@%#*=:.  :%@@@@%.   -*@@#*@@@@@@@#=:-              *@@@@+
//            =@@%=:.     :=:   *@@@@@%#-   =%*%@@@@#+-.        =+       :%@@@%-
//           -@@%.     .+@@@     =+=-.         @@#-           +@@@%-       =@@@@%:
//          :@@@.    .+@@#%:                   :    .=*=-::.-%@@@+*@@=       +@@@@#.
//          %@@:    +@%%*                         =%@@@@@@@@@@@#.  .*@%-       +@@@@*.
//         #@@=                                .+@@@@%:=*@@@@@-      :%@%:      .*@@@@+
//        *@@*                                +@@@#-@@%-:%@@*          +@@#.      :%@@@@-
//       -@@%           .:-=++*##%%%@@@@@@@@@@@@*. :@+.@@@%:            .#@@+       =@@@@#:
//      .@@@*-+*#%%%@@@@@@@@@@@@@@@@%%#**@@%@@@.   *@=*@@#                :#@%=      .#@@@@#-
//      -%@@@@@@@@@@@@@@@*+==-:-@@@=    *@# .#@*-=*@@@@%=                 -%@@@*       =@@@@@%-
//         -+%@@@#.   %@%%=   -@@:+@: -@@*    *@@*-::                   -%@@%=.         .*@@@@@#
//            *@@@*  +@* *@@##@@-  #@*@@+    -@@=          .         :+@@@#:           .-+@@@%+-
//             +@@@%*@@:..=@@@@*   .@@@*   .#@#.       .=+-       .=%@@@*.         :+#@@@@*=:
//              =@@@@%@@@@@@@@@@@@@@@@@@@@@@%-      :+#*.       :*@@@%=.       .=#@@@@%+:
//               .%@@=                 .....    .=#@@+.       .#@@@*:       -*%@@@@%+.
//                 +@@#+===---:::...         .=%@@*-         +@@@+.      -*@@@@@%+.
//                  -@@@@@@@@@@@@@@@@@@@@@@%@@@@=          -@@@+      -#@@@@@#=.
//                    ..:::---===+++***###%%%@@@#-       .#@@+     -*@@@@@#=.
//                                           @@@@@@+.   +@@*.   .+@@@@@%=.
//                                          -@@@@@=   =@@%:   -#@@@@%+.
//                                          +@@@@@. =@@@=  .+@@@@@*:
//                                          #@@@@#:%@@#. :*@@@@#-
//                                          @@@@@%@@@= :#@@@@+.
//                                         :@@@@@@@#.:#@@@%-
//                                         +@@@@@@-.*@@@*:
//                                         #@@@@#.=@@@+.
//                                         @@@@+-%@%=
//                                        :@@@#%@%=
//                                        +@@@@%-
//                                        :#%%=
//

// T-REX Smart Contracts
// The ERC-3643 token suite of the T-REX ecosystem.
//
// Copyright (C) 2026 Digital Asset Operational Services ISAC Ltd. ("T-REX Network")
//
// This file is part of the T-REX smart contract suite.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

pragma solidity 0.8.30;

import { IERC3643 } from "../ERC-3643/IERC3643.sol";
import { IModularCompliance } from "../compliance/modular/IModularCompliance.sol";
import { MovementKindLib } from "../compliance/modular/MovementKindLib.sol";
import { TREXMessagingLib } from "../interop/TREXMessagingLib.sol";
import { ErrorsLib } from "../libraries/ErrorsLib.sol";
import { EventsLib } from "../libraries/EventsLib.sol";
import { MessageTypesLib } from "../libraries/MessageTypesLib.sol";
import { WalletKeyLib } from "../libraries/WalletKeyLib.sol";
import { ITREXRegistry } from "../registry/interface/ITREXRegistry.sol";
import { TokenLedgerLib } from "./TokenLedgerLib.sol";

/**
 * @title TokenDelegationLib
 * @dev The checks, the bookkeeping and the messages of the two movements that change a position's location and
 * never its owner: a delegation-out, native wallet to satellite wallet of one identity, and a recall, the way
 * back. Deployed once and linked.
 *
 * The token reaches this by DELEGATECALL, so the storage is its own `erc3643.storage.TREXToken` namespace, the
 * registry, the compliance and the gateway see the token as their caller, and every event is the token's. The
 * registry and the compliance are read from the token's own getters, so the token passes neither. The native
 * half of each movement stays in the token, since it is the ERC-20 balance this library cannot reach: the token
 * burns before {delegateOut} and mints after {consumeBurnProof}; everything bridged happens here.
 *
 * Both instructions are retryable, never written off: {resend} sends an instruction again byte for byte, under
 * the same id, to the same peer, and the Lite acts on each id once. Nothing here checks a caller or the pause:
 * the token's entry points do, before they reach this.
 */
library TokenDelegationLib {

    /// @dev `resend` kind of a delegation's mint instruction.
    uint8 internal constant DELEGATION = 0;

    /// @dev `resend` kind of a recall instruction.
    uint8 internal constant RECALL = 1;

    /// @dev The rest of a delegation-out, once the token has burned `amount` of `holder`'s native balance: the burn
    ///  stayed inside the free balance, the bridged position of `toWallet` is credited under a fresh delegation id
    ///  (`DelegatedOut`), both wallets sit on one identity, the registry admits the recipient, the compliance is told,
    ///  the instruction is committed and sent. Any refusal reverts the burn with it, so nothing is delegated that the
    ///  satellite is not told to mint.
    /// @return sendId The gateway's id for the instruction.
    function delegateOut(address holder, bytes calldata toWallet, uint256 amount) external returns (bytes32 sendId) {
        uint256 delegationId = _creditDelegation(holder, toWallet, amount);

        _requireLinked(holder, toWallet);
        require(_registry().isWalletVerified(toWallet), ErrorsLib.UnverifiedWallet(toWallet));

        _compliance().agentTransferred(holder, holder, amount, MovementKindLib.DELEGATION);

        bytes32 chainKey = _chainKeyOf(toWallet);
        TokenLedgerLib.layout().delegations[delegationId] =
            _commitment(chainKey, keccak256(TREXMessagingLib.peerFor(chainKey)), toWallet, amount, holder);

        return _sendMint(chainKey, delegationId, toWallet, amount, holder);
    }

    /// @dev The bridged half of a delegation-out, through the ledger's own transition: the token already burned, so
    ///  the free balance before the burn is what it holds now plus `amount`, less what is frozen. Then the delegation
    ///  takes the next id.
    function _creditDelegation(address holder, bytes calldata toWallet, uint256 amount)
        private
        returns (uint256 delegationId)
    {
        require(amount != 0, ErrorsLib.ZeroValue());
        IERC3643 token = IERC3643(address(this));
        uint256 freeBefore = token.balanceOf(holder) + amount - token.getFrozenTokens(holder);
        bytes32 toKey = TokenLedgerLib.creditFromNative(holder, toWallet, amount, freeBefore);
        delegationId = ++TokenLedgerLib.layout().lastDelegationId;

        emit EventsLib.DelegatedOut(holder, toKey, delegationId, toWallet, amount);
    }

    /// @dev Records a recall instruction and sends it. One recall at a time per wallet. The amount is bounded by the
    ///  wallet's bridged position so the proof that answers can always apply, and not by its reservations: a
    ///  validation that executes on the satellite first leaves the satellite less to burn, and the proof carries
    ///  what it burned, down to zero. The gateway and the peer it goes to are recorded, and only they may answer.
    /// @return recallId The id the proof must answer with.
    function instructRecall(bytes calldata wallet, uint256 amount, address nativeWallet)
        external
        returns (uint256 recallId)
    {
        require(amount != 0, ErrorsLib.ZeroValue());
        require(nativeWallet != address(0), ErrorsLib.ZeroAddress());
        _requireLinked(nativeWallet, wallet);

        bytes32 walletKey = WalletKeyLib.satelliteKey(wallet);
        TokenLedgerLib.TokenStorage storage s = TokenLedgerLib.layout();
        require(s.openRecall[walletKey] == 0, ErrorsLib.RecallPending(walletKey, s.openRecall[walletKey]));
        uint256 balance = s.bridgedBalance[walletKey];
        require(amount <= balance, ErrorsLib.InsufficientBridgedBalance(wallet, balance, amount));

        bytes32 chainKey = _chainKeyOf(wallet);
        recallId = ++s.lastRecallId;
        s.recalls[recallId] = TokenLedgerLib.PendingRecall({
            walletKey: walletKey,
            chainKey: chainKey,
            nativeWallet: nativeWallet,
            amount: amount,
            gateway: TREXMessagingLib.layout().routes[chainKey],
            peerKey: keccak256(TREXMessagingLib.peerFor(chainKey))
        });
        s.openRecall[walletKey] = recallId;

        emit EventsLib.RecallInstructed(recallId, walletKey, nativeWallet, wallet, amount);

        _sendRecall(chainKey, recallId, wallet, amount, nativeWallet);
    }

    /// @dev Judges an attributed burn proof against the recall it answers, settles that recall, and applies the
    ///  bridged half of the credit.
    ///
    ///  A proof for an open recall must name its wallet and chain and prove at most the instructed amount, or it
    ///  reverts and stays deliverable. The identity link is checked again, then the compliance follows any relink
    ///  of the native wallet before the credit lands, so the wallet's existing balance is attributed to its current
    ///  owner and the credit, already counted in that identity's position as a bridged one, is not migrated with
    ///  it. The proof is recorded, the bridged position debited (`Recalled`), and the token mints the native half.
    ///  A zero proof settles the same way with nothing to move.
    ///
    ///  With no open recall: the very proof that settled it, from the same chain, is a duplicate and changes
    ///  nothing; anything else, a recall never issued or a different proof for a settled one, is a message the
    ///  token cannot account for and reports `halt`, as a replayed settlement does.
    /// @return halt Whether the token must pause itself.
    /// @return nativeWallet The wallet the token mints to, as the instruction recorded it.
    /// @return amount What the token mints: what the satellite proved it burned, zero when nothing is credited.
    function consumeBurnProof(bytes32 originChainKey, bytes memory body)
        external
        returns (bool halt, address nativeWallet, uint256 amount)
    {
        MessageTypesLib.BurnProof memory proof = MessageTypesLib.decodeBurnProof(body);
        bytes32 proofHash = keccak256(abi.encode(originChainKey, body));

        emit EventsLib.BurnProofReceived(originChainKey, proof.recallId, proof.burnedWallet, proof.amount);

        TokenLedgerLib.TokenStorage storage s = TokenLedgerLib.layout();
        TokenLedgerLib.PendingRecall storage recall = s.recalls[proof.recallId];
        if (recall.amount == 0) {
            if (s.settledProofs[proof.recallId] == proofHash) {
                emit EventsLib.BurnProofDuplicate(proof.recallId, originChainKey);
                return (false, address(0), 0);
            }
            emit EventsLib.ReplayedBurnProof(proof.recallId, originChainKey);
            return (true, address(0), 0);
        }

        require(
            keccak256(proof.burnedWallet) == recall.walletKey && originChainKey == recall.chainKey,
            ErrorsLib.RecallMismatch(proof.recallId)
        );
        require(
            proof.amount <= recall.amount, ErrorsLib.RecallAmountExceeded(proof.recallId, recall.amount, proof.amount)
        );

        nativeWallet = recall.nativeWallet;
        amount = proof.amount;
        bytes32 walletKey = recall.walletKey;
        _requireLinked(nativeWallet, proof.burnedWallet);
        _compliance().syncOwner(nativeWallet);

        delete s.openRecall[walletKey];
        delete s.recalls[proof.recallId];
        s.settledProofs[proof.recallId] = proofHash;

        emit EventsLib.RecallSettled(proof.recallId, originChainKey, amount);

        if (amount != 0) {
            TokenLedgerLib.debitToNative(proof.burnedWallet, nativeWallet, amount);
            emit EventsLib.Recalled(walletKey, nativeWallet, proof.burnedWallet, amount);
        }
    }

    /// @dev Sends an instruction again, unchanged, through the chain's current route, to the peer it first went to.
    ///
    ///  A delegation (`kind` 0) is resent from the fields the caller supplies, `abi.encode(toWallet, amount, holder,
    ///  peer)`, which must hash to the commitment stored at dispatch. A recall (`kind` 1) is resent from its stored
    ///  record and the `abi.encode(wallet)` the caller supplies, which must be the recorded wallet; it is refused
    ///  once settled, and its admitted gateway moves to the current route, since the Lite answers through the
    ///  gateway that delivered the resend. Either way the chain's peer must still be the one the instruction was
    ///  addressed to: a resend never retargets. Nothing moves on this chain.
    /// @return sendId The gateway's id for the resent instruction.
    function resend(uint8 kind, uint256 id, bytes calldata data) external returns (bytes32 sendId) {
        TokenLedgerLib.TokenStorage storage s = TokenLedgerLib.layout();
        bytes32 chainKey;
        if (kind == DELEGATION) {
            (bytes memory toWallet, uint256 amount, address holder, bytes memory peer) =
                abi.decode(data, (bytes, uint256, address, bytes));
            chainKey = _chainKeyOf(toWallet);
            bytes32 committed = s.delegations[id];
            require(
                committed != 0 && committed == _commitment(chainKey, keccak256(peer), toWallet, amount, holder),
                ErrorsLib.DelegationMismatch(id)
            );
            _requireSamePeer(chainKey, keccak256(peer));
            sendId = _sendMint(chainKey, id, toWallet, amount, holder);
        } else if (kind == RECALL) {
            bytes memory wallet = abi.decode(data, (bytes));
            TokenLedgerLib.PendingRecall storage recall = s.recalls[id];
            require(recall.amount != 0, ErrorsLib.RecallNotPending(id));
            require(keccak256(wallet) == recall.walletKey, ErrorsLib.RecallMismatch(id));
            chainKey = recall.chainKey;
            _requireSamePeer(chainKey, recall.peerKey);
            recall.gateway = TREXMessagingLib.layout().routes[chainKey];
            sendId = _sendRecall(chainKey, id, wallet, recall.amount, recall.nativeWallet);
        } else {
            revert ErrorsLib.UnknownResendKind(kind);
        }

        emit EventsLib.InstructionResent(kind, id, chainKey, sendId);
    }

    /// @dev Both wallets must resolve to one existing identity. Attribution, not admission: a revoked wallet
    ///  still resolves, since a recall is how a revoked position comes home.
    function _requireLinked(address nativeWallet, bytes memory wallet) private view {
        ITREXRegistry registry = _registry();
        address identity = address(registry.identity(nativeWallet));
        require(
            identity != address(0) && identity == address(registry.resolveIdentity(wallet)),
            ErrorsLib.WalletsNotLinked(nativeWallet, wallet)
        );
    }

    /// @dev The token's registry, through its own getter: run by DELEGATECALL, `address(this)` is the token.
    function _registry() private view returns (ITREXRegistry) {
        return ITREXRegistry(address(IERC3643(address(this)).identityRegistry()));
    }

    /// @dev The token's compliance, through its own getter.
    function _compliance() private view returns (IModularCompliance) {
        return IModularCompliance(address(IERC3643(address(this)).compliance()));
    }

    function _requireSamePeer(bytes32 chainKey, bytes32 peerKey) private view {
        require(keccak256(TREXMessagingLib.peerFor(chainKey)) == peerKey, ErrorsLib.PeerChanged(chainKey));
    }

    function _commitment(bytes32 chainKey, bytes32 peerKey, bytes memory toWallet, uint256 amount, address holder)
        private
        pure
        returns (bytes32)
    {
        return keccak256(abi.encode(chainKey, peerKey, toWallet, amount, holder));
    }

    function _sendMint(bytes32 chainKey, uint256 delegationId, bytes memory toWallet, uint256 amount, address holder)
        private
        returns (bytes32)
    {
        return TREXMessagingLib.sendMessage(
            chainKey,
            MessageTypesLib.Message.MINT_INSTRUCTION,
            abi.encode(
                MessageTypesLib.MintInstruction({
                    delegationId: delegationId, toWallet: toWallet, amount: amount, holder: holder
                })
            )
        );
    }

    function _sendRecall(bytes32 chainKey, uint256 recallId, bytes memory wallet, uint256 amount, address nativeWallet)
        private
        returns (bytes32)
    {
        return TREXMessagingLib.sendMessage(
            chainKey,
            MessageTypesLib.Message.RECALL_INSTRUCTION,
            abi.encode(
                MessageTypesLib.RecallInstruction({
                    recallId: recallId, wallet: wallet, amount: amount, nativeWallet: nativeWallet
                })
            )
        );
    }

    function _chainKeyOf(bytes memory wallet) private pure returns (bytes32) {
        (bytes2 chainType, bytes memory chainReference,) = WalletKeyLib.parse(wallet);
        return MessageTypesLib.chainKey(chainType, chainReference);
    }

}
Compiler settingsShow
{
  "viaIR": false,
  "metadata": {
    "appendCBOR": false,
    "bytecodeHash": "none",
    "useLiteralContent": false
  },
  "libraries": {
    "dependencies/trex-develop/contracts/token/TokenGuardsLib.sol": {
      "TokenGuardsLib": "0xC8E48bf2154d6f2aABC98B0a4743D0d3f327bf58"
    },
    "dependencies/trex-develop/contracts/token/TokenLedgerLib.sol": {
      "TokenLedgerLib": "0x543F036f6812c5a799C818465CE8760bFEdBBf0B"
    },
    "dependencies/trex-develop/contracts/token/TokenRecoveryLib.sol": {
      "TokenRecoveryLib": "0x5231A9f48994FBED949f051a53477349DCFBF9B4"
    },
    "dependencies/trex-develop/contracts/interop/TREXMessagingLib.sol": {
      "TREXMessagingLib": "0x4142e2230413f167Ab98D8A3DB5dFCe2A475c5c9"
    },
    "dependencies/trex-develop/contracts/token/TokenDelegationLib.sol": {
      "TokenDelegationLib": "0x52d24426b844737Fc5fdF704Bf68346CbB4F45BE"
    },
    "dependencies/trex-develop/contracts/compliance/modular/ModuleSetLib.sol": {
      "ModuleSetLib": "0xF674215e1FC78303C49960F9c0129A34f514900B"
    },
    "dependencies/trex-develop/contracts/compliance/modular/TransferValidationLib.sol": {
      "TransferValidationLib": "0xc49275Bea16629C86bF4636bf4bfB609102078d0"
    }
  },
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "@chainlink/=../dependencies/smartcontractkit-chainlink-evm-1.5.0/",
    "@dino/=../",
    "@createx/=../dependencies/createx-v1.0.0/",
    "@erc3643org/erc-3643/=../dependencies/erc3643org-4.2.0-beta1/",
    "@fee-collector/=../dependencies/FeeCollector-dev/",
    "@forge-std/=dependencies/forge-std-v1.14.0/",
    "@onchain-id/solidity/=dependencies/onchain-id-develop/",
    "@openzeppelin/contracts-upgradeable/=dependencies/@openzeppelin-contracts-upgradeable-5.7.0/",
    "@openzeppelin/contracts/=dependencies/@openzeppelin-contracts-5.7.0/",
    "@trex/=dependencies/trex-develop/",
    "@openzeppelin-contracts-5.7.0/=dependencies/@openzeppelin-contracts-5.7.0/",
    "@openzeppelin-contracts-upgradeable-5.7.0/=dependencies/@openzeppelin-contracts-upgradeable-5.7.0/",
    "@openzeppelin/accounts/=dependencies/onchain-id-develop/dependencies/@openzeppelin-accounts-0.0.0-4ceb836d/packages/contracts/src/",
    "forge-std-v1.14.0/=dependencies/forge-std-v1.14.0/src/",
    "forge-std/=dependencies/onchain-id-develop/dependencies/forge-std-v1.14.0/src/",
    "onchain-id-develop/=dependencies/onchain-id-develop/",
    "trex-develop/=dependencies/trex-develop/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.