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

WIP - bridge block #7

Closed
wants to merge 21 commits into from
1 change: 1 addition & 0 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ApproveAndSwapTest:testSwap() (gas: 285500)
ApproveAndSwapTest:testSwapFailsIfWeExpectedTooMuch() (gas: 364666)
ApproveAndSwapTest:testSwapFailsWithNoApproval() (gas: 122231)
CCTPBridge:testBridgeToBase() (gas: 146874)
CometClaimRewardsTest:testClaimComp() (gas: 131265)
CometRepayAndWithdrawMultipleAssetsTest:testInvalidInput() (gas: 68171)
CometRepayAndWithdrawMultipleAssetsTest:testRepayAndWithdrawMultipleAssets() (gas: 153860)
Expand Down
18 changes: 18 additions & 0 deletions src/BridgeScripts.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity 0.8.23;

import {IERC20} from "openzeppelin/token/ERC20/IERC20.sol";
import {ITokenMessenger} from "./interfaces/ITokenMessenger.sol";

contract CCTPBridgeActions {
function bridgeUSDC(
address tokenMessenger,
uint256 amount,
uint32 destinationDomain,
bytes32 mintRecipient,
address burnToken
) external {
IERC20(burnToken).approve(tokenMessenger, amount);
ITokenMessenger(tokenMessenger).depositForBurn(amount, destinationDomain, mintRecipient, burnToken);
}
}
103 changes: 103 additions & 0 deletions src/builder/BridgeRoutes.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity ^0.8.23;

library BridgeRoutes {
enum BridgeType {
NONE,
CCTP
}

uint256 constant BASE_CHAIN_ID = 8453;
uint256 constant MAINNET_CHAIN_ID = 1;

mapping(uint256 => uint32) public chainIdToCCTPDomainId;

constructor() {
// Right now we only have Mainnet and Base
chainIdToCCTPDomainId[MAINNET_CHAIN_ID] = 0;
chainIdToCCTPDomainId[BASE_CHAIN_ID] = 6;
}

struct Bridge {
// Note: Cannot name these `address` nor `type` because those are both reserved keywords
address bridgeAddress;
BridgeType bridgeType;
uint32 domainId;
}

function hasBridge(uint256 srcChainId, uint256 dstChainId, string memory assetSymbol) internal pure returns (bool) {
if (getBridge(srcChainId, dstChainid, assetSymbol).bridgeType == BridgeType.NONE) {
return false;
} else {
return true;
}
}

function getBridge(uint256 srcChainId, uint256 dstChainId, string memory assetSymbol) internal pure returns (Bridge memory) {
if (srcChainId == 1) {
return getBridgeForMainnet(dstChainId, assetSymbol);
} else if (srcChainId == 8453) {
return getBridgeForBase(dstChainId, assetSymbol);
} else {
return Bridge({
bridgeAddress: address(0),
bridgeType: BridgeType.NONE
});
// revert BridgeNotFound(1, dstChainid, assetSymbol);
}
}

function getBridge(uint256 srcChainId, uint256 dstChainId, string memory assetSymbol) internal pure returns (Bridge memory) {
if (srcChainId == MAINNET_CHAIN_ID) {
return getBridgeForMainnet(dstChainId, assetSymbol);
} else if (srcChainId == BASE_CHAIN_ID) {
return getBridgeForBase(dstChainId, assetSymbol);
} else {
return Bridge({
bridgeAddress: address(0),
bridgeType: BridgeType.NONE
});
// revert BridgeNotFound(1, dstChainid, assetSymbol);
}
}

function getBridgeForMainnet(uint256 dstChainId, string memory assetSymbol) internal pure returns (Bridge memory) {
if (dstChainId == MAINNET_CHAIN_ID) {
revert("Cannot bridge to the same chain");
}

if (compareStrings(assetSymbol, "USDC")) {
return Bridge({
bridgeAddress: 0xBd3fa81B58Ba92a82136038B25aDec7066af3155,
bridgeType: BridgeType.CCTP,
domainId: chainIdToCCTPDomainId[dstChainId]
});
} else {
return Bridge({
bridgeAddress: address(0),
bridgeType: BridgeType.NONE
});
// revert BridgeNotFound(1, dstChainid, assetSymbol);
}
}

function getBridgeForBase(uint256 dstChainId, string memory assetSymbol) internal pure returns (Bridge memory) {
if (dstChainId == BASE_CHAIN_ID) {
revert("Cannot bridge to the same chain");
}

if (compareStrings(assetSymbol, "USDC")) {
return Bridge({
bridgeAddress: 0x1682Ae6375C4E4A97e4B583BC394c861A46D8962,
bridgeType: BridgeType.CCTP,
domainId: chainIdToCCTPDomainId[dstChainId]
});
} else {
return Bridge({
bridgeAddress: address(0),
bridgeType: BridgeType.NONE
});
// revert BridgeNotFound(1, dstChainid, assetSymbol);
}
}
}
Loading
Loading