3

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 Answers2

4

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));
Patrick Collins
  • 11,186
  • 5
  • 44
  • 97
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);
    }