@@ -705,7 +705,7 @@ access(all) contract TidalProtocol {
705705 self .globalLedger [type ] ! = nil : " Invalid token type"
706706 amount > 0.0 : " Withdrawal amount must be positive" // TODO: consider empty vault early return
707707 }
708- log (" ...entered withdrawAndPull scope..." )
708+ log (" ...entered withdrawAndPull scope for pid \( pid ) vault \( type . identifier ) amount \( amount ) pullFromTopUpSource \( pullFromTopUpSource ) ..." )
709709
710710 // Get a reference to the user's position and global token state for the affected token.
711711 let position = (&self .positions [pid ] as auth (EImplementation ) &InternalPosition ? )!
@@ -1667,94 +1667,6 @@ access(all) contract TidalProtocol {
16671667 }
16681668 }
16691669
1670- // DFB.Sink implementation for TidalProtocol
1671- access (all ) struct TidalProtocolSink : DFB .Sink {
1672- access (contract ) let uniqueID : DFB .UniqueIdentifier ? // TODO: Consider how this field will be set
1673- access (contract ) let pool : Capability <auth (EPosition ) &Pool >
1674- access (contract ) let positionID : UInt64
1675- access (contract ) let tokenType : Type
1676-
1677- init (pool : Capability <auth (EPosition ) &Pool >, positionID : UInt64 , tokenType : Type ) {
1678- pre {
1679- pool .check (): " Invalid Pool Capability provided - cannot construct TidalProtocolSink"
1680- }
1681- self .uniqueID = nil
1682- self .pool = pool
1683- self .positionID = positionID
1684- self .tokenType = tokenType
1685- }
1686-
1687- access (all ) view fun getSinkType (): Type {
1688- return self .tokenType
1689- }
1690-
1691- access (all ) fun minimumCapacity (): UFix64 {
1692- // For now, return max as there's no limit
1693- // TODO: Consider sentinel value returned by DFB.Sink.minimumCapacity - perhaps make optional & `nil` == no_limit
1694- return UFix64.max
1695- }
1696-
1697- access (all ) fun depositCapacity (from : auth (FungibleToken.Withdraw ) &{FungibleToken .Vault }) {
1698- pre {
1699- self .pool .check (): " Internal Pool Capability is invalid - cannot depositCapacity"
1700- }
1701- if from .balance == 0.0 || self .getSinkType () ! = from .getType () {
1702- return
1703- }
1704- let vault <- from .withdraw (amount : from .balance )
1705- self .pool .borrow ()! .deposit (pid : self .positionID , funds : <- vault )
1706- }
1707- }
1708-
1709- // DFB.Source implementation for TidalProtocol
1710- access (all ) struct TidalProtocolSource : DFB .Source {
1711- access (contract ) let uniqueID : DFB .UniqueIdentifier ?
1712- access (contract ) let pool : Capability <auth (EPosition ) &Pool >
1713- access (contract ) let positionID : UInt64
1714- access (contract ) let tokenType : Type
1715-
1716- init (pool : Capability <auth (EPosition ) &Pool >, positionID : UInt64 , tokenType : Type ) {
1717- pre {
1718- pool .check (): " Invalid Pool Capability provided - cannot construct TidalProtocolSource"
1719- }
1720- self .uniqueID = nil
1721- self .pool = pool
1722- self .positionID = positionID
1723- self .tokenType = tokenType
1724- }
1725-
1726- access (all ) view fun getSourceType (): Type {
1727- return self .tokenType
1728- }
1729-
1730- access (all ) fun minimumAvailable (): UFix64 {
1731- pre {
1732- self .pool .check (): " Internal Pool Capability is invalid"
1733- }
1734- // Return the available balance for withdrawal
1735- let position = self .pool .borrow ()! .getPositionDetails (pid : self .positionID )
1736- for balance in position .balances {
1737- if balance .type == self .tokenType && balance .direction == BalanceDirection .Credit {
1738- return balance .balance
1739- }
1740- }
1741- return 0.0
1742- }
1743-
1744- access (FungibleToken.Withdraw ) fun withdrawAvailable (maxAmount : UFix64 ): @{FungibleToken .Vault } {
1745- pre {
1746- self .pool .check (): " Internal Pool Capability is invalid - cannot withdrawAvailable"
1747- }
1748- let available = self .minimumAvailable ()
1749- let withdrawAmount = available < maxAmount ? available : maxAmount
1750- if withdrawAmount > 0.0 {
1751- return <- self .pool .borrow ()! .withdraw (pid : self .positionID , amount : withdrawAmount , type : self .tokenType )
1752- } else {
1753- return <- DFBUtils .getEmptyVault (self .tokenType )
1754- }
1755- }
1756- }
1757-
17581670 // RESTORED: Enhanced position sink from Dieter's implementation
17591671 access (all ) struct PositionSink : DFB .Sink {
17601672 access (contract ) let uniqueID : DFB .UniqueIdentifier ?
@@ -1763,26 +1675,31 @@ access(all) contract TidalProtocol {
17631675 access (self ) let type : Type
17641676 access (self ) let pushToDrawDownSink : Bool
17651677
1678+ init (id : UInt64 , pool : Capability <auth (EPosition ) &Pool >, type : Type , pushToDrawDownSink : Bool ) {
1679+ self .uniqueID = nil
1680+ self .positionID = id
1681+ self .pool = pool
1682+ self .type = type
1683+ self .pushToDrawDownSink = pushToDrawDownSink
1684+ }
1685+
17661686 access (all ) view fun getSinkType (): Type {
17671687 return self .type
17681688 }
17691689
17701690 access (all ) fun minimumCapacity (): UFix64 {
1771- // A position object has no limit to deposits
1772- return UFix64.max
1691+ // A position object has no limit to deposits unless the Capability has been revoked
1692+ return self . pool . check () ? UFix64.max : 0.0
17731693 }
17741694
17751695 access (all ) fun depositCapacity (from : auth (FungibleToken.Withdraw ) &{FungibleToken .Vault }) {
1776- let pool = self .pool .borrow ()!
1777- pool .depositAndPush (pid : self .positionID , from : <- from .withdraw (amount : from .balance ), pushToDrawDownSink : self .pushToDrawDownSink )
1778- }
1779-
1780- init (id : UInt64 , pool : Capability <auth (EPosition ) &Pool >, type : Type , pushToDrawDownSink : Bool ) {
1781- self .uniqueID = nil
1782- self .positionID = id
1783- self .pool = pool
1784- self .type = type
1785- self .pushToDrawDownSink = pushToDrawDownSink
1696+ if let pool = self .pool .borrow () {
1697+ pool .depositAndPush (
1698+ pid : self .positionID ,
1699+ from : <- from .withdraw (amount : from .balance ),
1700+ pushToDrawDownSink : self .pushToDrawDownSink
1701+ )
1702+ }
17861703 }
17871704 }
17881705
@@ -1794,34 +1711,40 @@ access(all) contract TidalProtocol {
17941711 access (self ) let type : Type
17951712 access (self ) let pullFromTopUpSource : Bool
17961713
1714+ init (id : UInt64 , pool : Capability <auth (EPosition ) &Pool >, type : Type , pullFromTopUpSource : Bool ) {
1715+ self .uniqueID = nil
1716+ self .positionID = id
1717+ self .pool = pool
1718+ self .type = type
1719+ self .pullFromTopUpSource = pullFromTopUpSource
1720+ }
1721+
17971722 access (all ) view fun getSourceType (): Type {
17981723 return self .type
17991724 }
18001725
18011726 access (all ) fun minimumAvailable (): UFix64 {
1727+ if ! self .pool .check () {
1728+ return 0.0
1729+ }
18021730 let pool = self .pool .borrow ()!
18031731 return pool .availableBalance (pid : self .positionID , type : self .type , pullFromTopUpSource : self .pullFromTopUpSource )
18041732 }
18051733
18061734 access (FungibleToken.Withdraw ) fun withdrawAvailable (maxAmount : UFix64 ): @{FungibleToken .Vault } {
1735+ if ! self .pool .check () {
1736+ return <- DFBUtils .getEmptyVault (self .type )
1737+ }
18071738 let pool = self .pool .borrow ()!
18081739 let available = pool .availableBalance (pid : self .positionID , type : self .type , pullFromTopUpSource : self .pullFromTopUpSource )
18091740 let withdrawAmount = (available > maxAmount ) ? maxAmount : available
18101741 if withdrawAmount > 0.0 {
18111742 return <- pool .withdrawAndPull (pid : self .positionID , type : self .type , amount : withdrawAmount , pullFromTopUpSource : self .pullFromTopUpSource )
18121743 } else {
18131744 // Create an empty vault - this is a limitation we need to handle properly
1814- panic ( " Cannot create empty vault for type: " . concat (self .type . identifier ) )
1745+ return <- DFBUtils . getEmptyVault (self .type )
18151746 }
18161747 }
1817-
1818- init (id : UInt64 , pool : Capability <auth (EPosition ) &Pool >, type : Type , pullFromTopUpSource : Bool ) {
1819- self .uniqueID = nil
1820- self .positionID = id
1821- self .pool = pool
1822- self .type = type
1823- self .pullFromTopUpSource = pullFromTopUpSource
1824- }
18251748 }
18261749
18271750 access (all ) enum BalanceDirection : UInt8 {
0 commit comments