1

I have made a simple ERC20 token smart contract, but my transferFrom function does not work. I keep getting "Note: The constructor should be payable if you send value." even if I declared it as payable.

function transferFrom(address from, address to, uint256 value) payable public returns(bool success) {
  require(
    allowed[from][msg.sender] >= value
    && balances[from] >= value
    && value >= 0
  );
  balances[from] -= value;
  balances[to] += value;
  allowed[from][msg.sender] -= value;
  Transfer(from, to, value);
  return true;
}

Picture of my code

  • When do you get this error? You probably need at least three transactions to make it work: one to deploy contract, another to approve some value, and the third one to execute transferFrom. – Mikhail Vladimirov Apr 15 at 13:53
0

I guess your new to solidity. There might be two issues might be calling function and requirements. First let me explain payable function. When you declare a function as payable that means transation will send ethers to that function.

How to call payable function,

contractObj.sampleFunction.sendTransaction(ar1, arg2,{from: <FROM>, value:<Eth_in_wei>});

If your not calling function with eth transaction it will throw an error.Note: The constructor should be payable if you send value

And coming to your qus:

transferFrom() function will check user A has given permission for auto debit for B address with X number of tokens or not? If yes then it will deduct tokens from A and credited to B address

First user A has to call approve() function with B address and number of tokens. In approve method your directly allocating X number of tokens to B.

So you have to call approve() then, after some time or 3rd party contract will call transferFrom() method with address A, address B and Y tokes.

When you want to save storage variables in solidity you need to call sendTransaction() with {from: , gas: }, If function is payable then value key will be part.

Please refer below link's for ref:

payable() function In solidity

https://theethereum.wiki/w/index.php/ERC20_Token_Standard

https://medium.com/@jgm.orinoco/understanding-erc-20-token-contracts-a809a7310aa5

0

If you are using transfer, your function is initiating a form of payment. Payable is to designate which function responds to a payment of ONLY ETHER when getting ether to the contract.

You need to remove the transfer call from your function.

Transfer(from, to, value);

It'll work then. Then issue a different payable function if you wish.

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.