Skip to content

Instantly share code, notes, and snippets.

@emrecolako
Created April 4, 2022 21:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emrecolako/3ed0836fd05169bab6018d9d18822e2e to your computer and use it in GitHub Desktop.
Save emrecolako/3ed0836fd05169bab6018d9d18822e2e to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "https://github.com/0xsequence/sstore2/contracts/SSTORE2.sol";
contract LayersTest is Ownable, ReentrancyGuard {
struct Layer {
string name;
bytes hexString;
}
struct LayerInput {
string name;
bytes hexString;
uint8 layerIndex;
uint8 itemIndex;
}
uint256 private constant NUM_LAYERS = 9;
mapping(uint256 => Layer) [NUM_LAYERS] layers;
function setLayersOld(LayerInput[] calldata toSet) external onlyOwner { //2371714
for (uint16 i; i < toSet.length; ++i) {
layers[toSet[i].layerIndex][toSet[i].itemIndex] = Layer(toSet[i].name, toSet[i].hexString);
}
}
function setLayers(LayerInput[] calldata toSet) external onlyOwner{ //1948537
for (uint16 i; i < toSet.length; ++i) {
(string memory name,bytes memory hexString, uint8 layerIndex,uint8 itemIndex)=
abi.decode(
SSTORE2.read(
SSTORE2.write(abi.encode(toSet[i].name, toSet[i].hexString,toSet[i].layerIndex,toSet[i].itemIndex))),
(string,bytes,uint8,uint8)
);
layers[layerIndex][itemIndex]=Layer(name,hexString);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment