Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 6 additions & 7 deletions ignition/deployments/chain-8453/deployed_addresses.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@
"Main#Forwarder": "0xf3430970980d60BEEcEcC5f72b3868B891127c83",
"Main#InvoiceFactory": "0xaAc5Dfc0Cb60b5F829e67749a72101A9d7C67FaD",
"Main#TokenDistributorDeployLib": "0xe279F01DF9a69695D3203009be176dbB36106241",
"Main#InterestLib": "0xe8426F0675C9Da7340b4E8801cDE6a5C59f23Eb7",
"Main#InterestLib": "0xB2F91663c103CC8742720b1BFC0A094e9e3B3a92",
"Main#VaultMathLib": "0x67bCC483fAaD61c3B7eBAdb31c884b8Aba9A13Ff",
"Main#InputsLib": "0x8B77b2Bf6A592361aF18803d785c25a1BBE1104c",
"Main#InvestmentVaultLib": "0x3B0775204AF03c2E9940CfbCB9fbeA0949CadA7B",
"Main#InputModifierLib": "0x47F8FB8136BFAFb9791D4C0CBf8e0764b02D619e",
"Main#InputModifierLib": "0x83d234D764a86c0485bab15E2827b9bF430abD2A",
"Main#VaultInitLib": "0x262166E3d46dA3a796BF7BE448bdddfd11FFB56f",
"Main#VaultDepositLib": "0x60d879820974964b793B60a9FA14103B7f9527B0",
"Main#VaultSpendLib": "0x94CE27CDFD55208678FB250FeD21B7f1FfdEc4Ff",
"Main#VaultImplementation": "0xF136A71D02675DC797863DBab7fB4E30b63d7680",
"Main#VaultDepositLib": "0x51b2787EF2379a4Dc06e71Ef442Eb4be80A0300d",
"Main#VaultSpendLib": "0x8649b2650B98F96F334793FEAF7F3D5Db7ad77A7",
"Main#VaultImplementation": "0x1b366810246346c7e9f746D5306F6BC9bF65b67D",
"Main#VaultProxy": "0xE0Eefe4AA0cB32740aDFD8083AbeC255AaC3b379",
"Main#Vault": "0xE0Eefe4AA0cB32740aDFD8083AbeC255AaC3b379",
"Main#ProtocolManager": "0xCC499487F6a1780dB9410a65408B98E63346aCfe"
"Main#Vault": "0xE0Eefe4AA0cB32740aDFD8083AbeC255AaC3b379"
}
41 changes: 34 additions & 7 deletions ignition/modules/VaultUpgrade.module.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";

