[英]Is frontend better or backend for integration with smart contract (erc721)?
[英]Error while related to big number in react for input in smart contract erc721
我想将我的智能合约与 reactjs 集成。 我有一个名为 set_price() 的方法,我在其中输入令牌 ID (in uint256) 和 price (uint256) 的值。 TokenID 基本上是一些其他值的哈希,所以它是 byte32 类型的,我已经将它类型转换为 uint256。 现在,当我从反应输入值时,它显示以下错误:
我还在实例化 web3 的位置添加我的文件。 我试图解决的方法是 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 }); };
我们之所以使用大数,是因为 JS 不能直接处理大数……所以不能把“大数”作为整数传递。 BN 对象需要大数字作为字符串。 喜欢:
tokenid =
web3.utils.BN(
"4116505640912284550723191986264393474293570820512791522119157812802259305428"
);
这将解决断言错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.