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

Relay Bridge (v1.0.0) [RelayFacet v1.0.0,LibAsset v1.0.1] #843

Merged
merged 49 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
2e55975
Scaffold Relay Facet
ezynda3 Oct 29, 2024
37e76b0
Add verification modifier
ezynda3 Oct 29, 2024
c970202
Add calldata
ezynda3 Oct 29, 2024
9c85cf6
Implement ERC20
ezynda3 Oct 29, 2024
119b8b5
Implement tests
ezynda3 Oct 29, 2024
d669597
Deploy to staging
ezynda3 Oct 30, 2024
7622a00
Add deployment requirements
ezynda3 Oct 30, 2024
7d14117
Implement demo for Relay
ezynda3 Oct 31, 2024
8695c5e
Update documentation
ezynda3 Oct 31, 2024
0a0fa7e
Update doc link
ezynda3 Oct 31, 2024
e0d7f1c
Deploy to POL and OP staging
ezynda3 Oct 31, 2024
458674f
Remove unneeded comment
ezynda3 Oct 31, 2024
eb6d35f
Fixes
ezynda3 Oct 31, 2024
674e253
Fixes
ezynda3 Oct 31, 2024
edc0ddc
Merge branch 'main' into LF10073-relay-bridge
ezynda3 Oct 31, 2024
7049a82
Add swap and bridge TX to demo
ezynda3 Oct 31, 2024
2bae776
Add params needed to handle non EVM addresses
ezynda3 Nov 1, 2024
ecc49ce
Update tests to handle non-evm
ezynda3 Nov 1, 2024
ad145c0
Redeploy to staging
ezynda3 Nov 4, 2024
bc4295f
Update Relay Demo
ezynda3 Nov 4, 2024
a91b204
add revert case
ezynda3 Nov 5, 2024
c09b84d
use standardized method for creating hash
ezynda3 Nov 5, 2024
0da22f7
add comments for clarity
ezynda3 Nov 5, 2024
09c6be5
Merge branch 'main' into LF10073-relay-bridge
ezynda3 Nov 5, 2024
1f10049
add URL for listing chain ids
ezynda3 Nov 5, 2024
e104a62
fix typo
ezynda3 Nov 5, 2024
634a261
add explicit test for non-evm chains
ezynda3 Nov 7, 2024
4c1100f
add comments fix typo
ezynda3 Nov 7, 2024
250469b
add NON_EVM_ADDRESS constant to LibAsset
ezynda3 Nov 7, 2024
03f1711
update name
ezynda3 Nov 7, 2024
26c1ee7
fix typo
ezynda3 Nov 7, 2024
4e9b7d4
remove unused imports
ezynda3 Nov 7, 2024
54463f1
Update and redeploy to staging
ezynda3 Nov 7, 2024
d4bee97
Use floating pragma
ezynda3 Nov 11, 2024
0d4a6d8
Merge branch 'main' into LF10073-relay-bridge
ezynda3 Nov 11, 2024
aa4d0e6
Update version
ezynda3 Nov 11, 2024
5c18202
Add more tests
ezynda3 Nov 11, 2024
7f3f8d8
Add even more tests
ezynda3 Nov 11, 2024
c8de86d
Use mock to surgically make call fail
ezynda3 Nov 11, 2024
291d0a7
Add explicit test for _getMappedChainId()
ezynda3 Nov 12, 2024
2efd99e
update event
ezynda3 Nov 26, 2024
4b3535f
prevent replay
ezynda3 Nov 26, 2024
f285130
prevent empty non-evm addresses
ezynda3 Nov 26, 2024
a1fcd9a
remove unused import
ezynda3 Nov 26, 2024
fb3c383
Update audit log
ezynda3 Dec 12, 2024
e35fbd8
Merge branch 'main' into LF10073-relay-bridge
ezynda3 Dec 12, 2024
254de71
Update audit log
ezynda3 Dec 12, 2024
816030a
Test constructor
ezynda3 Dec 12, 2024
7968629
sort audited contracts
ezynda3 Dec 13, 2024
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
16 changes: 16 additions & 0 deletions config/relay.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"mainnet": {
"example": "0x0000000000000000000000000000000000000000",
"exampleAllowedTokens": [
"0x0000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000"
]
},
"arbitrum": {
"example": "0x0000000000000000000000000000000000000000",
"exampleAllowedTokens": [
"0x0000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000"
]
}
}
92 changes: 92 additions & 0 deletions docs/RelayFacet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Relay Facet

## How it works

The Relay Facet works by ...
ezynda3 marked this conversation as resolved.
Show resolved Hide resolved

```mermaid
graph LR;
D{LiFiDiamond}-- DELEGATECALL -->RelayFacet;
RelayFacet -- CALL --> C(Relay)
```

## Public Methods

- `function startBridgeTokensViaRelay(BridgeData calldata _bridgeData, RelayData calldata _relayData)`
- Simply bridges tokens using relay
- `swapAndStartBridgeTokensViarelay(BridgeData memory _bridgeData, LibSwap.SwapData[] calldata _swapData, relayData memory _relayData)`
- Performs swap(s) before bridging tokens using relay
ezynda3 marked this conversation as resolved.
Show resolved Hide resolved

