2020-02-05 11:08:47 +04:00
|
|
|
---
|
|
|
|
id: big-map-reference
|
2020-02-25 21:07:53 +04:00
|
|
|
title: Big Map — Scalable Maps
|
2020-02-05 11:08:47 +04:00
|
|
|
---
|
|
|
|
|
2020-02-11 13:03:46 +04:00
|
|
|
## Defining A Big Map Type
|
|
|
|
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
|
|
|
<!--Pascaligo-->
|
2020-02-25 21:07:53 +04:00
|
|
|
```pascaligo group=big_map
|
|
|
|
type move is int * int
|
|
|
|
type register is big_map (address, move)
|
2020-02-11 13:03:46 +04:00
|
|
|
```
|
|
|
|
|
|
|
|
<!--CameLIGO-->
|
2020-02-25 21:07:53 +04:00
|
|
|
```cameligo group=big_map
|
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
|
|
|
```
|
|
|
|
|
|
|
|
<!--ReasonLIGO-->
|
2020-02-25 21:07:53 +04:00
|
|
|
```reasonligo group=big_map
|
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
|
|
|
```
|
|
|
|
|
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
## Creating an Empty Big Map
|
|
|
|
|
|
|
|
Create an empty big map.
|
2020-02-11 13:03:46 +04:00
|
|
|
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
<!--PascaLIGO-->
|
|
|
|
```pascaligo group=big_map
|
|
|
|
const empty : register = big_map []
|
2020-02-11 13:03:46 +04:00
|
|
|
```
|
|
|
|
|
|
|
|
<!--CameLIGO-->
|
2020-02-25 21:07:53 +04:00
|
|
|
```cameligo group=big_map
|
|
|
|
let empty : register = Big_map.empty
|
2020-02-11 13:03:46 +04:00
|
|
|
```
|
|
|
|
|
|
|
|
<!--ReasonLIGO-->
|
2020-02-25 21:07:53 +04:00
|
|
|
```reasonligo group=big_map
|
|
|
|
let empty : register = Big_map.empty
|
2020-02-11 13:03:46 +04:00
|
|
|
```
|
|
|
|
|
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
## Populating A Big Map
|
2020-02-05 11:08:47 +04:00
|
|
|
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
|
|
|
<!--Pascaligo-->
|
2020-02-25 21:07:53 +04:00
|
|
|
|
|
|
|
```pascaligo group=big_map
|
|
|
|
const moves: register =
|
|
|
|
big_map [
|
|
|
|
("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address) -> (1,2);
|
|
|
|
("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address) -> (0,3)]
|
2020-02-05 11:08:47 +04:00
|
|
|
```
|
|
|
|
|
|
|
|
<!--CameLIGO-->
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
```cameligo group=big_map
|
|
|
|
let moves: register =
|
|
|
|
Big_map.literal [
|
|
|
|
(("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address), (1,2));
|
|
|
|
(("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address), (0,3))]
|
2020-02-05 11:08:47 +04:00
|
|
|
```
|
|
|
|
|
|
|
|
<!--ReasonLIGO-->
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
```reasonligo group=big_map
|
|
|
|
let moves: register =
|
|
|
|
Big_map.literal ([
|
|
|
|
("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address, (1,2)),
|
|
|
|
("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address, (0,3))]);
|
2020-02-05 11:08:47 +04:00
|
|
|
```
|
2020-02-25 21:07:53 +04:00
|
|
|
|
2020-02-05 11:08:47 +04:00
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
## Retrieving a Value in a Big Map
|
2020-02-05 11:08:47 +04:00
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
Retrieve the value associated with a particular key. This version
|
|
|
|
returns an option which can either shift logic in response to a
|
|
|
|
missing value or throw an error.
|
2020-02-05 11:08:47 +04:00
|
|
|
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
|
|
|
<!--Pascaligo-->
|
2020-02-25 21:07:53 +04:00
|
|
|
```pascaligo group=big_map
|
|
|
|
const my_balance : option (move) =
|
|
|
|
moves [("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address)]
|
2020-02-05 11:08:47 +04:00
|
|
|
```
|
|
|
|
|
|
|
|
<!--CameLIGO-->
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
The function is `Big_map.find_opt` whose type is `'key ->
|
|
|
|
('key,'value) big_map) -> 'value option`. Recall that the function
|
|
|
|
`Big_map.find_opt` must always be fully applied to its arguments. Here
|
|
|
|
is an example:
|
|
|
|
|
|
|
|
```cameligo group=big_map
|
|
|
|
let my_balance : move option =
|
|
|
|
Big_map.find_opt ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address) moves
|
2020-02-05 11:08:47 +04:00
|
|
|
```
|
|
|
|
|
|
|
|
<!--ReasonLIGO-->
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
The function is `Big_map.find_opt` whose type is `('key, ('key,
|
|
|
|
'value) big_map) : 'value option`. Here is an example:
|
2020-02-05 11:08:47 +04:00
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
```reasonligo group=big_map
|
|
|
|
let my_balance : option (move) =
|
|
|
|
Big_map.find_opt ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address, moves);
|
|
|
|
```
|
2020-02-05 11:08:47 +04:00
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
## Updating a Binding in a Big Map
|
2020-02-05 11:08:47 +04:00
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
Given a map, we may want to add a new binding, remove one, or modify
|
|
|
|
one by changing the value associated to an already existing key. We
|
|
|
|
may even want to retain the key but not the associated value. All
|
|
|
|
those operations are called *updates*.
|
2020-02-05 11:08:47 +04:00
|
|
|
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
|
|
|
|
|
|
|
<!--Pascaligo-->
|
|
|
|
|
|
|
|
The values of a PascaLIGO big map can be updated using the ordinary
|
|
|
|
assignment syntax:
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
```pascaligo group=big_map
|
|
|
|
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-02-25 21:07:53 +04:00
|
|
|
See further for the removal of bindings.
|
|
|
|
|
2020-02-05 11:08:47 +04:00
|
|
|
<!--Cameligo-->
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
In CameLIGO, you need the predefined function `Big_map.update` whose
|
|
|
|
type is `'key -> 'value option -> ('key, 'value) big_map -> ('key,
|
|
|
|
'value) big_map`. If the value (second argument) is `None`, then the
|
|
|
|
binding is to be removed. Recall that the function `Big_map.update`
|
|
|
|
must always be fully applied to its arguments. Here is an example:
|
|
|
|
|
|
|
|
```cameligo group=big_map
|
|
|
|
let updated_map : register =
|
|
|
|
Big_map.update
|
|
|
|
("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address) (Some (4,9)) moves
|
2020-02-05 11:08:47 +04:00
|
|
|
```
|
|
|
|
|
|
|
|
<!--Reasonligo-->
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
In ReasonLIGO, you need the predefined function `Big_map.update` whose
|
|
|
|
type is `('key, 'value option, ('key, 'value) big_map) : ('key,
|
|
|
|
'value) big_map`. If the value (second componenat) is `None`, then the
|
|
|
|
binding is to be removed. Here is an example:
|
|
|
|
|
|
|
|
```reasonligo group=big_map
|
|
|
|
let updated_map : register =
|
|
|
|
Big_map.update
|
|
|
|
(("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address), Some ((4,9)), moves);
|
2020-02-05 11:08:47 +04:00
|
|
|
```
|
|
|
|
|
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
## Adding a Binding to a Big Map
|
2020-02-05 11:08:47 +04:00
|
|
|
|
2020-02-11 13:03:46 +04:00
|
|
|
Add a key and its associated value to the big map.
|
|
|
|
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
```pascaligo group=big_map
|
|
|
|
function add (var m : register) : register is
|
|
|
|
block {
|
|
|
|
m [("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address)] := (4,9)
|
|
|
|
} with m
|
|
|
|
|
|
|
|
const updated_map : register = add (moves)
|
2020-02-11 13:03:46 +04:00
|
|
|
```
|
|
|
|
|
|
|
|
<!--CameLIGO-->
|
2020-02-25 21:07:53 +04:00
|
|
|
|
|
|
|
In CameLIGO, we use the predefined function `Big_map.add`, whose type
|
|
|
|
is `'key -> 'value -> ('key, 'value) big_map -> ('key, 'value)
|
|
|
|
big_map`. Recall that the function `Big_map.add` must always be fully
|
|
|
|
applied to its arguments. Here is an example:
|
|
|
|
|
|
|
|
```cameligo group=big_map
|
|
|
|
let add (m : register) : register =
|
|
|
|
Map.add
|
|
|
|
("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address) (4,9) m
|
2020-02-11 13:03:46 +04:00
|
|
|
```
|
|
|
|
|
|
|
|
<!--ReasonLIGO-->
|
2020-02-25 21:07:53 +04:00
|
|
|
|
|
|
|
In ReasonLIGO, we use the predefined function `Big_map.add`, whose
|
|
|
|
type is `('key, 'value, ('key, 'value) big_map : ('key, 'value)
|
|
|
|
big_map`. Here is an example:
|
|
|
|
|
|
|
|
```reasonligo group=big_map
|
|
|
|
let add = (m : register) : register =>
|
|
|
|
Map.add
|
|
|
|
(("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address), (4,9), m);
|
2020-02-11 13:03:46 +04:00
|
|
|
```
|
|
|
|
|
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
## Removing a Binding from a Big Map
|
2020-02-05 11:08:47 +04:00
|
|
|
|
|
|
|
Remove a key and its associated value from the big map.
|
|
|
|
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
|
|
|
|
|
|
|
<!--PascaLIGO-->
|
2020-02-25 21:07:53 +04:00
|
|
|
```pascaligo group=big_map
|
|
|
|
function delete (const key : address; var moves : register) : register is
|
|
|
|
block {
|
|
|
|
remove key from map moves
|
|
|
|
} with moves
|
2020-02-05 11:08:47 +04:00
|
|
|
```
|
|
|
|
|
|
|
|
<!--CameLIGO-->
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
In CameLIGO, we use the predefined function `Big_map.remove` whose
|
|
|
|
type is `'key -> ('key, 'value) big_map -> ('key, 'value)
|
|
|
|
big_map`. Note that, despite being curried, the calls to that function
|
|
|
|
must be complete. Here is an example:
|
2020-02-05 11:08:47 +04:00
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
```cameligo group=big_map
|
|
|
|
let delete (key, moves : address * register) : register =
|
|
|
|
Map.remove key moves
|
2020-02-05 11:08:47 +04:00
|
|
|
```
|
|
|
|
|
|
|
|
<!--ReasonLIGO-->
|
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
In ReasonLIGO, we use the predefined function `Big_map.remove` whose
|
|
|
|
type is `('key, ('key, 'value) big_map) : ('key, 'value)
|
|
|
|
big_map`. Here is an example:
|
2020-02-05 11:08:47 +04:00
|
|
|
|
2020-02-25 21:07:53 +04:00
|
|
|
```reasonligo group=big_map
|
|
|
|
let delete = ((key, moves) : (address, register)) : register =>
|
|
|
|
Map.remove (key, moves);
|
2020-02-05 11:08:47 +04:00
|
|
|
```
|
|
|
|
|
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|