Created
September 24, 2022 11:09
-
-
Save mudgen/42e2208e9b6fd66d6c33a36528bbb9f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.17; | |
struct Data { | |
uint a; | |
uint b; | |
} | |
bytes32 constant LOCATION = keccak256("place"); | |
library fun { | |
function setA(uint _a) external { | |
Data storage ds = diamondStorage(); | |
ds.a = _a; | |
} | |
function getA() external view returns (uint) { | |
Data storage ds = diamondStorage(); | |
return ds.a; | |
} | |
function diamondStorage() internal pure returns (Data storage ds) { | |
bytes32 position = LOCATION; | |
assembly { | |
ds.slot := position | |
} | |
} | |
} | |
interface ifun { | |
function setA(uint _a) external; | |
function getA() external view returns (uint); | |
} | |
contract A { | |
address libAddress = 0xd9145CCE52D386f254917e481eB44e9943F39138; | |
function setA(uint _a) external { | |
(bool success, bytes memory data) = libAddress.delegatecall(abi.encodeCall(ifun.setA, (_a))); | |
} | |
function getA() external returns (uint) { | |
(bool success, bytes memory data) = libAddress.delegatecall(abi.encodeCall(ifun.getA, ())); | |
return abi.decode(data, (uint)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment