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

[WEB-1232-26] Feat/add norm price to helper #26

Merged
merged 3 commits into from
Jan 23, 2022
Merged
Show file tree
Hide file tree
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
34 changes: 33 additions & 1 deletion contracts/Utilities/Helper/Helpers/PricesHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ interface IOracle {
external
view
returns (uint256);

function getNormalizedValueUsdc(address tokensAddress, uint256 amount)
external
view
returns (uint256);
}

contract PricesHelper is Manageable {
Expand All @@ -19,6 +24,11 @@ contract PricesHelper is Manageable {
uint256 priceUsdc;
}

struct Tokens {
address tokenId;
uint256 amount;
}

constructor(address _oracleAddress, address _managementListAddress) Manageable(_managementListAddress) {
require(_oracleAddress != address(0), "Missing oracle address");
oracleAddress = _oracleAddress;
Expand All @@ -44,7 +54,29 @@ contract PricesHelper is Manageable {
}
return _tokensPrices;
}


function tokensPricesNormalizedUsdc(Tokens[] memory tokens)
external
view
returns (TokenPrice[] memory)
{
TokenPrice[] memory _tokenPricesNormalized =
new TokenPrice[](tokens.length);
for (
uint256 tokenIdx = 0;
tokenIdx < tokens.length;
tokenIdx++
) {
address tokenAddress = tokens[tokenIdx].tokenId;
uint256 amount = tokens[tokenIdx].amount;
_tokenPricesNormalized[tokenIdx] = TokenPrice({
tokenId: tokenAddress,
priceUsdc: IOracle(oracleAddress).getNormalizedValueUsdc(tokenAddress, amount)
});
}
return _tokenPricesNormalized;
}

function updateOracleAddress(address _oracleAddress) external onlyManagers {
oracleAddress = _oracleAddress;
}
Expand Down
62 changes: 62 additions & 0 deletions tests/helpers/test_helper_prices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import pytest

from brownie import accounts

@pytest.fixture
def prices_helper(PricesHelper, management):
oracle_address = "0x83d95e0D5f402511dB06817Aff3f9eA88224B030"
return PricesHelper.deploy(oracle_address, management, {"from": accounts[0]})

@pytest.fixture
def token_addresses():
return {
"usdc": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
"crv": "0xD533a949740bb3306d119CC777fa900bA034cd52"
}


def test_tokens_prices(prices_helper, token_addresses):
'''
Solidity (Returns):
struct TokenPrice {
address tokenId;
uint256 priceUsdc;
}
TokenPrice[]

Brownie:
List[(tokenId, priceUsdc)]
'''
token_prices = prices_helper.tokensPrices(
[token_addresses['usdc'], token_addresses['crv']],
)
assert token_prices[0][0] == token_addresses['usdc']
assert token_prices[0][1] > 0
assert token_prices[1][0] == token_addresses['crv']
assert token_prices[1][1] > 0

def test_tokens_prices_normalized_usdc(prices_helper, token_addresses):
'''
Solidity (returns):
struct Tokens {
address tokenId;
uint256 amount;
}

Brownie:
List[(tokenId, priceUsdc)]

refer to Oracle.sol:70
usdc decimals: 6
erc20 decimals: 18
normalization is based on usdc decimals
use 18-6 = 12 decimals for erc20 testing
'''
token_prices = prices_helper.tokensPricesNormalizedUsdc([
(token_addresses['usdc'], 1),
(token_addresses['crv'], 1e12)
])
assert token_prices[0][0] == token_addresses['usdc']
assert token_prices[0][1] > 0
assert token_prices[1][0] == token_addresses['crv']
assert token_prices[1][1] > 0