简体   繁体   English

ERC721 mint()返回“无效地址”错误

[英]ERC721 mint() returns 'invalid address' error

I'm trying to create ERC721 token in private network with JavaScript. 我正在尝试使用JavaScript在专用网络中创建ERC721令牌。

I could create ERC721 token through truffle console but failed through JavaScript. 我可以通过松露控制台创建ERC721令牌,但通过JavaScript失败。

truffle(development)> myToken.mint() 松露(开发)> myToken.mint()

{ tx:'0xc1dc87a29fbe200ff180df67c01e454818feee433b13331c4ea9268624db077b', receipt: { blockHash: '0xf2ad0c70cda0efca3460ec74866ed61e77647493feb5edf2f81ad2a038c69956', blockNumber: 251489,... {tx:'0xc1dc87a29fbe200ff180df67c01e454818feee433b13331c4ea9268624db​​077b',收据:{blockHash:'0xf2ad0c70cda0efca3460ec74866ed61e77647493feb5edf2f81ad2a038c69956',块编号:251

The error message is 错误消息是

'UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: invalid address' 'UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:2):错误:无效的地址'

My code is like below: 我的代码如下:

var Web3 = require('web3');
var BigNumber = require('bignumber.js');
var contract = require("truffle-contract");
var contractJson = require("./build/contracts/MyToken.json");
var MyToken = contract(contractJson);
Web3.providers.HttpProvider.prototype.sendAsync = Web3.providers.HttpProvider.prototype.send;
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider(web3Provider));
MyToken.setProvider(web3.currentProvider);

MyToken.deployed().then(function(mytoken) {
    mytoken.mint();
}

Do I need something before the minting ? minting之前我需要一些东西吗?

Updated code 更新的代码

Here is an updated test that works with your code. 这是与您的代码一起使用的更新测试。

const NFToken = artifacts.require('NFTokenMock');

contract('NFTokenMock', (accounts) => {
    let nftoken;
    const id1 = 1;
    const id2 = 2;
    const id3 = 3;
    const id4 = 40000;

    beforeEach(async () => {
      nftoken = await NFToken.new();
    });

    it('returns correct balanceOf after mint', async () => {
      await nftoken.mint(accounts[0], id1);
      const count = await nftoken.balanceOf(accounts[0]);
      assert.equal(count.toNumber(), 1);
    });
});

Try it 试试吧

Setting up Truffle requires piles of boilerplate. 设置松露需要一堆样板。 Let's try it. 让我们尝试一下。

mkdir tmp && cd tmp

Then put this in your package.json 然后将其放入您的package.json

{
  "dependencies": {
    "@0xcert/ethereum-erc721": "^2.0.0-rc1",
    "truffle": "^5.0.2",
    "web3": "^1.0.0-beta.37"
  }
}

and run npm install . 并运行npm install Also we need a special hack to get Truffle using the version of the Solidity compiler we want (0.5.1): 我们还需要一个特殊的技巧来使用所需的Solidity编译器版本(0.5.1)来获得Truffle:

(cd node_modules/truffle && npm install solc@0.5.1)

Now set up a Truffle project: 现在设置一个松露项目:

mkdir contracts
echo > contracts/Migrations.sol <<EOL
pragma solidity ^0.5.1;
// Boilerplate required, https://github.com/trufflesuite/truffle/issues/1382
contract Migrations {}
EOL
mkdir Migrations
echo > Migrations/1.js <<EOL
// Boilerplate required, https://github.com/trufflesuite/truffle/issues/1382
module.exports = ($)=>{};
EOL

Here is your contract. 这是您的合同。 Save it to contracts/nf-token-mock.sol: 将其保存到contracts / nf-token-mock.sol:

pragma solidity 0.5.1;

import "@0xcert/ethereum-erc721/src/contracts/tokens/nf-token.sol";
import "@0xcert/ethereum-erc721/src/contracts/ownership/ownable.sol";

/**
 * @dev This is an example contract implementation of NFToken.
 */
contract NFTokenMock is
  NFToken,
  Ownable
{

  /**
   * @dev Mints a new NFT.
   * @param _to The address that will own the minted NFT.
   * @param _tokenId of the NFT to be minted by the msg.sender.
   */
  function mint(
    address _to,
    uint256 _tokenId
  )
    external
    onlyOwner
  {
    super._mint(_to, _tokenId);
  }    
}

And save the above test file to test/go.js. 并将以上测试文件保存到test / go.js。

If you are lucky then run: npx truffle test and you should see 如果幸运的话,请运行: npx truffle test ,您应该会看到

Using network 'test'.

Compiling ./contracts/Migrations.sol...


  Contract: NFTokenMock
    ✓ correctly checks all the supported interfaces (55ms)
    ✓ correctly approves account (156ms)


  2 passing (420ms)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 erc721/erc20 代币之间共享 ABI? - Sharing ABI between erc721/erc20 Tokens? 前端更好还是后端与智能合约(erc721)集成? - Is frontend better or backend for integration with smart contract (erc721)? 如何使用node.js检查opensea上的令牌是ERC721还是ERC1155 - How to check if the token on opensea is ERC721 or ERC1155 using node.js 对于有效的电子邮件地址正则表达式,NodeJS返回无效 - NodeJS returns invalid for valid email address regex 错误:ChatConnector:startConversation-地址无效 - Error : ChatConnector: startConversation - address is invalid 错误:来自电子邮件地址无效 - SendGrid - Error: Invalid from email address - SendGrid 未捕获的错误:u处的无效地址(web3.min.js:1) - Uncaught Error: invalid address at u (web3.min.js:1) ng 命令返回“未知错误:错误:无效的 JSON 字符:0:0 处的“a”” - ng command returns "Unknown Error : Error : Invalid JSON character: "a" at 0:0" NodeJS-请求库发出的请求返回[错误:无效协议:127.0.0.1:] - NodeJS - requests made by request library returns [Error: Invalid protocol: 127.0.0.1:] Spotify API 客户端凭据返回“错误”:“无效客户端” - Spotify API Client Credentials returns "error": "invalid_client"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM