Skip to content

Commit

Permalink
convert address to string
Browse files Browse the repository at this point in the history
  • Loading branch information
hujw77 committed Nov 20, 2024
1 parent 4674d31 commit 988f816
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/eco/XAPIOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ contract XAPIOracle is Verifier, IXAPIConsumer {
requestData._startNestedParam("variables");
{
requestData._addParamUint("chainId", chainId);
requestData._addParamBytes("channel", abi.encodePacked(channel));
requestData._addParam("channel", toHexString(channel));
requestData._addParamUint("msgIndex", msgIndex);
}
requestData._endNestedParam();
Expand Down Expand Up @@ -149,4 +149,24 @@ contract XAPIOracle is Verifier, IXAPIConsumer {
require(f != 0, "!fee");
return f;
}

// Inspired from https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.9.6/contracts/utils/Strings.sol
bytes16 private constant _SYMBOLS = "0123456789abcdef";
uint8 private constant _ADDRESS_LENGTH = 20;

function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
bytes memory buffer = new bytes(2 * length + 2);
buffer[0] = "0";
buffer[1] = "x";
for (uint256 i = 2 * length + 1; i > 1; --i) {
buffer[i] = _SYMBOLS[value & 0xf];
value >>= 4;
}
require(value == 0, "Strings: hex length insufficient");
return string(buffer);
}

function toHexString(address addr) internal pure returns (string memory) {
return toHexString(uint256(uint160(addr)), _ADDRESS_LENGTH);
}
}

0 comments on commit 988f816

Please sign in to comment.