hifi-finance / hifi

Monorepo implementing the Hifi fixed-rate, fixed-term lending protocol
https://app.hifi.finance
Other
106 stars 15 forks source link

[flash-swap] Improve pair contract assertion logic #37

Closed scorpion9979 closed 3 years ago

scorpion9979 commented 3 years ago

In HifiFlashUniswapV2.sol, there's this line asserting that the sender address is the pair contract that was initialized at the constructor. Having the pairs set at the constructor makes the flash swap contract very difficult to adapt to new listed collateral tokens. Instead, I would suggest initializing the constructor with the Uniswap V2 init hash and factory contract address (they would change depending on which chain and Uniswap clone is used). The pair contract address to be used in the assertion would then be computed from the collateral and underlying token addresses as follows:

(address token0, address token1) = sortTokens(underlying, collateral);
address pair = address(uint(keccak256(abi.encodePacked(
  hex'ff',
  factory,
  keccak256(abi.encodePacked(token0, token1)),
  initCodeHash
))));

This might be more costly in terms of gas, but it's a lot more dynamic and doesn't require any maintenance.

PaulRBerg commented 3 years ago

Sounds like a good idea.

This might be more costly in terms of gas, but it's a lot more dynamic and doesn't require any maintenance.

It would. But all in all, it would cost less gas than deploying a new flash swap contract for every other new collateral listing.