Skip to content

Instantly share code, notes, and snippets.

@5hanth
Created March 2, 2022 06:03
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 5hanth/537548a7186922104e3517b657b900d0 to your computer and use it in GitHub Desktop.
Save 5hanth/537548a7186922104e3517b657b900d0 to your computer and use it in GitHub Desktop.
Linky Pinky Ponky
library AppTypes {
struct ListItem {
string content;
}
struct List {
uint256 id;
uint256 listItemsCount;
ListItem[] listItems;
}
struct State {
mapping(LIST => uint256) id;
mapping(uint256 => List) lists;
}
enum Model {
LIST
}
}
import "./SVGList.sol";
import "./AppTypes.sol";
library Main {
using SVGList for AppTypes.List;
function newListWithContent(AppTypes.State storage currentState, string memory content) public returns(uint256 newListId) {
uint256 newListId = ++currentState.id[AppTypes.Model.LIST];
AppTypes.List storage newList = currentState.lists[newListId];
currentState.lists[currentState.id[AppTypes.Model.LIST]] = newList;
newList.id = newListId;
newList.addContent(content);
return newListId;
}
function addContentToList(AppTypes.State storage currentState, uint256 listId, string memory content) public {
AppTypes.List storage currentList = currentState.lists[listId];
currentList.addContent(content);
}
}
import "./Main.sol";
import "./AppTypes.sol";
contract Primary {
using Main for AppTypes.State;
AppTypes.State storage state;
function newListWithContent(string memory content) public returns(uint256 newListId) {
return state.newListWithContent(content);
}
function addContentToList(uint256 listId, string memory content) public {
state.addContentToList(listId, content);
}
function getContent(uint256 listId, uint256 index) public view returns (string memory) {
return state.getContent(listId, index);
}
function getId(AppTypes.Model model) public view returns (uint256) {
return state.id[model];
}
}
import "./AppTypes.sol";
library SVGList {
function addContent(AppTypes.List storage currentList, string memory content) public {
AppTypes.ListItem storage newListItem = currentList.listItems[++currentList.listItemsCount];
newListItem.content = content;
currentList.listItems[currentList.listItemsCount] = newListItem;
}
function getContent(AppTypes.List storage currentList, uint256 index) public view returns (string memory) {
return currentList.listItems[index].content;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment