Fetching an inputted contract address's token price

I’m looking to fetch the inputted tokenAddress's token price, from say a uniswap eth pair, and return it.

Any idea how to do this?

contract Initialise {
    using SafeMath for uint;

    mapping(address => userData) users;             // user address assigned to personal info sheet
    
    struct userData {
        uint256 _initialInstallmentAmount;           // the amount the user is initialising with
        uint256 _remainingInstallmentCount;          // a count of the remaining instalments to pay
        uint256 _debtAmount;                         // the amount of funds to repay in total
        uint256 _nextInstallmentDueDate;             // the block number the next instalment is due by, otherwise it is terminated
        bool _contractTermination;                   // if user doesn't pay before `_nextInstallmentDueDate`, contract is terminated 
    }

    function viewConversion (uint256 ethAmount, address tokenAddress) 
        external 
        view 
        returns (
            uint256 ethAmountInputted, 
            address tokenAdressInputted, 
            uint256 amountOfTokensConvertible
        ) {
            // fetch token address's price
            // div ethAmount by token price
            // return above.
    }

I assume it would be something like this:

function viewConversion (uint256 ethAmount, address tokenAddress) 
    external 
    view 
    returns (
        uint256 ethAmountInputted, 
        address tokenAdressInputted, 
        uint256 amountOfTokensConvertible
    ) {
        // call function of uniswap to get price of (tokenAddress);
        // uint256 convertibleAmount = tokenAddress.price.div(ethAmount)
        // return x, y, z
}
1 Like