Skip to content
Open
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
71 changes: 67 additions & 4 deletions cadence/contracts/FlowYieldVaults.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,53 @@ access(all) contract FlowYieldVaults {
owner: Address?
)

/* --- VIEWS --- */

/// YieldVaultInfo
///
/// Minimal view struct providing basic identification and configuration details for a YieldVault
access(all) struct YieldVaultInfo {
/// The YieldVault's ID (DeFiActions.UniqueIdentifier.id)
access(all) let id: UInt64
/// The YieldVault resource uuid
access(all) let uuid: UInt64
/// The type identifier of the Vault this YieldVault operates on
access(all) let vaultTypeIdentifier: String
/// The strategy type identifier for this YieldVault
access(all) let strategyTypeIdentifier: String
/// The YieldVault owner's address if available
access(all) let owner: Address?

init(
id: UInt64,
uuid: UInt64,
vaultTypeIdentifier: String,
strategyTypeIdentifier: String,
owner: Address?
) {
self.id = id
self.uuid = uuid
self.vaultTypeIdentifier = vaultTypeIdentifier
self.strategyTypeIdentifier = strategyTypeIdentifier
self.owner = owner
}
}

/// YieldVaultBalance
///
/// Minimal view struct providing the YieldVault's current available balance for the vault's denomination. ///
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Minimal view struct providing the YieldVault's current available balance for the vault's denomination. ///
/// Minimal view struct providing the YieldVault's current available balance for the vault's denomination.

access(all) struct YieldVaultBalance {
/// The type identifier of the Vault this YieldVault operates on
access(all) let tokenTypeIdentifier: String
/// The current available balance for withdrawal
access(all) let availableBalance: UFix64

init(tokenTypeIdentifier: String, availableBalance: UFix64) {
self.tokenTypeIdentifier = tokenTypeIdentifier
self.availableBalance = availableBalance
}
}

/* --- CONSTRUCTS --- */

