Solidity
在依賴於參數的查找後,成員“concat”未找到或不可見
我正在嘗試連接一系列字元串,但出現以下錯誤:
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
但是 my
concat()
在我呼叫它之前就已經定義好了。可能是什麼問題?或者我所說的方式不是有效的 Solidity 語法?//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))); } }
它失敗了,因為編譯器正在尋找
concat
作為類型成員的函式bytes
。您可以將 concat 用作正常函式
return string(concat(bytes(api0xUrl), concat(bytes(sellStr), ...)));
另一種選擇是將 concat 移動到庫並使用該
using for
機制。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)
現在它應該按預期工作
return string(bytes(api0xUrl) // ---> no error here .concat(bytes(sellStr))