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

feat: data provider for wrapped tokens #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
90 changes: 90 additions & 0 deletions src/WrappedTokenDataProvider.sol
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing docs in general

Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.10;

import {Ownable} from 'aave-v3-core/contracts/dependencies/openzeppelin/contracts/Ownable.sol';
import {IBaseTokenWrapper} from './interfaces/IBaseTokenWrapper.sol';
import {IPoolAddressesProvider} from 'aave-v3-core/contracts/interfaces/IPoolAddressesProvider.sol';
import {IPool} from 'aave-v3-core/contracts/interfaces/IPool.sol';
import {DataTypes} from 'aave-v3-core/contracts/protocol/libraries/types/DataTypes.sol';
import {IERC20Detailed} from 'aave-v3-core/contracts/dependencies/openzeppelin/contracts/IERC20Detailed.sol';
import {AggregatorInterface} from 'lib/aave-v3-core/contracts/dependencies/chainlink/AggregatorInterface.sol';

contract WrappedTokenDataProvider is Ownable {

struct TokenDetails {
address token;
uint256 balance;
int256 latestAnswer;
uint8 decimals;
string name;
string symbol;
}
Comment on lines +14 to +21
Copy link
Contributor

@miguelmtzinf miguelmtzinf Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
struct TokenDetails {
address token;
uint256 balance;
int256 latestAnswer;
uint8 decimals;
string name;
string symbol;
}
struct UserPosition {
address token;
uint256 balance;
int256 tokenPrice;
uint8 tokenDecimals;
string tokenName;
string tokenSymbol;
}

we can cast the int256 into uint256 as well.


struct WrappedToken {
TokenDetails tokenIn;
TokenDetails tokenOut;
address tokenWrapperContract;
}

struct WrappedTokenConfig {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
struct WrappedTokenConfig {
struct TokenWrapperData {

address tokenInOracle;
address tokenOutOracle;
address tokenWrapperContract;
}

mapping(address => WrappedTokenConfig[]) public wrappedTokenConfigMap;

constructor(address owner) {
transferOwnership(owner);
}

function addTokenWrapperConfig(address poolAddress, address tokenInOracleAddress, address tokenOutOracleAddress, address tokenWrapperContractAddress) external onlyOwner {
address tokenWrapperPoolAddress = address(IBaseTokenWrapper(tokenWrapperContractAddress).POOL());
require(tokenWrapperPoolAddress == poolAddress, 'Invalid pool address for provided token wrapper contract');

wrappedTokenConfigMap[poolAddress].push(WrappedTokenConfig({
tokenInOracle: tokenInOracleAddress,
tokenOutOracle: tokenOutOracleAddress,
tokenWrapperContract: tokenWrapperContractAddress
}));
}

function getWrappedTokenData(address poolAddress, address user) external view returns (WrappedToken[] memory) {
WrappedTokenConfig[] memory wrappedTokenConfigs = wrappedTokenConfigMap[poolAddress];
if (wrappedTokenConfigs.length == 0) {
return new WrappedToken[](0);
}

WrappedToken[] memory wrappedTokens = new WrappedToken[](wrappedTokenConfigs.length);

for (uint256 i = 0; i < wrappedTokenConfigs.length; i++) {
WrappedToken memory wrappedToken;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move out of the loop

WrappedTokenConfig memory wrappedTokenConfig = wrappedTokenConfigs[i];

address wrapper = wrappedTokenConfig.tokenWrapperContract;
address tokenIn = IBaseTokenWrapper(wrapper).TOKEN_IN();
address tokenOut = IBaseTokenWrapper(wrapper).TOKEN_OUT();

wrappedToken.tokenIn = getTokenDetails(tokenIn, wrappedTokenConfig.tokenInOracle, user);
wrappedToken.tokenOut = getTokenDetails(tokenOut, wrappedTokenConfig.tokenOutOracle, user);

wrappedToken.tokenWrapperContract = wrapper;

wrappedTokens[i] = wrappedToken;
}

return wrappedTokens;
}

function getTokenDetails(address token, address oracle, address user) internal view returns (TokenDetails memory) {
IERC20Detailed tokenInstance = IERC20Detailed(token);
return TokenDetails({
token: token,
decimals: tokenInstance.decimals(),
name: tokenInstance.name(),
symbol: tokenInstance.symbol(),
balance: tokenInstance.balanceOf(user),
latestAnswer: AggregatorInterface(oracle).latestAnswer()
});
}
}