2

I'm a solidity beginner...

This

Chainlink.Request memory request = buildChainlinkRequest(clJobId, address(this), this.registerConfirm.selector);
request.add("repo", msg.sender);

says TypeError: Invalid type for argument in function call. Invalid implicit conversion from bytes20 to bytes memory requested.

The chainlink docs tell me that there are also the methods addBytes, addInt, addUint, addStringArray and setBuffer.

My first thought was to convert the address to a string but that doesn't seem to be so trivial in solidity. So I wonder how to handle this all in all.

Answers like this one confuse me because comments say it doesn't do what you'd expect.

2

You can pass the address as a string or uint256.

Here is a function that will convert your address to a string:

function addressToString(address _address) public pure returns (string memory _uintAsString) {
      uint _i = uint256(_address);
      if (_i == 0) {
          return "0";
      }
      uint j = _i;
      uint len;
      while (j != 0) {
          len++;
          j /= 10;
      }
      bytes memory bstr = new bytes(len);
      uint k = len - 1;
      while (_i != 0) {
          bstr[k--] = byte(uint8(48 + _i % 10));
          _i /= 10;
      }
      return string(bstr);
    }

Then just pass that to your Chainlink request:

Chainlink.Request memory request = buildChainlinkRequest(clJobId, address(this), this.registerConfirm.selector);
request.add("repo", addressToString(msg.sender));
3
  • 1
    Thanks a lot. I wasn't sure if I really need to have my own function or some library. – Markus Kottländer Aug 27 '20 at 15:44
  • There is probably a library that does it, and ideally in the future we'd like to see one. But right now things are moving so fast that they don't often stand the test of time. – Patrick Collins Aug 27 '20 at 18:04
  • Not working with 0.6.6. Its converting address to all numbers. Alphabets are missing. – Anupam Kumar Apr 10 at 18:57
1

This is exactly what You need!!! Return real address not numbers representation

Works with Solidity ^0.6.0

function addressToString(address _address) public pure returns(string memory) {
       bytes32 _bytes = bytes32(uint256(_address));
       bytes memory HEX = "0123456789abcdef";
       bytes memory _string = new bytes(42);
       _string[0] = '0';
       _string[1] = 'x';
       for(uint i = 0; i < 20; i++) {
           _string[2+i*2] = HEX[uint8(_bytes[i + 12] >> 4)];
           _string[3+i*2] = HEX[uint8(_bytes[i + 12] & 0x0f)];
       }
       return string(_string);
    }

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.