|
| 1 | +import "FlowYieldVaultsInterfaces" |
1 | 2 |
|
2 | | -access(all) contract FlowYieldVaults { |
| 3 | +/// Registry of yield vault strategies on this account, keyed by name. |
| 4 | +/// An `Admin` resource (saved at `adminStoragePath` on the contract account) |
| 5 | +/// registers and removes strategies conforming to |
| 6 | +/// `FlowYieldVaultsInterfaces.Strategy`. Yield vaults are minted from a |
| 7 | +/// registered strategy by `name` through `createYieldVault`. |
| 8 | +/// |
| 9 | +/// This contract is strategy-agnostic: it does not depend on any specific |
| 10 | +/// strategy family. Concrete strategies live in their own contracts and are |
| 11 | +/// plugged in through `Admin.registerStrategy`. |
| 12 | +access(all) contract FlowYieldVaults: FlowYieldVaultsInterfaces { |
3 | 13 |
|
| 14 | + /// Emitted when a strategy is registered under `name`. |
| 15 | + access(all) event StrategyCreated(name: String) |
| 16 | + /// Emitted when a strategy is removed from the registry. |
| 17 | + access(all) event StrategyRemoved(name: String) |
| 18 | + /// Emitted when a yield vault is minted from the named strategy. |
| 19 | + access(all) event StrategyVaultCreated(name: String) |
| 20 | + |
| 21 | + /// Storage path where the `Admin` resource is saved on this account. |
| 22 | + access(all) let adminStoragePath: StoragePath |
| 23 | + |
| 24 | + /// Registered strategies, keyed by name. Names are unique; registering |
| 25 | + /// an already-used name panics. |
| 26 | + access(self) let strategies: {String: {FlowYieldVaultsInterfaces.Strategy}} |
| 27 | + |
| 28 | + /// Admin resource; holder may register / remove strategies and mint |
| 29 | + /// yield vaults directly (bypassing any external access gate). |
| 30 | + access(all) resource Admin { |
| 31 | + /// Registers `strategy` under `name`. |
| 32 | + /// Panics if a strategy is already registered under that name. |
| 33 | + /// |
| 34 | + /// **Parameters** |
| 35 | + /// - `name`: Unique identifier for the strategy in this registry. |
| 36 | + /// - `strategy`: Any value conforming to |
| 37 | + /// `FlowYieldVaultsInterfaces.Strategy`. |
| 38 | + access(all) fun registerStrategy( |
| 39 | + name: String, |
| 40 | + strategy: {FlowYieldVaultsInterfaces.Strategy} |
| 41 | + ) { |
| 42 | + assert( |
| 43 | + FlowYieldVaults.strategies[name] == nil, |
| 44 | + message: "Strategy already registered: \(name)" |
| 45 | + ) |
| 46 | + FlowYieldVaults.strategies[name] = strategy |
| 47 | + emit StrategyCreated(name: name) |
| 48 | + } |
| 49 | + |
| 50 | + /// Removes the strategy registered under `name`. |
| 51 | + /// Panics if no strategy is registered under that name. Does not |
| 52 | + /// affect already-minted yield vaults — those captured the strategy |
| 53 | + /// parameters at creation time. |
| 54 | + /// |
| 55 | + /// **Parameters** |
| 56 | + /// - `name`: Name of the strategy to remove. |
| 57 | + access(all) fun removeStrategy(name: String) { |
| 58 | + let strategy = FlowYieldVaults.strategies.remove(key: name) |
| 59 | + if strategy == nil { |
| 60 | + panic("Strategy not found") |
| 61 | + } |
| 62 | + emit StrategyRemoved(name: name) |
| 63 | + } |
| 64 | + |
| 65 | + /// Mints a yield vault from a registered strategy. |
| 66 | + /// Wrapper around the contract-level `createYieldVault` |
| 67 | + /// for callers that hold the admin resource. |
| 68 | + /// |
| 69 | + /// **Parameters** |
| 70 | + /// - `name`: Name of the registered strategy. |
| 71 | + /// |
| 72 | + /// **Returns** A new `YieldVault` for the caller to save in storage. |
| 73 | + access(all) fun createYieldVault(name: String): @{FlowYieldVaultsInterfaces.YieldVault} { |
| 74 | + return <- FlowYieldVaults.createYieldVault(name: name) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /// Mints a yield vault from a registered strategy. |
| 79 | + /// Panics if no strategy is registered under `name`. |
| 80 | + /// `access(account)` so that only contracts on this account (e.g. |
| 81 | + /// `FlowYieldVaultsEarlyAccess`) can gate or invoke vault creation. |
| 82 | + /// |
| 83 | + /// **Parameters** |
| 84 | + /// - `name`: Name of the registered strategy. |
| 85 | + /// |
| 86 | + /// **Returns** A new `YieldVault` for the caller to save in storage. |
| 87 | + access(account) fun createYieldVault(name: String): @{FlowYieldVaultsInterfaces.YieldVault} { |
| 88 | + let strategy = self.strategies[name] ?? panic("Strategy not found") |
| 89 | + let vault <- strategy.createYieldVault(name: name) |
| 90 | + emit StrategyVaultCreated(name: name) |
| 91 | + return <- vault |
| 92 | + } |
| 93 | + |
| 94 | + view access(all) fun strategyCount(): UInt64 { |
| 95 | + return UInt64(self.strategies.length) |
| 96 | + } |
| 97 | + |
| 98 | + view access(all) fun strategyNames(): [String] { |
| 99 | + return self.strategies.keys |
| 100 | + } |
| 101 | + |
| 102 | + init() { |
| 103 | + self.strategies = {} |
| 104 | + self.adminStoragePath = StoragePath(identifier: "FlowYieldVaultsAdmin")! |
| 105 | + self.account.storage.save(<- create Admin(), to: self.adminStoragePath) |
| 106 | + } |
4 | 107 | } |
0 commit comments