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

Add CCTP Bridger #1

Merged
merged 6 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 14 additions & 0 deletions src/DeFiScripts.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {QuarkScript} from "quark-core/src/QuarkScript.sol";

import {IComet} from "./interfaces/IComet.sol";
import {ICometRewards} from "./interfaces/ICometRewards.sol";
import {ITokenMessenger} from "./interfaces/ITokenMessenger.sol";
cwang25 marked this conversation as resolved.
Show resolved Hide resolved
import {DeFiScriptErrors} from "./lib/DeFiScriptErrors.sol";

contract CometSupplyActions {
Expand Down Expand Up @@ -326,3 +327,16 @@ contract ApproveAndSwap {
IERC20(sellToken).forceApprove(to, 0);
}
}

contract CCTPBridgeActions {
cwang25 marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
8 changes: 8 additions & 0 deletions src/interfaces/ITokenMessenger.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity 0.8.23;

interface ITokenMessenger {
function depositForBurn(uint256 amount, uint32 destinationDomain, bytes32 mintRecipient, address burnToken)
external
returns (uint64 _nonce);
}
70 changes: 70 additions & 0 deletions test/CCTPBridge.t.sol
cwang25 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity 0.8.23;

import "forge-std/Test.sol";
import "forge-std/console.sol";
import "forge-std/StdUtils.sol";
import "forge-std/StdMath.sol";

import {CodeJar} from "codejar/src/CodeJar.sol";

import {QuarkWallet} from "quark-core/src/QuarkWallet.sol";
import {QuarkStateManager} from "quark-core/src/QuarkStateManager.sol";

import {QuarkWalletProxyFactory} from "quark-proxy/src/QuarkWalletProxyFactory.sol";

import {YulHelper} from "./lib/YulHelper.sol";
import {SignatureHelper} from "./lib/SignatureHelper.sol";
import {QuarkOperationHelper, ScriptType} from "./lib/QuarkOperationHelper.sol";

import "src/DeFiScripts.sol";

/**
* Tests for CCTP Bridge
*/
contract CCTPBridge is Test {
QuarkWalletProxyFactory public factory;
uint256 alicePrivateKey = 0xa11ce;
address alice = vm.addr(alicePrivateKey);

// Contracts address on mainnet
address constant tokenMessenger = 0xBd3fa81B58Ba92a82136038B25aDec7066af3155;
address constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
// Domain value for the CCTP bridge, DIFFERENT from chain id
uint32 constant mainnetDomain = 0;
uint32 constant baseDomain = 6;

function setUp() public {
// Fork setup
vm.createSelectFork(
string.concat(
"https://node-provider.compound.finance/ethereum-mainnet/", vm.envString("NODE_PROVIDER_BYPASS_KEY")
),
18429607 // 2023-10-25 13:24:00 PST
);
factory = new QuarkWalletProxyFactory(address(new QuarkWallet(new CodeJar(), new QuarkStateManager())));
}

function testBridgeToBase() public {
vm.pauseGasMetering();
QuarkWallet wallet = QuarkWallet(factory.create(alice, address(0)));
bytes memory cctpBridgeScript = new YulHelper().getCode("DeFiScripts.sol/CCTPBridgeActions.json");

deal(USDC, address(wallet), 1_000_000e6);

QuarkWallet.QuarkOperation memory op = new QuarkOperationHelper().newBasicOpWithCalldata(
wallet,
cctpBridgeScript,
abi.encodeCall(
CCTPBridgeActions.bridgeUSDC,
(tokenMessenger, 500_000e6, 6, bytes32(uint256(uint160(address(wallet)))), USDC)
),
ScriptType.ScriptSource
);
(uint8 v, bytes32 r, bytes32 s) = new SignatureHelper().signOp(alicePrivateKey, wallet, op);
assertEq(IERC20(USDC).balanceOf(address(wallet)), 1_000_000e6);
vm.resumeGasMetering();
wallet.executeQuarkOperation(op, v, r, s);
assertEq(IERC20(USDC).balanceOf(address(wallet)), 500_000e6);
cwang25 marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading