Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: replace tuple value for LSP5/10 from bytes8 -> uint128 #486

Merged
merged 2 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions contracts/LSP10ReceivedVaults/LSP10Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ import "../LSP9Vault/LSP9Constants.sol";
error InvalidLSP10ReceivedVaultsArrayLength(bytes invalidValueStored, uint256 invalidValueLength);

/**
* @dev reverts when the vault index is superior to uint64
* @param index the vault index
* @dev reverts when the `LSP10Vaults[]` array reaches its maximum limit (max(uint128))
* @param notRegisteredVault the address of the vault that could not be registered
*/
error VaultIndexSuperiorToUint64(uint256 index);
error MaxLSP10VaultsCountReached(address notRegisteredVault);

/**
* @dev reverts when the vault index is superior to uint128
Expand Down Expand Up @@ -75,17 +75,18 @@ library LSP10Utils {
values[1] = bytes.concat(bytes20(vault));

keys[2] = vaultMapKey;
values[2] = bytes.concat(_INTERFACEID_LSP9, bytes8(0));
values[2] = bytes.concat(_INTERFACEID_LSP9, bytes16(0));

// If the storage is already initiated
} else if (encodedArrayLength.length == 16) {
uint128 oldArrayLength = uint128(bytes16(encodedArrayLength));
uint128 newArrayLength = oldArrayLength + 1;

if (newArrayLength > type(uint64).max) {
revert VaultIndexSuperiorToUint64(newArrayLength);
if (oldArrayLength == type(uint128).max) {
revert MaxLSP10VaultsCountReached({notRegisteredVault: vault});
}

uint128 newArrayLength = oldArrayLength + 1;

keys[0] = _LSP10_VAULTS_ARRAY_KEY;
values[0] = bytes.concat(bytes16(newArrayLength));

Expand All @@ -96,7 +97,7 @@ library LSP10Utils {
values[1] = bytes.concat(bytes20(vault));

keys[2] = vaultMapKey;
values[2] = bytes.concat(_INTERFACEID_LSP9, bytes8(uint64(oldArrayLength)));
values[2] = bytes.concat(_INTERFACEID_LSP9, bytes16(oldArrayLength));
} else {
revert InvalidLSP10ReceivedVaultsArrayLength({
invalidValueStored: encodedArrayLength,
Expand Down Expand Up @@ -136,7 +137,7 @@ library LSP10Utils {

uint128 newArrayLength = oldArrayLength - 1;

uint64 index = extractIndexFromMap(vaultInterfaceIdAndIndex);
uint128 index = extractIndexFromMap(vaultInterfaceIdAndIndex);
bytes32 vaultInArrayKey = LSP2Utils.generateArrayElementKeyAtIndex(
_LSP10_VAULTS_ARRAY_KEY,
index
Expand All @@ -153,7 +154,7 @@ library LSP10Utils {
values = new bytes[](3);

keys[0] = _LSP10_VAULTS_ARRAY_KEY;
values[0] = bytes.concat(bytes16(oldArrayLength - 1));
values[0] = bytes.concat(bytes16(newArrayLength));

keys[1] = vaultInArrayKey;
values[1] = "";
Expand All @@ -176,7 +177,7 @@ library LSP10Utils {
values = new bytes[](5);

keys[0] = _LSP10_VAULTS_ARRAY_KEY;
values[0] = bytes.concat(bytes16(oldArrayLength - 1));
values[0] = bytes.concat(bytes16(newArrayLength));

keys[1] = vaultMapKey;
values[1] = "";
Expand All @@ -202,7 +203,7 @@ library LSP10Utils {
values[3] = "";

keys[4] = lastVaultInArrayMapKey;
values[4] = bytes.concat(_INTERFACEID_LSP9, bytes8(index));
values[4] = bytes.concat(_INTERFACEID_LSP9, bytes16(index));
}
}

Expand All @@ -211,10 +212,10 @@ library LSP10Utils {
}

/**
* @dev returns the index from a maping
* @dev returns the index from the LSP10VaultMap
*/
function extractIndexFromMap(bytes memory mapValue) internal pure returns (uint64) {
bytes memory val = BytesLib.slice(mapValue, 4, 8);
return BytesLib.toUint64(val, 0);
function extractIndexFromMap(bytes memory mapValue) internal pure returns (uint128) {
bytes memory val = BytesLib.slice(mapValue, 4, 16);
return BytesLib.toUint128(val, 0);
}
}
26 changes: 13 additions & 13 deletions contracts/LSP5ReceivedAssets/LSP5Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ import "../LSP7DigitalAsset/LSP7Constants.sol";
error InvalidLSP5ReceivedAssetsArrayLength(bytes invalidValueStored, uint256 invalidValueLength);

