0

I put a getBalance function for a simulated Dex.sol contract and it was working before but now it just returns 0 when i try the function in sol.. even after sending a token to test.

I am sending 1 USDC to the Dex.sol contract (0x9F2D378b2778130e59C04A09A4B76dFF41878Fbf) that is deployed on the goerli network and you can see the 1 USDC being transferred into the contract (https://goerli.etherscan.io/address/0x9F2D378b2778130e59C04A09A4B76dFF41878Fbf#tokentxns).

The USDC token address is 0x9FD21bE27A2B059a288229361E2fA632D8D2d074 which can be seen from the transfer above.

This is the code for Dex.sol:

// contracts/FlashLoan.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.10;

import {IERC20} from "https://github.com/aave/aave-v3-core/blob/master/contracts/dependencies/openzeppelin/contracts/IERC20.sol";

contract Dex {
    address payable public owner;

    // Aave ERC20 Token addresses on Goerli network
    address private immutable daiAddress =
        0x75Ab5AB1Eef154C0352Fc31D2428Cef80C7F8B33;
    address private immutable usdcAddress =
        0x9FD21bE27A2B059a288229361E2fA632D8D2d074;

    IERC20 private dai;
    IERC20 private usdc;

    // exchange rate indexes
    uint256 dexARate = 90;
    uint256 dexBRate = 100;

    // keeps track of individuals' dai balances
    mapping(address => uint256) public daiBalances;

    // keeps track of individuals' USDC balances
    mapping(address => uint256) public usdcBalances;

    constructor() {
        owner = payable(msg.sender);
        dai = IERC20(daiAddress);
        usdc = IERC20(usdcAddress);
    }

    function depositUSDC(uint256 _amount) external {
        usdcBalances[msg.sender] += _amount;
        uint256 allowance = usdc.allowance(msg.sender, address(this));
        require(allowance >= _amount, "Check the token allowance");
        usdc.transferFrom(msg.sender, address(this), _amount);
    }

    function depositDAI(uint256 _amount) external {
        daiBalances[msg.sender] += _amount;
        uint256 allowance = dai.allowance(msg.sender, address(this));
        require(allowance >= _amount, "Check the token allowance");
        dai.transferFrom(msg.sender, address(this), _amount);
    }

    function buyDAI() external {
        uint256 daiToReceive = ((usdcBalances[msg.sender] / dexARate) * 100) *
            (10**12);
        dai.transfer(msg.sender, daiToReceive);
    }

    function sellDAI() external {
        uint256 usdcToReceive = ((daiBalances[msg.sender] * dexBRate) / 100) /
            (10**12);
        usdc.transfer(msg.sender, usdcToReceive);
    }

    function getBalance(address _tokenAddress) external view returns (uint256) {
        return IERC20(_tokenAddress).balanceOf(address(this));
    }

    function withdraw(address _tokenAddress) external onlyOwner {
        IERC20 token = IERC20(_tokenAddress);
        token.transfer(msg.sender, token.balanceOf(address(this)));
    }

    modifier onlyOwner() {
        require(
            msg.sender == owner,
            "Only the contract owner can call this function"
        );
        _;
    }

    receive() external payable {}
}

Obvioussly this is the getBalance function that should return a value of 1000000 which is equal to 1 * 10^6 USDC but it just returns 0 in remix:

function getBalance(address _tokenAddress) external view returns (uint256) {
    return IERC20(_tokenAddress).balanceOf(address(this));
}

What am i doing wrong? Everything was working fine before but i dont know what's gone wrong or am i an idiot. Thanks for any help!

i;m an idiot.. my remix environment was still RemixVM and not the injected.... funny how i realise that as soon as i ask the question after hours of no idea lol

1 Answer 1

0

It works fine for me. Maybe you used a wrong contract or a wrong usdc address.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.