Skip to content

Commit 5de2cc0

Browse files
authored
Merge pull request #306 from onflow/nialexsan/restore-back-compat-position-details
Nialexsan/restore back compat position details
2 parents 43270a9 + 8e1cbbe commit 5de2cc0

4 files changed

Lines changed: 20 additions & 19 deletions

File tree

cadence/contracts/FlowALPv0.cdc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,7 +1927,6 @@ access(all) contract FlowALPv0 {
19271927
)
19281928

19291929
return PositionDetails(
1930-
id: pid,
19311930
balances: balances,
19321931
poolDefaultToken: self.defaultToken,
19331932
defaultTokenAvailableBalance: defaultTokenAvailable,
@@ -4783,9 +4782,6 @@ access(all) contract FlowALPv0 {
47834782
/// This structure is NOT used internally.
47844783
access(all) struct PositionDetails {
47854784

4786-
/// The unique identifier of the position
4787-
access(all) let id: UInt64
4788-
47894785
/// Balance details about each Vault Type deposited to the related Position
47904786
access(all) let balances: [PositionBalance]
47914787

@@ -4799,13 +4795,11 @@ access(all) contract FlowALPv0 {
47994795
access(all) let health: UFix128
48004796

48014797
init(
4802-
id: UInt64,
48034798
balances: [PositionBalance],
48044799
poolDefaultToken: Type,
48054800
defaultTokenAvailableBalance: UFix64,
48064801
health: UFix128
48074802
) {
4808-
self.id = id
48094803
self.balances = balances
48104804
self.poolDefaultToken = poolDefaultToken
48114805
self.defaultTokenAvailableBalance = defaultTokenAvailableBalance

cadence/scripts/flow-alp/get_positions_by_ids.cdc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
// are silently skipped.
44
import "FlowALPv0"
55

6-
access(all) fun main(positionIDs: [UInt64]): [FlowALPv0.PositionDetails] {
6+
access(all) fun main(positionIDs: [UInt64]): [FlowALPv0.PositionDetails?] {
77
let protocolAddress = Type<@FlowALPv0.Pool>().address!
88
let account = getAccount(protocolAddress)
99
let pool = account.capabilities.borrow<&FlowALPv0.Pool>(FlowALPv0.PoolPublicPath)
1010
?? panic("Could not find Pool at path \(FlowALPv0.PoolPublicPath)")
1111

12-
let details: [FlowALPv0.PositionDetails] = []
12+
let details: [FlowALPv0.PositionDetails?] = []
1313
for id in positionIDs {
1414
if let detail = pool.tryGetPositionDetails(pid: id) {
1515
details.append(detail)
16+
} else {
17+
details.append(nil)
1618
}
1719
}
1820
return details

cadence/tests/get_positions_by_ids_test.cdc

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ fun test_getPositionsByIDs() {
5151
let details0 = getPositionDetails(pid: 0, beFailed: false)
5252
let details1 = getPositionDetails(pid: 1, beFailed: false)
5353

54-
Test.assertEqual(details0.health, details[0].health)
55-
Test.assertEqual(details0.balances.length, details[0].balances.length)
54+
Test.assertEqual(details0.health, details[0]!.health)
55+
Test.assertEqual(details0.balances.length, details[0]!.balances.length)
5656

57-
Test.assertEqual(details1.health, details[1].health)
58-
Test.assertEqual(details1.balances.length, details[1].balances.length)
57+
Test.assertEqual(details1.health, details[1]!.health)
58+
Test.assertEqual(details1.balances.length, details[1]!.balances.length)
5959

6060
// --- Empty input returns empty array ---
6161
let emptyDetails = getPositionsByIDs(positionIDs: [])
@@ -64,21 +64,26 @@ fun test_getPositionsByIDs() {
6464
// --- Single ID works ---
6565
let singleDetails = getPositionsByIDs(positionIDs: [UInt64(0)])
6666
Test.assertEqual(1, singleDetails.length)
67-
Test.assertEqual(details0.health, singleDetails[0].health)
67+
Test.assertEqual(details0.health, singleDetails[0]!.health)
6868

6969
// --- Closed positions are silently skipped ---
7070
// Close position 1, then request both IDs — should only return position 0
7171
closePosition(user: user, positionID: 1)
7272
let afterClose = getPositionsByIDs(positionIDs: [UInt64(0), UInt64(1)])
73-
Test.assertEqual(1, afterClose.length)
74-
Test.assertEqual(details0.health, afterClose[0].health)
73+
Test.assertEqual(2, afterClose.length)
74+
Test.assertEqual(details0.health, afterClose[0]!.health)
75+
Test.assertEqual(afterClose[1], nil)
7576

7677
// --- All IDs closed/invalid returns empty array ---
7778
closePosition(user: user, positionID: 0)
7879
let allClosed = getPositionsByIDs(positionIDs: [UInt64(0), UInt64(1)])
79-
Test.assertEqual(0, allClosed.length)
80+
Test.assertEqual(2, allClosed.length)
81+
Test.assertEqual(allClosed[0], nil)
82+
Test.assertEqual(allClosed[1], nil)
8083

8184
// --- Non-existent IDs are skipped ---
8285
let nonExistent = getPositionsByIDs(positionIDs: [UInt64(999), UInt64(1000)])
83-
Test.assertEqual(0, nonExistent.length)
86+
Test.assertEqual(2, nonExistent.length)
87+
Test.assertEqual(nonExistent[0], nil)
88+
Test.assertEqual(nonExistent[1], nil)
8489
}

cadence/tests/test_helpers.cdc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -801,13 +801,13 @@ fun getPositionIDs(): [UInt64] {
801801
}
802802

803803
access(all)
804-
fun getPositionsByIDs(positionIDs: [UInt64]): [FlowALPv0.PositionDetails] {
804+
fun getPositionsByIDs(positionIDs: [UInt64]): [FlowALPv0.PositionDetails?] {
805805
let res = _executeScript(
806806
"../scripts/flow-alp/get_positions_by_ids.cdc",
807807
[positionIDs]
808808
)
809809
Test.expect(res, Test.beSucceeded())
810-
return res.returnValue as! [FlowALPv0.PositionDetails]
810+
return res.returnValue as! [FlowALPv0.PositionDetails?]
811811
}
812812

813813
access(all)

0 commit comments

Comments
 (0)