Skip to content

add swapAndLombardTransfer #27

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
112 changes: 112 additions & 0 deletions EurekaHandler/broadcast/Upgrade.s.sol/1/run-1744726301.json

Large diffs are not rendered by default.

84 changes: 42 additions & 42 deletions EurekaHandler/broadcast/Upgrade.s.sol/1/run-latest.json

Large diffs are not rendered by default.

115 changes: 115 additions & 0 deletions EurekaHandler/broadcast/Upgrade.s.sol/11155111/run-1744724463.json

Large diffs are not rendered by default.

93 changes: 48 additions & 45 deletions EurekaHandler/broadcast/Upgrade.s.sol/11155111/run-latest.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion EurekaHandler/script/Upgrade.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ contract DeploymentScript is Script {

vm.startBroadcast();

EurekaHandler handler = EurekaHandler(0x46914a36365EC16600D81880903f3e95dcea3e5D);
EurekaHandler handler = EurekaHandler(0xFc2d0487A0ae42ae7329a80dc269916A9184cF7C);

EurekaHandler newHandlerImplementation = new EurekaHandler();

Expand Down
55 changes: 53 additions & 2 deletions EurekaHandler/src/EurekaHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ contract EurekaHandler is IEurekaHandler, Initializable, UUPSUpgradeable, Ownabl

amountOut = _swap(swapInputToken, transferParams.token, swapInputAmount, swapCalldata);
}



if (amountOut <= _totalFees(fees)) {
revert("Insufficient amount out to cover fees");
}
Expand All @@ -113,6 +112,58 @@ contract EurekaHandler is IEurekaHandler, Initializable, UUPSUpgradeable, Ownabl
emit EurekaTransfer(transferParams.token, amountOutAfterFees, fees.relayFee, fees.relayFeeRecipient);
}

function swapAndLombardTransfer(
address swapInputToken,
uint256 swapInputAmount,
bytes memory swapCalldata,
uint256 minAmountOut,
TransferParams memory transferParams,
Fees memory fees
) external payable {
require(block.timestamp < fees.quoteExpiry, "Fee quote expired");

uint256 amountOut;
if (swapInputToken == address(0)) {
require(msg.value == swapInputAmount, "Insufficient native token");

amountOut = _swapNative(address(lbtc), swapInputAmount, swapCalldata);
} else {
IERC20(swapInputToken).safeTransferFrom(msg.sender, address(this), swapInputAmount);

amountOut = _swap(swapInputToken, address(lbtc), swapInputAmount, swapCalldata);
}

if (amountOut <= _totalFees(fees)) {
revert("Insufficient amount out to cover fees");
}

// Collect fees
if (fees.relayFee > 0) {
IERC20(lbtc).safeTransfer(fees.relayFeeRecipient, fees.relayFee);
}

uint256 amountOutAfterFees = amountOut - _totalFees(fees);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we deduct the relay fee from the amount out here, when during the lombardTransfer function collect the fee but don't actually subtract it from the input amount. I'm assuming its some difference between lombardTransfer using safeTransferFrom(msg.sender, ...) and the swapAndLombardTransfer using safeTransfer to send the relay fee to the recipient, but not completely sure.


IERC20(lbtc).forceApprove(lbtcVoucher, amountOutAfterFees);

uint256 voucherAmount = IIBCVoucher(lbtcVoucher).wrap(amountOutAfterFees, minAmountOut);


_sendTransfer(
IICS20TransferMsgs.SendTransferMsg({
denom: lbtcVoucher,
amount: voucherAmount,
receiver: transferParams.recipient,
sourceClient: transferParams.sourceClient,
destPort: transferParams.destPort,
timeoutTimestamp: transferParams.timeoutTimestamp,
memo: transferParams.memo
})
);

emit EurekaTransfer(lbtcVoucher, voucherAmount, fees.relayFee, fees.relayFeeRecipient);
}

function lombardTransfer(
uint256 amount,
uint256 minAmountOut,
Expand Down
9 changes: 9 additions & 0 deletions EurekaHandler/src/interfaces/IEurekaHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ interface IEurekaHandler {
Fees memory fees
) external payable;

function swapAndLombardTransfer(
address swapInputToken,
uint256 swapInputAmount,
bytes memory swapCalldata,
uint256 minAmountOut,
TransferParams memory transferParams,
Fees memory fees
) external payable;

function lombardTransfer(
uint256 amount,
uint256 minAmountOut,
Expand Down
72 changes: 72 additions & 0 deletions EurekaHandler/test/EurekaHandler.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,78 @@ contract EurekaHandlerTest is Test {
handler.lombardTransfer(amountIn, 99000000, transferParams, fees);
}

function testSwapAndLombardTransfer() public {
address weth = 0xfFf9976782d46CC05630D1f6eBAb18b2324d6B14;

uint256 amountIn = 1000000000;

bytes memory swapCalldata = _encodeSwapExactInputCalldata(weth, lbtc, 500, address(handler), amountIn, 0, 0);

IEurekaHandler.TransferParams memory transferParams = IEurekaHandler.TransferParams({
token: lbtcVoucher,
recipient: "cosmos1234",
sourceClient: "client-0",
destPort: "transfer",
timeoutTimestamp: uint64(block.timestamp + 100),
memo: ""
});

IEurekaHandler.Fees memory fees = IEurekaHandler.Fees({
relayFee: 100,
relayFeeRecipient: relayFeeRecipient,
quoteExpiry: uint64(block.timestamp + 100)
});

address alice = makeAddr("alice");

vm.mockCall(
weth,
abi.encodeWithSelector(IERC20.transferFrom.selector, alice, address(handler), amountIn),
abi.encode(true)
);

vm.mockCall(
lbtc,
abi.encodeWithSelector(IERC20.transferFrom.selector, address(handler), relayFeeRecipient, fees.relayFee),
abi.encode(true)
);

{
bytes[] memory lbtcBalanceMockResponses = new bytes[](2);
lbtcBalanceMockResponses[0] = abi.encode(0);
lbtcBalanceMockResponses[1] = abi.encode(100000000);

vm.mockCalls(
lbtc, abi.encodeWithSelector(IERC20.balanceOf.selector, address(handler)), lbtcBalanceMockResponses
);
}

vm.mockCall(
lbtcVoucher, abi.encodeWithSelector(IIBCVoucher.wrap.selector, 99999900, 99000000), abi.encode(99000000)
);

vm.mockCall(
address(ics20Transfer),
abi.encodeWithSelector(
IICS20Transfer.sendTransferWithSender.selector,
IICS20TransferMsgs.SendTransferMsg({
denom: lbtcVoucher,
amount: 99000000,
receiver: transferParams.recipient,
sourceClient: transferParams.sourceClient,
destPort: transferParams.destPort,
timeoutTimestamp: transferParams.timeoutTimestamp,
memo: transferParams.memo
}),
alice
),
abi.encode(1)
);

vm.startPrank(alice);
handler.swapAndLombardTransfer(weth, amountIn, swapCalldata, 99000000, transferParams, fees);
}

function _encodeSwapExactInputCalldata(
address tokenIn,
address tokenOut,
Expand Down
Loading