Description:
Proxy contract enabling upgradeable smart contract patterns. Delegates calls to an implementation contract.
Blockchain: Ethereum
Source Code: View Code On The Blockchain
Solidity Source Code:
{{
"language": "Solidity",
"sources": {
"@openzeppelin/contracts/security/ReentrancyGuard.sol": {
"content": "// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (security/ReentrancyGuard.sol)
pragma solidity ^0.8.0;
/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/
abstract contract ReentrancyGuard {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor() {
_status = _NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and making it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
// On the first call to nonReentrant, _notEntered will be true
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
// Any calls to nonReentrant after this point will fail
_status = _ENTERED;
_;
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = _NOT_ENTERED;
}
}
"
},
"@openzeppelin/contracts/token/ERC20/IERC20.sol": {
"content": "// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC20/IERC20.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `to`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address to, uint256 amount) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);
/**
* @dev Moves `amount` tokens from `from` to `to` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(
address from,
address to,
uint256 amount
) external returns (bool);
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
}
"
},
"@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol": {
"content": "// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/utils/SafeERC20.sol)
pragma solidity ^0.8.0;
import "../IERC20.sol";
import "../../../utils/Address.sol";
/**
* @title SafeERC20
* @dev Wrappers around ERC20 operations that throw on failure (when the token
* contract returns false). Tokens that return no value (and instead revert or
* throw on failure) are also supported, non-reverting calls are assumed to be
* successful.
* To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
*/
library SafeERC20 {
using Address for address;
function safeTransfer(
IERC20 token,
address to,
uint256 value
) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
function safeTransferFrom(
IERC20 token,
address from,
address to,
uint256 value
) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
/**
* @dev Deprecated. This function has issues similar to the ones found in
* {IERC20-approve}, and its usage is discouraged.
*
* Whenever possible, use {safeIncreaseAllowance} and
* {safeDecreaseAllowance} instead.
*/
function safeApprove(
IERC20 token,
address spender,
uint256 value
) internal {
// safeApprove should only be called when setting an initial allowance,
// or when resetting it to zero. To increase and decrease it, use
// 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
require(
(value == 0) || (token.allowance(address(this), spender) == 0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
function safeIncreaseAllowance(
IERC20 token,
address spender,
uint256 value
) internal {
uint256 newAllowance = token.allowance(address(this), spender) + value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function safeDecreaseAllowance(
IERC20 token,
address spender,
uint256 value
) internal {
unchecked {
uint256 oldAllowance = token.allowance(address(this), spender);
require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
uint256 newAllowance = oldAllowance - value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
}
/**
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
* on the return value: the return value is optional (but if data is returned, it must not be false).
* @param token The token targeted by the call.
* @param data The call data (encoded using abi.encode or one of its variants).
*/
function _callOptionalReturn(IERC20 token, bytes memory data) private {
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
// we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that
// the target address contains contract code and also asserts for success in the low-level call.
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
if (returndata.length > 0) {
// Return data is optional
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
}
}
"
},
"@openzeppelin/contracts/utils/Address.sol": {
"content": "// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (utils/Address.sol)
pragma solidity ^0.8.1;
/**
* @dev Collection of functions related to the address type
*/
library Address {
/**
* @dev Returns true if `account` is a contract.
*
* [IMPORTANT]
* ====
* It is unsafe to assume that an address for which this function returns
* false is an externally-owned account (EOA) and not a contract.
*
* Among others, `isContract` will return false for the following
* types of addresses:
*
* - an externally-owned account
* - a contract in construction
* - an address where a contract will be created
* - an address where a contract lived, but was destroyed
* ====
*
* [IMPORTANT]
* ====
* You shouldn't rely on `isContract` to protect against flash loan attacks!
*
* Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
* like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
* constructor.
* ====
*/
function isContract(address account) internal view returns (bool) {
// This method relies on extcodesize/address.code.length, which returns 0
// for contracts in construction, since the code is only stored at the end
// of the constructor execution.
return account.code.length > 0;
}
/**
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
* `recipient`, forwarding all available gas and reverting on errors.
*
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
* of certain opcodes, possibly making contracts go over the 2300 gas limit
* imposed by `transfer`, making them unable to receive funds via
* `transfer`. {sendValue} removes this limitation.
*
* https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
*
* IMPORTANT: because control is transferred to `recipient`, care must be
* taken to not create reentrancy vulnerabilities. Consider using
* {ReentrancyGuard} or the
* https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
*/
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, "Address: insufficient balance");
(bool success, ) = recipient.call{value: amount}("");
require(success, "Address: unable to send value, recipient may have reverted");
}
/**
* @dev Performs a Solidity function call using a low level `call`. A
* plain `call` is an unsafe replacement for a function call: use this
* function instead.
*
* If `target` reverts with a revert reason, it is bubbled up by this
* function (like regular Solidity function calls).
*
* Returns the raw returned data. To convert to the expected return value,
* use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
*
* Requirements:
*
* - `target` must be a contract.
* - calling `target` with `data` must not revert.
*
* _Available since v3.1._
*/
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCall(target, data, "Address: low-level call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
* `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but also transferring `value` wei to `target`.
*
* Requirements:
*
* - the calling contract must have an ETH balance of at least `value`.
* - the called Solidity function must be `payable`.
*
* _Available since v3.1._
*/
function functionCallWithValue(
address target,
bytes memory data,
uint256 value
) internal returns (bytes memory) {
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
}
/**
* @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
* with `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCallWithValue(
address target,
bytes memory data,
uint256 value,
string memory errorMessage
) internal returns (bytes memory) {
require(address(this).balance >= value, "Address: insufficient balance for call");
require(isContract(target), "Address: call to non-contract");
(bool success, bytes memory returndata) = target.call{value: value}(data);
return verifyCallResult(success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
return functionStaticCall(target, data, "Address: low-level static call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/
function functionStaticCall(
address target,
bytes memory data,
string memory errorMessage
) internal view returns (bytes memory) {
require(isContract(target), "Address: static call to non-contract");
(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResult(success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/
function functionDelegateCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
require(isContract(target), "Address: delegate call to non-contract");
(bool success, bytes memory returndata) = target.delegatecall(data);
return verifyCallResult(success, returndata, errorMessage);
}
/**
* @dev Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the
* revert reason using the provided one.
*
* _Available since v4.3._
*/
function verifyCallResult(
bool success,
bytes memory returndata,
string memory errorMessage
) internal pure returns (bytes memory) {
if (success) {
return returndata;
} else {
// Look for revert reason and bubble it up if present
if (returndata.length > 0) {
// The easiest way to bubble the revert reason is using memory via assembly
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
}
"
},
"contracts/interfaces/IBridge.sol": {
"content": "// SPDX-License-Identifier: GPL-3.0-only\r
\r
pragma solidity >=0.8.0;\r
\r
interface IBridge {\r
function send(\r
address _receiver,\r
address _token,\r
uint256 _amount,\r
uint64 _dstChainId,\r
uint64 _nonce,\r
uint32 _maxSlippage\r
) external;\r
\r
function sendNative(\r
address _receiver,\r
uint256 _amount,\r
uint64 _dstChainId,\r
uint64 _nonce,\r
uint32 _maxSlippage\r
) external payable;\r
\r
function relay(\r
bytes calldata _relayRequest,\r
bytes[] calldata _sigs,\r
address[] calldata _signers,\r
uint256[] calldata _powers\r
) external;\r
\r
function transfers(bytes32 transferId) external view returns (bool);\r
\r
function withdraws(bytes32 withdrawId) external view returns (bool);\r
\r
function withdraw(\r
bytes calldata _wdmsg,\r
bytes[] calldata _sigs,\r
address[] calldata _signers,\r
uint256[] calldata _powers\r
) external;\r
\r
/**\r
* @notice Verifies that a message is signed by a quorum among the signers.\r
* @param _msg signed message\r
* @param _sigs list of signatures sorted by signer addresses in ascending order\r
* @param _signers sorted list of current signers\r
* @param _powers powers of current signers\r
*/\r
function verifySigs(\r
bytes memory _msg,\r
bytes[] calldata _sigs,\r
address[] calldata _signers,\r
uint256[] calldata _powers\r
) external view;\r
}\r
"
},
"contracts/interfaces/IOriginalTokenVault.sol": {
"content": "// SPDX-License-Identifier: GPL-3.0-only\r
\r
pragma solidity >=0.8.0;\r
\r
interface IOriginalTokenVault {\r
/**\r
* @notice Lock original tokens to trigger mint at a remote chain's PeggedTokenBridge\r
* @param _token local token address\r
* @param _amount locked token amount\r
* @param _mintChainId destination chainId to mint tokens\r
* @param _mintAccount destination account to receive minted tokens\r
* @param _nonce user input to guarantee unique depositId\r
*/\r
function deposit(\r
address _token,\r
uint256 _amount,\r
uint64 _mintChainId,\r
address _mintAccount,\r
uint64 _nonce\r
) external;\r
\r
/**\r
* @notice Lock native token as original token to trigger mint at a remote chain's PeggedTokenBridge\r
* @param _amount locked token amount\r
* @param _mintChainId destination chainId to mint tokens\r
* @param _mintAccount destination account to receive minted tokens\r
* @param _nonce user input to guarantee unique depositId\r
*/\r
function depositNative(\r
uint256 _amount,\r
uint64 _mintChainId,\r
address _mintAccount,\r
uint64 _nonce\r
) external payable;\r
\r
/**\r
* @notice Withdraw locked original tokens triggered by a burn at a remote chain's PeggedTokenBridge.\r
* @param _request The serialized Withdraw protobuf.\r
* @param _sigs The list of signatures sorted by signing addresses in ascending order. A relay must be signed-off by\r
* +2/3 of the bridge's current signing power to be delivered.\r
* @param _signers The sorted list of signers.\r
* @param _powers The signing powers of the signers.\r
*/\r
function withdraw(\r
bytes calldata _request,\r
bytes[] calldata _sigs,\r
address[] calldata _signers,\r
uint256[] calldata _powers\r
) external;\r
\r
function records(bytes32 recordId) external view returns (bool);\r
}\r
"
},
"contracts/interfaces/IOriginalTokenVaultV2.sol": {
"content": "// SPDX-License-Identifier: GPL-3.0-only\r
\r
pragma solidity >=0.8.0;\r
\r
interface IOriginalTokenVaultV2 {\r
/**\r
* @notice Lock original tokens to trigger mint at a remote chain's PeggedTokenBridge\r
* @param _token local token address\r
* @param _amount locked token amount\r
* @param _mintChainId destination chainId to mint tokens\r
* @param _mintAccount destination account to receive minted tokens\r
* @param _nonce user input to guarantee unique depositId\r
*/\r
function deposit(\r
address _token,\r
uint256 _amount,\r
uint64 _mintChainId,\r
address _mintAccount,\r
uint64 _nonce\r
) external returns (bytes32);\r
\r
/**\r
* @notice Lock native token as original token to trigger mint at a remote chain's PeggedTokenBridge\r
* @param _amount locked token amount\r
* @param _mintChainId destination chainId to mint tokens\r
* @param _mintAccount destination account to receive minted tokens\r
* @param _nonce user input to guarantee unique depositId\r
*/\r
function depositNative(\r
uint256 _amount,\r
uint64 _mintChainId,\r
address _mintAccount,\r
uint64 _nonce\r
) external payable returns (bytes32);\r
\r
/**\r
* @notice Withdraw locked original tokens triggered by a burn at a remote chain's PeggedTokenBridge.\r
* @param _request The serialized Withdraw protobuf.\r
* @param _sigs The list of signatures sorted by signing addresses in ascending order. A relay must be signed-off by\r
* +2/3 of the bridge's current signing power to be delivered.\r
* @param _signers The sorted list of signers.\r
* @param _powers The signing powers of the signers.\r
*/\r
function withdraw(\r
bytes calldata _request,\r
bytes[] calldata _sigs,\r
address[] calldata _signers,\r
uint256[] calldata _powers\r
) external returns (bytes32);\r
\r
function records(bytes32 recordId) external view returns (bool);\r
}\r
"
},
"contracts/interfaces/IPeggedTokenBridge.sol": {
"content": "// SPDX-License-Identifier: GPL-3.0-only\r
\r
pragma solidity >=0.8.0;\r
\r
interface IPeggedTokenBridge {\r
/**\r
* @notice Burn tokens to trigger withdrawal at a remote chain's OriginalTokenVault\r
* @param _token local token address\r
* @param _amount locked token amount\r
* @param _withdrawAccount account who withdraw original tokens on the remote chain\r
* @param _nonce user input to guarantee unique depositId\r
*/\r
function burn(\r
address _token,\r
uint256 _amount,\r
address _withdrawAccount,\r
uint64 _nonce\r
) external;\r
\r
/**\r
* @notice Mint tokens triggered by deposit at a remote chain's OriginalTokenVault.\r
* @param _request The serialized Mint protobuf.\r
* @param _sigs The list of signatures sorted by signing addresses in ascending order. A relay must be signed-off by\r
* +2/3 of the sigsVerifier's current signing power to be delivered.\r
* @param _signers The sorted list of signers.\r
* @param _powers The signing powers of the signers.\r
*/\r
function mint(\r
bytes calldata _request,\r
bytes[] calldata _sigs,\r
address[] calldata _signers,\r
uint256[] calldata _powers\r
) external;\r
\r
function records(bytes32 recordId) external view returns (bool);\r
}\r
"
},
"contracts/interfaces/IPeggedTokenBridgeV2.sol": {
"content": "// SPDX-License-Identifier: GPL-3.0-only\r
\r
pragma solidity >=0.8.0;\r
\r
interface IPeggedTokenBridgeV2 {\r
/**\r
* @notice Burn pegged tokens to trigger a cross-chain withdrawal of the original tokens at a remote chain's\r
* OriginalTokenVault, or mint at another remote chain\r
* @param _token The pegged token address.\r
* @param _amount The amount to burn.\r
* @param _toChainId If zero, withdraw from original vault; otherwise, the remote chain to mint tokens.\r
* @param _toAccount The account to receive tokens on the remote chain\r
* @param _nonce A number to guarantee unique depositId. Can be timestamp in practice.\r
*/\r
function burn(\r
address _token,\r
uint256 _amount,\r
uint64 _toChainId,\r
address _toAccount,\r
uint64 _nonce\r
) external returns (bytes32);\r
\r
// same with `burn` above, use openzeppelin ERC20Burnable interface\r
function burnFrom(\r
address _token,\r
uint256 _amount,\r
uint64 _toChainId,\r
address _toAccount,\r
uint64 _nonce\r
) external returns (bytes32);\r
\r
/**\r
* @notice Mint tokens triggered by deposit at a remote chain's OriginalTokenVault.\r
* @param _request The serialized Mint protobuf.\r
* @param _sigs The list of signatures sorted by signing addresses in ascending order. A relay must be signed-off by\r
* +2/3 of the sigsVerifier's current signing power to be delivered.\r
* @param _signers The sorted list of signers.\r
* @param _powers The signing powers of the signers.\r
*/\r
function mint(\r
bytes calldata _request,\r
bytes[] calldata _sigs,\r
address[] calldata _signers,\r
uint256[] calldata _powers\r
) external returns (bytes32);\r
\r
function records(bytes32 recordId) external view returns (bool);\r
}\r
"
},
"contracts/libraries/BridgeTransferLib.sol": {
"content": "// SPDX-License-Identifier: GPL-3.0-only\r
\r
pragma solidity >=0.8.0;\r
\r
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";\r
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";\r
\r
import "./PbBridge.sol";\r
import "./PbPegged.sol";\r
import "./PbPool.sol";\r
import "../interfaces/IBridge.sol";\r
import "../interfaces/IOriginalTokenVault.sol";\r
import "../interfaces/IOriginalTokenVaultV2.sol";\r
import "../interfaces/IPeggedTokenBridge.sol";\r
import "../interfaces/IPeggedTokenBridgeV2.sol";\r
\r
interface INativeWrap {\r
function nativeWrap() external view returns (address);\r
}\r
\r
library BridgeTransferLib {\r
using SafeERC20 for IERC20;\r
\r
enum BridgeSendType {\r
Null,\r
Liquidity,\r
PegDeposit,\r
PegBurn,\r
PegV2Deposit,\r
PegV2Burn,\r
PegV2BurnFrom\r
}\r
\r
enum BridgeReceiveType {\r
Null,\r
LqRelay,\r
LqWithdraw,\r
PegMint,\r
PegWithdraw,\r
PegV2Mint,\r
PegV2Withdraw\r
}\r
\r
struct ReceiveInfo {\r
bytes32 transferId;\r
address receiver;\r
address token; // 0 address for native token\r
uint256 amount;\r
bytes32 refid; // reference id, e.g., srcTransferId for refund\r
}\r
\r
// ============== Internal library functions called by apps ==============\r
\r
/**\r
* @notice Send a cross-chain transfer of ERC20 token either via liquidity pool-based bridge or in the form of pegged mint / burn.\r
* @param _receiver The address of the receiver.\r
* @param _token The address of the token.\r
* @param _amount The amount of the transfer.\r
* @param _dstChainId The destination chain ID.\r
* @param _nonce A number input to guarantee uniqueness of transferId. Can be timestamp in practice.\r
* @param _maxSlippage The max slippage accepted, given as percentage in point (pip). Eg. 5000 means 0.5%.\r
* Must be greater than minimalMaxSlippage. Receiver is guaranteed to receive at least\r
* (100% - max slippage percentage) * amount or the transfer can be refunded.\r
* Only applicable to the {BridgeSendType.Liquidity}.\r
* @param _bridgeSendType The type of the bridge used by this transfer. One of the {BridgeSendType} enum.\r
* @param _bridgeAddr The address of the bridge used.\r
*/\r
function sendTransfer(\r
address _receiver,\r
address _token,\r
uint256 _amount,\r
uint64 _dstChainId,\r
uint64 _nonce,\r
uint32 _maxSlippage, // slippage * 1M, eg. 0.5% -> 5000\r
BridgeSendType _bridgeSendType,\r
address _bridgeAddr\r
) internal returns (bytes32) {\r
bytes32 transferId;\r
IERC20(_token).safeIncreaseAllowance(_bridgeAddr, _amount);\r
if (_bridgeSendType == BridgeSendType.Liquidity) {\r
IBridge(_bridgeAddr).send(_receiver, _token, _amount, _dstChainId, _nonce, _maxSlippage);\r
transferId = keccak256(\r
abi.encodePacked(address(this), _receiver, _token, _amount, _dstChainId, _nonce, uint64(block.chainid))\r
);\r
} else if (_bridgeSendType == BridgeSendType.PegDeposit) {\r
IOriginalTokenVault(_bridgeAddr).deposit(_token, _amount, _dstChainId, _receiver, _nonce);\r
transferId = keccak256(\r
abi.encodePacked(address(this), _token, _amount, _dstChainId, _receiver, _nonce, uint64(block.chainid))\r
);\r
} else if (_bridgeSendType == BridgeSendType.PegBurn) {\r
IPeggedTokenBridge(_bridgeAddr).burn(_token, _amount, _receiver, _nonce);\r
transferId = keccak256(\r
abi.encodePacked(address(this), _token, _amount, _receiver, _nonce, uint64(block.chainid))\r
);\r
// handle cases where certain tokens do not spend allowance for role-based burn\r
IERC20(_token).safeApprove(_bridgeAddr, 0);\r
} else if (_bridgeSendType == BridgeSendType.PegV2Deposit) {\r
transferId = IOriginalTokenVaultV2(_bridgeAddr).deposit(_token, _amount, _dstChainId, _receiver, _nonce);\r
} else if (_bridgeSendType == BridgeSendType.PegV2Burn) {\r
transferId = IPeggedTokenBridgeV2(_bridgeAddr).burn(_token, _amount, _dstChainId, _receiver, _nonce);\r
// handle cases where certain tokens do not spend allowance for role-based burn\r
IERC20(_token).safeApprove(_bridgeAddr, 0);\r
} else if (_bridgeSendType == BridgeSendType.PegV2BurnFrom) {\r
transferId = IPeggedTokenBridgeV2(_bridgeAddr).burnFrom(_token, _amount, _dstChainId, _receiver, _nonce);\r
// handle cases where certain tokens do not spend allowance for role-based burn\r
IERC20(_token).safeApprove(_bridgeAddr, 0);\r
} else {\r
revert("bridge send type not supported");\r
}\r
return transferId;\r
}\r
\r
/**\r
* @notice Send a cross-chain transfer of native token either via liquidity pool-based bridge or in the form of pegged mint / burn.\r
* @param _receiver The address of the receiver.\r
* @param _amount The amount of the transfer.\r
* @param _dstChainId The destination chain ID.\r
* @param _nonce A number input to guarantee uniqueness of transferId. Can be timestamp in practice.\r
* @param _maxSlippage The max slippage accepted, given as percentage in point (pip). Eg. 5000 means 0.5%.\r
* Must be greater than minimalMaxSlippage. Receiver is guaranteed to receive at least\r
* (100% - max slippage percentage) * amount or the transfer can be refunded.\r
* Only applicable to the {BridgeSendType.Liquidity}.\r
* @param _bridgeSendType The type of the bridge used by this transfer. One of the {BridgeSendType} enum.\r
* @param _bridgeAddr The address of the bridge used.\r
*/\r
function sendNativeTransfer(\r
address _receiver,\r
uint256 _amount,\r
uint64 _dstChainId,\r
uint64 _nonce,\r
uint32 _maxSlippage, // slippage * 1M, eg. 0.5% -> 5000\r
BridgeSendType _bridgeSendType,\r
address _bridgeAddr\r
) internal returns (bytes32) {\r
require(\r
_bridgeSendType == BridgeSendType.Liquidity ||\r
_bridgeSendType == BridgeSendType.PegDeposit ||\r
_bridgeSendType == BridgeSendType.PegV2Deposit,\r
"Lib: invalid bridge send type"\r
);\r
address _token = INativeWrap(_bridgeAddr).nativeWrap();\r
bytes32 transferId;\r
if (_bridgeSendType == BridgeSendType.Liquidity) {\r
IBridge(_bridgeAddr).sendNative{value: msg.value}(_receiver, _amount, _dstChainId, _nonce, _maxSlippage);\r
transferId = keccak256(\r
abi.encodePacked(address(this), _receiver, _token, _amount, _dstChainId, _nonce, uint64(block.chainid))\r
);\r
} else if (_bridgeSendType == BridgeSendType.PegDeposit) {\r
IOriginalTokenVault(_bridgeAddr).depositNative{value: msg.value}(_amount, _dstChainId, _receiver, _nonce);\r
transferId = keccak256(\r
abi.encodePacked(address(this), _token, _amount, _dstChainId, _receiver, _nonce, uint64(block.chainid))\r
);\r
} else {\r
// _bridgeSendType == BridgeSendType.PegV2Deposit\r
transferId = IOriginalTokenVaultV2(_bridgeAddr).depositNative{value: msg.value}(\r
_amount,\r
_dstChainId,\r
_receiver,\r
_nonce\r
);\r
}\r
return transferId;\r
}\r
\r
/**\r
* @notice Receive a cross-chain transfer.\r
* @param _request The serialized request protobuf.\r
* @param _sigs The list of signatures sorted by signing addresses in ascending order.\r
* @param _signers The sorted list of signers.\r
* @param _powers The signing powers of the signers.\r
* @param _bridgeReceiveType The type of the received transfer. One of the {BridgeReceiveType} enum.\r
* @param _bridgeAddr The address of the bridge used.\r
*/\r
function receiveTransfer(\r
bytes calldata _request,\r
bytes[] calldata _sigs,\r
address[] calldata _signers,\r
uint256[] calldata _powers,\r
BridgeReceiveType _bridgeReceiveType,\r
address _bridgeAddr\r
) internal returns (ReceiveInfo memory) {\r
if (_bridgeReceiveType == BridgeReceiveType.LqRelay) {\r
return receiveLiquidityRelay(_request, _sigs, _signers, _powers, _bridgeAddr);\r
} else if (_bridgeReceiveType == BridgeReceiveType.LqWithdraw) {\r
return receiveLiquidityWithdraw(_request, _sigs, _signers, _powers, _bridgeAddr);\r
} else if (_bridgeReceiveType == BridgeReceiveType.PegWithdraw) {\r
return receivePegWithdraw(_request, _sigs, _signers, _powers, _bridgeAddr);\r
} else if (_bridgeReceiveType == BridgeReceiveType.PegMint) {\r
return receivePegMint(_request, _sigs, _signers, _powers, _bridgeAddr);\r
} else if (_bridgeReceiveType == BridgeReceiveType.PegV2Withdraw) {\r
return receivePegV2Withdraw(_request, _sigs, _signers, _powers, _bridgeAddr);\r
} else if (_bridgeReceiveType == BridgeReceiveType.PegV2Mint) {\r
return receivePegV2Mint(_request, _sigs, _signers, _powers, _bridgeAddr);\r
} else {\r
revert("bridge receive type not supported");\r
}\r
}\r
\r
/**\r
* @notice Receive a liquidity bridge relay.\r
* @param _request The serialized request protobuf.\r
* @param _sigs The list of signatures sorted by signing addresses in ascending order.\r
* @param _signers The sorted list of signers.\r
* @param _powers The signing powers of the signers.\r
* @param _bridgeAddr The address of liquidity bridge.\r
*/\r
function receiveLiquidityRelay(\r
bytes calldata _request,\r
bytes[] calldata _sigs,\r
address[] calldata _signers,\r
uint256[] calldata _powers,\r
address _bridgeAddr\r
) internal returns (ReceiveInfo memory) {\r
ReceiveInfo memory recv;\r
PbBridge.Relay memory request = PbBridge.decRelay(_request);\r
recv.transferId = keccak256(\r
abi.encodePacked(\r
request.sender,\r
request.receiver,\r
request.token,\r
request.amount,\r
request.srcChainId,\r
uint64(block.chainid),\r
request.srcTransferId\r
)\r
);\r
recv.refid = request.srcTransferId;\r
recv.receiver = request.receiver;\r
recv.token = request.token;\r
recv.amount = request.amount;\r
if (!IBridge(_bridgeAddr).transfers(recv.transferId)) {\r
IBridge(_bridgeAddr).relay(_request, _sigs, _signers, _powers);\r
}\r
return recv;\r
}\r
\r
/**\r
* @notice Receive a liquidity bridge withdrawal.\r
* @param _request The serialized request protobuf.\r
* @param _sigs The list of signatures sorted by signing addresses in ascending order.\r
* @param _signers The sorted list of signers.\r
* @param _powers The signing powers of the signers.\r
* @param _bridgeAddr The address of liquidity bridge.\r
*/\r
function receiveLiquidityWithdraw(\r
bytes calldata _request,\r
bytes[] calldata _sigs,\r
address[] calldata _signers,\r
uint256[] calldata _powers,\r
address _bridgeAddr\r
) internal returns (ReceiveInfo memory) {\r
ReceiveInfo memory recv;\r
PbPool.WithdrawMsg memory request = PbPool.decWithdrawMsg(_request);\r
recv.transferId = keccak256(\r
abi.encodePacked(request.chainid, request.seqnum, request.receiver, request.token, request.amount)\r
);\r
recv.refid = request.refid;\r
recv.receiver = request.receiver;\r
if (INativeWrap(_bridgeAddr).nativeWrap() == request.token) {\r
recv.token = address(0);\r
} else {\r
recv.token = request.token;\r
}\r
recv.amount = request.amount;\r
if (!IBridge(_bridgeAddr).withdraws(recv.transferId)) {\r
IBridge(_bridgeAddr).withdraw(_request, _sigs, _signers, _powers);\r
}\r
return recv;\r
}\r
\r
/**\r
* @notice Receive an OriginalTokenVault withdrawal.\r
* @param _request The serialized request protobuf.\r
* @param _sigs The list of signatures sorted by signing addresses in ascending order.\r
* @param _signers The sorted list of signers.\r
* @param _powers The signing powers of the signers.\r
* @param _bridgeAddr The address of OriginalTokenVault.\r
*/\r
function receivePegWithdraw(\r
bytes calldata _request,\r
bytes[] calldata _sigs,\r
address[] calldata _signers,\r
uint256[] calldata _powers,\r
address _bridgeAddr\r
) internal returns (ReceiveInfo memory) {\r
ReceiveInfo memory recv;\r
PbPegged.Withdraw memory request = PbPegged.decWithdraw(_request);\r
recv.transferId = keccak256(\r
abi.encodePacked(\r
request.receiver,\r
request.token,\r
request.amount,\r
request.burnAccount,\r
request.refChainId,\r
request.refId\r
)\r
);\r
recv.refid = request.refId;\r
recv.receiver = request.receiver;\r
if (INativeWrap(_bridgeAddr).nativeWrap() == request.token) {\r
recv.token = address(0);\r
} else {\r
recv.token = request.token;\r
}\r
recv.amount = request.amount;\r
if (!IOriginalTokenVault(_bridgeAddr).records(recv.transferId)) {\r
IOriginalTokenVault(_bridgeAddr).withdraw(_request, _sigs, _signers, _powers);\r
}\r
return recv;\r
}\r
\r
/**\r
* @notice Receive a PeggedTokenBridge mint.\r
* @param _request The serialized request protobuf.\r
* @param _sigs The list of signatures sorted by signing addresses in ascending order.\r
* @param _signers The sorted list of signers.\r
* @param _powers The signing powers of the signers.\r
* @param _bridgeAddr The address of PeggedTokenBridge.\r
*/\r
function receivePegMint(\r
bytes calldata _request,\r
bytes[] calldata _sigs,\r
address[] calldata _signers,\r
uint256[] calldata _powers,\r
address _bridgeAddr\r
) internal returns (ReceiveInfo memory) {\r
ReceiveInfo memory recv;\r
PbPegged.Mint memory request = PbPegged.decMint(_request);\r
recv.transferId = keccak256(\r
abi.encodePacked(\r
request.account,\r
request.token,\r
request.amount,\r
request.depositor,\r
request.refChainId,\r
request.refId\r
)\r
);\r
recv.refid = request.refId;\r
recv.receiver = request.account;\r
recv.token = request.token;\r
recv.amount = request.amount;\r
if (!IPeggedTokenBridge(_bridgeAddr).records(recv.transferId)) {\r
IPeggedTokenBridge(_bridgeAddr).mint(_request, _sigs, _signers, _powers);\r
}\r
return recv;\r
}\r
\r
/**\r
* @notice Receive an OriginalTokenVaultV2 withdrawal.\r
* @param _request The serialized request protobuf.\r
* @param _sigs The list of signatures sorted by signing addresses in ascending order. A request must be signed-off by\r
* +2/3 of the bridge's current signing power to be delivered.\r
* @param _signers The sorted list of signers.\r
* @param _powers The signing powers of the signers.\r
* @param _bridgeAddr The address of OriginalTokenVaultV2.\r
*/\r
function receivePegV2Withdraw(\r
bytes calldata _request,\r
bytes[] calldata _sigs,\r
address[] calldata _signers,\r
uint256[] calldata _powers,\r
address _bridgeAddr\r
) internal returns (ReceiveInfo memory) {\r
ReceiveInfo memory recv;\r
PbPegged.Withdraw memory request = PbPegged.decWithdraw(_request);\r
if (IOriginalTokenVaultV2(_bridgeAddr).records(request.refId)) {\r
recv.transferId = keccak256(\r
abi.encodePacked(\r
request.receiver,\r
request.token,\r
request.amount,\r
request.burnAccount,\r
request.refChainId,\r
request.refId,\r
_bridgeAddr\r
)\r
);\r
} else {\r
recv.transferId = IOriginalTokenVaultV2(_bridgeAddr).withdraw(_request, _sigs, _signers, _powers);\r
}\r
recv.refid = request.refId;\r
recv.receiver = request.receiver;\r
if (INativeWrap(_bridgeAddr).nativeWrap() == request.token) {\r
recv.token = address(0);\r
} else {\r
recv.token = request.token;\r
}\r
recv.amount = request.amount;\r
return recv;\r
}\r
\r
/**\r
* @notice Receive a PeggedTokenBridgeV2 mint.\r
* @param _request The serialized request protobuf.\r
* @param _sigs The list of signatures sorted by signing addresses in ascending order. A request must be signed-off by\r
* +2/3 of the bridge's current signing power to be delivered.\r
* @param _signers The sorted list of signers.\r
* @param _powers The signing powers of the signers.\r
* @param _bridgeAddr The address of PeggedTokenBridgeV2.\r
*/\r
function receivePegV2Mint(\r
bytes calldata _request,\r
bytes[] calldata _sigs,\r
address[] calldata _signers,\r
uint256[] calldata _powers,\r
address _bridgeAddr\r
) internal returns (ReceiveInfo memory) {\r
ReceiveInfo memory recv;\r
PbPegged.Mint memory request = PbPegged.decMint(_request);\r
if (IPeggedTokenBridgeV2(_bridgeAddr).records(request.refId)) {\r
recv.transferId = keccak256(\r
abi.encodePacked(\r
request.account,\r
request.token,\r
request.amount,\r
request.depositor,\r
request.refChainId,\r
request.refId,\r
_bridgeAddr\r
)\r
);\r
} else {\r
recv.transferId = IPeggedTokenBridgeV2(_bridgeAddr).mint(_request, _sigs, _signers, _powers);\r
}\r
recv.refid = request.refId;\r
recv.receiver = request.account;\r
recv.token = request.token;\r
recv.amount = request.amount;\r
return recv;\r
}\r
\r
function bridgeRefundType(BridgeSendType _bridgeSendType) internal pure returns (BridgeReceiveType) {\r
if (_bridgeSendType == BridgeSendType.Liquidity) {\r
return BridgeReceiveType.LqWithdraw;\r
}\r
if (_bridgeSendType == BridgeSendType.PegDeposit) {\r
return BridgeReceiveType.PegWithdraw;\r
}\r
if (_bridgeSendType == BridgeSendType.PegBurn) {\r
return BridgeReceiveType.PegMint;\r
}\r
if (_bridgeSendType == BridgeSendType.PegV2Deposit) {\r
return BridgeReceiveType.PegV2Withdraw;\r
}\r
if (_bridgeSendType == BridgeSendType.PegV2Burn || _bridgeSendType == BridgeSendType.PegV2BurnFrom) {\r
return BridgeReceiveType.PegV2Mint;\r
}\r
return BridgeReceiveType.Null;\r
}\r
}\r
"
},
"contracts/libraries/Pb.sol": {
"content": "// SPDX-License-Identifier: GPL-3.0-only\r
\r
pragma solidity 0.8.17;\r
\r
// runtime proto sol library\r
library Pb {\r
enum WireType {\r
Varint,\r
Fixed64,\r
LengthDelim,\r
StartGroup,\r
EndGroup,\r
Fixed32\r
}\r
\r
struct Buffer {\r
uint256 idx; // the start index of next read. when idx=b.length, we're done\r
bytes b; // hold serialized proto msg, readonly\r
}\r
\r
// create a new in-memory Buffer object from raw msg bytes\r
function fromBytes(bytes memory raw) internal pure returns (Buffer memory buf) {\r
buf.b = raw;\r
buf.idx = 0;\r
}\r
\r
// whether there are unread bytes\r
function hasMore(Buffer memory buf) internal pure returns (bool) {\r
return buf.idx < buf.b.length;\r
}\r
\r
// decode current field number and wiretype\r
function decKey(Buffer memory buf) internal pure returns (uint256 tag, WireType wiretype) {\r
uint256 v = decVarint(buf);\r
tag = v / 8;\r
wiretype = WireType(v & 7);\r
}\r
\r
// count tag occurrences, return an array due to no memory map support\r
// have to create array for (maxtag+1) size. cnts[tag] = occurrences\r
// should keep buf.idx unchanged because this is only a count function\r
function cntTags(Buffer memory buf, uint256 maxtag) internal pure returns (uint256[] memory cnts) {\r
uint256 originalIdx = buf.idx;\r
cnts = new uint256[](maxtag + 1); // protobuf's tags are from 1 rather than 0\r
uint256 tag;\r
WireType wire;\r
while (hasMore(buf)) {\r
(tag, wire) = decKey(buf);\r
cnts[tag] += 1;\r
skipValue(buf, wire);\r
}\r
buf.idx = originalIdx;\r
}\r
\r
// read varint from current buf idx, move buf.idx to next read, return the int value\r
function decVarint(Buffer memory buf) internal pure returns (uint256 v) {\r
bytes10 tmp; // proto int is at most 10 bytes (7 bits can be used per byte)\r
bytes memory bb = buf.b; // get buf.b mem addr to use in assembly\r
v = buf.idx; // use v to save one additional uint variable\r
assembly {\r
tmp := mload(add(add(bb, 32), v)) // load 10 bytes from buf.b[buf.idx] to tmp\r
}\r
uint256 b; // store current byte content\r
v = 0; // reset to 0 for return value\r
for (uint256 i = 0; i < 10; i++) {\r
assembly {\r
b := byte(i, tmp) // don't use tmp[i] because it does bound check and costs extra\r
}\r
v |= (b & 0x7F) << (i * 7);\r
if (b & 0x80 == 0) {\r
buf.idx += i + 1;\r
return v;\r
}\r
}\r
revert(); // i=10, invalid varint stream\r
}\r
\r
// read length delimited field and return bytes\r
function decBytes(Buffer memory buf) internal pure returns (bytes memory b) {\r
uint256 len = decVarint(buf);\r
uint256 end = buf.idx + len;\r
require(end <= buf.b.length); // avoid overflow\r
b = new bytes(len);\r
bytes memory bufB = buf.b; // get buf.b mem addr to use in assembly\r
uint256 bStart;\r
uint256 bufBStart = buf.idx;\r
assembly {\r
bStart := add(b, 32)\r
bufBStart := add(add(bufB, 32), bufBStart)\r
}\r
for (uint256 i = 0; i < len; i += 32) {\r
assembly {\r
mstore(add(bStart, i), mload(add(bufBStart, i)))\r
}\r
}\r
buf.idx = end;\r
}\r
\r
// return packed ints\r
function decPacked(Buffer memory buf) internal pure returns (uint256[] memory t) {\r
uint256 len = decVarint(buf);\r
uint256 end = buf.idx + len;\r
require(end <= buf.b.length); // avoid overflow\r
// array in memory must be init w/ known length\r
// so we have to create a tmp array w/ max possible len first\r
uint256[] memory tmp = new uint256[](len);\r
uint256 i = 0; // count how many ints are there\r
while (buf.idx < end) {\r
tmp[i] = decVarint(buf);\r
i++;\r
}\r
t = new uint256[](i); // init t with correct length\r
for (uint256 j = 0; j < i; j++) {\r
t[j] = tmp[j];\r
}\r
return t;\r
}\r
\r
// move idx pass current value field, to beginning of next tag or msg end\r
function skipValue(Buffer memory buf, WireType wire) internal pure {\r
if (wire == WireType.Varint) {\r
decVarint(buf);\r
} else if (wire == WireType.LengthDelim) {\r
uint256 len = decVarint(buf);\r
buf.idx += len; // skip len bytes value data\r
require(buf.idx <= buf.b.length); // avoid overflow\r
} else {\r
revert();\r
} // unsupported wiretype\r
}\r
\r
// type conversion help utils\r
function _bool(uint256 x) internal pure returns (bool v) {\r
return x != 0;\r
}\r
\r
function _uint256(bytes memory b) internal pure returns (uint256 v) {\r
require(b.length <= 32); // b's length must be smaller than or equal to 32\r
assembly {\r
v := mload(add(b, 32))\r
} // load all 32bytes to v\r
v = v >> (8 * (32 - b.length)); // only first b.length is valid\r
}\r
\r
function _address(bytes memory b) internal pure returns (address v) {\r
v = _addressPayable(b);\r
}\r
\r
function _addressPayable(bytes memory b) internal pure returns (address payable v) {\r
require(b.length == 20);\r
//load 32bytes then shift right 12 bytes\r
assembly {\r
v := div(mload(add(b, 32)), 0x1000000000000000000000000)\r
}\r
}\r
\r
function _bytes32(bytes memory b) internal pure returns (bytes32 v) {\r
require(b.length == 32);\r
assembly {\r
v := mload(add(b, 32))\r
}\r
}\r
\r
// uint[] to uint8[]\r
function uint8s(uint256[] memory arr) internal pure returns (uint8[] memory t) {\r
t = new uint8[](arr.length);\r
for (uint256 i = 0; i < t.length; i++) {\r
t[i] = uint8(arr[i]);\r
}\r
}\r
\r
function uint32s(uint256[] memory arr) internal pure returns (uint32[] memory t) {\r
t = new uint32[](arr.length);\r
for (uint256 i = 0; i < t.length; i++) {\r
t[i] = uint32(arr[i]);\r
}\r
}\r
\r
function uint64s(uint256[] memory arr) internal pure returns (uint64[] memory t) {\r
t = new uint64[](arr.length);\r
for (uint256 i = 0; i < t.length; i++) {\r
t[i] = uint64(arr[i]);\r
}\r
}\r
\r
function bools(uint256[] memory arr) internal pure returns (bool[] memory t) {\r
t = new bool[](arr.length);\r
for (uint256 i = 0; i < t.length; i++) {\r
t[i] = arr[i] != 0;\r
}\r
}\r
}\r
"
},
"contracts/libraries/PbBridge.sol": {
"content": "// SPDX-License-Identifier: GPL-3.0-only\r
\r
// Code generated by protoc-gen-sol. DO NOT EDIT.\r
// source: bridge.proto\r
pragma solidity 0.8.17;\r
import "./Pb.sol";\r
\r
library PbBridge {\r
using Pb for Pb.Buffer; // so we can call Pb funcs on Buffer obj\r
\r
struct Relay {\r
address sender; // tag: 1\r
address receiver; // tag: 2\r
address token; // tag: 3\r
uint256 amount; // tag: 4\r
uint64 srcChainId; // tag: 5\r
uint64 dstChainId; // tag: 6\r
bytes32 srcTransferId; // tag: 7\r
} // end struct Relay\r
\r
function decRelay(bytes memory raw) internal pure returns (Relay memory m) {\r
Pb.Buffer memory buf = Pb.fromBytes(raw);\r
\r
uint256 tag;\r
Pb.WireType wire;\r
while (buf.hasMore()) {\r
(tag, wire) = buf.decKey();\r
if (false) {}\r
// solidity has no switch/case\r
else if (tag == 1) {\r
m.sender = Pb._address(buf.decBytes());\r
} else if (tag == 2) {\r
m.receiver = Pb._address(buf.decBytes());\r
} else if (tag == 3) {\r
m.token = Pb._address(buf.decBytes());\r
} else if (tag == 4) {\r
m.amount = Pb._uint256(buf.decBytes());\r
} else if (tag == 5) {\r
m.srcChainId = uint64(buf.decVarint());\r
} else if (tag == 6) {\r
m.dstChainId = uint64(buf.decVarint());\r
} else if (tag == 7) {\r
m.srcTransferId = Pb._bytes32(buf.decBytes());\r
} else {\r
buf.skipValue(wire);\r
} // skip value of unknown tag\r
}\r
} // end decoder Relay\r
}\r
"
},
"contracts/libraries/PbPegged.sol": {
"content": "// SPDX-License-Identifier: GPL-3.0-only\r
\r
// Code generated by protoc-gen-sol. DO NOT EDIT.\r
// source: contracts/libraries/proto/pegged.proto\r
pragma solidity 0.8.17;\r
import "./Pb.sol";\r
\r
library PbPegged {\r
using Pb for Pb.Buffer; // so we can call Pb funcs on Buffer obj\r
\r
struct Mint {\r
address token; // tag: 1\r
address account; // tag: 2\r
uint256 amount; // tag: 3\r
address depositor; // tag: 4\r
uint64 refChainId; // tag: 5\r
bytes32 refId; // tag: 6\r
} // end struct Mint\r
\r
function decMint(bytes memory raw) internal pure returns (Mint memory m) {\r
Pb.Buffer memory buf = Pb.fromBytes(raw);\r
\r
uint256 tag;\r
Pb.WireType wire;\r
while (buf.hasMore()) {\r
(tag, wire) = buf.decKey();\r
if (false) {}\r
// solidity has no switch/case\r
else if (tag == 1) {\r
m.token = Pb._address(buf.decBytes());\r
} else if (tag == 2) {\r
m.account = Pb._address(buf.decBytes());\r
} else if (tag == 3) {\r
m.amount = Pb._uint256(buf.decBytes());\r
} else if (tag == 4) {\r
m.depositor = Pb._address(buf.decBytes());\r
} else if (tag == 5) {\r
m.refChainId = uint64(buf.decVarint());\r
} else if (tag == 6) {\r
m.refId = Pb._bytes32(buf.decBytes());\r
} else {\r
buf.skipValue(wire);\r
} // skip value of unknown tag\r
}\r
} // end decoder Mint\r
\r
struct Withdraw {\r
address token; // tag: 1\r
address receiver; // tag: 2\r
uint256 amount; // tag: 3\r
address burnAccount; // tag: 4\r
uint64 refChainId; // tag: 5\r
bytes32 refId; // tag: 6\r
} // end struct Withdraw\r
\r
function decWithdraw(bytes memory raw) internal pure returns (Withdraw memory m) {\r
Pb.Buffer memory buf = Pb.fromBytes(raw);\r
\r
uint256 tag;\r
Pb.WireType wire;\r
while (buf.hasMore()) {\r
(tag, wire) = buf.decKey();\r
if (false) {}\r
// solidity has no switch/case\r
else if (tag == 1) {\r
m.token = Pb._address(buf.decBytes());\r
} else if (tag == 2) {\r
m.receiver = Pb._address(buf.decBytes());\r
} else if (tag == 3) {\r
m.amount = Pb._uint256(buf.decBytes());\r
} else if (tag == 4) {\r
m.burnAccount = Pb._address(buf.decBytes());\r
} else if (tag == 5) {\r
m.refChainId = uint64(buf.decVarint());\r
} else if (tag == 6) {\r
m.refId = Pb._bytes32(buf.decBytes());\r
} else {\r
buf.skipValue(wire);\r
} // skip value of unknown tag\r
}\r
} // end decoder Withdraw\r
}\r
"
},
"contracts/libraries/PbPool.sol": {
"content": "// SPDX-License-Identifier: GPL-3.0-only\r
\r
// Code generated by protoc-gen-sol. DO NOT EDIT.\r
// source: contracts/libraries/proto/pool.proto\r
pragma solidity 0.8.17;\r
import "./Pb.sol";\r
\r
library PbPool {\r
using Pb for Pb.Buffer; // so we can call Pb funcs on Buffer obj\r
\r
struct WithdrawMsg {\r
uint64 chainid; // tag: 1\r
uint64 seqnum; // tag: 2\r
address receiver; // tag: 3\r
address token; // tag: 4\r
uint256 amount; // tag: 5\r
bytes32 refid; // tag: 6\r
} // end struct WithdrawMsg\r
\r
function decWithdrawMsg(bytes memory raw) internal pure returns (WithdrawMsg memory m) {\r
Pb.Buffer memory buf = Pb.fromBytes(raw);\r
\r
uint256 tag;\r
Pb.WireType wire;\r
while (buf.hasMore()) {\r
(tag, wire) = buf.decKey();\r
if (false) {}\r
// solidity has no switch/case\r
else if (tag == 1) {\r
m.chainid = uint64(buf.decVarint());\r
} else if (tag == 2) {\r
m.seqnum = uint64(buf.decVarint());\r
} else if (tag == 3) {\r
m.receiver = Pb._address(buf.decBytes());\r
} else if (tag == 4) {\r
m.token = Pb._address(buf.decBytes());\r
} else if (tag == 5) {\r
m.amount = Pb._uint256(buf.decBytes());\r
} else if (tag == 6) {\r
m.refid = Pb._bytes32(buf.decBytes());\r
} else {\r
buf.skipValue(wire);\r
} // skip value of unknown tag\r
}\r
} // end decoder WithdrawMsg\r
}\r
"
},
"contracts/proxy/TransferAgent.sol": {
"content": "// SPDX-License-Identifier: GPL-3.0-only\r
\r
pragma solidity 0.8.17;\r
\r
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";\r
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";\r
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";\r
\r
import "../libraries/BridgeTransferLib.sol";\r
import "../safeguard/Ownable.sol";\r
\r
/**\r
* @title Transfer agent. Designed to support arbitrary length receiver address for transfer. Supports the liquidity pool-based {Bridge}, the {OriginalTokenVault} for pegged\r
* deposit and the {PeggedTokenBridge} for pegged burn.\r
*/\r
contract TransferAgent is ReentrancyGuard, Ownable {\r
using SafeERC20 for IERC20;\r
\r
struct Extension {\r
uint8 Type;\r
bytes Value;\r
}\r
\r
mapping(BridgeTransferLib.BridgeSendType => address) public bridges;\r
\r
event Supplement(\r
BridgeTransferLib.BridgeSendType bridgeSendType,\r
bytes32 transferId,\r
address sender,\r
bytes receiver,\r
Extension[] extensions\r
);\r
event BridgeUpdated(BridgeTransferLib.BridgeSendType bridgeSendType, address bridgeAddr);\r
\r
/**\r
* @notice Send a cross-chain transfer of ERC20 token either via liquidity pool-based bridge or in form of mint/burn.\r
* @param _receiver The address of the receiver.\r
* @param _token The address of the token.\r
* @param _amount The amount of the transfer.\r
* @param _dstChainId The destination chain ID.\r
* @param _nonce A number input to guarantee uniqueness of transferId. Can be timestamp in practice.\r
* @param _maxSlippage The max slippage accepted, given as percentage in point (pip). Eg. 5000 means 0.5%.\r
* Must be greater than minimalMaxSlippage. Receiver is guaranteed to receive at least\r
* (100% - max slippage percentage) * amount or the transfer can be refunded.\r
* Only applicable to the {BridgeSendType.Liquidity}.\r
* @param _bridgeSendType The type of bridge used by this transfer. One of the {BridgeSendType} enum.\r
* @param _extensions A list of extension to be processed by agent, is designed to be used for extending\r
* present transfer. Contact Celer team to learn about already supported type of extension.\r
*/\r
function transfer(\r
bytes calldata _receiver,\r
address _token,\r
uint256 _amount,\r
uint64 _dstChainId,\r
uint64 _nonce,\r
uint32 _maxSlippage, // slippage * 1M, eg. 0.5% -> 5000\r
BridgeTransferLib.BridgeSendType _bridgeSendType,\r
Extension[] calldata _extensions\r
) external nonReentrant returns (bytes32) {\r
bytes32 transferId;\r
{\r
address _bridgeAddr = bridges[_bridgeSendType];\r
require(_bridgeAddr != address(0), "unknown bridge type");\r
Submitted on: 2025-11-07 13:30:46
Comments
Log in to comment.
No comments yet.