1

I have created a contract that creates an ERC20 token with no supply, and then once a donation comes, I want to issue a receipt of the equivalent of that donation in USD.

So if they donate 10 MATIC, and its price is $0.8, my contract should mint 8 tokens, and then that new supply to the address passed through as an argument. They don't have value, they just serve as proof of donation.

This is what I have so far:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

contract DonationPolygon is ERC20, Ownable {
    address payable public recipient;

    constructor(address payable _recipient) ERC20("Co2 Dept Receipt", "Co2") {
        recipient = _recipient;
    }

    // The issue is here...
    function sendDonation(/*address deptAddress*/) external payable {
        recipient.transfer(msg.value);
        // _mint(deptAddress, msg.value * _getNativeCurrencyPrice());
    }

    // Is used as a placeholder for Chainlink
    function _getNativeCurrencyPrice() public pure returns (uint256) {
        return uint256(858700000000000000);
    }
}

In hardhat, I have the following code:

const [address, fund] = await ethers.getSigners()

    // Create contract
    const donation = (
      await createContract<DonationPolygon>('DonationPolygon', fund.address)
    ).connect(address)

    console.log(`Contract deployed to: ${donation.address}`)
    console.log('Fetching current native currency value...')

    console.log(await donation._getNativeCurrencyPrice())

    console.log('Sending donation...')

    // Get the current native currency value
    donation.sendDonation({
      value: ethers.utils.parseEther('5')
    })

    console.log('Donation sent!')

I successfully get the current price of MATIC, and it even works with Chainlink, but the transfer never occurs...

Herbie Vine
  • 1,643
  • 3
  • 17
  • 32

1 Answers1

-1

The answer is simple, I wasn't awaiting my function call... So just to iterate over what I changed:

const [address, fund] = await ethers.getSigners()

// Create contract
const donation = (
  await createContract<DonationPolygon>('DonationPolygon', fund.address)
).connect(address)

console.log(`Contract deployed to: ${donation.address}`)
console.log('Fetching current native currency value...')

console.log(await donation._getNativeCurrencyPrice())

console.log('Sending donation...')

// Get the current native currency value
[-] donation.sendDonation({
[+] await donation.sendDonation({
  value: ethers.utils.parseEther('5')
})

console.log('Donation sent!')
Herbie Vine
  • 1,643
  • 3
  • 17
  • 32