beefyfinance / beefy-contracts

Public repo for the community devs to advance the Beefy protocol.
https://app.beefy.finance
180 stars 165 forks source link

StratFeeManagerInitializable, VaultFactory, update VaultV7 #218

Closed roman-monk closed 2 years ago

roman-monk commented 2 years ago

Fixed init in VaultV7. StratFeeManager with init function instead of constructor. StrategyConvex as a sample.

address prediction is not needed, vault deployment would be something like:

let tx;
const Factory = await ethers.getContractAt("BeefyVaultV7Factory", factoryAddress);
tx = await Factory.cloneVault();
await tx.wait();
const vaultAddress = await getProxyCreatedAddress(Factory, tx);

tx = await Factory.cloneContract(strategyImplAddress);
await tx.wait();
const stratAddress = await getProxyCreatedAddress(Factory, tx);

const Vault = await ethers.getContractAt(config.vaultName, vaultAddress);
tx = await Vault.initialize(stratAddress, mooName, mooSymbol, delay);
await tx.wait();

const Strategy = await ethers.getContractAt(strategyName, stratAddress);
tx = await Strategy.initialize(...stratArgs);
await tx.wait();
roman-monk commented 1 year ago

Wdyt about extending the vault with ERC4626 so we're compliant while keeping all our infrastructre still?

yeah very smart to make it as a wrapper

roman-monk commented 1 year ago

@sircryptomaxi @MirthFutures about gas saving, its more complex than just packing or ++i to save some real amounts.

changing lastHarvest type could actually cost more because of the type conversion. but the simple update of lastHarvest on every harvest() costs 5000 gas = 0.0001 ether on 20 gwei. thats like $0.2 on every single harvest. just to compare with 5-10 gas saved on ++i, or some weird unchecked {i++} (which is easy to miss and difficult to read) on our 3-iteration-max loops.

feeConfig.getFees() costs 15k-30k gas, i checked few on tenderly on different chains, these values practically "never" change but we read it on every harvest and thats $1 every time. could this be cached? or can we drop "string label" which is not used on chain but probably breaks etherscan and costs $$ for reading and sending (not entirely sure).

Deposit and Harvest events make 4-5 external calls for balanceOf though some values not changed in between.

HOD actually deposits twice which adds insane gas, because most of the gas is spent on external deposits and swaps, almost unoptimizable.

or 3 transfers on harvest, could be consolidated in strat and send later as a batch? maybe without callFee and it could be hardcoded, but strategist and beefyFee could be calculated and sent later saving also on getFees().

some group of strats like Aura could send BAL and AURA rewards to the 1 swapper and get WETH back later when swapper is harvested. that would save lots of gas on swaps but adds more complexity. 1 swap is at least 100,000 gas (0.002 eth on 20 gwei). 2 swaps for both tokens, 1 swap from native, then addLiquidity, thats close to 0.01 eth total on every harvest of every strat.

so in short amount of external calls matters, and optimization would require quite refactoring