Remove 18 dec conversions

This commit is contained in:
Shriya Tyagi 2023-08-22 01:13:00 +05:30
parent f8841aa704
commit 2424035fa4
2 changed files with 23 additions and 70 deletions

View File

@ -15,7 +15,7 @@ contract Events {
address indexed token, address indexed token,
uint256 shareAmt, uint256 shareAmt,
uint256 maxTokenPerShares, uint256 maxTokenPerShares,
uint256 underlyingTokenAmount, uint256 tokensDeducted,
uint256 getId, uint256 getId,
uint256 setId uint256 setId
); );

View File

@ -41,20 +41,15 @@ abstract contract BasicConnector is Events, DSMath, Basic {
? _underlyingTokenContract.balanceOf(address(this)) ? _underlyingTokenContract.balanceOf(address(this))
: _underlyingAmt; : _underlyingAmt;
// Making sure that the underlying amount is always in 18 decimals. // Returns final amount in token decimals.
uint256 _underlyingAmt18 = convertTo18( uint256 _minShares = wmul(minSharesPerToken, _underlyingAmt);
_underlyingTokenContract.decimals(),
_underlyingAmt
);
uint256 _minShares = convert18ToDec(
vaultTokenContract.decimals(),
wmul(minSharesPerToken, _underlyingAmt18) // Returns final amount in the floor value of the two token decimals.
);
// Initial share balance
uint256 _initialVaultBal = vaultTokenContract.balanceOf(address(this)); uint256 _initialVaultBal = vaultTokenContract.balanceOf(address(this));
approve(_underlyingTokenContract, vaultToken, _underlyingAmt); approve(_underlyingTokenContract, vaultToken, _underlyingAmt);
// Deposit tokens for shares
vaultTokenContract.deposit(_underlyingAmt, address(this)); vaultTokenContract.deposit(_underlyingAmt, address(this));
uint256 _sharesReceieved = sub( uint256 _sharesReceieved = sub(
@ -100,46 +95,32 @@ abstract contract BasicConnector is Events, DSMath, Basic {
vaultTokenContract.asset() vaultTokenContract.asset()
); );
// In vault token decimals
_shareAmt = _shareAmt == uint256(-1) _shareAmt = _shareAmt == uint256(-1)
? vaultTokenContract.balanceOf(address(this)) ? vaultTokenContract.balanceOf(address(this))
: _shareAmt; : _shareAmt;
// In 18 decimals // Returns final amount in token decimals.
maxTokenPerShares = convertTo18( uint256 _maxTokens = wmul(maxTokenPerShares, _shareAmt);
vaultTokenContract.decimals(),
wmul(maxTokenPerShares, _shareAmt) // Returns final amount in the vault token decimals.
);
// In token decimals
maxTokenPerShares = convert18ToDec(
underlyingTokenContract.decimals(),
maxTokenPerShares
);
// In token decimals
uint256 _underlyingTokenAmount = vaultTokenContract.previewMint( uint256 _underlyingTokenAmount = vaultTokenContract.previewMint(
_shareAmt _shareAmt
); );
// In token decimals
uint256 _initalUnderlyingBal = underlyingTokenContract.balanceOf( uint256 _initalUnderlyingBal = underlyingTokenContract.balanceOf(
address(this) address(this)
); );
approve(underlyingTokenContract, vaultToken, _underlyingTokenAmount); approve(underlyingTokenContract, vaultToken, _underlyingTokenAmount);
// Mint shares for tokens
vaultTokenContract.mint(_shareAmt, address(this)); vaultTokenContract.mint(_shareAmt, address(this));
// In token decimals uint256 _tokensDeducted = sub(
uint256 _finalUnderlyingBal = underlyingTokenContract.balanceOf( _initalUnderlyingBal,
address(this) underlyingTokenContract.balanceOf(address(this))
); );
require( require(_maxTokens >= _tokensDeducted, "maxTokenPerShares-exceeds");
maxTokenPerShares >= sub(_initalUnderlyingBal, _finalUnderlyingBal),
"maxTokenPerShares-exceeds"
);
setUint(setId, _shareAmt); setUint(setId, _shareAmt);
@ -148,7 +129,7 @@ abstract contract BasicConnector is Events, DSMath, Basic {
vaultToken, vaultToken,
_shareAmt, _shareAmt,
maxTokenPerShares, maxTokenPerShares,
_underlyingTokenAmount, _tokensDeducted,
getId, getId,
setId setId
); );
@ -179,33 +160,19 @@ abstract contract BasicConnector is Events, DSMath, Basic {
vaultTokenContract.asset() vaultTokenContract.asset()
); );
// In token decimals
_underlyingAmt = _underlyingAmt == uint256(-1) _underlyingAmt = _underlyingAmt == uint256(-1)
? underlyingTokenContract.balanceOf(address(this)) ? underlyingTokenContract.balanceOf(address(this))
: _underlyingAmt; : _underlyingAmt;
// In 18 decimals // Returns final amount in token decimals.
_underlyingAmt = convertTo18( uint256 _maxShares = wmul(maxSharesPerToken, _underlyingAmt);
underlyingTokenContract.decimals(),
_underlyingAmt
);
// In vault token decimals
uint256 _maxShares = convert18ToDec(
vaultTokenContract.decimals(),
wmul(maxSharesPerToken, _underlyingAmt) // In 18 decimals
);
// In vault token decimals
uint256 _initialVaultBal = vaultTokenContract.balanceOf(to); uint256 _initialVaultBal = vaultTokenContract.balanceOf(to);
// Withdraw tokens for shares
vaultTokenContract.withdraw(_underlyingAmt, to, address(this)); vaultTokenContract.withdraw(_underlyingAmt, to, address(this));
// In vault token decimals uint256 _sharesBurned = sub(_initialVaultBal, vaultTokenContract.balanceOf(to));
uint256 _finalVaultBal = vaultTokenContract.balanceOf(to);
// In vault token decimals
uint256 _sharesBurned = sub(_initialVaultBal, _finalVaultBal);
require(_maxShares >= _sharesBurned, "maxShares-exceeds"); require(_maxShares >= _sharesBurned, "maxShares-exceeds");
@ -249,38 +216,24 @@ abstract contract BasicConnector is Events, DSMath, Basic {
vaultTokenContract.asset() vaultTokenContract.asset()
); );
// In vault token decimals
_shareAmt = _shareAmt == uint256(-1) _shareAmt = _shareAmt == uint256(-1)
? vaultTokenContract.balanceOf(address(this)) ? vaultTokenContract.balanceOf(address(this))
: _shareAmt; : _shareAmt;
// In 18 decimals // Returns final amount in token decimals.
_shareAmt = convertTo18(vaultTokenContract.decimals(), _shareAmt); uint256 _minUnderlyingAmt = wmul(minTokenPerShares, _shareAmt);
// In token decimals
uint256 _minUnderlyingAmt = convert18ToDec(
underlyingTokenContract.decimals(),
wmul(minTokenPerShares, _shareAmt) // In 18 decimals
);
// In token decimals
uint256 _initalUnderlyingBal = underlyingTokenContract.balanceOf(to); uint256 _initalUnderlyingBal = underlyingTokenContract.balanceOf(to);
// Redeem tokens for shares
vaultTokenContract.redeem(_shareAmt, to, address(this)); vaultTokenContract.redeem(_shareAmt, to, address(this));
// In token decimals
uint256 _finalUnderlyingBal = underlyingTokenContract.balanceOf(to);
// In token decimals
uint256 _underlyingAmtReceieved = sub( uint256 _underlyingAmtReceieved = sub(
_finalUnderlyingBal, underlyingTokenContract.balanceOf(to),
_initalUnderlyingBal _initalUnderlyingBal
); );
require( require(_minUnderlyingAmt <= _underlyingAmtReceieved, "_minUnderlyingAmt-exceeds");
_minUnderlyingAmt <= _underlyingAmtReceieved,
"_minUnderlyingAmt-exceeds"
);
setUint(setId, _shareAmt); setUint(setId, _shareAmt);