mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
feat: Update function calculations
This commit is contained in:
parent
ba274cab5b
commit
36e3d9a0a3
|
@ -6,6 +6,7 @@ contract Events {
|
|||
address indexed token,
|
||||
uint256 underlyingAmt,
|
||||
uint256 minSharesPerToken,
|
||||
uint256 sharesReceieved,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
|
@ -14,6 +15,7 @@ contract Events {
|
|||
address indexed token,
|
||||
uint256 shareAmt,
|
||||
uint256 maxTokenPerShares,
|
||||
uint256 underlyingTokenAmount,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
|
@ -22,6 +24,7 @@ contract Events {
|
|||
address indexed token,
|
||||
uint256 underlyingAmt,
|
||||
uint256 maxSharesPerToken,
|
||||
uint256 sharedBurned,
|
||||
address indexed to,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
|
@ -31,8 +34,9 @@ contract Events {
|
|||
address indexed token,
|
||||
uint256 shareAmt,
|
||||
uint256 minTokenPerShares,
|
||||
uint256 underlyingAmtReceieved,
|
||||
address to,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
}
|
||||
}
|
|
@ -17,57 +17,61 @@ abstract contract BasicConnector is Events, DSMath, Basic {
|
|||
/**
|
||||
* @dev Deposit underlying asset to ERC4626 Vault.
|
||||
* @notice Mints vault shares by depositing exactly amount of underlying assets
|
||||
* @param token ERC4626 Token address.
|
||||
* @param vaultToken ERC4626 Token address.
|
||||
* @param underlyingAmt The amount of the underlying asset to deposit. (For max: `uint256(-1)`)
|
||||
* @param minSharesPerToken The min share rate of deposit
|
||||
* @param minSharesPerToken The min share rate of deposit. Should always be in 18 decimals.
|
||||
* @param getId ID to retrieve amt.
|
||||
* @param setId ID stores the amount of tokens deposited.
|
||||
*/
|
||||
|
||||
function deposit(
|
||||
address token,
|
||||
address vaultToken,
|
||||
uint256 underlyingAmt,
|
||||
uint256 minSharesPerToken,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
) public returns (string memory _eventName, bytes memory _eventParam) {
|
||||
uint256 _underlyingAmt = getUint(getId, underlyingAmt);
|
||||
IERC4626 vaultTokenContract = IERC4626(token);
|
||||
|
||||
address _underlyingToken = vaultTokenContract.asset();
|
||||
uint8 _vaultShareDecimal = vaultTokenContract.decimals();
|
||||
TokenInterface underlyingTokenContract = TokenInterface(
|
||||
_underlyingToken
|
||||
IERC4626 vaultTokenContract = IERC4626(vaultToken);
|
||||
TokenInterface _underlyingTokenContract = TokenInterface(
|
||||
vaultTokenContract.asset()
|
||||
);
|
||||
|
||||
_underlyingAmt = _underlyingAmt == uint256(-1)
|
||||
? underlyingTokenContract.balanceOf(address(this))
|
||||
? _underlyingTokenContract.balanceOf(address(this))
|
||||
: _underlyingAmt;
|
||||
|
||||
// Making sure that the underlying amount is always in 18 decimals.
|
||||
uint256 _underlyingAmt18 = convertTo18(
|
||||
_underlyingTokenContract.decimals(),
|
||||
_underlyingAmt
|
||||
);
|
||||
|
||||
uint256 _minShares = convert18ToDec(
|
||||
_vaultShareDecimal,
|
||||
wmul(minSharesPerToken, _underlyingAmt)
|
||||
vaultTokenContract.decimals(),
|
||||
wmul(minSharesPerToken, _underlyingAmt18) // Returns final amount in the floor value of the two token decimals.
|
||||
);
|
||||
|
||||
uint256 _initialVaultBal = vaultTokenContract.balanceOf(address(this));
|
||||
|
||||
approve(underlyingTokenContract, token, _underlyingAmt);
|
||||
approve(_underlyingTokenContract, vaultToken, _underlyingAmt);
|
||||
vaultTokenContract.deposit(_underlyingAmt, address(this));
|
||||
|
||||
uint256 _finalVaultBal = vaultTokenContract.balanceOf(address(this));
|
||||
|
||||
require(
|
||||
_minShares <= sub(_finalVaultBal, _initialVaultBal),
|
||||
"minShares-exceeds"
|
||||
uint256 _sharesReceieved = sub(
|
||||
vaultTokenContract.balanceOf(address(this)),
|
||||
_initialVaultBal
|
||||
);
|
||||
|
||||
setUint(setId, _underlyingAmt);
|
||||
require(_minShares <= _sharesReceieved, "Less shares received");
|
||||
|
||||
_eventName = "LogDeposit(address,uint256,uint256,uint256,uint256)";
|
||||
setUint(setId, _sharesReceieved);
|
||||
|
||||
_eventName = "LogDeposit(address,uint256,uint256,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(
|
||||
token,
|
||||
vaultToken,
|
||||
_underlyingAmt,
|
||||
minSharesPerToken,
|
||||
_sharesReceieved,
|
||||
getId,
|
||||
setId
|
||||
);
|
||||
|
@ -76,51 +80,59 @@ abstract contract BasicConnector is Events, DSMath, Basic {
|
|||
/**
|
||||
* @dev Mint underlying asset to ERC4626 Vault.
|
||||
* @notice Mints vault shares by minting exactly amount of underlying assets
|
||||
* @param token ERC4626 Token address.
|
||||
* @param vaultToken ERC4626 Token address.
|
||||
* @param shareAmt The amount of the share to mint. (For max: `uint256(-1)`)
|
||||
* @param maxTokenPerShares The max underyling token rate of mint
|
||||
* @param maxTokenPerShares The max underyling token rate of mint. Always in 18 decimals.
|
||||
* @param getId ID to retrieve amt.
|
||||
* @param setId ID stores the amount of tokens minted.
|
||||
*/
|
||||
|
||||
function mint(
|
||||
address token,
|
||||
address vaultToken,
|
||||
uint256 shareAmt,
|
||||
uint256 maxTokenPerShares,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
) public returns (string memory _eventName, bytes memory _eventParam) {
|
||||
uint256 _shareAmt = getUint(getId, shareAmt);
|
||||
IERC4626 vaultTokenContract = IERC4626(token);
|
||||
|
||||
address _underlyingToken = vaultTokenContract.asset();
|
||||
uint8 _vaultShareDecimal = vaultTokenContract.decimals();
|
||||
IERC4626 vaultTokenContract = IERC4626(vaultToken);
|
||||
TokenInterface underlyingTokenContract = TokenInterface(
|
||||
_underlyingToken
|
||||
vaultTokenContract.asset()
|
||||
);
|
||||
|
||||
// In vault token decimals
|
||||
_shareAmt = _shareAmt == uint256(-1)
|
||||
? vaultTokenContract.balanceOf(address(this))
|
||||
: _shareAmt;
|
||||
|
||||
// In 18 decimals
|
||||
maxTokenPerShares = convertTo18(
|
||||
_vaultShareDecimal,
|
||||
wmul(maxTokenPerShares, _shareAmt)
|
||||
vaultTokenContract.decimals(),
|
||||
wmul(maxTokenPerShares, _shareAmt) // Returns final amount in the vault token decimals.
|
||||
);
|
||||
|
||||
uint256 _approveUnderlyingTokenAmount = vaultTokenContract.previewMint(
|
||||
// In token decimals
|
||||
maxTokenPerShares = convert18ToDec(
|
||||
underlyingTokenContract.decimals(),
|
||||
maxTokenPerShares
|
||||
);
|
||||
|
||||
// In token decimals
|
||||
uint256 _underlyingTokenAmount = vaultTokenContract.previewMint(
|
||||
_shareAmt
|
||||
);
|
||||
|
||||
uint256 _initalUnderlyingBal = IERC20(_underlyingToken).balanceOf(
|
||||
// In token decimals
|
||||
uint256 _initalUnderlyingBal = underlyingTokenContract.balanceOf(
|
||||
address(this)
|
||||
);
|
||||
|
||||
approve(underlyingTokenContract, token, _approveUnderlyingTokenAmount);
|
||||
approve(underlyingTokenContract, vaultToken, _underlyingTokenAmount);
|
||||
|
||||
vaultTokenContract.mint(_shareAmt, address(this));
|
||||
|
||||
uint256 _finalUnderlyingBal = IERC20(_underlyingToken).balanceOf(
|
||||
// In token decimals
|
||||
uint256 _finalUnderlyingBal = underlyingTokenContract.balanceOf(
|
||||
address(this)
|
||||
);
|
||||
|
||||
|
@ -131,11 +143,12 @@ abstract contract BasicConnector is Events, DSMath, Basic {
|
|||
|
||||
setUint(setId, _shareAmt);
|
||||
|
||||
_eventName = "LogMint(address,uint256,uint256,uint256,uint256)";
|
||||
_eventName = "LogMint(address,uint256,uint256,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(
|
||||
token,
|
||||
vaultToken,
|
||||
_shareAmt,
|
||||
maxTokenPerShares,
|
||||
_underlyingTokenAmount,
|
||||
getId,
|
||||
setId
|
||||
);
|
||||
|
@ -144,16 +157,15 @@ abstract contract BasicConnector is Events, DSMath, Basic {
|
|||
/**
|
||||
* @dev Withdraw underlying asset from ERC4626 Vault.
|
||||
* @notice Withdraw vault shares with exactly amount of underlying assets
|
||||
* @param token ERC4626 Token address.
|
||||
* @param vaultToken ERC4626 Token address.
|
||||
* @param underlyingAmt The amount of the token to withdraw. (For max: `uint256(-1)`)
|
||||
* @param maxSharesPerToken The max share rate of withdrawn amount.
|
||||
* @param maxSharesPerToken The max share rate of withdrawn amount. Always send in 18 decimals.
|
||||
* @param to The address of receiver.
|
||||
* @param getId ID to retrieve amt.
|
||||
* @param setId ID stores the amount of tokens withdrawn.
|
||||
*/
|
||||
|
||||
function withdraw(
|
||||
address token,
|
||||
address vaultToken,
|
||||
uint256 underlyingAmt,
|
||||
uint256 maxSharesPerToken,
|
||||
address payable to,
|
||||
|
@ -161,41 +173,50 @@ abstract contract BasicConnector is Events, DSMath, Basic {
|
|||
uint256 setId
|
||||
) public returns (string memory _eventName, bytes memory _eventParam) {
|
||||
uint256 _underlyingAmt = getUint(getId, underlyingAmt);
|
||||
IERC4626 vaultTokenContract = IERC4626(token);
|
||||
|
||||
address _underlyingToken = vaultTokenContract.asset();
|
||||
uint8 _vaultShareDecimal = vaultTokenContract.decimals();
|
||||
IERC4626 vaultTokenContract = IERC4626(vaultToken);
|
||||
TokenInterface underlyingTokenContract = TokenInterface(
|
||||
_underlyingToken
|
||||
vaultTokenContract.asset()
|
||||
);
|
||||
|
||||
// In token decimals
|
||||
_underlyingAmt = _underlyingAmt == uint256(-1)
|
||||
? underlyingTokenContract.balanceOf(address(this))
|
||||
: _underlyingAmt;
|
||||
|
||||
uint256 _maxShares = convert18ToDec(
|
||||
_vaultShareDecimal,
|
||||
wmul(maxSharesPerToken, _underlyingAmt)
|
||||
// In 18 decimals
|
||||
_underlyingAmt = convertTo18(
|
||||
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);
|
||||
|
||||
vaultTokenContract.withdraw(_underlyingAmt, to, address(this));
|
||||
|
||||
// In vault token decimals
|
||||
uint256 _finalVaultBal = vaultTokenContract.balanceOf(to);
|
||||
|
||||
require(
|
||||
_maxShares >= sub(_finalVaultBal, _initialVaultBal),
|
||||
"maxShares-exceeds"
|
||||
);
|
||||
// In vault token decimals
|
||||
uint256 _sharesBurned = sub(_initialVaultBal, _finalVaultBal);
|
||||
|
||||
require(_maxShares >= _sharesBurned, "maxShares-exceeds");
|
||||
|
||||
setUint(setId, _underlyingAmt);
|
||||
|
||||
_eventName = "LogWithdraw(address,uint256,uint256,address,uint256,uint256)";
|
||||
_eventName = "LogWithdraw(address,uint256,uint256,uint256,address,uint256,uint256)";
|
||||
_eventParam = abi.encode(
|
||||
token,
|
||||
vaultToken,
|
||||
_underlyingAmt,
|
||||
maxSharesPerToken,
|
||||
_sharesBurned,
|
||||
to,
|
||||
getId,
|
||||
setId
|
||||
|
@ -205,16 +226,16 @@ abstract contract BasicConnector is Events, DSMath, Basic {
|
|||
/**
|
||||
* @dev Redeem underlying asset from ERC4626 Vault.
|
||||
* @notice Redeem vault shares with exactly amount of underlying assets
|
||||
* @param token ERC4626 Token address.
|
||||
* @param vaultToken ERC4626 Token address.
|
||||
* @param shareAmt The amount of the token to redeem. (For max: `uint256(-1)`)
|
||||
* @param minTokenPerShares The min underlying token rate of withdraw.
|
||||
* @param minTokenPerShares The min underlying token rate of withdraw. Always in 18 decimals.
|
||||
* @param to The address of receiver.
|
||||
* @param getId ID to retrieve amt.
|
||||
* @param setId ID stores the amount of tokens redeem.
|
||||
*/
|
||||
|
||||
function redeem(
|
||||
address token,
|
||||
address vaultToken,
|
||||
uint256 shareAmt,
|
||||
uint256 minTokenPerShares,
|
||||
address payable to,
|
||||
|
@ -222,44 +243,53 @@ abstract contract BasicConnector is Events, DSMath, Basic {
|
|||
uint256 setId
|
||||
) public returns (string memory _eventName, bytes memory _eventParam) {
|
||||
uint256 _shareAmt = getUint(getId, shareAmt);
|
||||
IERC4626 vaultTokenContract = IERC4626(token);
|
||||
|
||||
address _underlyingToken = vaultTokenContract.asset();
|
||||
uint8 _vaultShareDecimal = vaultTokenContract.decimals();
|
||||
IERC4626 vaultTokenContract = IERC4626(vaultToken);
|
||||
TokenInterface underlyingTokenContract = TokenInterface(
|
||||
_underlyingToken
|
||||
vaultTokenContract.asset()
|
||||
);
|
||||
|
||||
// In vault token decimals
|
||||
_shareAmt = _shareAmt == uint256(-1)
|
||||
? vaultTokenContract.balanceOf(address(this))
|
||||
: _shareAmt;
|
||||
|
||||
// In 18 decimals
|
||||
_shareAmt = convertTo18(vaultTokenContract.decimals(), _shareAmt);
|
||||
|
||||
// In token decimals
|
||||
uint256 _minUnderlyingAmt = convert18ToDec(
|
||||
_vaultShareDecimal,
|
||||
wmul(minTokenPerShares, _shareAmt)
|
||||
);
|
||||
|
||||
uint256 _initalUnderlyingBal = IERC20(_underlyingToken).balanceOf(
|
||||
to
|
||||
underlyingTokenContract.decimals(),
|
||||
wmul(minTokenPerShares, _shareAmt) // In 18 decimals
|
||||
);
|
||||
|
||||
// In token decimals
|
||||
uint256 _initalUnderlyingBal = underlyingTokenContract.balanceOf(to);
|
||||
|
||||
vaultTokenContract.redeem(_shareAmt, to, address(this));
|
||||
|
||||
uint256 _finalUnderlyingBal = IERC20(_underlyingToken).balanceOf(
|
||||
to
|
||||
// In token decimals
|
||||
uint256 _finalUnderlyingBal = underlyingTokenContract.balanceOf(to);
|
||||
|
||||
// In token decimals
|
||||
uint256 _underlyingAmtReceieved = sub(
|
||||
_finalUnderlyingBal,
|
||||
_initalUnderlyingBal
|
||||
);
|
||||
|
||||
require(
|
||||
_minUnderlyingAmt <= sub(_finalUnderlyingBal, _initalUnderlyingBal),
|
||||
_minUnderlyingAmt <= _underlyingAmtReceieved,
|
||||
"_minUnderlyingAmt-exceeds"
|
||||
);
|
||||
|
||||
setUint(setId, _shareAmt);
|
||||
|
||||
_eventName = "LogRedeem(address,uint256,uint256,address,uint256,uint256)";
|
||||
_eventName = "LogRedeem(address,uint256,uint256,uint256,address,uint256,uint256)";
|
||||
_eventParam = abi.encode(
|
||||
token,
|
||||
vaultToken,
|
||||
_shareAmt,
|
||||
minTokenPerShares,
|
||||
_underlyingAmtReceieved,
|
||||
to,
|
||||
getId,
|
||||
setId
|
||||
|
@ -269,4 +299,4 @@ abstract contract BasicConnector is Events, DSMath, Basic {
|
|||
|
||||
contract ConnectV2BasicERC4626 is BasicConnector {
|
||||
string public constant name = "BASIC-ERC4626-v1.0";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user