export default buildModule("VaultUpgrade", (m) => {
const interestLib = m.contractAt("InterestLib", "0xe8426F0675C9Da7340b4E8801cDE6a5C59f23Eb7");

const vaultMathLib = m.contractAt("VaultMathLib", "0x67bCC483fAaD61c3B7eBAdb31c884b8Aba9A13Ff");
const investmentVaultLib = m.contractAt("InvestmentVaultLib", "0x3B0775204AF03c2E9940CfbCB9fbeA0949CadA7B");
const vaultInitLib = m.contractAt("VaultInitLib", "0x262166E3d46dA3a796BF7BE448bdddfd11FFB56f");
const inputsLib = m.contractAt("InputsLib", "0x8B77b2Bf6A592361aF18803d785c25a1BBE1104c");

const interestLib = m.library("InterestLib", {
id: `InterestLib_6`,
});

const inputModifierLib = m.contractAt("InputModifierLib", "0x47F8FB8136BFAFb9791D4C0CBf8e0764b02D619e");

const vaultDepositLib = m.contractAt("VaultDepositLib", "0x60d879820974964b793B60a9FA14103B7f9527B0");
const inputModifierLib = m.library("InputModifierLib", {
id: `InputModifierLib_6`,
libraries: { VaultMathLib: vaultMathLib, InterestLib: interestLib, InvestmentVaultLib: investmentVaultLib },
after: [inputsLib],
});

const vaultSpendLib = m.contractAt("VaultSpendLib", "0x94CE27CDFD55208678FB250FeD21B7f1FfdEc4Ff");
const vaultDepositLib = m.library("VaultDepositLib", {
id: `VaultDepositLib_6`,
libraries: {
VaultMathLib: vaultMathLib,
InterestLib: interestLib,
InvestmentVaultLib: investmentVaultLib,
InputsLib: inputsLib,
},
after: [inputModifierLib],
});

const vaultInitLib = m.contractAt("VaultInitLib", "0x262166E3d46dA3a796BF7BE448bdddfd11FFB56f");
const vaultSpendLib = m.library("VaultSpendLib", {
id: `VaultSpendLib_6`,
libraries: {
VaultMathLib: vaultMathLib,
InterestLib: interestLib,
InvestmentVaultLib: investmentVaultLib,
InputsLib: inputsLib,
},
after: [vaultDepositLib],
});

const newImplementation = m.contract("Vault", [], {
libraries: {
Expand All @@ -22,7 +48,8 @@ export default buildModule("VaultUpgrade", (m) => {
VaultSpendLib: vaultSpendLib,
InterestLib: interestLib,
},
id: `Vault_5`,
id: `Vault_6`,
after: [vaultSpendLib],
});

return {
Expand Down
4 changes: 2 additions & 2 deletions scripts/deployVaultUpgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import hre from "hardhat";
import { NetworkConfig } from "hardhat/types";
import { mkCreateXSalt } from "./mkCreateXSalt";
import VaultUpgradeModule from "../ignition/modules/VaultUpgrade.module";
import { deploymentChange } from "./deploymentChange";
// import { deploymentChange } from "./deploymentChange";
import { getIgnitionConfig } from "./getIgnitionConfig";

type Params = {
Expand Down Expand Up @@ -41,7 +41,7 @@ async function main() {
`New vault implementation deployed. Address: ${newImplementationAddress}, empty bytes params hash: 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470`,
);

await deploymentChange(deployer, params.donor);
// await deploymentChange(deployer, params.donor);
}

main().catch(console.error);
66 changes: 39 additions & 27 deletions src/vault/Interest.lib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ library InterestLib {
* @param token The token to preview the assets supply for
* @return The new assets supply
*/
function previewModifyAssets(
State storage state,
uint208 assets,
bool isGain,
address token
) public view returns (uint208) {
function previewModifyAssets(State storage state, uint208 assets, bool isGain, address token)
public
view
returns (uint208)
{
if (isGain) {
return state.interestStates[token].assetsSupply + assets;
} else {
Expand Down Expand Up @@ -90,15 +89,30 @@ library InterestLib {
function withdraw(State storage state, uint208 assets, address token) external {
if (assets == 0) revert AmountIsZero();
uint208 shares = _convertToShares(
state.interestStates[token].sharesSupply,
state.interestStates[token].assetsSupply,
assets,
Math.Rounding.Ceil
state.interestStates[token].sharesSupply, state.interestStates[token].assetsSupply, assets, Math.Rounding.Ceil
);
state.interestStates[token].sharesSupply -= shares;
state.interestStates[token].assetsSupply -= assets;
}

/**
* @notice Burns shares proportional to `feeAssets` without reducing `assetsSupply`.
* @dev Used to distribute protocol fees that stay in the vault as real tokens
* but were already counted in `assetsSupply`. Burning shares makes
* remaining shares worth more, effectively distributing the fee as interest.
* @param state The state of the vault
* @param feeAssets The fee amount (in asset units) to redistribute
* @param token The token to burn shares for
*/
function burnSharesForFee(State storage state, uint208 feeAssets, address token) external {
uint208 sharesSupply = state.interestStates[token].sharesSupply;
uint208 assetsSupply = state.interestStates[token].assetsSupply;
if (sharesSupply == 0 || assetsSupply == 0 || feeAssets == 0) return;
uint208 sharesToBurn = _convertToShares(sharesSupply, assetsSupply, feeAssets, Math.Rounding.Ceil);
if (sharesToBurn > sharesSupply) sharesToBurn = sharesSupply;
state.interestStates[token].sharesSupply = sharesSupply - sharesToBurn;
}

/**
* @notice Returns the assets per `scale()` shares
* @param state The state of the vault
Expand All @@ -116,11 +130,11 @@ library InterestLib {
* @param token The token to get the assets per `scale()` shares for
* @return The assets per `scale()` shares
*/
function previewAssetsPerScaledShare(
State storage state,
uint208 assets,
address token
) public view returns (uint208) {
function previewAssetsPerScaledShare(State storage state, uint208 assets, address token)
public
view
returns (uint208)
{
return _convertToAssets(state.interestStates[token].sharesSupply, assets, scale(), Math.Rounding.Floor);
}

Expand All @@ -132,12 +146,11 @@ library InterestLib {
* @param rounding The rounding direction
* @return The amount of assets
*/
function _convertToAssets(
uint256 sharesSupply,
uint208 assetsSupply,
uint208 shares,
Math.Rounding rounding
) private pure returns (uint208) {
function _convertToAssets(uint256 sharesSupply, uint208 assetsSupply, uint208 shares, Math.Rounding rounding)
private
pure
returns (uint208)
{
if (sharesSupply == 0 || assetsSupply == 0) {
return shares;
}
Expand All @@ -152,12 +165,11 @@ library InterestLib {
* @param rounding The rounding direction
* @return The amount of shares
*/
function _convertToShares(
uint208 sharesSupply,
uint208 assetsSupply,
uint208 assets,
Math.Rounding rounding
) private pure returns (uint208) {
function _convertToShares(uint208 sharesSupply, uint208 assetsSupply, uint208 assets, Math.Rounding rounding)
private
pure
returns (uint208)
{
if (sharesSupply == 0 || assetsSupply == 0) {
return assets;
}
Expand Down
10 changes: 8 additions & 2 deletions src/vault/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
CommitmentRecord,
DepositParams,
Transaction,
ICommitmentsRecipient
ICommitmentsRecipient,
InterestState
} from "src/vault/Vault.types.sol";

// libs
Expand Down Expand Up @@ -81,7 +82,7 @@ contract Vault is
);
}

function upgradeCallBack() external reinitializer(4) {}
function upgradeCallBack() external reinitializer(5) {}

/// @inheritdoc IVault
function approveUpgrade(address newImplementation, bytes32 paramsHash) external restricted {
Expand Down Expand Up @@ -268,4 +269,9 @@ contract Vault is
function getTokenDistributor() external view returns (address) {
return address(_getStorage().tokenDistributor);
}

/// @inheritdoc IVault
function getInterestState(address token) external view returns (InterestState memory) {
return _getStorage().interestStates[token];
}
}
7 changes: 7 additions & 0 deletions src/vault/Vault.types.sol
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,13 @@ interface IVault is IVaultEvents {
* @return The address of the TokenDistributor contract
*/
function getTokenDistributor() external view returns (address);

/**
* @notice Returns the interest state for a token
* @param token The token to get the interest state for
* @return interestState The interest state for the token
*/
function getInterestState(address token) external view returns (InterestState memory);
}

/**
Expand Down
39 changes: 15 additions & 24 deletions src/vault/VaultDeposit.lib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ library VaultDepositLib {
* @param depositParams The deposit parameters.
* @return netAmount The net deposit value excluding fees.
*/
function computeDepositValues(
State storage state,
DepositParams calldata depositParams
) internal view returns (uint208) {
function computeDepositValues(State storage state, DepositParams calldata depositParams)
internal
view
returns (uint208)
{
IProtocolManager manager = state.manager;
uint208 computedProtocolFee = VaultMathLib.calculateShareUp(
depositParams.amount,
manager.getFees(depositParams.token).deposit,
manager.getFeeScale()
depositParams.amount, manager.getFees(depositParams.token).deposit, manager.getFeeScale()
);

if (depositParams.protocolFee < computedProtocolFee) {
Expand All @@ -54,11 +53,8 @@ library VaultDepositLib {
function preDeposit(State storage state, DepositParams calldata depositParams) private {
(uint208 strategyDelta, bool isStrategyGain) = InvestmentVaultLib.snapshotStrategyDelta(state, depositParams.token);

(uint208 assetsDelta, bool isGain) = VaultMathLib.calculateAssetsDelta(
strategyDelta,
isStrategyGain,
depositParams.protocolFee
);
(uint208 assetsDelta, bool isGain) =
VaultMathLib.calculateAssetsDelta(strategyDelta, isStrategyGain, depositParams.protocolFee);

InterestLib.modifyAssets(state, assetsDelta, isGain, depositParams.token);
}
Expand All @@ -78,8 +74,7 @@ library VaultDepositLib {
revert IVaultErrors.CommitmentAlreadyUsed(poseidonHash);
}
state.commitmentsMap[depositParams.token][poseidonHash] = CommitmentRecord(
depositParams.depositCommitmentParams[i].owner,
InterestLib.assetsPerScaledShare(state, depositParams.token)
depositParams.depositCommitmentParams[i].owner, InterestLib.assetsPerScaledShare(state, depositParams.token)
);
emit IVaultEvents.CommitmentCreated(
depositParams.depositCommitmentParams[i].owner,
Expand All @@ -98,17 +93,14 @@ library VaultDepositLib {
* @param depositParams The deposit parameters.
* @param proof The proof of the deposit.
*/
function deposit(
State storage state,
address from,
DepositParams calldata depositParams,
uint256[24] calldata proof
) external {
function deposit(State storage state, address from, DepositParams calldata depositParams, uint256[24] calldata proof)
external
{
IERC20 t = IERC20(depositParams.token);
uint208 netAmount = computeDepositValues(state, depositParams);
if (
state.interestStates[depositParams.token].assetsSupply + depositParams.amount - depositParams.forwarderFee >
state.manager.getMaxTVL(depositParams.token)
state.interestStates[depositParams.token].assetsSupply + depositParams.amount - depositParams.forwarderFee
> state.manager.getMaxTVL(depositParams.token)
) {
revert IVaultErrors.AmountExceedsMaxTVL(
state.interestStates[depositParams.token].assetsSupply,
Expand All @@ -118,8 +110,7 @@ library VaultDepositLib {
}
if (
!state.verifiers.depositVerifier().verify(
proof,
InputsLib.depositInputs(depositParams.depositCommitmentParams, netAmount)
proof, InputsLib.depositInputs(depositParams.depositCommitmentParams, netAmount)
)
) {
revert IVaultErrors.InvalidZKProof();
Expand Down
2 changes: 1 addition & 1 deletion src/vault/VaultSpend.lib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ library VaultSpendLib {
processPublicOutputs(state, transaction, totalWithdraw);

if (protocolFee > 0) {
InterestLib.modifyAssets(state, protocolFee, true, token);
InterestLib.burnSharesForFee(state, protocolFee, token);
}

emit IVaultEvents.Spend(
Expand Down
Loading
Loading