Skip to content

Commit 276a16d

Browse files
authored
added a exteral method to compute value of each vault asset (#152)
1 parent 4d5b5e2 commit 276a16d

3 files changed

Lines changed: 81 additions & 2 deletions

File tree

spot-contracts/contracts/_interfaces/IVault.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ interface IVault {
7676
/// @return The total value of assets currently held by the vault, denominated in a standard unit of account.
7777
function getTVL() external returns (uint256);
7878

79+
/// @param token The address of the asset ERC-20 token held by the vault.
80+
/// @return The vault's asset token value, denominated in a standard unit of account.
81+
function getVaultAssetValue(IERC20Upgradeable token) external returns (uint256);
82+
7983
/// @notice The ERC20 token that can be deposited into this vault.
8084
function underlying() external view returns (IERC20Upgradeable);
8185

spot-contracts/contracts/vaults/RolloverVault.sol

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,31 @@ contract RolloverVault is
269269
return totalAssets;
270270
}
271271

272+
/// @inheritdoc IVault
273+
/// @dev The asset value is denominated in the underlying asset.
274+
function getVaultAssetValue(IERC20Upgradeable token) external override returns (uint256) {
275+
uint256 balance = token.balanceOf(address(this));
276+
if (balance <= 0) {
277+
return 0;
278+
}
279+
280+
// Underlying asset
281+
if (token == underlying) {
282+
return balance;
283+
}
284+
// Deployed asset
285+
else if (_deployed.contains(address(token))) {
286+
(uint256 collateralBalance, uint256 debt) = ITranche(address(token)).getTrancheCollateralization();
287+
return balance.mulDiv(collateralBalance, debt);
288+
}
289+
// Earned asset
290+
else if (address(token) == address(perp)) {
291+
return balance.mulDiv(IPerpetualTranche(address(perp)).getAvgPrice(), PERP_UNIT_PRICE);
292+
}
293+
294+
return 0;
295+
}
296+
272297
//--------------------------------------------------------------------------
273298
// External & Public read methods
274299

spot-contracts/test/vaults/RolloverVault_deposit_redeem.ts

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,12 @@ describe("RolloverVault", function () {
167167
expect(await vault.vaultAssetBalance(await vault.earnedAt(0))).to.eq(0);
168168
});
169169

170-
describe("#getTVL", function () {
170+
describe("get asset value", function () {
171171
describe("when vault is empty", function () {
172-
it("should return 0", async function () {
172+
it("should return 0 vaule", async function () {
173173
expect(await vault.callStatic.getTVL()).to.eq(0);
174+
expect(await vault.callStatic.getVaultAssetValue(collateralToken.address)).to.eq(0);
175+
expect(await vault.callStatic.getVaultAssetValue(perp.address)).to.eq(0);
174176
});
175177
});
176178

@@ -181,6 +183,10 @@ describe("RolloverVault", function () {
181183
it("should return tvl", async function () {
182184
expect(await vault.callStatic.getTVL()).to.eq(toFixedPtAmt("100"));
183185
});
186+
it("should return asset value", async function () {
187+
expect(await vault.callStatic.getVaultAssetValue(collateralToken.address)).to.eq(toFixedPtAmt("100"));
188+
expect(await vault.callStatic.getVaultAssetValue(perp.address)).to.eq(0);
189+
});
184190
});
185191

186192
describe("when vault has only deployed balance", function () {
@@ -199,6 +205,11 @@ describe("RolloverVault", function () {
199205
it("should return tvl", async function () {
200206
expect(await vault.callStatic.getTVL()).to.eq(toFixedPtAmt("100"));
201207
});
208+
it("should return asset value", async function () {
209+
expect(await vault.callStatic.getVaultAssetValue(collateralToken.address)).to.eq(0);
210+
expect(await vault.callStatic.getVaultAssetValue(reserveTranches[0].address)).to.eq(toFixedPtAmt("100"));
211+
expect(await vault.callStatic.getVaultAssetValue(perp.address)).to.eq(0);
212+
});
202213
});
203214

204215
describe("when vault has only earned balance", function () {
@@ -208,6 +219,10 @@ describe("RolloverVault", function () {
208219
it("should return tvl", async function () {
209220
expect(await vault.callStatic.getTVL()).to.eq(toFixedPtAmt("100"));
210221
});
222+
it("should return asset value", async function () {
223+
expect(await vault.callStatic.getVaultAssetValue(collateralToken.address)).to.eq(0);
224+
expect(await vault.callStatic.getVaultAssetValue(perp.address)).to.eq(toFixedPtAmt("100"));
225+
});
211226
});
212227

213228
describe("when vault has many balances", function () {
@@ -220,6 +235,14 @@ describe("RolloverVault", function () {
220235
it("should return tvl", async function () {
221236
expect(await vault.callStatic.getTVL()).to.eq(toFixedPtAmt("2200"));
222237
});
238+
it("should return asset value", async function () {
239+
expect(await vault.callStatic.getVaultAssetValue(collateralToken.address)).to.eq(toFixedPtAmt("100"));
240+
expect(await vault.callStatic.getVaultAssetValue(reserveTranches[0].address)).to.eq(toFixedPtAmt("200"));
241+
expect(await vault.callStatic.getVaultAssetValue(reserveTranches[1].address)).to.eq(toFixedPtAmt("200"));
242+
expect(await vault.callStatic.getVaultAssetValue(rolloverInTranches[1].address)).to.eq(toFixedPtAmt("600"));
243+
expect(await vault.callStatic.getVaultAssetValue(rolloverInTranches[2].address)).to.eq(toFixedPtAmt("1000"));
244+
expect(await vault.callStatic.getVaultAssetValue(perp.address)).to.eq(toFixedPtAmt("100"));
245+
});
223246
});
224247

225248
describe("when vault has many balances and rebases up", function () {
@@ -233,6 +256,14 @@ describe("RolloverVault", function () {
233256
it("should return tvl", async function () {
234257
expect(await vault.callStatic.getTVL()).to.eq(toFixedPtAmt("2410"));
235258
});
259+
it("should return asset value", async function () {
260+
expect(await vault.callStatic.getVaultAssetValue(collateralToken.address)).to.eq(toFixedPtAmt("110"));
261+
expect(await vault.callStatic.getVaultAssetValue(reserveTranches[0].address)).to.eq(toFixedPtAmt("200"));
262+
expect(await vault.callStatic.getVaultAssetValue(reserveTranches[1].address)).to.eq(toFixedPtAmt("200"));
263+
expect(await vault.callStatic.getVaultAssetValue(rolloverInTranches[1].address)).to.eq(toFixedPtAmt("600"));
264+
expect(await vault.callStatic.getVaultAssetValue(rolloverInTranches[2].address)).to.eq(toFixedPtAmt("1200"));
265+
expect(await vault.callStatic.getVaultAssetValue(perp.address)).to.eq(toFixedPtAmt("100"));
266+
});
236267
});
237268

238269
describe("when vault has many balances and rebases down", function () {
@@ -246,6 +277,14 @@ describe("RolloverVault", function () {
246277
it("should return tvl", async function () {
247278
expect(await vault.callStatic.getTVL()).to.eq(toFixedPtAmt("1990"));
248279
});
280+
it("should return asset value", async function () {
281+
expect(await vault.callStatic.getVaultAssetValue(collateralToken.address)).to.eq(toFixedPtAmt("90"));
282+
expect(await vault.callStatic.getVaultAssetValue(reserveTranches[0].address)).to.eq(toFixedPtAmt("200"));
283+
expect(await vault.callStatic.getVaultAssetValue(reserveTranches[1].address)).to.eq(toFixedPtAmt("200"));
284+
expect(await vault.callStatic.getVaultAssetValue(rolloverInTranches[1].address)).to.eq(toFixedPtAmt("600"));
285+
expect(await vault.callStatic.getVaultAssetValue(rolloverInTranches[2].address)).to.eq(toFixedPtAmt("800"));
286+
expect(await vault.callStatic.getVaultAssetValue(perp.address)).to.eq(toFixedPtAmt("100"));
287+
});
249288
});
250289

251290
describe("when vault has many balances and rebases down below threshold", function () {
@@ -287,6 +326,17 @@ describe("RolloverVault", function () {
287326
it("should return tvl", async function () {
288327
expect(await vault.callStatic.getTVL()).to.eq(toFixedPtAmt("390"));
289328
});
329+
330+
it("should return asset value", async function () {
331+
expect(await vault.callStatic.getVaultAssetValue(collateralToken.address)).to.eq(toFixedPtAmt("10"));
332+
expect(await vault.callStatic.getVaultAssetValue(reserveTranches[0].address)).to.eq(toFixedPtAmt("100"));
333+
expect(await vault.callStatic.getVaultAssetValue(reserveTranches[1].address)).to.eq(toFixedPtAmt("0"));
334+
expect(await vault.callStatic.getVaultAssetValue(reserveTranches[2].address)).to.eq(toFixedPtAmt("0"));
335+
expect(await vault.callStatic.getVaultAssetValue(rolloverInTranches[0].address)).to.eq(toFixedPtAmt("250"));
336+
expect(await vault.callStatic.getVaultAssetValue(rolloverInTranches[1].address)).to.eq(toFixedPtAmt("0"));
337+
expect(await vault.callStatic.getVaultAssetValue(rolloverInTranches[2].address)).to.eq(toFixedPtAmt("0"));
338+
expect(await vault.callStatic.getVaultAssetValue(perp.address)).to.eq(toFixedPtAmt("30"));
339+
});
290340
});
291341
});
292342

0 commit comments

Comments
 (0)