/**
* @dev reverts when the received assets index is superior to uint64
* @param index the received assets index
* @dev reverts when the `LSP5ReceivedAssets[]` array reaches its maximum limit (max(uint128))
* @param notRegisteredAsset the address of the asset that could not be registered
*/
error ReceivedAssetsIndexSuperiorToUint64(uint256 index);
error MaxLSP5ReceivedAssetsCountReached(address notRegisteredAsset);

/**
* @dev reverts when the received assets index is superior to uint128
Expand Down Expand Up @@ -77,14 +77,14 @@ library LSP5Utils {
values[1] = bytes.concat(bytes20(asset));

keys[2] = assetMapKey;
values[2] = bytes.concat(interfaceID, bytes8(0));
values[2] = bytes.concat(interfaceID, bytes16(0));

// If the storage is already initiated
} else if (encodedArrayLength.length == 16) {
uint128 oldArrayLength = uint128(bytes16(encodedArrayLength));

if (oldArrayLength + 1 >= type(uint64).max) {
revert ReceivedAssetsIndexSuperiorToUint64(oldArrayLength);
if (oldArrayLength == type(uint128).max) {
revert MaxLSP5ReceivedAssetsCountReached({notRegisteredAsset: asset});
}

keys[0] = _LSP5_RECEIVED_ASSETS_ARRAY_KEY;
Expand All @@ -97,7 +97,7 @@ library LSP5Utils {
values[1] = bytes.concat(bytes20(asset));

keys[2] = assetMapKey;
values[2] = bytes.concat(interfaceID, bytes8(uint64(oldArrayLength)));
values[2] = bytes.concat(interfaceID, bytes16(oldArrayLength));
} else {
revert InvalidLSP5ReceivedAssetsArrayLength({
invalidValueStored: encodedArrayLength,
Expand Down Expand Up @@ -132,7 +132,7 @@ library LSP5Utils {
uint128 oldArrayLength = uint128(bytes16(lsp5ReceivedAssetsCountValue));
uint128 newArrayLength = oldArrayLength - 1;

uint64 index = extractIndexFromMap(assetInterfaceIdAndIndex);
uint128 index = extractIndexFromMap(assetInterfaceIdAndIndex);
bytes32 assetInArrayKey = LSP2Utils.generateArrayElementKeyAtIndex(
_LSP5_RECEIVED_ASSETS_ARRAY_KEY,
index
Expand Down Expand Up @@ -205,7 +205,7 @@ library LSP5Utils {
values[3] = "";

keys[4] = lastAssetInArrayMapKey;
values[4] = bytes.concat(interfaceID, bytes8(index));
values[4] = bytes.concat(interfaceID, bytes16(index));
}
}

Expand All @@ -214,10 +214,10 @@ library LSP5Utils {
}

/**
* @dev returns the index from a maping
* @dev returns the index from the LSP5ReceivedAssetMap
*/
function extractIndexFromMap(bytes memory mapValue) internal pure returns (uint64) {
bytes memory val = BytesLib.slice(mapValue, 4, 8);
return BytesLib.toUint64(val, 0);
function extractIndexFromMap(bytes memory mapValue) internal pure returns (uint128) {
bytes memory val = BytesLib.slice(mapValue, 4, 16);
return BytesLib.toUint128(val, 0);
}
}
Loading