【问题标题】:Solidity .transfer() function not forwarding fundsSolidity .transfer() 函数不转发资金
【发布时间】:2022-12-17 16:51:14
【问题描述】:

我已经创建了一个合同,它创建了一个没有供应的 ERC20 代币,然后一旦捐款到来,我想开一张等值于该捐款的美元收据。

因此,如果他们捐赠 10 个 MATIC,其价格为 0.8 美元,我的合约应该铸造 8 个代币,然后该地址的新供应作为参数传递。它们没有价值,它们只是作为捐赠的证明。

这是我到目前为止所拥有的:

// 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);
    }
}

在安全帽中,我有以下代码:

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!')

我成功获得了 MATIC 的当前价格,它甚至可以与 Chainlink 一起使用,但从未发生过转移......

【问题讨论】:

  • 你能awaitdonation.sendDonation 并写在 try/catch 块中吗

标签: typescript ethereum solidity hardhat ganache


【解决方案1】:

答案很简单,我没有在等待我的函数调用......所以只是为了迭代我所做的更改:

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!')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-20
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    • 2021-10-08
    • 2014-12-16
    • 1970-01-01
    • 2022-12-17
    相关资源
    最近更新 更多