aavegotchi / aavegotchi-realm-diamond

23 stars 9 forks source link

Gas optimization in craftInstallations function #10

Closed mudgen closed 2 years ago

mudgen commented 2 years ago

The craftInstallations has this code: https://github.com/aavegotchi/aavegotchi-realm-diamond/blob/cee38d37307c49dc41cdb737a962d5d313c1cd4f/contracts/InstallationDiamond/facets/InstallationFacet.sol#L214-L222

External functions are gas expensive. The RealmDiamond(s.realmDiamond).getAlchemicaAddresses() function call can be made one time outside the loop.

State reads are also gas expensive. Line 214 read all state from the InstallationType struct and in line 220 reads more of it from state from storage repeatedly. I think you can use the installationType memory variable to read the state on line 220 instead of reading from contract storage.

It might be better not to read the entire InstallationType from contract storage to memory because not all of its state variables are needed.