|
| 1 | +import Test |
| 2 | +import BlockchainHelpers |
| 3 | + |
| 4 | +import "MOET" |
| 5 | +import "FlowALPv0" |
| 6 | +import "test_helpers.cdc" |
| 7 | + |
| 8 | +// ----------------------------------------------------------------------------- |
| 9 | +// tryGetPositionDetails Test |
| 10 | +// |
| 11 | +// Verifies that Pool.tryGetPositionDetails() returns position details for |
| 12 | +// existing positions and nil for non-existent or closed positions. |
| 13 | +// ----------------------------------------------------------------------------- |
| 14 | + |
| 15 | +access(all) |
| 16 | +fun setup() { |
| 17 | + deployContracts() |
| 18 | +} |
| 19 | + |
| 20 | +// ============================================================================= |
| 21 | +// Test: tryGetPositionDetails returns details for open positions and nil otherwise |
| 22 | +// ============================================================================= |
| 23 | +access(all) |
| 24 | +fun test_tryGetPositionDetails() { |
| 25 | + // --- Setup --- |
| 26 | + setMockOraclePrice(signer: PROTOCOL_ACCOUNT, forTokenIdentifier: FLOW_TOKEN_IDENTIFIER, price: 1.0) |
| 27 | + |
| 28 | + createAndStorePool(signer: PROTOCOL_ACCOUNT, defaultTokenIdentifier: MOET_TOKEN_IDENTIFIER, beFailed: false) |
| 29 | + addSupportedTokenZeroRateCurve( |
| 30 | + signer: PROTOCOL_ACCOUNT, |
| 31 | + tokenTypeIdentifier: FLOW_TOKEN_IDENTIFIER, |
| 32 | + collateralFactor: 0.8, |
| 33 | + borrowFactor: 1.0, |
| 34 | + depositRate: 1_000_000.0, |
| 35 | + depositCapacityCap: 1_000_000.0 |
| 36 | + ) |
| 37 | + |
| 38 | + let user = Test.createAccount() |
| 39 | + setupMoetVault(user, beFailed: false) |
| 40 | + mintFlow(to: user, amount: 10_000.0) |
| 41 | + |
| 42 | + // --- Non-existent position returns nil --- |
| 43 | + let nonExistent = tryGetPositionDetails(pid: 0) |
| 44 | + Test.assertEqual(nil, nonExistent) |
| 45 | + |
| 46 | + // --- Open a position --- |
| 47 | + createPosition(signer: user, amount: 100.0, vaultStoragePath: FLOW_VAULT_STORAGE_PATH, pushToDrawDownSink: false) |
| 48 | + |
| 49 | + // --- Existing position returns details --- |
| 50 | + let details = tryGetPositionDetails(pid: 0) |
| 51 | + Test.assert(details != nil, message: "Expected non-nil details for open position") |
| 52 | + |
| 53 | + // --- Result matches getPositionDetails --- |
| 54 | + let expected = getPositionDetails(pid: 0, beFailed: false) |
| 55 | + Test.assertEqual(expected.health, details!.health) |
| 56 | + Test.assertEqual(expected.balances.length, details!.balances.length) |
| 57 | + |
| 58 | + // --- Still nil for non-existent ID --- |
| 59 | + let stillNil = tryGetPositionDetails(pid: 999) |
| 60 | + Test.assertEqual(nil, stillNil) |
| 61 | + |
| 62 | + // --- Close the position, should return nil --- |
| 63 | + closePosition(user: user, positionID: 0) |
| 64 | + let afterClose = tryGetPositionDetails(pid: 0) |
| 65 | + Test.assertEqual(nil, afterClose) |
| 66 | +} |
0 commit comments