首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从ERC20调用ERC721函数会引发一个错误

从ERC20调用ERC721函数会引发一个错误
EN

Ethereum用户
提问于 2022-01-13 07:40:45
回答 1查看 176关注 0票数 0

我在ERC721合同内打电话给ERC721 20。我在编译和迁移它时没有问题,但是当执行这一行代码时,元问询会抛出一个错误。

这是我的代码:

导致错误的行: Erc20Contract.transfer(nftInfo从….creator,10)

ERC721

代码语言:javascript
复制
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;

import "../openzeppelin-contracts/contracts/token/ERC721/IERC721.sol";
import "../openzeppelin-contracts/contracts/token/ERC721/ERC721.sol";
import "../openzeppelin-contracts/contracts/token/ERC721/IERC721Receiver.sol";
import "../openzeppelin-contracts/contracts/math/SafeMath.sol";
import "../openzeppelin-contracts/contracts/utils/Address.sol";
import "../openzeppelin-contracts/contracts/utils/Counters.sol";
import "./ERC20Token.sol";

contract NFTtoken is ERC721 {
    using SafeMath for uint256;
    using Address for address;
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;
    ERC20Token Erc20Contract;

    constructor(address tokenAddress) ERC721("NC NFT example", "NCNFT") {
        owner = msg.sender;
        decimals = 0;
        Erc20Contract = ERC20Token(tokenAddress);
    }
    function transferNFT(address from, address to, uint256 tokenId)  public returns (bool){
        transferFrom(from, to, tokenId);
        Erc20Contract.transfer(nftInfo[from].creator, 10);
        //^^^^^^^^^ this line is causing an errror. 
    }
}

ERC20

代码语言:javascript
复制
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
import "../openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import "../openzeppelin-contracts/contracts/math/SafeMath.sol";

contract ERC20Token is IERC20 {
    function transfer(address to, uint256 tokens) public override returns (bool) {
        balances[msg.sender] = SafeMath.sub(balances[msg.sender], tokens);
        balances[to] = SafeMath.sub(balances[to], tokens);
        emit Transfer(msg.sender, to, tokens);
        return true;
    }
}

2_deploy_contracts.js

代码语言:javascript
复制
const NFTtoken = artifacts.require("NFTtoken");
const ERC20token = artifacts.require("ERC20Token")
let _totalSupply = 20000;

module.exports = function(deployer) {
    deployer.deploy(ERC20token, _totalSupply, web3.utils.toWei('0.01', 'ether'), [web3.utils.asciiToHex('Rama'), web3.utils.asciiToHex('Nick'), web3.utils.asciiToHex('Jose')]); 
    deployer.deploy(NFTtoken, ERC20token.address);
};

错误:

代码语言:javascript
复制
MetaMask - RPC Error: [ethjs-query] 
while formatting outputs from RPC 
'{"value":{"code":-32603,"data":{"message":"VM Exception while processing transaction: revert",
"code":-32000,"data": {"0x18abb17dda4df83e94eb19c0db814e9d4d3ec65a5f96001c0597fb9e93f1a816":{"error":"revert","program_counter":4807,"return":"0x"},
"stack":"RuntimeError: VM Exception while processing transacti
on: revert\n    at Function.RuntimeError.fromResults (C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\lib\\utils\\runtimeerror.js:94:13)\n    at BlockchainDouble.processBlock (C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\lib\\blockchain_double.js:627:24)\n    at runMicrotasks (<anonymous>)\n    at processTicksAndRejections (internal/process/task_queues.js:93:5)","name":"RuntimeError"}}}}' {code: -32603, message: `[ethjs-query] while formatting outputs from RPC '{…/task_queues.js:93:5)","name":"RuntimeError"}}}}'`}

Uncaught (in promise) {code: -32603, message: `[ethjs-query] while formatting outputs from RPC '{…/task_queues.js:93:5)","name":"RuntimeError"}}}}'`, 
stack: '{\n  "code": -32603,\n  "message": "[ethjs-query] wh…gaeaoehlefnkodbefgpgknn/background-0.js:1:216902)'}
EN

回答 1

Ethereum用户

发布于 2022-01-13 13:58:26

假设您的NFTtoken有足够的ERC20令牌余额(当您将10个令牌从NFT契约传输到NFT创建者时),我会说您在ERC20契约上实现的transfer方法是不正确的。

当您将令牌传输到to地址时,您应该将金额addto余额,而不是减除它:

代码语言:javascript
复制
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
import "../openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import "../openzeppelin-contracts/contracts/math/SafeMath.sol";

contract ERC20Token is IERC20 {
    function transfer(address to, uint256 tokens) public override returns (bool) {
        balances[msg.sender] = SafeMath.sub(balances[msg.sender], tokens);
        balances[to] = SafeMath.add(balances[to], tokens);
        emit Transfer(msg.sender, to, tokens);
        return true;
    }
}
票数 2
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/118798

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档