Skip to content

Instantly share code, notes, and snippets.

@mudgen
Created September 24, 2022 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mudgen/42e2208e9b6fd66d6c33a36528bbb9f6 to your computer and use it in GitHub Desktop.
Save mudgen/42e2208e9b6fd66d6c33a36528bbb9f6 to your computer and use it in GitHub Desktop.
// 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