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

chore: issue 69 fix - stargateComposer swapEthAndCall #174

Merged
merged 5 commits into from
Apr 27, 2024
Merged
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
63 changes: 39 additions & 24 deletions contracts/Balancer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ contract Balancer is Ownable {
*/
mapping(address => mapping(uint16 => OFTData)) public connectedOFTs;


struct OFTData {
uint256 srcPoolId;
uint256 dstPoolId;
Expand All @@ -61,6 +62,9 @@ contract Balancer is Ownable {
// @dev swapEth is not available on some chains
bool public disableEth;

mapping(uint16 => uint256) private _sgReceiveGas;


event ConnectedChainUpdated(address indexed _srcOft, uint16 indexed _dstChainId, address indexed _dstOft);
event Rebalanced(
address indexed _srcOft, uint16 indexed _dstChainId, uint256 indexed _slippage, uint256 _amount, bool _isNative
Expand All @@ -84,6 +88,7 @@ contract Balancer is Ownable {
error SwapNotEnabled();
error AlreadyInitialized();
error RebalanceAmountNotValid();
error GasNotValid();

modifier onlyValidDestination(address _srcOft, uint16 _dstChainId) {
if (connectedOFTs[_srcOft][_dstChainId].dstOft == address(0)) {
Expand Down Expand Up @@ -138,6 +143,14 @@ contract Balancer is Ownable {
/// =====================
/// Owner
/// =====================
/**
* @notice set lzTxObj `dstGasForCall`
* @param eid the endpoint address
* @param gas the gas amount
*/
function setSgReceiveGas(uint16 eid, uint256 gas) external onlyOwner {
_sgReceiveGas[eid] = gas;
}

/**
* @notice set rebalancer role
Expand Down Expand Up @@ -269,13 +282,11 @@ contract Balancer is Ownable {
function _sendNative(address payable _oft, uint256 _amount, uint16 _dstChainId, uint256 _slippage) private {
if (address(this).balance < _amount) revert ExceedsBalance();
uint256 valueAmount = msg.value + _amount;
routerETH.swapETH{value: valueAmount}(
_dstChainId,
payable(this),
abi.encodePacked(connectedOFTs[_oft][_dstChainId].dstOft),
_amount,
_computeMinAmount(_amount, _slippage)
);
uint256 gas = _sgReceiveGas[_dstChainId];
if (gas == 0) revert GasNotValid();
IStargateRouterBase.SwapAmount memory swapAmounts = IStargateRouterBase.SwapAmount({amountLD: _amount, minAmountLD: _computeMinAmount(_amount, _slippage)});
IStargateRouterBase.lzTxObj memory lzTxObj = IStargateRouterBase.lzTxObj({dstGasForCall: gas, dstNativeAmount: 0, dstNativeAddr: "0x0"});
routerETH.swapETHAndCall{value: valueAmount}(_dstChainId, payable(this), abi.encodePacked(connectedOFTs[_oft][_dstChainId].dstOft), swapAmounts, lzTxObj, "0x");
}

function _sendToken(
Expand All @@ -291,30 +302,34 @@ contract Balancer is Ownable {
}
{
(uint256 _srcPoolId, uint256 _dstPoolId) = abi.decode(_data, (uint256, uint256));
_routerSwap(_dstChainId, _srcPoolId, _dstPoolId, _amount, _slippage, _oft, erc20);
_routerSwap(__RouterSwapInternal(_dstChainId, _srcPoolId, _dstPoolId, _amount, _slippage, _oft, erc20));
}
}

struct __RouterSwapInternal {
uint16 _dstChainId;
uint256 _srcPoolId;
uint256 _dstPoolId;
uint256 _amount;
uint256 _slippage;
address payable _oft;
address _erc20;
}
function _routerSwap(
uint16 _dstChainId,
uint256 _srcPoolId,
uint256 _dstPoolId,
uint256 _amount,
uint256 _slippage,
address payable _oft,
address _erc20
__RouterSwapInternal memory swapInternal
) private {
bytes memory _dst = abi.encodePacked(connectedOFTs[_oft][_dstChainId].dstOft);
IERC20(_erc20).safeApprove(address(router), _amount);
uint256 gas = _sgReceiveGas[swapInternal._dstChainId];
if (gas == 0) revert GasNotValid();
IERC20(swapInternal._erc20).safeApprove(address(router), swapInternal._amount);
router.swap{value: msg.value}(
_dstChainId,
_srcPoolId,
_dstPoolId,
swapInternal._dstChainId,
swapInternal._srcPoolId,
swapInternal._dstPoolId,
payable(this),
_amount,
_computeMinAmount(_amount, _slippage),
IStargateRouterBase.lzTxObj({dstGasForCall: 0, dstNativeAmount: 0, dstNativeAddr: "0x0"}),
_dst,
swapInternal._amount,
_computeMinAmount(swapInternal._amount, swapInternal._slippage),
IStargateRouterBase.lzTxObj({dstGasForCall: gas, dstNativeAmount: 0, dstNativeAddr: "0x0"}),
abi.encodePacked(connectedOFTs[swapInternal._oft][swapInternal._dstChainId].dstOft),
"0x"
);
}
Expand Down
2 changes: 2 additions & 0 deletions test/Balancer.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ contract TOFTTest is TOFTTestHelper {

balancer.initConnectedOFT(address(aTOFT), uint16(bEid), address(bTOFT), abi.encode(uint256(1), uint256(1)));
balancer.addRebalanceAmount(address(aTOFT), uint16(bEid), erc20Amount_);
balancer.setSgReceiveGas(uint16(aEid), 500_000);
balancer.setSgReceiveGas(uint16(bEid), 500_000);

mTOFT.SetOwnerStateData memory dataB = mTOFT.SetOwnerStateData({
stargateRouter: address(routerA),
Expand Down