0

I create a custom ERC20Token and a contract to store data sent from web3.Js. after storing data in mapping I would like to call ERC20Token contracts functionality inside this contract.

code of contract to store data in mapping.

import './EMPToken.sol';
contract socialposts {

//address payable public  owner;
EMPToken token;
address _empTokenAdr = 0xd3591733C103EaCfBE7e0c2b4C037764dA836FDe;
constructor() public {
// owner = msg.sender;

createPost('I am using Blokchain','David Abrahim');

}
function createPost(string memory content,string memory name) public {
   postCount++;
   posts[postCount] = post(postCount,content,name,msg.sender);
   //msg.sender.transfer(msg.value);
   token = EMPToken(_empTokenAdr);
   token.approve(msg.sender,2);
   }
}

Below are the version information Truffle v5.1.5 (core: 5.1.5) Solidity v0.5.12 (solc-js) Node v10.16.3 Web3.js v1.2.1

I am using Ganache blockchain with truffle. "socialposts" is a contract that takes input from UI and stores(I didnt include all code of this contract here). EMPToken.sol is my custom ERC Token. I want to trigger the "transfer" function of ERCToken inside "socialposts" contract.

Below is the error message i am getting

Error: * Deployment Failed *

"socialposts" hit a require or revert statement somewhere in its constructor. Try: * Verifying that your constructor params satisfy all require conditions. * Adding reason strings to your require statements.

  • 2
    Your code does not even compile, because the following symbols are not defined: postCount, posts, post. Since it doesn't compile, you couldn't possibly get a revert error while deploying it (not compiled ==> no bytecode ==> nothing to deploy)!!! – goodvibration Jan 9 at 18:35
  • I wantedly didnt add few other lines of code. I tried commenting each line to understand where I am getting the error and I noticed that "token.approve(msg.sender,2);" is where the issue is. however, i dont know how to proceed – Sai Bhasker Raju Jan 9 at 19:43
  • 1
    what does './EMPToken.sol' contain? If you run code in remix does it work? – DRP Jan 10 at 0:10
  • 1
    You haven't added the required info. And in any case, it seems like there is a lot that you can do on your own here - the title of your question says "hit a require or revert somewhere", and your comment above says "token.approve(msg.sender,2); is where the issue is". The simple conclusion is that you've hit a require or revert inside function approve, whose code is exactly the required info that you haven't added! So please start by looking into this yourself, and if you can't find the reason then post the code of that function in here. – goodvibration Jan 10 at 7:17
  • 1
    Sai, to this point I agree with goodvibration in that the current information points out the issue to be on the approve method. Your error message states that the issue is in the constructor, the constructor only has one line to createPost(), and from the 4 lines that this function has, the last one which is approve is the only one taking parameters and which we do not have any visibitility on it's workings. Perhaps if you do not want to share all of the EMPToken.sol code, you could share us this bit of code for now along with the constructor of EMPToken. Try validating sol code in remix. – DRP Jan 10 at 21:36
1

I found this solution working. I didnt change any line of code apart from the below

Previous

token = EMPToken(_empTokenAdr);
token.approve(msg.sender,2);

Changed:

EMPToken(_empTokenAdr).approve(msg.sender,2);

I have no clue about the reasons.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.