Skip to content

Instantly share code, notes, and snippets.

@0xmikko
Last active February 15, 2021 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xmikko/c2a7ea4eb1faadad2e975777aeb96fdc to your computer and use it in GitHub Desktop.
Save 0xmikko/c2a7ea4eb1faadad2e975777aeb96fdc to your computer and use it in GitHub Desktop.
Gearbox swap
// store balances before swap
uint256 balanceInBefore = _vaultService.getBalance(tokenIn);
uint256 balanceOutBefore = _vaultService.getBalance(tokenOut);
// swapTokens
_vaultService.swapTokensForExactTokens(
amountOut,
amountInMax,
path,
deadline
);
// compute changes in balances
uint256 amountInSpent =
balanceInBefore.sub(_vaultService.getBalance(tokenIn));
uint256 amountOutGot =
_vaultService.getBalance(tokenOut).sub(balanceOutBefore);
// update stored balances with differences
_PositionRepository.swapAssets(
msg.sender,
tokenIn,
amountInSpent,
tokenOut,
amountOutGot
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment