Skip to content

Commit

Permalink
fix: not using dex adapter to swap from within the paymaster
Browse files Browse the repository at this point in the history
  • Loading branch information
livingrockrises committed May 31, 2023
1 parent a88357e commit a0c1ef9
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 199 deletions.
107 changes: 1 addition & 106 deletions contracts/token/BiconomyTokenPaymaster.sol
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,6 @@ contract BiconomyTokenPaymaster is
uint256 indexed charge
);

/**
* Record the information of swap made on dex router and native tokens deposited
*/
event TokenSwappedAndGasDeposited(
address indexed dexRouterAddress,
address indexed token,
bytes indexed routerCalldata,
bool success,
uint256 gasAmountDeposited
);

constructor(
address _owner,
IEntryPoint _entryPoint,
Expand Down Expand Up @@ -249,100 +238,6 @@ contract BiconomyTokenPaymaster is
}
}

// review can be marked for removal
/**
* @dev approve ERC20 tokens from this paymaster contract to the dex router. required to generate data for cases like 1Inch
* @param _token the token address
* @param _dexRouter dex router address to approve
* @param _amount amount to approve
*/
function approveRouterPrior(
address _token,
address _dexRouter,
uint256 _amount
) public payable onlyOwner {
SafeTransferLib.safeApprove(_token, _dexRouter, _amount);
}

// review could pack attributes in single / 2 structs. A label field string memory label can be added
// review if an adapter has to be called instead of router directly then this would add steps to transfer tokens to adapter and make approval of adapter to router
/**
* @dev Helper function to trigger periodic swap to convert tokens received inside this paymaster back to native tokens and immediately deposit on entry point.
* @notice actual swap data is generated offchain basen on i. router ii. token to swap iii. amount of tokens iv. route v. slippage etc
* @notice Biconomy may not use feeReceiver to be paymaster contract itself. In this case tokens do not need to be pulled or swapped from the contract
* @param _dexRouter dex router/adapter address
* @param _swapData calldata for making the swap on chosen router. as function signature also depends on the choice
* @param _approveRouter caller sends if the router being used is to be approved or not
* @param _token ERC20 token address being swapped. useless is above bool flag is false
* @param _amount ERC20 token amount to be approved. useless is above bool flag is false
* @param _maxDepositToEP sender can pass maximum amount of gas to be deposited to entry point
*/
function swapTokenForNativeAndDeposit(
address _dexRouter,
bytes calldata _swapData,
bool _approveRouter,
address _token,
uint256 _amount,
uint256 _maxDepositToEP
)
public
payable
onlyOwner
nonReentrant
returns (bool success, uint256 depositAmount)
{
// only proceed if router is not 0 address
if (_dexRouter == address(0)) revert DEXRouterCannotBeZero();
// make approval to router
if (_approveRouter) {
SafeTransferLib.safeApprove(_token, _dexRouter, _amount);
}

// make the swap
// review could take snapshot of token balance and native token balance before and after the swap
(bool success, bytes memory returndata) = address(_dexRouter).call(
_swapData
);

{
// if we made a swap to wrapped native asset
uint256 weth9Balance = IERC20(WETH9).balanceOf(address(this));
if (weth9Balance != 0) {
// unwrap
bytes memory _data = abi.encodeWithSelector(
IWETH9.withdraw.selector,
weth9Balance
);

(bool success, bytes memory returndata) = address(WETH9).call(
_data
);
}
}

// here we are assuming that after above call to the router, Native tokens would have been received in the contract
uint256 depositAmount = address(this).balance;

if (depositAmount > _maxDepositToEP) {
depositAmount = _maxDepositToEP;
}

if (depositAmount != 0) {
// entrypoint.depositTo
IEntryPoint(entryPoint).depositTo{value: depositAmount}(
address(this)
);
}

emit TokenSwappedAndGasDeposited(
_dexRouter,
_token,
_swapData,
success,
depositAmount
);
}

/**
* @dev pull tokens out of paymaster in case they were sent to the paymaster at any point.
* @param token the token deposit to withdraw
Expand Down Expand Up @@ -410,7 +305,7 @@ contract BiconomyTokenPaymaster is
}

/**
* @dev pull native tokens out of paymaster in case they were sent to the paymaster at any point or excess funds left after swapping tokens and not deposited fully to entry point.
* @dev pull native tokens out of paymaster in case they were sent to the paymaster at any point
* @param dest address to send to
*/
function withdrawAllNative(
Expand Down
Loading

0 comments on commit a0c1ef9

Please sign in to comment.