Skip to content

Instantly share code, notes, and snippets.

@himanshupal
Created June 2, 2023 05:14
Show Gist options
  • Save himanshupal/6e668e5b3f73d5381de9b1f2b7460af0 to your computer and use it in GitHub Desktop.
Save himanshupal/6e668e5b3f73d5381de9b1f2b7460af0 to your computer and use it in GitHub Desktop.
Alternative solution for bytes memory to any data type conversion
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract MyToken is ERC20, ERC20Burnable, Pausable, Ownable {
constructor() ERC20("MyToken", "MTK") {}
function pause() public onlyOwner {
_pause();
}
function unpause() public onlyOwner {
_unpause();
}
function mint(address to, uint256 amount) public onlyOwner {
_mint(to, amount);
}
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal override whenNotPaused {
super._beforeTokenTransfer(from, to, amount);
}
function bitMask() public pure returns (uint256, bytes memory) {
uint256 mask = type(uint256).max >> (256 - 32);
return (mask, bytes(abi.encode(mask)));
}
function toBin(uint256 d) external pure returns (bytes memory) {
return abi.encode(d);
}
function timestamp() external view returns (uint256) {
return block.timestamp;
}
function price(uint256 index, uint256 precision)
external
pure
returns (
uint256,
uint256,
bytes memory
)
{
(uint256 mask, ) = bitMask();
uint256 unmasked = (534710868936698191551043727000124 >> (32 * index)) &
mask;
return (
unmasked,
((unmasked * 1e30) / precision),
abi.encode(unmasked)
);
}
function decode(uint256 _priceBits, uint256 tokensLength)
external
pure
returns (uint256[] memory)
{
uint256[] memory prices = new uint256[](8);
for (uint256 j; j < 8; ) {
uint256 index = j;
if (index >= tokensLength) {
return prices;
}
uint256 startBit = 32 * j;
(uint256 mask, ) = bitMask();
uint256 _price = (_priceBits >> startBit) & mask;
uint256 adjustedPrice = (_price * 1e30) / 1000;
prices[index] = adjustedPrice;
unchecked {
++j;
}
}
return prices;
}
function revertMessage(bytes calldata _errData)
external
pure
returns (string memory)
{
return abi.decode(_errData[4:], (string));
}
function str(bytes memory t) external pure returns (string memory) {
return string(t);
}
function getReservesData() external view returns (address aToken) {
(address asset, address pool) = (
0x4200000000000000000000000000000000000006,
0x794a61358D6845594F94dc1DB02A252b5b4814aD
);
bytes memory data = abi.encodeWithSignature(
"getReserveData(address)",
(asset)
);
(bool success, bytes memory returnData) = pool.staticcall(data);
require(success, "External call failed!");
return this.dec(returnData, 0x100, 0x120);
}
function dec(
bytes calldata data,
uint256 s,
uint256 e
) public pure returns (address) {
return abi.decode(data[s:e], (address));
}
function max() external pure returns (uint256) {
return type(uint256).max;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment