簡體   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