简体   繁体   English

如何使用 web3.js 将数字作为 uint256 传递给 Solidity 函数

[英]How to pass number as uint256 to Solidity function using web3.js

I have a contract in Solidity :我在 Solidity 有一份合同:

function mint(uint256 quantity_) public payable {
        require(isPublicMintEnabled, 'minting not enabled');
        require(msg.value == quantity_ * mintPrice, 'wrong mint value');
        require(totalSupply + quantity_ <= maxSupply, 'sold out');
        require(walletMints[msg.sender] + quantity_ <= maxPerWallet, 'exceed max wallet');

        
        for (uint256 i = 0; i < quantity_; i++) {
        uint256 newTokenId = totalSupply + 1;
        totalSupply++;
        _safeMint(msg.sender, newTokenId);
        }
    }

I am trying to use the mint function using Web3.js like so:我正在尝试使用 Web3.js 来使用 mint 函数,如下所示:

  contract.methods.mint(amount).send({ from: state.connectedWallet })

The problem is that if I pass the amount as a JavaScript Number/String the contract fails.问题是,如果我将amount作为 JavaScript 数字/字符串传递,则合同将失败。 I tried using BigNumber from web3.utils and web3.eth.abi.encodeParameter('uint256',amount) .我尝试使用 web3.utils 和web3.eth.abi.encodeParameter('uint256',amount)中的 BigNumber。

The contract is deployed on Harmony ONE pops testnet.该合约部署在 Harmony ONE pops 测试网上。

The issue has been resolved.问题已解决。 I had to call the contract like so:我不得不这样称呼合同:

    const value = 1 * 1e18 // 1ONE
    const amount = 1
    contract.methods.mint(amount).send({ from: state.connectedWallet, value })

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

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