简体   繁体   English

使用 javascript 调用 Solidity 智能合约来铸造代币似乎不起作用

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

I have a newby ethereum question--just getting started and trying to understand the Ethereum dev environment.我有一个关于以太坊的新问题——刚刚开始并试图了解以太坊开发环境。

I have a very simple 721 test contract that I deployed to Ropsten and I can use REMIX to use it and it works to mint and to view balance of tokens (tokenCounter) etc.我有一个非常简单的 721 测试合约,我部署到 Ropsten,我可以使用 REMIX 来使用它,它可以用于铸造和查看代币余额(tokenCounter)等。

Here is the 'contract': https://ropsten.etherscan.io/address/0x97E0175415cB7D758cFB0ffc27Be727360664B90这是“合同”: 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;
  }
}

The contract seems to work for minting tokens when I test locally with this test javascript and hardhat当我使用此测试 javascript 和安全帽进行本地测试时,该合约似乎适用于铸造代币

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

The PROBLEM: When I try to mint a token from a web page I can't seem to get the 'mint' transaction to work (when I need gas).问题:当我尝试从 web 页面铸造令牌时,我似乎无法让“铸造”交易工作(当我需要汽油时)。 However, viewing variables works (like name and owner of contract).但是,查看变量是有效的(例如合同的名称和所有者)。 Any idea what I'm doing wrong here.知道我在这里做错了什么。

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

I'm building the object, then signing it and then sending it.我正在构建 object,然后对其进行签名然后发送。 The transaction seems to work but I don't see the additional token.交易似乎有效,但我没有看到额外的令牌。

Here is one of the example 'mint' transaction using code below: https://ropsten.etherscan.io/tx/0x6f3fc389355ffedfe135ac049837ac2d1a6eb6aad1dd10b055addfa70814e0fd But using REMIX to query 'tokenCounter' shows that I never increase the count.这是使用以下代码的示例“薄荷”交易之一: 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 - here is a link to the console.log https://imgur.com/XBQTAxT帕特里克 - 这是 console.log https://imgur.com/XBQTAxT的链接

Solution: I had the wrong address in the transaction object.解决方法:我在交易 object 中输入了错误的地址。 I had the account address and not the contract address.我有帐户地址而不是合同地址。 Correction below.下面更正。

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

Solution: I had the wrong address in the transaction object.解决方法:我在交易 object 中输入了错误的地址。 I had the account address and not the contract address.我有帐户地址而不是合同地址。 Correction below.下面更正。

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

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

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