Skip to content

Commit

Permalink
Merge pull request ferrumnet#9 from lukmaan-ferrum/updated-fee-distri…
Browse files Browse the repository at this point in the history
…butor

Updated fee distributor
  • Loading branch information
lukmaan-ferrum committed May 14, 2024
2 parents 4398550 + 21dc6e0 commit 0f9d16b
Show file tree
Hide file tree
Showing 15 changed files with 913 additions and 524 deletions.
41 changes: 23 additions & 18 deletions constants/addresses.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
"weth": "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
"foundry": "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
"deployments": {
"fiberRouter": "0x68902Ec3058fdBb37Ac3D832a62112d7764a08ed",
"fundManager": "0xC7b23d5Da44f3d421aD27863788bEdcce4b34B0d",
"cctpFundManager": "0x7bD02622F39E458D5d5eaAD51C304Cac064ACFf4",
"multiSwapForge": "0x3DC32De3FE7Acd6206Fe77Ad13367caD645eA7e5",
"forgeFundManager": "0xEfb28F03C581010995388D8035cbD0ed1d997219",
"forgeCCTPFundManager": "0x9ed1D73B823563067B08133F61f495744cFcCb27"
"fiberRouter": "0xaDE10E8522AeCadBBe02279c133Fc46E45f92194",
"fundManager": "0x5dBF50235fdD9D4D5669E6e0B3Bef3655dEAffe7",
"cctpFundManager": "0x82496D3bDE9168dfeF6bc8dF0439B51790ff42DF",
"multiSwapForge": "0x3CAf342BCe0f4c0451184973984DE479EcD16C69",
"forgeFundManager": "0xA2d585247Fa56F6C50E554dBAa732c6fdc31eE9b",
"forgeCCTPFundManager": "0xbefD116064500794fD56f582672e7710217B5A72"
},
"cctp": {
"tokenMessenger": "0x19330d10D9Cc8751218eaf51E8885D058642E08A",
Expand Down Expand Up @@ -55,12 +55,12 @@
"weth": "0x4200000000000000000000000000000000000006",
"foundry": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
"deployments": {
"fiberRouter": "0x84250F8d4617485C3D5f1d7b1bC8C624e5695A80",
"fundManager": "0x7022a2dE2434AbA5985e87A84E93E5C9Cc3AB05F",
"cctpFundManager": "0x7EA16f3a967db7133D50807e5Fb3ca5FBc4A3dD6",
"multiSwapForge": "0xfff75b0e798c61709779566D006B98b41143CD31",
"forgeFundManager": "0x6a8A46D77fd2144FF1BAEF19e8c2c2B4C2f40a84",
"forgeCCTPFundManager": "0xeC11d853f05e7174F43834AfCf31d52D6d01B552"
"fiberRouter": "0x851930502D363D75C4a0C0c8366ADc43980aA048",
"fundManager": "0x1938b36110e4016c54647de123a07F4dB13026C7",
"cctpFundManager": "0x59C1CF95a57F63094637Af70008c55452fFD1b9f",
"multiSwapForge": "0x62d114BD13a177E80db8a380cB5E83886Fe33783",
"forgeFundManager": "0xC71C76633B21C6367C1364744aace19873407891",
"forgeCCTPFundManager": "0xF7F9b482F3cbF7E9D2cbBCA4a7599Ded76ce8056"
},
"cctp": {
"tokenMessenger": "0x1682Ae6375C4E4A97e4B583BC394c861A46D8962",
Expand Down Expand Up @@ -97,11 +97,10 @@
"chainId": 11155111,
"weth": "0x5300000000000000000000000000000000000004",
"foundry": "0x06eFdBFf2a14a7c8E15944D1F4A48F9F95F663A4",
"deployments": {},
"cctp": {
"tokenMessenger": "0x1682Ae6375C4E4A97e4B583BC394c861A46D8962",
"domain": 6,
"cctpFundManager": "0xe49a23c8179e1cDb180464CE51ADcdaf72bB55cD",
"forgeCctpFundManager": "0xEc2A682216B06926a2149Dde374D074e77E2626d"
"domain": 6
},
"routers": [
{
Expand All @@ -118,11 +117,17 @@
"chainId": 11155111,
"weth": "0x5300000000000000000000000000000000000004",
"foundry": "0x06eFdBFf2a14a7c8E15944D1F4A48F9F95F663A4",
"deployments": {
"fiberRouter": "0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6",
"fundManager": "0x8A791620dd6260079BF849Dc5567aDC3F2FdC318",
"multiSwapForge": "0x610178dA211FEF7D417bC0e6FeD39F05609AD788",
"forgeFundManager": "0xB7f8BC63BbcaD18155201308C8f3540b07f84F5e",
"cctpFundManager": "0xA51c1fc2f0D1a1b8494Ed1FE312d7C3a78Ed91C0",
"forgeCCTPFundManager": "0x0DCd1Bf9A1b36cE34237eEaFef220932846BCD82"
},
"cctp": {
"tokenMessenger": "0x1682Ae6375C4E4A97e4B583BC394c861A46D8962",
"domain": 6,
"cctpFundManager": "0xe49a23c8179e1cDb180464CE51ADcdaf72bB55cD",
"forgeCctpFundManager": "0xEc2A682216B06926a2149Dde374D074e77E2626d"
"domain": 6
},
"routers": [
{
Expand Down
25 changes: 0 additions & 25 deletions contracts/common/SwapTypeDecoder.sol

This file was deleted.

26 changes: 0 additions & 26 deletions contracts/common/its/ChainIDLookup.sol

This file was deleted.

82 changes: 0 additions & 82 deletions contracts/common/its/InterchainTokenExecutable.sol

This file was deleted.

44 changes: 0 additions & 44 deletions contracts/common/its/interfaces/IInterchainTokenStandard.sol

This file was deleted.

66 changes: 48 additions & 18 deletions contracts/multiswap-contracts/FeeDistributor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,31 @@ import "@openzeppelin/contracts/access/Ownable.sol";
contract FeeDistributor is EIP712, Ownable {
using SafeERC20 for IERC20;
using ECDSA for bytes32;
string public constant NAME = "FEE_DISTRIBUTOR";
string public constant VERSION = "000.001";

string constant NAME = "FEE_DISTRIBUTOR";
string constant VERSION = "000.001";
uint32 constant MINUTE = 60;

mapping(address => bool) public signers;
mapping(bytes32 => bool) public usedSalt;

bytes32 public constant DISTRIBUTE_FEES_TYPEHASH = keccak256(
"DistributeFees(address token,address recipient,uint16 platformFee,uint256 sourceAmountIn,uint256 sourceAmountOut,bytes32 salt,uint256 expiry,bytes signature)"
bytes32 constant FEE_ALLOCATION_TYPEHASH = keccak256("FeeAllocation(address recipient,uint256 platformFee)");
bytes32 constant DISTRIBUTE_FEES_TYPEHASH = keccak256(
"DistributeFees(address token,FeeAllocation[] feeAllocations,uint256 totalPlatformFee,uint256 sourceAmountIn,uint256 sourceAmountOut,uint256 destinationAmountIn,uint256 destinationAmountOut,bytes32 salt,uint256 expiry)FeeAllocation(address recipient,uint256 platformFee)"
);

struct FeeDistributionData {
struct FeeAllocation {
address recipient;
uint16 platformFee;
uint256 platformFee;
}

struct FeeDistributionData {
FeeAllocation[] feeAllocations;
uint256 totalPlatformFee;
uint256 sourceAmountIn;
uint256 sourceAmountOut;
uint256 destinationAmountIn;
uint256 destinationAmountOut;
bytes32 salt;
uint256 expiry;
bytes signature;
Expand All @@ -33,7 +44,7 @@ contract FeeDistributor is EIP712, Ownable {
address indexed token,
uint256 preFeeAmount,
uint256 afterFeeAmount,
uint256 totalFee
uint256 totalPlatformFee
);

constructor() EIP712(NAME, VERSION) {}
Expand Down Expand Up @@ -61,37 +72,56 @@ contract FeeDistributor is EIP712, Ownable {
FeeDistributionData memory fdd
) internal returns (uint256) {
require(_verify(token, fdd), "FD: Invalid signature");
// Transfer the token fee to the recipient
SafeERC20.safeTransfer(IERC20(token), fdd.recipient, fdd.platformFee);
uint256 postFeeAmount = preFeeAmount - fdd.platformFee;
// Emit event
emit FeesDistributed(token, preFeeAmount, postFeeAmount, fdd.platformFee);
return postFeeAmount;

uint256 totalFees;
for (uint256 i = 0; i < fdd.feeAllocations.length; i++) {
IERC20(token).safeTransfer(fdd.feeAllocations[i].recipient, fdd.feeAllocations[i].platformFee);
totalFees += fdd.feeAllocations[i].platformFee;
}

emit FeesDistributed(token, preFeeAmount, preFeeAmount - totalFees, totalFees);

// require(preFeeAmount == fdd.sourceAmountIn, "FD: Incorrect source amount");
// require(totalFees == fdd.totalPlatformFee, "FD: Incorrect total fee");

return preFeeAmount - totalFees;
}

function _verify(
address token,
FeeDistributionData memory fdd
) private returns (bool) {
require(block.timestamp < fdd.expiry, "FD: signature timed out");
require(fdd.expiry < block.timestamp + (20 * MINUTE), "FD: expiry too far"); // 20 minutes probably too generous. Users should be submitting tx soon after quote on source chain
require(fdd.expiry < block.timestamp + (20 * MINUTE) , "FD: expiry too far"); // 20 minutes probably too generous. Users should be submitting tx soon after quote on source chain
require(!usedSalt[fdd.salt], "FM: salt already used");
usedSalt[fdd.salt] = true;

bytes32 structHash = keccak256(
abi.encode(
DISTRIBUTE_FEES_TYPEHASH,
token,
fdd.recipient,
fdd.platformFee,
_encodeFeeAllocations(fdd.feeAllocations),
fdd.totalPlatformFee,
fdd.sourceAmountIn,
fdd.sourceAmountOut,
fdd.destinationAmountIn,
fdd.destinationAmountOut,
fdd.salt,
fdd.expiry,
fdd.signature
fdd.expiry
)
);

bytes32 digest = _hashTypedDataV4(structHash);
address signer = ECDSA.recover(digest, fdd.signature);
return signers[signer];
}

function _encodeFeeAllocations(FeeAllocation[] memory feeAllocations) private pure returns (bytes32) {
bytes32[] memory encodedFeeAllocations = new bytes32[](feeAllocations.length);
for (uint256 i = 0; i < feeAllocations.length; i++) {
encodedFeeAllocations[i] = keccak256(abi.encode(FEE_ALLOCATION_TYPEHASH, feeAllocations[i].recipient, feeAllocations[i].platformFee));
}

return keccak256(abi.encodePacked(encodedFeeAllocations));
}
}
Loading

0 comments on commit 0f9d16b

Please sign in to comment.