【发布时间】: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 一起使用,但从未发生过转移......
【问题讨论】:
-
你能
await、donation.sendDonation并写在 try/catch 块中吗
标签: typescript ethereum solidity hardhat ganache