I am writing a smart contract that uses USDC when a NFT is purchased (so the USDC is transferred from the seller to the buyer).
I am trying to test it in Hardhat but I am having troubles when using USDC on my hardhat tests. I forked the goerli testnet and impersonated by wallet has it has USDC in goerli.
When I do an NFT purchase with my account, it revert with an error "ERC20: transfer amount exceeds balance" so I am trying to display the USDC balance to understand where the problem is, but the USDC balance is returning 0, when I have in my account USDC.
What am I missing?
This is my solidity function:
function createMarketSale(uint256 tokenId)
public
{
uint priceUSD = idToMarketItem[tokenId].priceUSD;
address seller = idToMarketItem[tokenId].seller;
USDc.transfer(seller, priceUSD * 10 ** 6);
_transfer(idToMarketItem[tokenId].owner, msg.sender, tokenId);
idToMarketItem[tokenId].owner = msg.sender;
idToMarketItem[tokenId].sold = true;
_itemsSold.increment();
}
This is my hardhat test:
const USDC_ADDRESS = "0x98339D8C260052B7ad81c28c16C0b98420f2B46a";
const USDC_WHALE = "0x682...";
const ERC20ABI = require('./ERC20.json');
beforeEach(async () => {
[owner, addr1, addr2] = await ethers.getSigners();
const ArtworkContract = await ethers.getContractFactory("Artwork");
artworkcontract = await ArtworkContract.deploy();
// This should send USDC to addr1 but addr1 is not receiving USDC (or I cannot see it!)
await impersonateFundErc20(
USDC_ADDRESS,
USDC_WHALE,
addr1,
"50.0",
6
);
})
describe("MarketPlace", () => {
it("Should create an NFT", async function () {
const [owner, addr1, addr2] = await ethers.getSigners();
const whaleAddress = await ethers.getSigner(USDC_WHALE);
await artworkcontract.deployed();
await owner.sendTransaction({
to: whaleAddress.address,
value: ethers.utils.parseEther("1000.0"), // Send more ether to impersonated account
});
const USDC = new ethers.Contract(USDC_ADDRESS, ERC20ABI, ethers.provider);
USDCBalance = await USDC.balanceOf(USDC_WHALE);
console.log("USDCBalance: " + USDCBalance); // This is returning 0
await artworkcontract.connect(addr1).createToken("uri1","url1",2)
const allItems = await artworkcontract.fetchAllItems()
console.log("allItems:",allItems)
const ETHBalance = await ethers.provider.getBalance(USDC_WHALE);
console.log("ETHBalance: ",ETHBalance);
await artworkcontract.connect(whaleAddress).createMarketSale(1) // reverts with error ERC20: transfer amount exceeds balance
});