2

I'm trying to get myself familiarized with ethereum. So I was going through a repo, and tried to rum the code :

function transferFundOnResolve(uint cid) private {
        // Could also be used: complainMap[cid]._status == Status.Resolved && 
        if(complainMap[cid]._reward > 0) {
            if(policeAccounts[complainSolver[cid]] != true) {
                complainSolver[cid].transfer( complainMap[cid]._reward );
                complainMap[cid]._reward = 0;
            }
            else {
                for(uint i=0; i<complainMap[cid]._contributors.length; i++) {
                    complainMap[cid]._contributors[i].transfer( complainMap[cid]._contAmount[i] );
                }
                complainMap[cid]._reward = 0;

            }
        }
    }

As it says TypeError: "send" and "transfer" are only available for objects of type "address payable", not "address".

So I modified and added payable() like :

function transferFundOnResolve(uint cid) private {
        // Could also be used: complainMap[cid]._status == Status.Resolved && 
        if(complainMap[cid]._reward > 0) {
            if(policeAccounts[complainSolver[cid]] != true) {
                complainSolver[cid].transfer( payable(complainMap[cid]._reward) );
                complainMap[cid]._reward = 0;
            }
            else {
                for(uint i=0; i<complainMap[cid]._contributors.length; i++) {
                    complainMap[cid]._contributors[i].transfer( payable(complainMap[cid]._contAmount[i]) );
                }
                complainMap[cid]._reward = 0;

            }
        }
    }

Now it says : ParserError: Expected primary expression.

How should I resolve this ?

I have pragma solidity ^0.5.16;

1
  • Please add the code where you declared variables. (Like complainMap and contributors)
    – Masoud jt
    Jun 11, 2020 at 2:53

2 Answers 2

1

The reason you got that error because there is no function name payable that take an integer value and return payable address. The parser doesn't recognise it and give you an error like that.

I don't know your complainMap struct but i can guess that in it contain contrivutor variable and it's type is address. In solidity version greater than 0.5.0 they add payable address (you can see more at here: What's the difference between 'address' and 'address payable'?). The transfer function can only be use on address payable not address so the solution is to change the variable type of contributor from address to address payable or cast it to address payable.

4
  • Can you please tell me, how to cast from address to address payable ?? And updating the question with my structure soon Jun 11, 2020 at 4:59
  • I found here : ethereum.stackexchange.com/a/64109/61100, that I can use payable like this Jun 11, 2020 at 5:17
  • 1
    @Maifee UI Asad To convert from address to address payable you need payable function which can only be use in solidity 0.6.0 and up. But you are using solidity 0.5.18, that mean it is impossible to convert from address to address payable. So the only way is to change variable address to address payable
    – haxerl
    Jun 11, 2020 at 6:02
  • hey @haxerl can you show some example code on how casting to address payable worked for you. Ive tried casting to address payable and still getting the same error. To reproduce error: do truffle test on github.com/iansamz/Simple-Bank-Solidity.git
    – Ian Samz
    Apr 21, 2021 at 23:01
1

Add this in your truffle-config.js

solc version: 0.6.0 and up will support payable(address)

module.exports = {
  // See <http://truffleframework.com/docs/advanced/configuration>
  // to customize your Truffle configuration!
  networks: {
    development: {
      host: "localhost",
      port: 7545,
      network_id: "5777",
      gas: 4600000
    }
  },
  compilers: {
    solc: {
      version: "0.6.0",    // Fetch exact version from solc-bin (default: truffle's version)
    }
  },
};

In your contract .sol use: pragma solidity ^0.6.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.

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