2

I'm trying to concatenate a series of strings but I'm getting the following error:

TypeError: Member "concat" not found or not visible after argument-dependent lookup in bytes storage pointer.
  --> contracts/Swaper0x.sol:28:23:
   |
28 |         return string(bytes(api0xUrl)
   |                       ^ (Relevant source part starts here and spans across multiple lines).


Error HH600: Compilation failed

But my concat() is defined before I call it. What could be the issue? Or the way that I'm calling it is not valid Solidity syntax?

//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;


contract Swaper0x {

    string private api0xUrl = 'https://api.0x.org/swap/v1/quote';
    string private sellStr = '?sellToken=';
    string private buyStr = '&buyToken=';
    string private buyAmountStr = '&buyAmount=';
    

    function concat(bytes memory a, bytes memory b) internal pure returns (bytes memory) {
        return abi.encodePacked(a, b);
    }

    function getRequestSELLBUY(
        string memory _sellToken, 
        string memory _buyToken,
        string memory _buyAmount
    ) internal view returns (string memory) {
           
        return string(bytes(api0xUrl) // ---> error starts here
            .concat(bytes(sellStr))
            .concat(bytes(_sellToken))
            .concat(bytes(buyStr))
            .concat(bytes(_buyToken))
            .concat(bytes(buyAmountStr))
            .concat(abi.encodePacked(_buyAmount)));
    }
}

1 Answer 1

2

It fails because the compiler is looking for the concat function as a member of bytes type.

You can use concat as a regular function

return string(concat(bytes(api0xUrl),
                     concat(bytes(sellStr), 
                            ...)));

Another alternative is move concat to a library and use the using for mechanism.

library L {
    function concat(bytes memory a, bytes memory b) internal pure returns (bytes memory) {
        return abi.encodePacked(a, b);
    }
}

contract Swaper0x {
    using L for bytes;  // X.concat(Y) => concat(X, Y)

Now it should work as expected

return string(bytes(api0xUrl) // ---> no error here
        .concat(bytes(sellStr))
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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