Omniscia Tangible Audit

OnSaleTracker Code Style Findings

OnSaleTracker Code Style Findings

OST-01C: Inefficient Array Shifts

Description:

The referenced array shift operations will assign the last element of the array to the index of the element to-be-removed, however, these operations are inefficiently performed when the index of the element-to-be-removed is the actual last element of the array.

Example:

contracts/OnSaleTracker.sol
109function _removeCurrentlySellingTnft(ITangibleNFT tnft, uint256 index) internal {
110 require(index < tnftTokensOnSale[tnft].length, "IndexT");
111
112 //take last token
113 uint256 tokenId = tnftTokensOnSale[tnft][tnftTokensOnSale[tnft].length - 1];
114
115 //replace it with the one we are removing
116 tnftTokensOnSale[tnft][index] = tokenId;
117 //set it's new index in saleData
118 tnftSaleMapper[tnft][tokenId].indexInCurrentlySelling = index;
119 tnftTokensOnSale[tnft].pop();
120}

Recommendation:

We advise all operations to avoid shifting elements if the element-to-be-removed is the last element of the array, performing a direct pop operation instead.

Alleviation (1f394a00cc2ed1dc2020a9c07f982cff9029077d):

The array pop optimization has been properly applied to both referenced functions, optimizing the codebase as advised.

OST-02C: Inefficient mapping Lookups

Description:

The linked statements perform key-based lookup operations on mapping declarations from storage multiple times for the same key redundantly.

Example:

contracts/OnSaleTracker.sol
109function _removeCurrentlySellingTnft(ITangibleNFT tnft, uint256 index) internal {
110 require(index < tnftTokensOnSale[tnft].length, "IndexT");
111
112 //take last token
113 uint256 tokenId = tnftTokensOnSale[tnft][tnftTokensOnSale[tnft].length - 1];
114
115 //replace it with the one we are removing
116 tnftTokensOnSale[tnft][index] = tokenId;
117 //set it's new index in saleData
118 tnftSaleMapper[tnft][tokenId].indexInCurrentlySelling = index;
119 tnftTokensOnSale[tnft].pop();
120}

Recommendation:

As the lookups internally perform an expensive keccak256 operation, we advise the lookups to be cached wherever possible to a single local declaration that either holds the value of the mapping in case of primitive types or holds a storage pointer to the struct contained.

Alleviation (1f394a00cc2ed1dc2020a9c07f982cff9029077d):

All referenced inefficient mapping lookups have been optimized to the greatest extent possible, significantly reducing the gas cost of the functions the statements were located in.

OST-03C: Inexistent Error Message

Description:

The linked require check has no error message explicitly defined.

Example:

contracts/OnSaleTracker.sol
69require(msg.sender == marketplace);

Recommendation:

We advise one to be set so to increase the legibility of the codebase and aid in validating the require check's condition.

Alleviation (2ad448279d9e8e4b6edd94bcd2eb22129b6f7357):

An explicit error message has been introduced to the referenced require check, addressing this exhibit.

OST-04C: Redundant Parenthesis Statement

Description:

The referenced statement is redundantly wrapped in parenthesis (()).

Example:

contracts/OnSaleTracker.sol
86(tnftTokensOnSale[tnft].length - 1)

Recommendation:

We advise them to be safely omitted, increasing the legibility of the codebase.

Alleviation (2ad448279d9e8e4b6edd94bcd2eb22129b6f7357):

The redundant parenthesis in the referenced statement have been safely omitted.

OST-05C: Suboptimal Struct Declaration Styles

Description:

The linked declaration styles of the referenced structs are using index-based argument initialization.

Example:

contracts/OnSaleTracker.sol
83TnftSaleItem memory tsi = TnftSaleItem(
84 tnft,
85 tokenId,
86 (tnftTokensOnSale[tnft].length - 1)
87);

Recommendation:

We advise the key-value declaration format to be utilized instead in each instance, greatly increasing the legibility of the codebase.

Alleviation (1f394a00cc2ed1dc2020a9c07f982cff9029077d):

The key-value declaration style has been applied to both referenced struct instantiations, optimizing the legibility of the codebase.