Skip to content

Commit

Permalink
Update tests to handle non-evm
Browse files Browse the repository at this point in the history
  • Loading branch information
ezynda3 committed Nov 1, 2024
1 parent 2bae776 commit ecc49ce
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 11 deletions.
33 changes: 32 additions & 1 deletion src/Facets/RelayFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ contract RelayFacet is ILiFi, ReentrancyGuard, SwapperV2, Validatable {
bytes signature;
}

/// Events ///

event BridgeToNonEVMChain(
bytes32 indexed transactionId,
uint256 indexed destinationChainId,
bytes32 receiver
);

/// Errors ///

error InvalidQuote();
Expand All @@ -59,7 +67,7 @@ contract RelayFacet is ILiFi, ReentrancyGuard, SwapperV2, Validatable {
block.chainid,
bytes32(uint256(uint160(address(this)))),
bytes32(uint256(uint160(_bridgeData.sendingAssetId))),
_bridgeData.destinationChainId,
_getMappedChainId(_bridgeData.destinationChainId),
_bridgeData.receiver == NON_EVM_ADDRESS
? _relayData.nonEVMReceiver
: bytes32(uint256(uint160(_bridgeData.receiver))),
Expand Down Expand Up @@ -175,6 +183,29 @@ contract RelayFacet is ILiFi, ReentrancyGuard, SwapperV2, Validatable {
revert(LibUtil.getRevertMsg(reason));
}
}

if (_bridgeData.receiver == NON_EVM_ADDRESS) {
emit BridgeToNonEVMChain(
_bridgeData.transactionId,
_bridgeData.destinationChainId,
_relayData.nonEVMReceiver
);
}

emit LiFiTransferStarted(_bridgeData);
}

function _getMappedChainId(
uint256 chainId
) internal pure returns (uint256) {
if (chainId == 20000000000001) {
return 8253038;
}

if (chainId == 1151111081099710) {
return 792703809;
}

return chainId;
}
}
65 changes: 55 additions & 10 deletions test/solidity/Facets/RelayFacet.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import { LibAllowList, TestBaseFacet, console, ERC20 } from "../utils/TestBaseFa
import { RelayFacet } from "lifi/Facets/RelayFacet.sol";
import { ILiFi } from "lifi/Interfaces/ILiFi.sol";

// TODO: Upgrade forge-std lib
// This is a hack to be able to use newer capabilities of forge without having
// to update the forge-std lib as this will break some tests at the moment
interface VmWithUnixTime {
/// Returns the time since unix epoch in milliseconds.
function unixTime() external returns (uint256 milliseconds);
}

// Stub RelayFacet Contract
contract TestRelayFacet is RelayFacet {
constructor(
Expand Down Expand Up @@ -64,15 +72,38 @@ contract RelayFacetTest is TestBaseFacet {
bridgeData.bridge = "relay";
bridgeData.destinationChainId = 137;

validRelayData = RelayFacet.RelayData({
requestId: bytes32("1234"),
nonEVMReceiver: "",
receivingAssetId: bytes32(
uint256(uint160(0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174))
), // Polygon USDC
callData: "",
signature: ""
});
// This will randomly setup bridging to EVM or non-EVM
if (VmWithUnixTime(address(vm)).unixTime() % 2 == 0) {
validRelayData = RelayFacet.RelayData({
requestId: bytes32("1234"),
nonEVMReceiver: "",
receivingAssetId: bytes32(
uint256(
uint160(0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174)
)
), // Polygon USDC
callData: "",
signature: ""
});
} else {
bridgeData.receiver = NON_EVM_ADDRESS;
bridgeData.destinationChainId = 792703809;
validRelayData = RelayFacet.RelayData({
requestId: bytes32("1234"),
nonEVMReceiver: bytes32(
abi.encodePacked(
"EoW7FWTdPdZKpd3WAhH98c2HMGHsdh5yhzzEtk1u68Bb"
)
), // DEV Wallet
receivingAssetId: bytes32(
abi.encodePacked(
"EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"
)
), // Solana USDC
callData: "",
signature: ""
});
}
}

function initiateBridgeTxWithFacet(bool isNative) internal override {
Expand Down Expand Up @@ -126,7 +157,7 @@ contract RelayFacetTest is TestBaseFacet {
block.chainid,
bytes32(uint256(uint160(address(relayFacet)))),
bytes32(uint256(uint160(_bridgeData.sendingAssetId))),
_bridgeData.destinationChainId,
_getMappedChainId(_bridgeData.destinationChainId),
_bridgeData.receiver == NON_EVM_ADDRESS
? _relayData.nonEVMReceiver
: bytes32(uint256(uint160(_bridgeData.receiver))),
Expand All @@ -140,4 +171,18 @@ contract RelayFacetTest is TestBaseFacet {
bytes memory signature = abi.encodePacked(r, s, v);
return signature;
}

function _getMappedChainId(
uint256 chainId
) internal pure returns (uint256) {
if (chainId == 20000000000001) {
return 8253038;
}

if (chainId == 1151111081099710) {
return 792703809;
}

return chainId;
}
}

0 comments on commit ecc49ce

Please sign in to comment.