繁体   English   中英

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

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

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

我正在尝试使用 Web3.js 来使用 mint 函数,如下所示:

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

问题是,如果我将amount作为 JavaScript 数字/字符串传递,则合同将失败。 我尝试使用 web3.utils 和web3.eth.abi.encodeParameter('uint256',amount)中的 BigNumber。

该合约部署在 Harmony ONE pops 测试网上。

问题已解决。 我不得不这样称呼合同:

    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