@@ -18,6 +18,8 @@ access(all) contract TidalProtocol {
1818 /// The canonical PublicPath where the primary TidalProtocol Pool can be accessed publicly
1919 access (all ) let PoolPublicPath : PublicPath
2020
21+ access (all ) let PoolCapStoragePath : StoragePath
22+
2123 /* --- EVENTS ---- */
2224
2325 access (all ) event Opened (pid : UInt64 , poolUUID : UInt64 )
@@ -31,6 +33,9 @@ access(all) contract TidalProtocol {
3133 access (all ) entitlement EGovernance
3234 access (all ) entitlement EImplementation
3335
36+ access (all ) entitlement EParticipant
37+
38+
3439 /// InternalBalance
3540 ///
3641 /// A structure used internally to track a position's balance for a particular token
@@ -55,7 +60,7 @@ access(all) contract TidalProtocol {
5560 /// provided TokenState. It's assumed the TokenState and InternalBalance relate to the same token Type, but
5661 /// since neither struct have values defining the associated token, callers should be sure to make the arguments
5762 /// do in fact relate to the same token Type.
58- access (all ) fun recordDeposit (amount : UInt128 , tokenState : auth (EImplementation ) &TokenState ) {
63+ access (contract ) fun recordDeposit (amount : UInt128 , tokenState : auth (EImplementation ) &TokenState ) {
5964 if self .direction == BalanceDirection .Credit {
6065 // Depositing into a credit position just increases the balance.
6166
@@ -216,7 +221,7 @@ access(all) contract TidalProtocol {
216221 /// deposits or the operation will revert.
217222 access (EImplementation ) fun setDrawDownSink (_ sink : {DeFiActions .Sink }? ) {
218223 pre {
219- sink ?. getSinkType () ?? Type <@MOET.Vault >() == Type <@MOET.Vault >():
224+ ( sink ?. getSinkType () ?? Type <@MOET.Vault >() ) == Type <@MOET.Vault >():
220225 " Invalid Sink provided - Sink \( sink .getType ().identifier ) must accept MOET"
221226 }
222227 self .drawDownSink = sink
@@ -743,7 +748,7 @@ access(all) contract TidalProtocol {
743748 access (all ) fun getPositionDetails (pid : UInt64 ): PositionDetails {
744749 log (" [CONTRACT] getPositionDetails(pid: \( pid ) )" )
745750 let position = self ._borrowPosition (pid : pid )
746- let balances : [PositionBalance ] = []
751+ var balances : [PositionBalance ] = []
747752
748753 for type in position .balances .keys {
749754 let balance = position .balances [type ]!
@@ -1298,7 +1303,7 @@ access(all) contract TidalProtocol {
12981303 /// Creates a lending position against the provided collateral funds, depositing the loaned amount to the
12991304 /// given Sink. If a Source is provided, the position will be configured to pull loan repayment when the loan
13001305 /// becomes undercollateralized, preferring repayment to outright liquidation.
1301- access (all ) fun createPosition (
1306+ access (EParticipant ) fun createPosition (
13021307 funds : @{FungibleToken .Vault },
13031308 issuanceSink : {DeFiActions .Sink },
13041309 repaymentSource : {DeFiActions .Source }? ,
@@ -1333,7 +1338,7 @@ access(all) contract TidalProtocol {
13331338
13341339 /// Allows anyone to deposit funds into any position. If the provided Vault is not supported by the Pool, the
13351340 /// operation reverts.
1336- access (all ) fun depositToPosition (pid : UInt64 , from : @{FungibleToken .Vault }) {
1341+ access (EParticipant ) fun depositToPosition (pid : UInt64 , from : @{FungibleToken .Vault }) {
13371342 self .depositAndPush (pid : pid , from : <- from , pushToDrawDownSink : false )
13381343 }
13391344
@@ -1845,9 +1850,9 @@ access(all) contract TidalProtocol {
18451850 /// The unique ID of the Position used to track deposits and withdrawals to the Pool
18461851 access (self ) let id : UInt64
18471852 /// An authorized Capability to which the Position was opened
1848- access (self ) let pool : Capability <auth (EPosition ) &Pool >
1853+ access (self ) let pool : Capability <auth (EPosition , EParticipant ) &Pool >
18491854
1850- init (id : UInt64 , pool : Capability <auth (EPosition ) &Pool >) {
1855+ init (id : UInt64 , pool : Capability <auth (EPosition , EParticipant ) &Pool >) {
18511856 pre {
18521857 pool .check (): " Invalid Pool Capability provided - cannot construct Position"
18531858 }
@@ -1905,13 +1910,13 @@ access(all) contract TidalProtocol {
19051910 }
19061911 /// Deposits funds to the Position without pushing to the drawDownSink if the deposit puts the Position above
19071912 /// its maximum health
1908- access (all ) fun deposit (from : @{FungibleToken .Vault }) {
1913+ access (EParticipant ) fun deposit (from : @{FungibleToken .Vault }) {
19091914 let pool = self .pool .borrow ()!
19101915 pool .depositAndPush (pid : self .id , from : <- from , pushToDrawDownSink : false )
19111916 }
19121917 /// Deposits funds to the Position enabling the caller to configure whether excess value should be pushed to the
19131918 /// drawDownSink if the deposit puts the Position above its maximum health
1914- access (all ) fun depositAndPush (from : @{FungibleToken .Vault }, pushToDrawDownSink : Bool ) {
1919+ access (EParticipant ) fun depositAndPush (from : @{FungibleToken .Vault }, pushToDrawDownSink : Bool ) {
19151920 let pool = self .pool .borrow ()!
19161921 pool .depositAndPush (pid : self .id , from : <- from , pushToDrawDownSink : pushToDrawDownSink )
19171922 }
@@ -1973,7 +1978,7 @@ access(all) contract TidalProtocol {
19731978 /// Each position can have only one source, and the source must accept the default token type configured for the
19741979 /// pool. Providing a new source will replace the existing source. Pass nil to configure the position to not
19751980 /// pull tokens.
1976- access (all ) fun provideSource (source : {DeFiActions .Source }? ) {
1981+ access (EParticipant ) fun provideSource (source : {DeFiActions .Source }? ) {
19771982 let pool = self .pool .borrow ()!
19781983 pool .provideTopUpSource (pid : self .id , source : source )
19791984 }
@@ -2157,38 +2162,6 @@ access(all) contract TidalProtocol {
21572162 }
21582163
21592164 /* --- PUBLIC METHODS ---- */
2160-
2161- /// Takes out a TidalProtocol loan with the provided collateral, returning a Position that can be used to manage
2162- /// collateral and borrowed fund flows
2163- ///
2164- /// @param collateral: The collateral used as the basis for a loan. Only certain collateral types are supported, so
2165- /// callers should be sure to check the provided Vault is supported to prevent reversion.
2166- /// @param issuanceSink: The DeFiActions Sink connector where the protocol will deposit borrowed funds. If the
2167- /// position becomes overcollateralized, additional funds will be borrowed (to maintain target LTV) and
2168- /// deposited to the provided Sink.
2169- /// @param repaymentSource: An optional DeFiActions Source connector from which the protocol will attempt to source
2170- /// borrowed funds in the event of undercollateralization prior to liquidating. If none is provided, the
2171- /// position health will not be actively managed on the down side, meaning liquidation is possible as soon as
2172- /// the loan becomes undercollateralized.
2173- ///
2174- /// @return the Position via which the caller can manage their position
2175- ///
2176- access (all ) fun openPosition (
2177- collateral : @{FungibleToken .Vault },
2178- issuanceSink : {DeFiActions .Sink },
2179- repaymentSource : {DeFiActions .Source }? ,
2180- pushToDrawDownSink : Bool
2181- ): Position {
2182- let pid = self ._borrowPool ().createPosition (
2183- funds : <- collateral ,
2184- issuanceSink : issuanceSink ,
2185- repaymentSource : repaymentSource ,
2186- pushToDrawDownSink : pushToDrawDownSink
2187- )
2188- let cap = self .account .capabilities .storage .issue <auth (EPosition ) &Pool >(self .PoolStoragePath )
2189- return Position (id : pid , pool : cap )
2190- }
2191-
21922165 /// Returns a health value computed from the provided effective collateral and debt values where health is a ratio
21932166 /// of effective collateral over effective debt
21942167 access (all ) view fun healthComputation (effectiveCollateral : UInt128 , effectiveDebt : UInt128 ): UInt128 {
@@ -2214,7 +2187,7 @@ access(all) contract TidalProtocol {
22142187
22152188 /// Returns the compounded interest index reflecting the passage of time
22162189 /// The result is: newIndex = oldIndex * perSecondRate ^ seconds
2217- access (all ) fun compoundInterestIndex (oldIndex : UInt128 , perSecondRate : UInt128 , elapsedSeconds : UFix64 ): UInt128 {
2190+ access (all ) view fun compoundInterestIndex (oldIndex : UInt128 , perSecondRate : UInt128 , elapsedSeconds : UFix64 ): UInt128 {
22182191 var result = oldIndex
22192192 var current = UInt128 (perSecondRate )
22202193 var secondsCounter = UInt128 (elapsedSeconds )
@@ -2258,12 +2231,6 @@ access(all) contract TidalProtocol {
22582231
22592232 /* --- INTERNAL METHODS --- */
22602233
2261- /// Returns an authorized reference to the contract account's Pool resource
2262- access (self ) view fun _borrowPool (): auth (EPosition ) &Pool {
2263- return self .account .storage .borrow <auth (EPosition ) &Pool >(from : self .PoolStoragePath )
2264- ?? panic (" Could not borrow reference to internal TidalProtocol Pool resource" )
2265- }
2266-
22672234 /// Returns a reference to the contract account's MOET Minter resource
22682235 access (self ) view fun _borrowMOETMinter (): &MOET.Minter {
22692236 return self .account .storage .borrow <&MOET.Minter >(from : MOET .AdminStoragePath )
@@ -2274,6 +2241,7 @@ access(all) contract TidalProtocol {
22742241 self .PoolStoragePath = StoragePath (identifier : " tidalProtocolPool_\( self .account .address ) " )!
22752242 self .PoolFactoryPath = StoragePath (identifier : " tidalProtocolPoolFactory_\( self .account .address ) " )!
22762243 self .PoolPublicPath = PublicPath (identifier : " tidalProtocolPool_\( self .account .address ) " )!
2244+ self .PoolCapStoragePath = StoragePath (identifier : " tidalProtocolPoolCap_\( self .account .address ) " )!
22772245
22782246 // save PoolFactory in storage
22792247 self .account .storage .save (
0 commit comments