/// Strategy
Expand Down Expand Up @@ -268,12 +315,28 @@ access(all) contract FlowYieldVaults {
// Force unwrap to ensure burnCallback is called on the Strategy
Burner.burn(<-_strategy!)
}
/// TODO: FlowYieldVaults specific views
access(all) view fun getViews(): [Type] {
return []
return [
Type<YieldVaultInfo>(),
Type<YieldVaultBalance>()
]
}
/// TODO: FlowYieldVaults specific view resolution
access(all) fun resolveView(_ view: Type): AnyStruct? {
switch view {
case Type<YieldVaultInfo>():
return YieldVaultInfo(
id: self.id(),
uuid: self.uuid,
vaultTypeIdentifier: self.vaultType.identifier,
strategyTypeIdentifier: self.getStrategyType(),
owner: self.owner?.address
)
case Type<YieldVaultBalance>():
return YieldVaultBalance(
tokenTypeIdentifier: self.vaultType.identifier,
availableBalance: self.getYieldVaultBalance()
)
}
return nil
}
/// Deposits the provided Vault to the Strategy
Expand Down Expand Up @@ -308,7 +371,7 @@ access(all) contract FlowYieldVaults {
}
/// Returns the strategy type identifier for this YieldVault
access(all) view fun getStrategyType(): String {
return self.strategy.getType().identifier
return self._borrowStrategy().getType().identifier
}
/// Withdraws the requested amount from the Strategy
access(FungibleToken.Withdraw) fun withdraw(amount: UFix64): @{FungibleToken.Vault} {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import "FlowYieldVaults"

/// Returns the YieldVaultBalance view for the yieldVault with the given ID at the provided address or nil if either the
/// address does not have a YieldVaultManager stored, the YieldVault is not available, or the view cannot be resolved.
///
/// @param address: The address of the account to look for the YieldVault
/// @param id: The ID of the YieldVault to query
///
access(all)
fun main(address: Address, id: UInt64): FlowYieldVaults.YieldVaultBalance? {
if let manager = getAccount(address)
.capabilities.borrow<&FlowYieldVaults.YieldVaultManager>(FlowYieldVaults.YieldVaultManagerPublicPath)
{
if let yieldVault = manager.borrowYieldVault(id: id) {
if !yieldVault.getViews().contains(Type<FlowYieldVaults.YieldVaultBalance>()) {
return nil
}
return yieldVault.resolveView(Type<FlowYieldVaults.YieldVaultBalance>()) as? FlowYieldVaults.YieldVaultBalance
}
}
return nil
}
22 changes: 22 additions & 0 deletions cadence/scripts/flow-yield-vaults/get_yield_vault_info_view.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import "FlowYieldVaults"

/// Returns the YieldVaultInfo view for the yieldVault with the given ID at the provided address or nil if either the
/// address does not have a YieldVaultManager stored, the YieldVault is not available, or the view cannot be resolved.
///
/// @param address: The address of the account to look for the YieldVault
/// @param id: The ID of the YieldVault to query
///
access(all)
fun main(address: Address, id: UInt64): FlowYieldVaults.YieldVaultInfo? {
if let manager = getAccount(address)
.capabilities.borrow<&FlowYieldVaults.YieldVaultManager>(FlowYieldVaults.YieldVaultManagerPublicPath)
{
if let yieldVault = manager.borrowYieldVault(id: id) {
if !yieldVault.getViews().contains(Type<FlowYieldVaults.YieldVaultInfo>()) {
return nil
}
return yieldVault.resolveView(Type<FlowYieldVaults.YieldVaultInfo>()) as? FlowYieldVaults.YieldVaultInfo
}
}
return nil
}
15 changes: 15 additions & 0 deletions cadence/tests/test_helpers.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "MetadataViews"
import "FlowToken"
import "MOET"
import "FlowCreditMarket"
import "FlowYieldVaults"

access(all) let serviceAccount = Test.serviceAccount()

Expand Down Expand Up @@ -399,6 +400,20 @@ fun getYieldVaultBalance(address: Address, yieldVaultID: UInt64): UFix64? {
return res.returnValue as! UFix64?
}

access(all)
fun getYieldVaultInfoView(address: Address, yieldVaultID: UInt64): FlowYieldVaults.YieldVaultInfo? {
let res = _executeScript("../scripts/flow-yield-vaults/get_yield_vault_info_view.cdc", [address, yieldVaultID])
Test.expect(res, Test.beSucceeded())
return res.returnValue as! FlowYieldVaults.YieldVaultInfo?
}

access(all)
fun getYieldVaultBalanceView(address: Address, yieldVaultID: UInt64): FlowYieldVaults.YieldVaultBalance? {
let res = _executeScript("../scripts/flow-yield-vaults/get_yield_vault_balance_view.cdc", [address, yieldVaultID])
Test.expect(res, Test.beSucceeded())
return res.returnValue as! FlowYieldVaults.YieldVaultBalance?
}

access(all)
fun getAutoBalancerBalance(id: UInt64): UFix64? {
let res = _executeScript("../scripts/flow-yield-vaults/get_auto_balancer_balance_by_id.cdc", [id])
Expand Down
13 changes: 13 additions & 0 deletions cadence/tests/yield_vault_lifecycle_test.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "test_helpers.cdc"
import "FlowToken"
import "MOET"
import "YieldToken"
import "FlowYieldVaults"
import "FlowYieldVaultsStrategies"
import "FlowCreditMarket"

Expand Down Expand Up @@ -109,6 +110,18 @@ fun testLifecycle() {

log("✅ YieldVault created with ID: \(yieldVaultID)")

// Validate minimal YieldVault views
let info = getYieldVaultInfoView(address: user.address, yieldVaultID: yieldVaultID)
Test.assert(info != nil, message: "Expected YieldVaultInfo view to resolve")
Test.assertEqual(yieldVaultID, info!.id)
Test.assertEqual(flowTokenIdentifier, info!.vaultTypeIdentifier)
Test.assertEqual(strategyIdentifier, info!.strategyTypeIdentifier)

let balanceView = getYieldVaultBalanceView(address: user.address, yieldVaultID: yieldVaultID)
Test.assert(balanceView != nil, message: "Expected YieldVaultBalance view to resolve")
Test.assertEqual(flowTokenIdentifier, balanceView!.tokenTypeIdentifier)
Test.assertEqual(getYieldVaultBalance(address: user.address, yieldVaultID: yieldVaultID)!, balanceView!.availableBalance)

// 2. Deposit to YieldVault
depositToYieldVault(
signer: user,
Expand Down
Loading