0

I've made an twitter contract, where we can send and get tweets from the frontend React. While sending the data to the contract, The metamask popup is opening, But it shows warning like this. enter image description here

I don't know why this is happening as I'm new to web3. My ABI data and other configurations are done well.

My code for sending the data is,

  const [loading, setLoading] = useState(false);
  const [tweetsData, setTweetsData] = useState([]);

  const createTweet = async () => {
    try {
      setLoading(true);
      const contract = new web3.eth.Contract(contractABI, contractAddress);

      await contract.methods.createTweet(content).send({
        from: accountId,
      });
      setLoading(false);
      setContent("");
      getAllTweets()
    } catch (error) {
      setLoading(false);
      console.error("ERROR", error);
    }
  };

The contract address and contractABI is correct as the fetching method from the contract is working fine. Only the sending method is throwing this error.

This is the contract code,

 
pragma solidity ^0.8.18;


contract Twitter {
    uint16 public MAX_LENGTH_LIMIT = 280;

    struct Tweet{
        uint256 id;
        address author;
        string content;
        uint256 timeStamp;
        uint256 likes;
    }

    mapping (address => Tweet[]) public tweets;
    
    address public owner;

    //Events creating:
    event TweetCreated(uint256 id, address author, string content, uint256 timeStamp);
    event TweetLiked(address liker, address tweetAuthor, uint256 tweetId, uint256 newLikeCount);
    event TweetUnLiked(address unLiker, address tweetAuthor, uint256 tweetId, uint256 newLikeCount);

    constructor() {
        owner = msg.sender;
    }


    modifier onlyOwner() {
        require(owner == msg.sender, "You are restricted since you;re not the owner.");
        _;
    }

    function changeTweetLength(uint16 tweetLength) public onlyOwner {
        MAX_LENGTH_LIMIT = tweetLength;
    }


    function createTweet(string memory _tweet) public {

        require(bytes(_tweet).length <= MAX_LENGTH_LIMIT, "Tweet is too long!");

        Tweet memory newTweet = Tweet({
            id: tweets[msg.sender].length ,
            author: msg.sender,
            content: _tweet,
            timeStamp: block.timestamp,
            likes: 0
        });

        tweets[msg.sender].push(newTweet);

        emit TweetCreated(newTweet.id, newTweet.author, newTweet.content, newTweet.timeStamp);
    }

function getTweet( uint _i) public view returns (Tweet memory) {
        return tweets[msg.sender][_i];
    }

    function getAllTweets(address _owner) public view returns (Tweet[] memory){
        return tweets[_owner];
    }
}```

Any help on this would be appreciated.
5
  • Can you provide the contract you are calling as well as the rest of the code for your function call?
    – Rohan Nero
    Sep 21 at 18:16
  • Without contract code it difficult to determine why the error is occurring. There must be error with the parameters set on contract function that has not been fulfilled thus giving an error not able to estimate gas in which case the transaction is likely to fail. Sep 21 at 21:46
  • @SagarAtalatti But the contract code is working in the remix environment while I'm testing it. I've also added the contract code above. Please look at it.
    – Santhosh
    Sep 22 at 5:27
  • have you checked the MAX_LENGTH_LIMIT of the tweet? Sep 22 at 7:19
  • @SagarAtalatti Yes. It didn't affect anything. Also the function is working fine in remix.
    – Santhosh
    Sep 22 at 7:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.