Skip to content

Commit

Permalink
Make ForeighHash inherit the default value of the referenced Hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Endogen committed Sep 28, 2024
1 parent 9eeeace commit 81cd639
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/contracting/storage/orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def __init__(self, contract, name, driver: Driver = driver, default_value=None):
self._delimiter = constants.DELIMITER
self._default_value = default_value

# Store the default_value in storage if it's not None
if default_value is not None:
self._driver.set(f'{self._key}{self._delimiter}__default__', default_value)

def _set(self, key, value):
self._driver.set(f'{self._key}{self._delimiter}{key}', value)

Expand All @@ -46,9 +50,12 @@ def _get(self, item):

# Add Python defaultdict behavior for easier smart contracting
if value is None:
# Retrieve the default_value from storage if not set
if self._default_value is None:
self._default_value = self._driver.get(f'{self._key}{self._delimiter}__default__')
value = self._default_value

if type(value) == float or type(value) == ContractingDecimal:
if isinstance(value, (float, ContractingDecimal)):
return ContractingDecimal(str(value))

return value
Expand Down Expand Up @@ -127,6 +134,10 @@ class ForeignHash(Hash):
def __init__(self, contract, name, foreign_contract, foreign_name, driver: Driver = driver):
super().__init__(contract, name, driver=driver)
self._key = self._driver.make_key(foreign_contract, foreign_name)
self._delimiter = constants.DELIMITER

# Retrieve the default_value from the foreign Hash's storage
self._default_value = self._driver.get(f'{self._key}{self._delimiter}__default__')

def _set(self, key, value):
raise ReferenceError
Expand Down

0 comments on commit 81cd639

Please sign in to comment.