Implementing CRUD for mapping type in Solidity

A Oommen
BCS Technology
Published in
2 min readJan 16, 2019

Working on decentralized applications built on Ethereum blockchain and Solidity, I have observed the need for a mapping library that can provide fine-grained CRUD like capabilities. The current state of the art does not allow for any iteration on mapping type.

Most of these applications would need to navigate the keys and values in the contract and be able to edit relevant entries. With accuracy being the primary driver of smart contract design in enterprise private chains and zero transaction costs, the need for Solidity libraries that can be re-used in our applications will be crucial to cut down development timelines.

This was what I was trying to make iterable

mapping< string => mapping< string => string > >

The Ethereum foundation has a great library that solves the same for a simple data structure

mapping < uint => uint >

https://github.com/ethereum/dapp-bin

I have forked from this and adapted it for my requirement. I am sharing the gist for the final libraries here.

  1. The first one is for a string to string mapping (stringMap) which was then embedded as value in the next one

iter_mapping_str2str < string => string >

The second one is the top-level mapping that includes the previous one as part of its value field

iter_mapping< string => iter_mapping_str2str >

--

--