Verified contractPartialMIT
StargateImplL2V2
Address0x7d185d7c75305a73d2bf77ba8ac92c265cea98db
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.7
- Same code
Only this contract
Details
- Compiler
- solc 0.8.7+commit.e28d00a7
- Language
- solidity
- Optimization
- Yes, 1,000,000 runs
- EVM version
- london
- License
- MIT
- Match
- Partialcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 18,343,353
- Creation transaction
- 0x073d07ea1e…0f0a51dca0
65 files
The source code is large: one file is shown at a time. Choose a file in the list.
- src/bridges/stargate/l2/StargateV2.solmain16.04 KB
- lib/solmate/src/tokens/ERC20.sol6.81 KB
- lib/solmate/src/utils/SafeTransferLib.sol5.96 KB
- src/SocketGateway.sol131.03 KB
- src/SocketGatewayDeployment.sol131.02 KB
- src/bridges/BridgeImplBase.sol5.01 KB
- src/bridges/across/Across.sol11.26 KB
- src/bridges/across/interfaces/across.sol1.84 KB
- src/bridges/anyswap-router-v4/l1/Anyswap.sol8.34 KB
- src/bridges/anyswap-router-v4/l2/Anyswap.sol7.87 KB
- src/bridges/anyswap-router-v6/AnyswapV6.sol10.35 KB
- src/bridges/arbitrum/interfaces/arbitrum.sol1.55 KB
- src/bridges/arbitrum/l1/NativeArbitrum.sol10.48 KB
- src/bridges/cbridge/CelerImpl.sol18.39 KB
- src/bridges/cbridge/CelerImplV2.sol26.16 KB
- src/bridges/cbridge/CelerStorageWrapper.sol2.85 KB
- src/bridges/cbridge/interfaces/ICelerStorageWrapper.sol1.92 KB
- src/bridges/cbridge/interfaces/cbridge.sol706 B
- src/bridges/cctp/Cctp.sol7.59 KB
- src/bridges/cctp/interfaces/cctp.sol278 B
- src/bridges/connnext/Connext.sol9.54 KB
- src/bridges/hop/interfaces/IHopL1Bridge.sol1.38 KB
- src/bridges/hop/interfaces/amm.sol1.04 KB
- src/bridges/hop/l1/HopImplL1.sol12.03 KB
- src/bridges/hop/l1/HopImplL1V2.sol13.91 KB
- src/bridges/hop/l2/HopImplL2.sol12.72 KB
- src/bridges/hop/l2/HopImplL2V2.sol16.36 KB
- src/bridges/hyphen/Hyphen.sol8.88 KB
- src/bridges/hyphen/interfaces/hyphen.sol1.23 KB
- src/bridges/optimism/interfaces/optimism.sol2.28 KB
- src/bridges/optimism/l1/NativeOpStack.sol12.55 KB
- src/bridges/optimism/l1/NativeOptimism.sol16.08 KB
- src/bridges/polygon/NativePolygon.sol9.48 KB
- src/bridges/polygon/interfaces/polygon.sol1.09 KB
- src/bridges/refuel/interfaces/refuel.sol478 B
- src/bridges/refuel/refuel.sol5.76 KB
- src/bridges/stargate/interfaces/IStargateEthVault.sol435 B
- src/bridges/stargate/interfaces/stargate.sol2.44 KB
- src/bridges/stargate/l1/Stargate.sol13.59 KB
- src/bridges/stargate/l2/Stargate.sol13.85 KB
- src/bridges/synapse/SynapseBridgeImpl.sol10.99 KB
- src/bridges/synapse/interfaces/ISynapseRouter.sol462 B
- src/bridges/zksync/ZkSyncBridgeImpl.sol11.54 KB
- src/bridges/zksync/interfaces/IZkSyncL1ERC20Bridge.sol1.25 KB
- src/bridges/zksync/interfaces/IZkSyncL1Mailbox.sol1.28 KB
- src/controllers/BaseController.sol2.28 KB
- src/controllers/FeesTakerController.sol12.78 KB
- src/controllers/RefuelSwapAndBridgeController.sol2.34 KB
- src/deployFactory/DisabledSocketRoute.sol2.42 KB
- src/deployFactory/SocketDeployFactory.sol12.77 KB
- src/errors/SocketErrors.sol530 B
- src/interfaces/ISocketBridgeBase.sol121 B
- src/interfaces/ISocketController.sol1.6 KB
- src/interfaces/ISocketGateway.sol852 B
- src/interfaces/ISocketRequest.sol2.17 KB
- src/interfaces/ISocketRoute.sol1.33 KB
- src/libraries/LibBytes.sol6.45 KB
- src/libraries/LibUtil.sol1.07 KB
- src/libraries/Pb.sol4.2 KB
- src/static/RouteIdentifiers.sol924 B
- src/swap/SwapImplBase.sol5.79 KB
- src/swap/oneinch/OneInchImpl.sol6.04 KB
- src/swap/rainbow/Rainbow.sol6.8 KB
- src/swap/zerox/ZeroXSwapImpl.sol7.72 KB
- src/utils/Ownable.sol1.21 KB
src/bridges/stargate/l2/StargateV2.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import {SafeTransferLib} from "lib/solmate/src/utils/SafeTransferLib.sol";
import {ERC20} from "lib/solmate/src/tokens/ERC20.sol";
import "../interfaces/stargate.sol";
import "../../../errors/SocketErrors.sol";
import {BridgeImplBase} from "../../BridgeImplBase.sol";
import {STARGATE} from "../../../static/RouteIdentifiers.sol";
/**
* @title Stargate-L2-Route Implementation
* @notice Route implementation with functions to bridge ERC20 and Native via Stargate-L2-Bridge
* Called via SocketGateway if the routeId in the request maps to the routeId of Stargate-L2-Implementation
* Contains function to handle bridging as post-step i.e linked to a preceeding step for swap
* RequestData is different to just bride and bridging chained with swap
* @author Socket dot tech.
*/
contract StargateImplL2V2 is BridgeImplBase {
/// @notice SafeTransferLib - library for safe and optimised operations on ERC20 tokens
using SafeTransferLib for ERC20;
bytes32 public immutable StargateIdentifier = STARGATE;
/// @notice max value for uint256
uint256 public constant UINT256_MAX = type(uint256).max;
/// @notice Function-selector for ERC20-token bridging on Stargate-L2-Route
/// @dev This function selector is to be used while buidling transaction-data to bridge ERC20 tokens
bytes4
public immutable STARGATE_L2_ERC20_EXTERNAL_BRIDGE_FUNCTION_SELECTOR =
bytes4(
keccak256(
"bridgeERC20To(address,address,address,uint256,uint256,(uint256,uint256,uint256,uint256,bytes32,bytes,uint16))"
)
);
bytes4 public immutable STARGATE_L1_SWAP_BRIDGE_SELECTOR =
bytes4(
keccak256(
"swapAndBridge(uint32,bytes,(address,address,uint16,uint256,uint256,uint256,uint256,uint256,uint256,bytes32,bytes))"
)
);
/// @notice Function-selector for Native bridging on Stargate-L2-Route
/// @dev This function selector is to be used while buidling transaction-data to bridge Native tokens
bytes4
public immutable STARGATE_L2_NATIVE_EXTERNAL_BRIDGE_FUNCTION_SELECTOR =
bytes4(
keccak256(
"bridgeNativeTo(address,address,uint16,uint256,uint256,uint256,bytes32)"
)
);
/// @notice Stargate Router to bridge ERC20 tokens
IBridgeStargate public immutable router;
/// @notice socketGatewayAddress to be initialised via storage variable BridgeImplBase
/// @dev ensure router, routerEth are set properly for the chainId in which the contract is being deployed
constructor(
address _router,
address _socketGateway,
address _socketDeployFactory
) BridgeImplBase(_socketGateway, _socketDeployFactory) {
router = IBridgeStargate(_router);
}
/// @notice Struct to be used as a input parameter for Bridging tokens via Stargate-L2-route
/// @dev while building transactionData,values should be set in this sequence of properties in this struct
struct StargateBridgeExtraData {
uint256 srcPoolId;
uint256 dstPoolId;
uint256 destinationGasLimit;
uint256 minReceivedAmt;
uint256 value;
uint16 stargateDstChainId;
uint32 swapId;
bytes32 metadata;
bytes swapData;
bytes destinationPayload;
// stargate defines chain id in its way
}
/// @notice Struct to be used in decode step from input parameter - a specific case of bridging after swap.
/// @dev the data being encoded in offchain or by caller should have values set in this sequence of properties in this struct
struct StargateBridgeDataNoToken {
address receiverAddress;
address senderAddress;
// stargate defines chain id in its way
uint256 value;
// a unique identifier that is uses to dedup transfers
// this value is the a timestamp sent from frontend, but in theory can be any unique number
uint256 srcPoolId;
uint256 dstPoolId;
uint256 minReceivedAmt; // defines the slippage, the min qty you would accept on the destination
uint256 destinationGasLimit;
bool isNativeSwapRequired;
uint16 stargateDstChainId;
uint32 swapId;
bytes swapData;
bytes32 metadata;
bytes destinationPayload;
}
struct StargateBridgeData {
address token;
address receiverAddress;
address senderAddress;
uint16 stargateDstChainId; // stargate defines chain id in its way
uint256 value;
// a unique identifier that is uses to dedup transfers
// this value is the a timestamp sent from frontend, but in theory can be any unique number
uint256 srcPoolId;
uint256 dstPoolId;
uint256 minReceivedAmt; // defines the slippage, the min qty you would accept on the destination
bool isNativeSwapRequired;
uint256 destinationGasLimit;
uint32 swapId;
bytes swapData;
bytes32 metadata;
bytes destinationPayload;
}
/**
* @notice function to bridge tokens after swap.
* @notice this is different from swapAndBridge, this function is called when the swap has already happened at a different place.
* @notice This method is payable because the caller is doing token transfer and briding operation
* @dev for usage, refer to controller implementations
* encodedData for bridge should follow the sequence of properties in Stargate-BridgeData struct
* @param amount amount of tokens being bridged. this can be ERC20 or native
* @param bridgeData encoded data for Stargate-L1-Bridge
*/
function bridgeAfterSwap(
uint256 amount,
bytes calldata bridgeData
) external payable override {
StargateBridgeData memory stargateBridgeData = abi.decode(
bridgeData,
(StargateBridgeData)
);
if (stargateBridgeData.token == NATIVE_TOKEN_ADDRESS) {
router.swapETHAndCall{value: amount + stargateBridgeData.value}(
stargateBridgeData.stargateDstChainId,
payable(stargateBridgeData.senderAddress), // default to refund to main contract
abi.encodePacked(stargateBridgeData.receiverAddress),
IBridgeStargate.SwapAmount(
amount,
stargateBridgeData.minReceivedAmt
),
IBridgeStargate.lzTxObj(
stargateBridgeData.destinationGasLimit,
0,
"0x"
),
stargateBridgeData.destinationPayload
);
} else {
if (stargateBridgeData.isNativeSwapRequired)
_performNativeSwap(
stargateBridgeData.swapData,
stargateBridgeData.swapId,
stargateBridgeData.value
);
if (
amount >
ERC20(stargateBridgeData.token).allowance(
address(this),
address(router)
)
) {
ERC20(stargateBridgeData.token).safeApprove(
address(router),
UINT256_MAX
);
}
{
router.swap{value: stargateBridgeData.value}(
stargateBridgeData.stargateDstChainId,
stargateBridgeData.srcPoolId,
stargateBridgeData.dstPoolId,
payable(stargateBridgeData.senderAddress), // default to refund to main contract
amount,
stargateBridgeData.minReceivedAmt,
IBridgeStargate.lzTxObj(
stargateBridgeData.destinationGasLimit,
0, // zero amount since this is a ERC20 bridging
"0x" //empty data since this is for only ERC20
),
abi.encodePacked(stargateBridgeData.receiverAddress),
stargateBridgeData.destinationPayload
);
}
}
emit SocketBridge(
amount,
stargateBridgeData.token,
stargateBridgeData.stargateDstChainId,
StargateIdentifier,
msg.sender,
stargateBridgeData.receiverAddress,
stargateBridgeData.metadata
);
}
/**
* @notice function to bridge tokens after swapping.
* @notice this is different from bridgeAfterSwap since this function holds the logic for swapping tokens too.
* @notice This method is payable because the caller is doing token transfer and briding operation
* @dev for usage, refer to controller implementations
* encodedData for bridge should follow the sequence of properties in Stargate-BridgeData struct
* @param swapId routeId for the swapImpl
* @param swapData encoded data for swap
* @param stargateBridgeData encoded data for StargateBridgeData
*/
function swapAndBridge(
uint32 swapId,
bytes calldata swapData,
StargateBridgeDataNoToken calldata stargateBridgeData
) external payable {
(bool success, bytes memory result) = socketRoute
.getRoute(swapId)
.delegatecall(swapData);
if (!success) {
assembly {
revert(add(result, 32), mload(result))
}
}
(uint256 bridgeAmount, address token) = abi.decode(
result,
(uint256, address)
);
if (token == NATIVE_TOKEN_ADDRESS) {
router.swapETHAndCall{
value: bridgeAmount + stargateBridgeData.value
}(
stargateBridgeData.stargateDstChainId,
payable(stargateBridgeData.senderAddress), // default to refund to main contract
abi.encodePacked(stargateBridgeData.receiverAddress),
IBridgeStargate.SwapAmount(
bridgeAmount,
stargateBridgeData.minReceivedAmt
),
IBridgeStargate.lzTxObj(
stargateBridgeData.destinationGasLimit,
0,
"0x"
),
stargateBridgeData.destinationPayload
);
} else {
if (stargateBridgeData.isNativeSwapRequired)
_performNativeSwap(
stargateBridgeData.swapData,
stargateBridgeData.swapId,
stargateBridgeData.value
);
if (
bridgeAmount >
ERC20(token).allowance(address(this), address(router))
) {
ERC20(token).safeApprove(address(router), UINT256_MAX);
}
{
router.swap{value: stargateBridgeData.value}(
stargateBridgeData.stargateDstChainId,
stargateBridgeData.srcPoolId,
stargateBridgeData.dstPoolId,
payable(stargateBridgeData.senderAddress), // default to refund to main contract
bridgeAmount,
stargateBridgeData.minReceivedAmt,
IBridgeStargate.lzTxObj(
stargateBridgeData.destinationGasLimit,
0,
"0x"
),
abi.encodePacked(stargateBridgeData.receiverAddress),
stargateBridgeData.destinationPayload
);
}
}
emit SocketBridge(
bridgeAmount,
token,
stargateBridgeData.stargateDstChainId,
StargateIdentifier,
msg.sender,
stargateBridgeData.receiverAddress,
stargateBridgeData.metadata
);
}
/**
* @notice function to handle ERC20 bridging to receipent via Stargate-L1-Bridge
* @notice This method is payable because the caller is doing token transfer and briding operation
* @param token address of token being bridged
* @param senderAddress address of sender
* @param receiverAddress address of recipient
* @param amount amount of token being bridge
* @param stargateBridgeExtraData stargate bridge extradata
*/
function bridgeERC20To(
address token,
address senderAddress,
address receiverAddress,
uint256 amount,
StargateBridgeExtraData calldata stargateBridgeExtraData
) external payable {
_performNativeSwap(
stargateBridgeExtraData.swapData,
stargateBridgeExtraData.swapId,
stargateBridgeExtraData.value
);
// token address might not be indication thats why passed through extraData
ERC20 tokenInstance = ERC20(token);
tokenInstance.safeTransferFrom(msg.sender, socketGateway, amount);
if (amount > tokenInstance.allowance(address(this), address(router))) {
tokenInstance.safeApprove(address(router), UINT256_MAX);
}
{
router.swap{value: stargateBridgeExtraData.value}(
stargateBridgeExtraData.stargateDstChainId,
stargateBridgeExtraData.srcPoolId,
stargateBridgeExtraData.dstPoolId,
payable(senderAddress), // default to refund to main contract
amount,
stargateBridgeExtraData.minReceivedAmt,
IBridgeStargate.lzTxObj(
stargateBridgeExtraData.destinationGasLimit,
0, // zero amount since this is a ERC20 bridging
"0x" //empty data since this is for only ERC20
),
abi.encodePacked(receiverAddress),
stargateBridgeExtraData.destinationPayload
);
}
emit SocketBridge(
amount,
token,
stargateBridgeExtraData.stargateDstChainId,
StargateIdentifier,
msg.sender,
receiverAddress,
stargateBridgeExtraData.metadata
);
}
function bridgeNativeTo(
address senderAddress,
address receiverAddress,
uint256 amount,
StargateBridgeExtraData calldata stargateBridgeExtraData
) external payable {
router.swapETHAndCall{value: amount + stargateBridgeExtraData.value}(
stargateBridgeExtraData.stargateDstChainId,
payable(senderAddress), // default to refund to main contract
abi.encodePacked(receiverAddress),
IBridgeStargate.SwapAmount(
amount,
stargateBridgeExtraData.minReceivedAmt
),
IBridgeStargate.lzTxObj(
stargateBridgeExtraData.destinationGasLimit,
0,
"0x"
),
stargateBridgeExtraData.destinationPayload
);
emit SocketBridge(
amount,
NATIVE_TOKEN_ADDRESS,
stargateBridgeExtraData.stargateDstChainId,
StargateIdentifier,
msg.sender,
receiverAddress,
stargateBridgeExtraData.metadata
);
}
function _performNativeSwap(
bytes memory swapData,
uint32 swapId,
uint256 valueRequired
) private {
(bool success, bytes memory result) = socketRoute
.getRoute(swapId)
.delegatecall(swapData);
if (!success) {
assembly {
revert(add(result, 32), mload(result))
}
}
(uint256 valueReceived, ) = abi.decode(result, (uint256, address));
if (valueReceived > valueRequired) {
msg.sender.call{value: valueReceived - valueRequired}("");
}
}
}
Compiler settingsShow
{
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"libraries": {},
"optimizer": {
"runs": 1000000,
"enabled": true
},
"evmVersion": "london",
"remappings": []
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.