2020-02-05 11:08:47 +04:00
|
|
|
---
|
|
|
|
id: big-map-reference
|
2020-02-26 21:31:58 +04:00
|
|
|
title: Big Maps — Scalable Maps
|
2020-02-05 11:08:47 +04:00
|
|
|
---
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
import Syntax from '@theme/Syntax';
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
Ordinary maps are fine for contracts with a finite lifespan or a
|
|
|
|
bounded number of users. For many contracts however, the intention is
|
|
|
|
to have a map holding *many* entries, potentially millions of
|
|
|
|
them. The cost of loading those entries into the environment each time
|
|
|
|
a user executes the contract would eventually become too expensive
|
|
|
|
were it not for *big maps*. Big maps are a data structure offered by
|
|
|
|
Michelson which handles the scaling concerns for us. In LIGO, the
|
|
|
|
interface for big maps is analogous to the one used for ordinary maps.
|
|
|
|
|
|
|
|
# Declaring a Map
|
2020-02-11 13:03:46 +04:00
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```pascaligo group=big_maps
|
2020-02-25 21:07:53 +04:00
|
|
|
type move is int * int
|
|
|
|
type register is big_map (address, move)
|
2020-02-11 13:03:46 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```cameligo group=big_maps
|
2020-02-11 13:03:46 +04:00
|
|
|
type move = int * int
|
2020-02-25 21:07:53 +04:00
|
|
|
type register = (address, move) big_map
|
2020-02-11 13:03:46 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```reasonligo group=big_maps
|
2020-02-11 13:03:46 +04:00
|
|
|
type move = (int, int);
|
2020-02-25 21:07:53 +04:00
|
|
|
type register = big_map (address, move);
|
2020-02-11 13:03:46 +04:00
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
</Syntax>
|
|
|
|
|
2020-02-11 13:03:46 +04:00
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
# Creating an Empty Big Map
|
2020-02-11 13:03:46 +04:00
|
|
|
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```pascaligo group=big_maps
|
2020-02-25 21:07:53 +04:00
|
|
|
const empty : register = big_map []
|
2020-02-11 13:03:46 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```cameligo group=big_maps
|
2020-02-25 21:07:53 +04:00
|
|
|
let empty : register = Big_map.empty
|
2020-02-11 13:03:46 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```reasonligo group=big_maps
|
2020-02-25 21:07:53 +04:00
|
|
|
let empty : register = Big_map.empty
|
2020-02-11 13:03:46 +04:00
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
</Syntax>
|
|
|
|
|
2020-02-11 13:03:46 +04:00
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
# Creating a Non-empty Map
|
2020-02-05 11:08:47 +04:00
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```pascaligo group=big_maps
|
|
|
|
const moves : register =
|
2020-02-25 21:07:53 +04:00
|
|
|
big_map [
|
|
|
|
("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address) -> (1,2);
|
|
|
|
("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address) -> (0,3)]
|
2020-02-05 11:08:47 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```cameligo group=big_maps
|
|
|
|
let moves : register =
|
2020-02-25 21:07:53 +04:00
|
|
|
Big_map.literal [
|
|
|
|
(("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address), (1,2));
|
|
|
|
(("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address), (0,3))]
|
2020-02-05 11:08:47 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```reasonligo group=big_maps
|
|
|
|
let moves : register =
|
2020-02-25 21:07:53 +04:00
|
|
|
Big_map.literal ([
|
|
|
|
("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address, (1,2)),
|
|
|
|
("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address, (0,3))]);
|
2020-02-05 11:08:47 +04:00
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
</Syntax>
|
|
|
|
|
2020-02-05 11:08:47 +04:00
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
# Accessing Values
|
2020-02-05 11:08:47 +04:00
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```pascaligo group=big_maps
|
2020-02-25 21:07:53 +04:00
|
|
|
const my_balance : option (move) =
|
|
|
|
moves [("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address)]
|
2020-02-05 11:08:47 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```cameligo group=big_maps
|
2020-02-25 21:07:53 +04:00
|
|
|
let my_balance : move option =
|
|
|
|
Big_map.find_opt ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address) moves
|
2020-02-05 11:08:47 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```reasonligo group=big_maps
|
2020-02-25 21:07:53 +04:00
|
|
|
let my_balance : option (move) =
|
|
|
|
Big_map.find_opt ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address, moves);
|
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
</Syntax>
|
|
|
|
|
2020-02-05 11:08:47 +04:00
|
|
|
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
# Updating Big Maps
|
2020-02-05 11:08:47 +04:00
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```pascaligo group=big_maps
|
2020-02-25 21:07:53 +04:00
|
|
|
function add (var m : register) : register is
|
2020-02-05 11:08:47 +04:00
|
|
|
block {
|
2020-02-25 21:07:53 +04:00
|
|
|
m [("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address)] := (4,9)
|
2020-02-05 11:08:47 +04:00
|
|
|
} with m
|
2020-02-25 21:07:53 +04:00
|
|
|
|
|
|
|
const updated_map : register = add (moves)
|
2020-02-05 11:08:47 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```cameligo group=big_maps
|
2020-02-25 21:07:53 +04:00
|
|
|
let updated_map : register =
|
|
|
|
Big_map.update
|
|
|
|
("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address) (Some (4,9)) moves
|
2020-02-05 11:08:47 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```reasonligo group=big_maps
|
2020-02-25 21:07:53 +04:00
|
|
|
let updated_map : register =
|
|
|
|
Big_map.update
|
|
|
|
(("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address), Some ((4,9)), moves);
|
2020-02-05 11:08:47 +04:00
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
</Syntax>
|
|
|
|
|
2020-02-05 11:08:47 +04:00
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
# Removing Bindings
|
2020-02-11 13:03:46 +04:00
|
|
|
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```pascaligo group=big_maps
|
|
|
|
function rem (var m : register) : register is
|
2020-02-25 21:07:53 +04:00
|
|
|
block {
|
2020-02-26 21:31:58 +04:00
|
|
|
remove ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address) from map moves
|
2020-02-25 21:07:53 +04:00
|
|
|
} with m
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
const updated_map : register = rem (moves)
|
2020-02-05 11:08:47 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```cameligo group=big_maps
|
|
|
|
let updated_map : register =
|
|
|
|
Map.remove ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address) moves
|
2020-02-05 11:08:47 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```reasonligo group=big_maps
|
|
|
|
let updated_map : register =
|
|
|
|
Map.remove (("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address), moves)
|
2020-02-05 11:08:47 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
|