## relay Specific Parameters

The methods listed above take a variable labeled `_relayData`. This data is specific to relay and is represented as the following struct type:

```solidity
/// @param example Example parameter.
struct relayData {
string example;
}
```

## Swap Data

Some methods accept a `SwapData _swapData` parameter.

Swapping is performed by a swap specific library that expects an array of calldata to can be run on various DEXs (i.e. Uniswap) to make one or multiple swaps before performing another action.

The swap library can be found [here](../src/Libraries/LibSwap.sol).

## LiFi Data

Some methods accept a `BridgeData _bridgeData` parameter.

This parameter is strictly for analytics purposes. It's used to emit events that we can later track and index in our subgraphs and provide data on how our contracts are being used. `BridgeData` and the events we can emit can be found [here](../src/Interfaces/ILiFi.sol).

## Getting Sample Calls to interact with the Facet

In the following some sample calls are shown that allow you to retrieve a populated transaction that can be sent to our contract via your wallet.

All examples use our [/quote endpoint](https://apidocs.li.fi/reference/get_quote) to retrieve a quote which contains a `transactionRequest`. This request can directly be sent to your wallet to trigger the transaction.

The quote result looks like the following:

```javascript
const quoteResult = {
id: '0x...', // quote id
type: 'lifi', // the type of the quote (all lifi contract calls have the type "lifi")
tool: 'relay', // the bridge tool used for the transaction
action: {}, // information about what is going to happen
estimate: {}, // information about the estimated outcome of the call
includedSteps: [], // steps that are executed by the contract as part of this transaction, e.g. a swap step and a cross step
transactionRequest: {
// the transaction that can be sent using a wallet
data: '0x...',
to: '0x...',
value: '0x00',
from: '{YOUR_WALLET_ADDRESS}',
chainId: 100,
gasLimit: '0x...',
gasPrice: '0x...',
},
}
```

A detailed explanation on how to use the /quote endpoint and how to trigger the transaction can be found [here](https://docs.li.fi/products/more-integration-options/li.fi-api/transferring-tokens-example).

**Hint**: Don't forget to replace `{YOUR_WALLET_ADDRESS}` with your real wallet address in the examples.

### Cross Only

To get a transaction for a transfer from 30 USDC.e on Avalanche to USDC on Binance you can execute the following request:

```shell
curl 'https://li.quest/v1/quote?fromChain=AVA&fromAmount=30000000&fromToken=USDC&toChain=BSC&toToken=USDC&slippage=0.03&allowBridges=relay&fromAddress={YOUR_WALLET_ADDRESS}'
```

### Swap & Cross

To get a transaction for a transfer from 30 USDT on Avalanche to USDC on Binance you can execute the following request:

```shell
curl 'https://li.quest/v1/quote?fromChain=AVA&fromAmount=30000000&fromToken=USDT&toChain=BSC&toToken=USDC&slippage=0.03&allowBridges=relay&fromAddress={YOUR_WALLET_ADDRESS}'
```
33 changes: 33 additions & 0 deletions script/deploy/facets/DeployRelayFacet.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.17;

import { DeployScriptBase } from "./utils/DeployScriptBase.sol";
import { stdJson } from "forge-std/Script.sol";
import { RelayFacet } from "lifi/Facets/RelayFacet.sol";

contract DeployScript is DeployScriptBase {
using stdJson for string;

constructor() DeployScriptBase("RelayFacet") {}

function run()
public
returns (RelayFacet deployed, bytes memory constructorArgs)
{
constructorArgs = getConstructorArgs();

deployed = RelayFacet(deploy(type(RelayFacet).creationCode));
}
ezynda3 marked this conversation as resolved.
Show resolved Hide resolved

function getConstructorArgs() internal override returns (bytes memory) {
// If you don't have a constructor or it doesn't take any arguments, you can remove this function
string memory path = string.concat(root, "/config/relay.json");
string memory json = vm.readFile(path);

address example = json.readAddress(
string.concat(".", network, ".example")
);

return abi.encode(example);
}
ezynda3 marked this conversation as resolved.
Show resolved Hide resolved
}
24 changes: 24 additions & 0 deletions script/deploy/facets/UpdateRelayFacet.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.17;

import { UpdateScriptBase } from "./utils/UpdateScriptBase.sol";
import { stdJson } from "forge-std/StdJson.sol";
import { DiamondCutFacet, IDiamondCut } from "lifi/Facets/DiamondCutFacet.sol";
ezynda3 marked this conversation as resolved.
Show resolved Hide resolved
import { RelayFacet } from "lifi/Facets/RelayFacet.sol";
ezynda3 marked this conversation as resolved.
Show resolved Hide resolved

contract DeployScript is UpdateScriptBase {
using stdJson for string;

struct Config {
uint256 a;
bool b;
address c;
}
ezynda3 marked this conversation as resolved.
Show resolved Hide resolved

function run()
public
returns (address[] memory facets, bytes memory cutData)
{
return update("RelayFacet");
}
}
141 changes: 141 additions & 0 deletions src/Facets/RelayFacet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
0xDEnYO marked this conversation as resolved.
Show resolved Hide resolved

import { ILiFi } from "../Interfaces/ILiFi.sol";
import { LibDiamond } from "../Libraries/LibDiamond.sol";
import { LibAsset } from "../Libraries/LibAsset.sol";
import { LibSwap } from "../Libraries/LibSwap.sol";
import { ReentrancyGuard } from "../Helpers/ReentrancyGuard.sol";
import { SwapperV2 } from "../Helpers/SwapperV2.sol";
import { Validatable } from "../Helpers/Validatable.sol";
import { ECDSA } from "solady/utils/ECDSA.sol";

/// @title Relay Facet
/// @author LI.FI (https://li.fi)
/// @notice Provides functionality for bridging through Relay Protocol
/// @custom:version 1.0.0
contract RelayFacet is ILiFi, ReentrancyGuard, SwapperV2, Validatable {
/// Storage ///

// Receiver for native transfers
address public immutable relayReceiver;
address public immutable relaySolver;

/// Types ///

/// @dev Optional bridge specific struct
/// @param exampleParam Example parameter
struct RelayData {
ezynda3 marked this conversation as resolved.
Show resolved Hide resolved
bytes32 requestId;
address receivingAssetId;
bytes signature;
}

/// Errors ///

error InvalidQuote();

/// Modifiers ///

modifier onlyValidQuote(
ILiFi.BridgeData memory _bridgeData,
RelayData calldata _relayData
) {
// Verify that the bridging quote has been signed by the Relay solver
// as attested using the attestaion API
// API URL: https://api.relay.link/requests/{requestId}/signature/v2
bytes32 hash = keccak256(
abi.encodePacked(
_relayData.requestId,
block.chainid,
bytes32(uint256(uint160(address(this)))),
bytes32(uint256(uint160(_bridgeData.sendingAssetId))),
_bridgeData.destinationChainId,
bytes32(uint256(uint160(_bridgeData.receiver))),
bytes32(uint256(uint160(_relayData.receivingAssetId)))
)
);
address signer = ECDSA.recover(hash, _relayData.signature);
ezynda3 marked this conversation as resolved.
Show resolved Hide resolved
if (signer != relaySolver) {
revert InvalidQuote();
}
_;
}

/// Constructor ///

constructor(address _relayReceiver, address _relaySolver) {
ezynda3 marked this conversation as resolved.
Show resolved Hide resolved
relayReceiver = _relayReceiver;
relaySolver = _relaySolver;
}
ezynda3 marked this conversation as resolved.
Show resolved Hide resolved

/// External Methods ///

/// @notice Bridges tokens via Relay
/// @param _bridgeData The core information needed for bridging
/// @param _relayData Data specific to Relay
function startBridgeTokensViaRelay(
ILiFi.BridgeData calldata _bridgeData,
RelayData calldata _relayData
)
external
payable
nonReentrant
onlyValidQuote(_bridgeData, _relayData)
refundExcessNative(payable(msg.sender))
validateBridgeData(_bridgeData)
doesNotContainSourceSwaps(_bridgeData)
doesNotContainDestinationCalls(_bridgeData)
{
LibAsset.depositAsset(
_bridgeData.sendingAssetId,
_bridgeData.minAmount
);
_startBridge(_bridgeData, _relayData);
}

/// @notice Performs a swap before bridging via Relay
/// @param _bridgeData The core information needed for bridging
/// @param _swapData An array of swap related data for performing swaps before bridging
/// @param _relayData Data specific to Relay
function swapAndStartBridgeTokensViaRelay(
ILiFi.BridgeData memory _bridgeData,
LibSwap.SwapData[] calldata _swapData,
RelayData calldata _relayData
)
external
payable
nonReentrant
onlyValidQuote(_bridgeData, _relayData)
refundExcessNative(payable(msg.sender))
containsSourceSwaps(_bridgeData)
doesNotContainDestinationCalls(_bridgeData)
validateBridgeData(_bridgeData)
{
_bridgeData.minAmount = _depositAndSwap(
_bridgeData.transactionId,
_bridgeData.minAmount,
_swapData,
payable(msg.sender)
);
_startBridge(_bridgeData, _relayData);
}

/// Internal Methods ///

/// @dev Contains the business logic for the bridge via Relay
/// @param _bridgeData The core information needed for bridging
/// @param _relayData Data specific to Relay
function _startBridge(
ILiFi.BridgeData memory _bridgeData,
RelayData calldata _relayData
) internal {
// check if sendingAsset is native or ERC20
if (LibAsset.isNativeAsset(_bridgeData.sendingAssetId)) {
// Native
} else {
// ERC20
}
ezynda3 marked this conversation as resolved.
Show resolved Hide resolved
emit LiFiTransferStarted(_bridgeData);
}
}
2 changes: 1 addition & 1 deletion templates/facetDeployScript.template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ contract DeployScript is DeployScriptBase {
string memory path = string.concat(root, "/config/{{camelCase name}}.json");
string memory json = vm.readFile(path);

address acrossSpokePool = json.readAddress(
address example = json.readAddress(
string.concat(".", network, ".example")
);

Expand Down
Loading