Working with Michelson Maps in Taquito
Tags: tezos, smartcontracts, cryptocurrency, blockchain
Published: 2020-12-02 07:10:35

---
title: Working with Michelson Maps in Taquito
published: true
date: 2020-11-30 11:52:53 UTC
tags: tezos,smartcontracts,cryptocurrency,blockchain
canonical_url: https://medium.com/coinmonks/working-with-michelson-maps-in-taquito-8d8be9930662
---

#### Unlock the power of Michelson maps with Taquito

![](https://cdn-images-1.medium.com/max/1024/1*Y5L7NrFR4yViHmLOGM9GbQ.jpeg)<figcaption>Illustration from <a href="https://pixabay.com/photos/blogging-blogger-office-business-336375/">Pixabay</a></figcaption>

One of the most attractive features of smart contracts is the possibility to store a substantial amount of data into them that can then be used in the contract code. Although Michelson provides different structures to store data, the object of this article is going to be its maps. Maps are hash tables that contain key/value pairs, which means that when you want to find a value in a map, you search for its key. This allows you to store complex data that can be referenced with a single word or number or even more complex data like a pair!

Unlike big maps, all the values in a map are deserialized, which allows developers to have access to all of them at once. While maps become more expensive to use when the number of key/value pairs increases, they are well-suited for smaller databases because of the extra features Michelson (like mapping or folding) and Taquito offer on maps.

Taquito reads maps in the storage of smart contracts and translates them into an [instance of the](https://tezostaquito.io/typedoc/classes/_taquito_michelson_encoder.michelsonmap-1.html)[MichelsonMap class](https://tezostaquito.io/typedoc/classes/_taquito_michelson_encoder.michelsonmap-1.html). The class and its instances expose different features that give developers a lot of flexibility to use Michelson maps in their dapps. These features can be grouped into four groups:

- _The instantiation_: there are three different ways of creating a new MichelsonMap in Taquito
- _The general methods_: they give you information about the map, for example, its size or the elements it contains
- _The key/value methods_: they allow you to manipulate the keys and values in the map
- _The update methods_: they transform the map itself, for example by deleting elements or clearing out the map completely.

This tutorial is based on a [simple smart contract deployed on Delphinet](https://better-call.dev/delphinet/KT1FPjRutHDkdrLfLE6dRZw4sjtEJLQkgnxK/storage) with a map that contains addresses as keys and tez as values. We will use all the methods available in Taquito’s MichelsonMap to check the map, extract values and modify them!

If you want to see all these functions in action, you can have a look at [this Replit repo](https://repl.it/@claudebarde/TaquitoMichelsonMap#index.ts) and click on the “Run” button.

> _Note: as Taquito is written in TypeScript, we will also use TypeScript to interact with the contract storage._

### Loading the smart contract storage

This paragraph is a little reminder of how to use Taquito to fetch the storage of a smart contract:

![](https://cdn-images-1.medium.com/max/1024/1*VE4_8cFovsxfp54Nv9ZkwA.png)

The setup code is pretty straightforward:

1. We import TezosToolkit and MichelsonMap from the @taquito/taquito package. We also import BigNumber from bignumber.js (the library is installed by Taquito) as TypeScript will need it for this particular example.

2. We instantiate the TezosToolkit object with the RPC address.

3. We fetch the contract using await Tezos.contract.at(contractAddress).

4. We extract the storage from the contract using the storage method on the ContractAbstraction object that was created one line above. We also type the storage variable with the MichelsonMap type which requires 2 type arguments: the type for the key and the type for the value (the address is basically a string and the tez will be converted to a BigNumber by Taquito).

### Creating a MichelsonMap instance

Taquito provides three different ways of creating a new Michelson map: two of them can be used to create an empty map and the third one is used to create a map with default values.

The most simple way is to create an instance with no argument:

![](https://cdn-images-1.medium.com/max/1010/1*W34Sli4KxqSYApNGp_zm1w.png)

If you prefer, you can also pass an argument to the MichelsonMap constructor to indicate the type you want for the keys and the values:

![](https://cdn-images-1.medium.com/max/1024/1*XaWTfWgK5HfH8cRtX4_ijg.png)

Finally, you can also pass some values you want to create the instance with and let Taquito figure out the types using the fromLiteral static method:

![](https://cdn-images-1.medium.com/max/1024/1*6zXxQofo8Cs3ar_Uk6sKBg.png)

### The general properties and methods: isMichelsonMap, size, has and get

The first thing you may want to check after fetching the data from a contract storage is if the part of the storage that you expect to be a map is indeed a map. This can be achieved by using the isMichelsonMap static method on the MichelsonMap class:

![](https://cdn-images-1.medium.com/max/1024/1*JUB5QRgdFknSLQzyPFRbdQ.png)

> _Note: this is a static method so you can use it without creating a new instance of_ _MichelsonMap._

Once you are sure you are dealing with a map, you can check how many key/value pairs it holds with the size property:

![](https://cdn-images-1.medium.com/max/1024/1*boM-bvnV1jAc_RYKtsMRlg.png)

Sometimes, you don’t want to do anything with the values in a map but you want to verify whether a key appears in the map, you can then use the has method and pass it the key you are looking for:

![](https://cdn-images-1.medium.com/max/1024/1*1yyfwdoZWS1hXR_KCjiS4Q.png)

After that, you can fetch the value associated with the key you are looking for with the get method:

![](https://cdn-images-1.medium.com/max/1024/1*CNYKm_HtQX5x_T4aQoJJvQ.png)

### The key/value methods

One of the main advantages of maps over big maps is that the key/value pairs are readily available in your dapp without any extra step. If you are looking for a simple solution to loop over all the pairs and get the key and the value, the MichelsonMap instance exposes a forEach method that allows you to get these values:

![](https://cdn-images-1.medium.com/max/1024/1*F1hSu959ua4LThGbaMYJzQ.png)

The code above will output:

![](https://cdn-images-1.medium.com/max/1024/1*HScEa23bJKDk7-J7_YuP7Q.png)

The MichelsonMap instance exposes another method that will yield the same result, albeit in a different way. The entries method is a [generator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators) function that you can use if you wish to. This is how it works:

![](https://cdn-images-1.medium.com/max/1024/1*MRg9LvGylZlJmMuiOitYRQ.png)

This code will yield the exact same result as the one above. A generator may be preferable according to your use case.

The same idea is available for keys and values, the keys and values methods are generators that will allow you to loop over the keys or the values of the map:

![](https://cdn-images-1.medium.com/max/876/1*yuSsodJgz2U2JtdYX1V0pw.png)

This example will output the following array containing all the keys of the map:

```
[
    "tz1MnmtP4uAcgMpeZN6JtyziXeFqqwQG6yn6",
    "tz1R2oNqANNy2vZhnZBJc8iMEqW79t85Fv7L",
    "tz1VSUr8wwNhLAzempoch5d6hLRiTh8Cjcjb",
    "tz1aSkwEot3L2kmUvcoxzjMomb9mvBNuzFK6"
]
```

Similarly, you can use values instead of keys to output some or all the values in the map:

![](https://cdn-images-1.medium.com/max/942/1*CYmJIdqdjQTMhQ-T0PbJig.png)

This will output all the values of the map inside an array:

```
[
    789000000,
    912000000,
    123000000,
    456000000
]
```

### The update methods

Although reading and organizing the keys or the values fetched from a Michelson map is a common use case, you may also want to modify a map, for example, before originating a new contract. Taquito also thought about it and provides different methods that will help you add or remove key/value pairs from a map.

First, you can use the set method to add a new value to an instance of MichelsonMap:

![](https://cdn-images-1.medium.com/max/1024/1*IMvfpsUR9Lf_QKX8DhXi2Q.png)

This adds a new entry in the map with the address being the first argument and the BigNumber being the value.

Comment

> _Note: it is important to use_ _new BigNumber(345) for the value and not simply_ _345 as TypeScript will throw a type error because earlier, we set the type argument of the_ _MichelsonMap to_ _BigNumber._

You can also delete one of the entries of the map with the delete method:

![](https://cdn-images-1.medium.com/max/1024/1*emvxoO0F3I0884YeVOB29A.png)

> _Note: deleting a key that doesn’t exist doesn’t throw an error, it will just have no effect on the map._

To finish, you can also delete all the entries in a Michelson map if you want with the clear method:

![](https://cdn-images-1.medium.com/max/1024/1*MYxBkxLNRda8PUxQkBo9zQ.png)

### To go further

If you want to know more about MichelsonMap and some advanced usages (for example, how to use pairs as the map keys), you can learn in the [advanced tutorial](https://tezostaquito.io/docs/maps_bigmaps) available in the Taquito documentation.

#### Also, Read

- The [Best Crypto Trading Bots](https://medium.com/coinmonks/whats-the-best-crypto-trading-bot-in-2020-top-8-bitcoin-trading-bot-c16adeb13317)
- [Deribit Review](https://medium.com/coinmonks/deribit-review-options-fees-apis-and-testnet-2ca16c4bbdb2) | Options, Fees, APIs and Testnet
- [FTX Crypto Exchange Review](https://medium.com/coinmonks/ftx-crypto-exchange-review-53664ac1198f)
- The Best Bitcoin [Hardware wallet](https://medium.com/coinmonks/the-best-cryptocurrency-hardware-wallets-of-2020-e28b1c124069?source=friends_link&sk=324dd9ff8556ab578d71e7ad7658ad7c)
- [Crypto Copy Trading Platforms](https://medium.com/coinmonks/top-10-crypto-copy-trading-platforms-for-beginners-d0c37c7d698c)
- The Best [Crypto Tax Software](https://medium.com/coinmonks/best-crypto-tax-tool-for-my-money-72d4b430816b)
- [Best Crypto Trading Platforms](https://medium.com/coinmonks/the-best-crypto-trading-platforms-in-2020-the-definitive-guide-updated-c72f8b874555)
- Best [Crypto Lending Platforms](https://medium.com/coinmonks/top-5-crypto-lending-platforms-in-2020-that-you-need-to-know-a1b675cec3fa)
- [Ledger vs Trezor](https://medium.com/coinmonks/ledger-vs-trezor-best-hardware-wallet-to-secure-cryptocurrency-22c7a3fd391e)
- [BlockFi vs Celsius](https://medium.com/coinmonks/blockfi-vs-celsius-vs-hodlnaut-8a1cc8c26630) vs Hodlnaut
- [Bitsgap review](https://medium.com/coinmonks/bitsgap-review-a-crypto-trading-bot-that-makes-easy-money-a5d88a336df2) — A Crypto Trading Bot That Makes Easy Money
- [Quadency Review](https://medium.com/coinmonks/quadency-review-a-crypto-trading-automation-platform-3068eaa374e1)- A Crypto Trading Bot Made For Professionals
- [PrimeXBT Review](https://medium.com/coinmonks/primexbt-review-88e0815be858) | Leverage Trading, Fee and Covesting
- [HaasOnline Review](https://medium.com/coinmonks/haasonline-review-d8d1a3400419) and Get a 10% discount
- The Idiots Guide to [Margin Trading on Bitmex](https://medium.com/coinmonks/the-idiots-guide-to-margin-trading-on-bitmex-dbbd7742c6fc?source=friends_link&sk=7bfa99d2a181142510c8442c8ddb0786)
- [eToro Review](https://medium.com/coinmonks/etoro-review-78807ddeb33c) | Trade Stocks, Crypto, ETFs, CFDs, and commodities
- [BlockFi Review](https://medium.com/coinmonks/blockfi-review-53096053c097) | Earn up to 8.6% interests on your Crypto
- [Best Crypto APIs](https://medium.com/coinmonks/best-crypto-apis-for-developers-5efe3a597a9f) for Developers
- [Best Blockchain Analysis Tools](https://bitquery.io/blog/best-blockchain-analysis-tools-and-software)
- [Crypto arbitrage](https://medium.com/coinmonks/crypto-arbitrage-guide-how-to-make-money-as-a-beginner-62bfe5c868f6) guide: How to make money as a beginner
- Top [Bitcoin Node](https://blog.coincodecap.com/bitcoin-node-solutions) Providers
- Best [Crypto Charting Tool](https://medium.com/coinmonks/what-are-the-best-charting-platforms-for-cryptocurrency-trading-85aade584d80)
- What are the [best books to learn about Bitcoin](https://medium.com/coinmonks/what-are-the-best-books-to-learn-bitcoin-409aeb9aff4b)?

> [Get Best Software Deals Directly In Your Inbox](https://medium.com/coinmonks/newsletters/coinmonks)

[![](https://cdn-images-1.medium.com/max/584/1*BoDnKUyK8p4hitHAJZ5Pdw.png)](https://medium.com/coinmonks/newsletters/coinmonks)

* * *