Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hyperlane Tribunal implementation #6

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "lib/tribunal"]
path = lib/tribunal
url = https://github.com/uniswap/tribunal
Binary file modified .yarn/install-state.gz
Binary file not shown.
1 change: 1 addition & 0 deletions lib/tribunal
Submodule tribunal added at 0b6317
1 change: 1 addition & 0 deletions remappings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ solady/=lib/the-compact/lib/solady/src/
soledge/=lib/the-compact/lib/soledge/src/
solmate/=lib/the-compact/lib/permit2/lib/solmate/
the-compact/=lib/the-compact/
tribunal/=lib/tribunal/src/
tstorish/=lib/the-compact/lib/tstorish/src/
hyperlane/=node_modules/@hyperlane-xyz/core
@openzeppelin/contracts/=node_modules/@openzeppelin/contracts
Expand Down
42 changes: 42 additions & 0 deletions snapshots/HyperlaneArbiterTest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"basicTransfer": "56867",
"basicWithdrawal": "59992",
"batchClaim": "112512",
"batchClaimRegisteredWithDeposit": "112512",
"batchClaimRegisteredWithDepositWithWitness": "113271",
"batchClaimWithWitness": "113265",
"batchDepositAndRegisterViaPermit2": "222059",
"batchDepositAndRegisterWithWitnessViaPermit2": "222037",
"batchTransfer": "81344",
"batchWithdrawal": "100093",
"claim": "56974",
"claimAndWithdraw": "73432",
"claimWithWitness": "59471",
"depositAndRegisterViaPermit2": "124429",
"depositBatchSingleERC20": "68050",
"depositBatchSingleNative": "28353",
"depositBatchViaPermit2NativeAndERC20": "129751",
"depositBatchViaPermit2SingleERC20": "104905",
"depositERC20AndURI": "67276",
"depositERC20Basic": "67302",
"depositERC20ViaPermit2AndURI": "98471",
"depositETHAndURI": "26754",
"depositETHBasic": "28391",
"qualifiedBatchClaim": "114134",
"qualifiedBatchClaimWithWitness": "113672",
"qualifiedClaim": "60406",
"qualifiedClaimWithWitness": "59098",
"qualifiedSplitBatchClaim": "141486",
"qualifiedSplitBatchClaimWithWitness": "141552",
"qualifiedSplitClaim": "86977",
"qualifiedSplitClaimWithWitness": "87452",
"register": "25379",
"splitBatchClaim": "140788",
"splitBatchClaimWithWitness": "140749",
"splitBatchTransfer": "110652",
"splitBatchWithdrawal": "140361",
"splitClaim": "86751",
"splitClaimWithWitness": "86232",
"splitTransfer": "82482",
"splitWithdrawal": "93770"
}
159 changes: 159 additions & 0 deletions src/HyperlaneTribunal.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.27;

import {ITheCompactClaims} from "the-compact/src/interfaces/ITheCompactClaims.sol";
import {ClaimWithWitness} from "the-compact/src/types/Claims.sol";
import {Compact} from "the-compact/src/types/EIP712Types.sol";
import {Tribunal} from "tribunal/Tribunal.sol";

import {Router} from "hyperlane/contracts/client/Router.sol";

string constant WITNESS_TYPESTRING =
"Mandate mandate)Mandate(uint256 chainId,address tribunal,address recipient,uint256 expires,address token,uint256 minimumAmount,uint256 baselinePriorityFee,uint256 scalingFactor,bytes32 salt)";

library Message {
function encode(Tribunal.Compact calldata compact, bytes32 mandateHash, uint256 claimedAmount, address claimant)
internal
pure
returns (bytes memory)
{
require(
compact.allocatorSignature.length == 64 && compact.sponsorSignature.length == 64, "invalid signature length"
);

return abi.encodePacked(
compact.arbiter,
compact.sponsor,
compact.nonce,
compact.expires,
compact.id,
compact.maximumAmount,
compact.allocatorSignature,
compact.sponsorSignature,
mandateHash,
claimedAmount,
claimant
);
}

function decode(bytes calldata message)
internal
view
returns (
address sponsor,
uint256 nonce,
uint256 expires,
uint256 id,
uint256 allocatedAmount,
bytes calldata allocatorSignature,
bytes calldata sponsorSignature,
bytes32 witness,
uint256 claimedAmount,
address claimant
)
{
require(message.length == 380, "invalid message length");
address arbiter = address(bytes20(message[0:20]));
require(arbiter == address(this), "invalid arbiter");
Comment on lines +56 to +57
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the mailbox will only call handle on the message headers recipient so this is unnecessary to include in the message body


sponsor = address(bytes20(message[20:40]));
nonce = uint256(bytes32(message[40:72]));
expires = uint256(bytes32(message[72:104]));
id = uint256(bytes32(message[104:136]));
allocatedAmount = uint256(bytes32(message[136:168]));
allocatorSignature = message[168:232];
sponsorSignature = message[232:296];
witness = bytes32(message[296:328]);
claimedAmount = uint256(bytes32(message[328:360]));
claimant = address(bytes20(message[360:380]));
}
}

contract HyperlaneTribunal is Router, Tribunal {
using Message for bytes;

ITheCompactClaims public immutable theCompact;

constructor(address _mailbox, address _theCompact) Router(_mailbox) {
theCompact = ITheCompactClaims(_theCompact);
}

/**
* @dev Process the directive for token claims
* @param compact The claim parameters
* @param mandateHash The derived mandate hash
* @param directive The execution details
* @param claimAmount The amount to claim
*/
function _processDirective(
Tribunal.Compact calldata compact,
bytes32 mandateHash,
Directive memory directive,
uint256 claimAmount
) internal virtual override {
_Router_dispatch(
uint32(compact.chainId),
directive.dispensation,
Message.encode(compact, mandateHash, claimAmount, directive.claimant),
"",
address(hook)
);
}

/**
* @dev Derive the quote for the dispensation required for
* the directive for token claims
* @param compact The claim parameters
* @param mandateHash The derived mandate hash
* @param claimant The address of the claimant
* @param claimAmount The amount to claim
* @return dispensation The quoted dispensation amount
*/
function _quoteDirective(
Tribunal.Compact calldata compact,
bytes32 mandateHash,
address claimant,
uint256 claimAmount
) internal view virtual override returns (uint256 dispensation) {
return _Router_quoteDispatch(
uint32(compact.chainId), Message.encode(compact, mandateHash, claimAmount, claimant), "", address(hook)
);
}

function _handle(
uint32,
/*origin*/
bytes32,
/*sender*/
bytes calldata message
) internal override {
(
address sponsor,
uint256 nonce,
uint256 expires,
uint256 id,
uint256 allocatedAmount,
bytes calldata allocatorSignature,
bytes calldata sponsorSignature,
bytes32 witness,
uint256 claimedAmount,
address claimant
) = message.decode();

ClaimWithWitness memory claimPayload = ClaimWithWitness({
witnessTypestring: WITNESS_TYPESTRING,
witness: witness,
allocatorSignature: allocatorSignature,
sponsorSignature: sponsorSignature,
sponsor: sponsor,
nonce: nonce,
expires: expires,
id: id,
allocatedAmount: allocatedAmount,
amount: claimedAmount,
claimant: claimant
});

theCompact.claim(claimPayload);
}
}
Loading