代码之家  ›  专栏  ›  技术社区  ›  raimtoon

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

  •  0
  • raimtoon  · 技术社区  · 6 年前

    我可以通过truffle控制台创建ERC721令牌,但通过JavaScript失败了。

    {发送:'0xc1dc87a29fbe200ff180df67c01e454818feee433b13331c4ea9268624db077b', 收据:{blockHash: 区块号:251489,。。。

    错误消息是

    'UnhandledPromiseEjectionWarning:未处理的promise拒绝(拒绝id:2):错误:无效地址'

    我的代码如下:

    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();
    }
    

    minting

    1 回复  |  直到 6 年前
        1
  •  0
  •   William Entriken    5 年前

    更新的代码

    下面是一个与代码一起工作的更新测试。

    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);
        });
    });
    

    建立块菌需要一堆堆样板。我们试试吧。

    mkdir tmp && cd tmp
    

    把这个放进你的包.json

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

    然后跑 npm install

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

    现在建立一个松露项目:

    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
    

    这是你的合同。保存到contracts/nf token-模拟索尔:

    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);
      }    
    }
    

    如果你运气好,那就跑: 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)