Skip to content

Commit

Permalink
Merge pull request #250 from ERC725Alliance/feat/setdatabatch-internal
Browse files Browse the repository at this point in the history
feat: add internal `_setDataBatch` function without owner modifier
  • Loading branch information
CJ42 authored Jan 31, 2024
2 parents 990aac0 + ee0948a commit bcfaabe
Showing 1 changed file with 44 additions and 23 deletions.
67 changes: 44 additions & 23 deletions implementations/contracts/ERC725YCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ abstract contract ERC725YCore is OwnableUnset, ERC165, IERC725Y {
function getData(
bytes32 dataKey
) public view virtual override returns (bytes memory dataValue) {
dataValue = _getData(dataKey);
return _getData(dataKey);
}

/**
Expand Down Expand Up @@ -65,8 +65,9 @@ abstract contract ERC725YCore is OwnableUnset, ERC165, IERC725Y {
* - SHOULD only be callable by the {owner}.
*
* @custom:warning
* **Note for developers:** despite the fact that this function is set as `payable`, if the function is not intended to receive value
* (= native tokens), **an additional check should be implemented to ensure that `msg.value` sent was equal to 0**.
* **Note for developers:** despite the fact that this function is set as `payable`, the function is not intended to receive value
* (= native tokens). **An additional check has been implemented to ensure that `msg.value` sent was equal to 0**.
* If you want to allow this function to receive value in your inheriting contract, this function can be overriden to remove this check.
*
* @custom:events {DataChanged} event.
*/
Expand All @@ -84,8 +85,9 @@ abstract contract ERC725YCore is OwnableUnset, ERC165, IERC725Y {
* - SHOULD only be callable by the {owner} of the contract.
*
* @custom:warning
* **Note for developers:** despite the fact that this function is set as `payable`, if the function is not intended to receive value
* (= native tokens), **an additional check should be implemented to ensure that `msg.value` sent was equal to 0**.
* **Note for developers:** despite the fact that this function is set as `payable`, the function is not intended to receive value
* (= native tokens). **An additional check has been implemented to ensure that `msg.value` sent was equal to 0**.
* If you want to allow this function to receive value in your inheriting contract, this function can be overriden to remove this check.
*
* @custom:events {DataChanged} event **for each data key/value pair set**.
*/
Expand All @@ -95,28 +97,12 @@ abstract contract ERC725YCore is OwnableUnset, ERC165, IERC725Y {
) public payable virtual override onlyOwner {
/// @dev do not allow to send value by default when setting data in ERC725Y
if (msg.value != 0) revert ERC725Y_MsgValueDisallowed();

if (dataKeys.length != dataValues.length) {
revert ERC725Y_DataKeysValuesLengthMismatch();
}

if (dataKeys.length == 0) {
revert ERC725Y_DataKeysValuesEmptyArray();
}

for (uint256 i = 0; i < dataKeys.length; ) {
_setData(dataKeys[i], dataValues[i]);

// Increment the iterator in unchecked block to save gas
unchecked {
++i;
}
}
_setDataBatch(dataKeys, dataValues);
}

/**
* @dev Read the value stored under a specific `dataKey` inside the underlying ERC725Y storage,
* represented as a mapping of `bytes32` data keys mapped to their `bytes` data values.
* represented as a mapping of `bytes32` data keys mapped to their `bytes` data values.
*
* ```solidity
* mapping(bytes32 => bytes) _store
Expand Down Expand Up @@ -152,6 +138,41 @@ abstract contract ERC725YCore is OwnableUnset, ERC165, IERC725Y {
emit DataChanged(dataKey, dataValue);
}

/**
* @dev Write a set of `dataValues` to the underlying ERC725Y storage for each associated `dataKeys`. The ERC725Y storage is
* represented as a mapping of `bytes32` data keys mapped to their `bytes` data values.
*
* ```solidity
* mapping(bytes32 => bytes) _store
* ```
*
* @param dataKeys A bytes32 array of data keys to write the associated `bytes` value to the store.
* @param dataValues The `bytes` values to associate with each given `dataKeys` in the ERC725Y storage.
*
* @custom:events {DataChanged} event emitted for each successful data key-value pairs set.
*/
function _setDataBatch(
bytes32[] memory dataKeys,
bytes[] memory dataValues
) internal virtual {
if (dataKeys.length != dataValues.length) {
revert ERC725Y_DataKeysValuesLengthMismatch();
}

if (dataKeys.length == 0) {
revert ERC725Y_DataKeysValuesEmptyArray();
}

for (uint256 i = 0; i < dataKeys.length; ) {
_setData(dataKeys[i], dataValues[i]);

// Increment the iterator in unchecked block to save gas
unchecked {
++i;
}
}
}

/**
* @inheritdoc ERC165
*/
Expand Down

0 comments on commit bcfaabe

Please sign in to comment.