Skip to content

Commit

Permalink
Update storage.md
Browse files Browse the repository at this point in the history
  • Loading branch information
crosschainer authored Apr 24, 2024
1 parent 27d06fb commit 0cea63f
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions docs/concepts/storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Variable(Datum):
#### \_\_init\_\_(self, contract, name, driver, t)
The \_\_init\_\_ arguments are automatically filled in for you during compilation and runtime. You do not have to provide any of them.

some_contract.py
some_contract.py (Smart Contract)
```python
owner = Variable()
```
Expand All @@ -100,7 +100,7 @@ Driver is pulled from the Runtime (`rt`) module when the contract is being execu

#### set(self, value)

some_contract.py
some_contract.py (Smart Contract)
```python
owner = Variable()
owner.set('stu')
Expand All @@ -126,7 +126,7 @@ owner

#### get(self)

some_contract.py
some_contract.py (Smart Contract)
```python
owner = Variable()
owner.set('stu')
Expand Down Expand Up @@ -179,7 +179,7 @@ class Hash(Datum):

Similar to Variable's \_\_init\_\_ except that a different keyword argument `default_value` allows you to set a value to return when the key does not exist. This is good for ledgers or applications where you need to have a base value.

some_contract.py
some_contract.py (Smart Contract)
```python
balances = Hash(default_value=0)
balances['stu'] = 1_000_000
Expand All @@ -192,7 +192,7 @@ balances['raghu'] == 0 # True

Equivalent to Variable's `get` but accepts an additional argument to specify the key. For example, the following code executed would result in the following state space.

some_contract.py
some_contract.py (Smart Contract)
```python
balances = Hash(default_value=0)
balances.set('stu', 1_000_000)
Expand All @@ -210,7 +210,7 @@ balances.set('tejas', 777)

You can provide an arbitrary number of keys (up to 16) to `set` and it will react accordingly, writing data to the dimension of keys that you provided. For example:

subaccounts.py
subaccounts.py (Smart Contract)
```python
balances = Hash(default_value=0)
balances.set('stu', 1_000_000)
Expand All @@ -232,7 +232,7 @@ This will create the following state space:

Inverse of `set`, where the value for a provided key is returned. If it is `None`, it will set it to the `default_value` provided on initialization.

some_contract.py
some_contract.py (Smart Contract)
```python
balances = Hash(default_value=0)
balances.set('stu', 1_000_000)
Expand All @@ -249,7 +249,7 @@ The same caveat applies here
#### Multihashes
Just like `set`, you retrieve data stored in multihashes by providing the list of keys used to write data to that location. Just like `get` with a single key, the default value will be returned if no value at the storage location is found.

subaccounts.py
subaccounts.py (Smart Contract)
```python
balances = Hash(default_value=0)
balances.set('stu', 1_000_000)
Expand Down Expand Up @@ -301,7 +301,7 @@ d['complex'] == e['complex'] # True!
#### \_\_setitem\_\_(self, key, value):
Equal functionality to `set`, but allows slice notation for convenience. __This is less verbose and the preferred method of setting storage on a Hash.__

subaccounts.py
subaccounts.py (Smart Contract)
```python
balances = Hash(default_value=0)
balances['stu'] = 1_000_000
Expand All @@ -321,7 +321,7 @@ owner['stu']
#### \_\_getitem\_\_(self, key):
Equal functionality to `set`, but allows slice notation for convenience. __This is less verbose and the preferred method of setting storage on a Hash.__

subaccounts.py
subaccounts.py (Smart Contract)
```python
balances = Hash(default_value=0)
balances['stu'] = 1_000_000
Expand Down

0 comments on commit 0cea63f

Please sign in to comment.