[英]Error while related to big number in react for input in smart contract erc721
I want to integrate my smart contract with reactjs.我想将我的智能合约与 reactjs 集成。 I have this method called set_price() where i enter the value of token ID (in uint256) and price (uint256) .我有一个名为 set_price() 的方法,我在其中输入令牌 ID (in uint256) 和 price (uint256) 的值。 TokenID is basically a hash of some other values so it was of type byte32 and i have typecasted it into uint256. TokenID 基本上是一些其他值的哈希,所以它是 byte32 类型的,我已经将它类型转换为 uint256。 Now that when i enter the value from react it shows the following error:现在,当我从反应输入值时,它显示以下错误:
I have also added this line of code where I am trying to convert into big number我还在尝试转换为大数字的地方添加了这行代码
and now i get this error:现在我得到这个错误:
I am also adding my file where I have instantiated web3.我还在实例化 web3 的位置添加我的文件。 The method that I am trying to solve is set_price.我试图解决的方法是 set_price。
import Web3 from 'web3'; import TokenContract from './token.json'; let selectedAccount; let tokenid; let tokenContract; let isInitialized = false; export const init = async () => { let provider = window.ethereum; // if (typeof provider !== 'undefined') { //metamask is installed provider .request({ method: 'eth_requestAccounts' }) .then((accounts) => { selectedAccount = accounts[0]; console.log(`selected account is ${selectedAccount}`); }) .catch((err) => { console.log(err); return; }); window.ethereum.on('accountsChanged', function (accounts) { selectedAccount = accounts[0]; console.log(`Selected account changed to ${selectedAccount}`); }); } const web3 = new Web3(provider); tokenid = web3.utils.BN( 4116505640912284550723191986264393474293570820512791522119157812802259305428 ); const networkId = await web3.eth.net.getId(); tokenContract = new web3.eth.Contract( TokenContract.abi, TokenContract.networks[networkId].address ); isInitialized = true; }; export const itembyS = async () => { if (!isInitialized) { await init(); } // console.log(nftContract.methods.name.call()); return tokenContract.methods .itemBySupplier(4, 1, 1, 1) .send({ from: selectedAccount }); }; export const set_price = async () => { // var tokenid= BigNumber.from(4116505640912284550723191986264393474293570820512791522119157812802259305428); if (!isInitialized) { await init(); } // console.log(nftContract.methods.name.call()); return tokenContract.methods .itemForSale(tokenid, 200) .send({ from: selectedAccount }); }; export const showNum = async () => { if (!isInitialized) { await init(); } // const number = nftContract.methods.myFunction().call(); // console.log(number); return tokenContract.methods.enternum(1).send({ from: selectedAccount }); };
The reason why we use big numbers is that JS can't directly handle big numbers...so you can't pass the "big number" as the whole number.我们之所以使用大数,是因为 JS 不能直接处理大数……所以不能把“大数”作为整数传递。 The BN object expects the big number as a string. BN 对象需要大数字作为字符串。 Like:喜欢:
tokenid =
web3.utils.BN(
"4116505640912284550723191986264393474293570820512791522119157812802259305428"
);
That would solve the assertion error.这将解决断言错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.