Holding USDC address => balances in a smart contract

I am trying to develop a smart contract that can take USDC deposits from an EOA and keep track of address balances etc…

I am following along the Ethereum 201 course which has a Dex contract that seems like a good template to follow but I am not sure how to define a data structure that holds USDC tokens from testnet. Can i just define a function like this on my contract?

function depositUsdc() external payable {
        balances[msg.sender][bytes32("USDC")] = balances[msg.sender][
            bytes32("USDC")
        ].add(msg.value);
    }

@filip

Hi @Emmett

function depositUsdc() external payable {
        balances[msg.sender][bytes32("USDC")] = balances[msg.sender][
            bytes32("USDC")
        ].add(msg.value);
    }

Erc 20 tokens are just numbers in a mapping, therefore your function should not be payable and will not have a msg.value.
You will have to use the transferFrom function in the USDC contract (with IERC20) and then add the balance to the mapping.

Check this out -> https://academy.ivanontech.com/products/ethereum-smart-contract-programming-201/categories/4821918/posts/16224667

Cheers,
Dani

how would I go about implementing a version of the USDC contract for use within truffle ‘development’ environment ? is there some smart simple way ?

Hi @Emmett

Filips does it in the new Solidity 201 course, although he call its token “LINK” you can just name it “USDC”.
Basically you create a contract that inherits the ERC20 contract from openzeppelin nothing more :slight_smile:

Filip contract: https://github.com/filipmartinsson/dex/blob/master/contracts/tokens.sol

Cheers,
Dani

1 Like

yeah I didnt something similar:

contract TestUsdc is ERC20 {
    constructor() ERC20 ("Test Usdc", "USDC") public {
        _mint(msg.sender, 1000000);
    }
}

then in my migration I did:

switch(network){
    case "develop":
      await deployer.deploy(TestUsdc);  
      usdcContract = await TestUsdc.deployed();
      await wallet.addToken(web3.utils.fromUtf8(await usdcContract.symbol()), usdcContract.address, {from: accounts[0]})
      
      break;
    case "ropsten":
      let usdcAddress = '0x07865c6e87b9f70255377e024ace6630c1eaa37f' 
      let abi = [{"an...]
      
      usdcContract = await new web3.eth.Contract(abi,usdcAddress)
      await wallet.addToken(web3.utils.fromUtf8(await usdcContract.methods.symbol().call()), usdcContract._address, {from: accounts[0]})
..
..