Skip to content

Commit 399ab8e

Browse files
committed
validate position exists at createPaidRebalancer time
1 parent df47225 commit 399ab8e

5 files changed

Lines changed: 30 additions & 3 deletions

File tree

cadence/contracts/FlowALPModels.cdc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,6 +2123,9 @@ access(all) contract FlowALPModels {
21232123
pullFromTopUpSource: Bool
21242124
): @{FungibleToken.Vault}
21252125

2126+
/// Returns true if a position with the given ID exists in the pool.
2127+
access(all) view fun positionExists(pid: UInt64): Bool
2128+
21262129
/// Rebalances the specified position.
21272130
access(EPosition | ERebalance) fun rebalancePosition(pid: UInt64, force: Bool)
21282131

cadence/contracts/FlowALPRebalancerPaidv1.cdc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,9 @@ access(all) contract FlowALPRebalancerPaidv1 {
239239
/// Returns a RebalancerPaid resource; the underlying PositionRebalancer is stored in this contract
240240
/// and the first run is scheduled.
241241
access(all) fun createPaidRebalancer(positionID: UInt64) {
242-
let rebalancer <- create PositionRebalancer(
243-
positionID: positionID
244-
)
242+
let pool = self.poolCap!.borrow()!
243+
assert(pool.positionExists(pid: positionID), message: "Invalid position ID \(positionID) - position does not exist")
244+
let rebalancer <- create PositionRebalancer(positionID: positionID)
245245
// will panic if the rebalancer already exists
246246
self.storeRebalancer(rebalancer: <-rebalancer, positionID: positionID)
247247
self.setSelfCapability(positionID: positionID).fixReschedule()

cadence/contracts/FlowALPv0.cdc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,10 @@ access(all) contract FlowALPv0 {
16941694
/// Rebalancing is done on a best effort basis (even when force=true). If the position has no sink/source,
16951695
/// of either cannot accept/provide sufficient funds for rebalancing, the rebalance will still occur but will
16961696
/// not cause the position to reach its target health.
1697+
access(all) view fun positionExists(pid: UInt64): Bool {
1698+
return self.positions[pid] != nil
1699+
}
1700+
16971701
access(FlowALPModels.EPosition | FlowALPModels.ERebalance) fun rebalancePosition(pid: UInt64, force: Bool) {
16981702
pre {
16991703
!self.isPaused(): "Withdrawal, deposits, and liquidations are paused by governance"

cadence/tests/paid_auto_balance_test.cdc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,19 @@ access(all) fun test_supervisor_stale_uuid_does_not_panic() {
320320
Test.assertEqual(1, removedEvts2.length)
321321
}
322322

323+
access(all) fun test_invalid_position_id() {
324+
let invalidPositionID: UInt64 = 9999
325+
326+
let createRes = Test.executeTransaction(Test.Transaction(
327+
code: Test.readFile("./transactions/rebalancer/add_paid_rebalancer_with_id.cdc"),
328+
authorizers: [protocolAccount.address],
329+
signers: [protocolAccount],
330+
arguments: [invalidPositionID]
331+
))
332+
Test.expect(createRes, Test.beFailed())
333+
Test.assertError(createRes, errorMessage: "position does not exist")
334+
}
335+
323336
access(all) fun test_supervisor() {
324337
Test.moveTime(by: 100.0)
325338
Test.commitBlock()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import "FlowALPRebalancerPaidv1"
2+
3+
transaction(positionID: UInt64) {
4+
prepare(signer: auth(Storage) &Account) {
5+
FlowALPRebalancerPaidv1.createPaidRebalancer(positionID: positionID)
6+
}
7+
}

0 commit comments

Comments
 (0)