简体   繁体   中英

ERC721 mint() returns 'invalid address' error

I'm trying to create ERC721 token in private network with JavaScript.

I could create ERC721 token through truffle console but failed through JavaScript.

truffle(development)> myToken.mint()

{ tx:'0xc1dc87a29fbe200ff180df67c01e454818feee433b13331c4ea9268624db077b', receipt: { blockHash: '0xf2ad0c70cda0efca3460ec74866ed61e77647493feb5edf2f81ad2a038c69956', blockNumber: 251489,...

The error message is

'UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: invalid address'

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 ?

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

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

and run npm install . Also we need a special hack to get Truffle using the version of the Solidity compiler we want (0.5.1):

(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:

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.

If you are lucky then run: npx truffle test and you should see

Using network 'test'.

Compiling ./contracts/Migrations.sol...


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


  2 passing (420ms)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM