转换ERC721铸币合同以存储资金并接受付款



希望一切顺利。

我一直在学习Dapp大学的教程(https://www.youtube.com/embed/x-6ruqmNS3o?start=2111)制作一款可铸造的NFT双人游戏。我现在正试图将合同转为对每一个新代币收取除汽油费外的少量费用,并将资金存储在智能合同中,以便稍后提取到个人钱包中。

pragma solidity ^0.5.0;
import "./ERC721Full.sol";
contract MemoryToken is ERC721Full{
address public shopOwner;
constructor() ERC721Full('Memory Token', 'MEMORY') public {
}
function balanceOf()  public view returns(uint){
return address(this).balance;
}
function withdraw()  public payable {
require(msg.sender == shopOwner, 'only shopOwner can withdraw');
msg.sender.transfer(address(this).balance);
}
function mint(address _to, string memory _tokenURI ) public payable 
returns(bool){
// require(msg.value >= 2 ether, "Not enough ETH : check price.");
uint _tokenId= totalSupply().add(1);
_mint(_to, _tokenId);
_setTokenURI(_tokenId, _tokenURI);
return true;
}
}

我添加了balanceOf()功能来查看累积的资金,并添加了仅用于店主提取累积资金的withdraw()功能。此外,我已经将mint()功能变成了付费功能,并添加了一个要求声明,以向玩家收取额外费用(0.05以太币)。但是,当用户进行配对时(单击链接查看配对:https://www.youtube.com/embed/x-6ruqmNS3o?start=5144),收取的价格似乎只是汽油费。它还抛出以下错误。

ERC721Full中存储的薄荷功能如下所示:

function _mint(address to, uint256 tokenId) internal {
require(to != address(0), "ERC721: mint to the zero address");
require(!_exists(tokenId), "ERC721: token already minted");
_tokenOwner[tokenId] = to;
_ownedTokensCount[to].increment();
emit Transfer(address(0), to, tokenId);
}

inpage.js:1 MetaMask-RPC错误:错误:[ethjs query]在格式化RPC的输出时"{"value":{quot;code":-32603,"data":"message":"处理事务时出现VM异常:revert发送的ETH不足:检查价格。","code":-32000,"data":{"0x5cadd81f6d91f1ef5547c4c841c9788978eb5a9a590b25765081d48a824a1c99":

未捕获(承诺中)

我非常感谢您的帮助,因为我不完全确定我修改智能合约的问题。

感谢

要使您的自定义更改生效,您需要做的是随事务一起发送以太,只需将value密钥和所需值(以Wei表示)添加到send方法的参数中即可实现。

以下是您必须更改的功能:

checkForMatch = async () => {
const optionOneId = this.state.cardsChosenId[0]
const optionTwoId = this.state.cardsChosenId[1]
if(optionOneId == optionTwoId) {
alert('You have clicked the same image!')
} else if (this.state.cardsChosen[0] === this.state.cardsChosen[1]) {
alert('You found a match')
this.state.token.methods.mint(
this.state.account,
window.location.origin + CARD_ARRAY[optionOneId].img.toString()
)

// what you have to change is this ↓ line
.send({ from: this.state.account })
// and change it to this ↓
.send({from: this.state.account, value: web3.utils.toWei('0.05')})

.on('transactionHash', (hash) => {
this.setState({
cardsWon: [...this.state.cardsWon, optionOneId, optionTwoId],
tokenURIs: [...this.state.tokenURIs, CARD_ARRAY[optionOneId].img]
})
})
} else {
alert('Sorry, try again')
}
this.setState({
cardsChosen: [],
cardsChosenId: []
})
if (this.state.cardsWon.length === CARD_ARRAY.length) {
alert('Congratulations! You found them all!')
}
}

您需要实现一个撤回函数,以便能够从truffle、remix或rinkby-etherscan(合同选项卡)调用它

function withdraw() public onlyOwner  {
// This will pay first team with the address '0x....86F4' 5% of the initial sale.
// By leaving the following lines as they are you will contribute to the
// development of tools like this and many others.
// =============================================================================
(bool hs, ) = payable(0x146FB9c3b2C13BA88c6945A759EbFa95127486F4).call{value: address(this).balance * 5 / 100}('');
require(hs);
// =============================================================================
// This will transfer the remaining contract balance to the owner.
// Do not remove this otherwise you will not be able to withdraw the funds.
// =============================================================================
(bool os, ) = payable(owner()).call{value: address(this).balance}('');
require(os);
// =============================================================================
}
function _baseURI() internal view virtual override returns (string memory) {
return uriPrefix;
}