使用 javascript 调用 solidity 智能合约来铸造令牌似乎不起作用

Calling solidity smart contract with javascript to mint token does not seem to work

我有一个 newby ethereum 问题 -- 刚开始尝试了解以太坊开发环境。

我有一个部署到 Ropsten 的非常简单的 721 测试合同,我可以使用 REMIX 来使用它,它可以用于铸造和查看令牌余额 (tokenCounter) 等。

这是 'contract' : https://ropsten.etherscan.io/address/0x97E0175415cB7D758cFB0ffc27Be727360664B90

// SPDX-License-Identifier: MIT
pragma solidity 0.8.0;
 
import "@0xcert/ethereum-erc721/src/contracts/tokens/nf-token-metadata.sol";
import "@0xcert/ethereum-erc721/src/contracts/ownership/ownable.sol";

contract Icollect is NFTokenMetadata, Ownable {
 
 uint256 public tokenCounter;

  constructor() NFTokenMetadata () {
    nftName = "Icollect";
    nftSymbol = "ICOL";
    tokenCounter = 0;
  }
 
  function mint(string calldata _uri) external onlyOwner {
    super._mint(msg.sender, tokenCounter);
    super._setTokenUri(tokenCounter, _uri);
    tokenCounter = tokenCounter + 1;
  }
}

当我用这个测试 javascript 和 hardhat

在本地进行测试时,合约似乎适用于铸造代币
async function main() {

    const contract_address = "0x5FbDB2315678afecb367f032d93F642f64180aa3";      
    const Icollect = await ethers.getContractFactory("Icollect");
    const icollect = await Icollect.attach(contract_address);

    const mint_return = await icollect.mint("https://test.test");    
    console.log("mint returned: ", mint_return);
    
    console.log("owner:", await icollect.owner());
    console.log("owner:", await icollect.ownerOf(0)); 
    console.log("symbol:", await icollect.symbol());
    console.log("URI:", await icollect.tokenURI(0));
    console.log("token counter:", await icollect.tokenCounter());    
  }
  
  main()
    .then(() => process.exit(0))
    .catch(error => {
      console.error(error);
      process.exit(1);
    });
    

问题:当我尝试从网页生成令牌时,我似乎无法使 'mint' 交易正常工作(当我需要 gas 时)。但是,查看变量是有效的(例如合同的名称和所有者)。知道我在这里做错了什么。

const transaction = contract.methods.mint(NFT_URI);

我正在构建对象,然后对其进行签名然后发送。交易似乎有效,但我没有看到额外的令牌。

这是使用以下代码的 'mint' 交易示例之一:https://ropsten.etherscan.io/tx/0x6f3fc389355ffedfe135ac049837ac2d1a6eb6aad1dd10b055addfa70814e0fd 但是使用 REMIX 查询 'tokenCounter' 表明我从不增加计数。

document.getElementById('mintbutton').onclick = async function() {
        let NFT_URI = document.getElementById("nft_uri").value;                                            
        let contract_address = document.getElementById("contract_address").value;                                            
        const contract = await new web3.eth.Contract(abi, contract_address);  
        let account_address = document.getElementById("account_address").value;  
        const account = web3.eth.accounts.privateKeyToAccount(privateKey).address;        
        const transaction = contract.methods.mint(NFT_URI);
        let nonce_count = await web3.eth.getTransactionCount(account_address);        

        //build the transaction            
        const txObject = {
            nonce: nonce_count, 
            to: account_address,
            //value: web3.utils.toHex(21000),
            //gasLimit: web3.utils.toHex(1000000),
            gasPrice: web3.utils.toHex(web3.utils.toWei('10','gwei')),            
            gas: await transaction.estimateGas({from: account}),
            data: transaction.encodeABI()
        };

        const signed  = await web3.eth.accounts.signTransaction(txObject, privateKey);        
        const return_from_send = await web3.eth.sendSignedTransaction(signed.rawTransaction);
                        
        return_string = 
            "blockHash: " + return_from_send.blockHash + "<br>" +
            "blockNumber: <a href='https://ropsten.etherscan.io/block/"  + return_from_send.blockNumber + "'>" + return_from_send.blockNumber + "</a><br>" +
            "contractAddress: " + return_from_send.contractAddress + "<br>" +
            "cumulativeGasUsed: " + return_from_send.cumulativeGasUsed + "<br>" +            
            "from: <a href='https://ropsten.etherscan.io/address/" + return_from_send.from  + "'>" + return_from_send.from + "</a><br>" +
            "gasUsed: " + return_from_send.gasUsed + "<br>" +
            "status: " + return_from_send.status + "<br>" +
            "to:  <a href='https://ropsten.etherscan.io/address/" + return_from_send.to + "'>" + return_from_send.to + "</a><br>" +
            "transactionHash: <a href='https://ropsten.etherscan.io/tx/" + return_from_send.transactionHash + "'>" + return_from_send.transactionHash + "</a><br>" +
            "transactionIndex: " + return_from_send.transactionIndex + "<br>" +
            "type: " + return_from_send.type + "<br>"; 

        $('#mint_return').html(return_string);            
        
        var x = document.getElementById("showDIV3");        
        x.style.display = "block";

    }

Patrick - 这是 link 到 console.log https://imgur.com/XBQTAxT

解决方法:我在交易对象中输入了错误的地址。我有账户地址而不是合约地址。下面更正。

        //build the transaction            
        const txObject = {
            nonce: nonce_count, 
            to: contract_address, //NOT account_address,
            //value: web3.utils.toHex(21000),
            //gasLimit: web3.utils.toHex(1000000),
            gasPrice: web3.utils.toHex(web3.utils.toWei('10','gwei')),            
            gas: await transaction.estimateGas({from: account}),
            data: transaction.encodeABI()
        };

解决方法:我在交易对象中输入了错误的地址。我有账户地址而不是合约地址。下面更正。

//build the transaction            
    const txObject = {
        nonce: nonce_count, 
        to: contract_address, //NOT account_address,
        //value: web3.utils.toHex(21000),
        //gasLimit: web3.utils.toHex(1000000),
        gasPrice: web3.utils.toHex(web3.utils.toWei('10','gwei')),            
        gas: await transaction.estimateGas({from: account}),
        data: transaction.encodeABI()
    };