Example response
+
+ ```json
+ [
+ {
+ "probAfter": 0.44418877319153904,
+ "shares": -645.8346334931828,
+ "outcome": "YES",
+ "contractId": "tgB1XmvFXZNhjr3xMNLp",
+ "sale": {
+ "betId": "RcOtarI3d1DUUTjiE0rx",
+ "amount": 474.9999999999998
+ },
+ "createdTime": 1644602886293,
+ "userId": "94YYTk1AFWfbWMpfYcvnnwI1veP2",
+ "probBefore": 0.7229189477449224,
+ "id": "x9eNmCaqQeXW8AgJ8Zmp",
+ "amount": -499.9999999999998
+ },
+ {
+ "probAfter": 0.9901970375647697,
+ "contractId": "zdeaYVAfHlo9jKzWh57J",
+ "outcome": "YES",
+ "amount": 1,
+ "id": "8PqxKYwXCcLYoXy2m2Nm",
+ "shares": 1.0049875638533763,
+ "userId": "94YYTk1AFWfbWMpfYcvnnwI1veP2",
+ "probBefore": 0.9900000000000001,
+ "createdTime": 1644705818872
+ }
+ ]
+ ```
+
+
+
+
## Changelog
- 2022-06-08: Add paging to markets endpoint
diff --git a/firestore.indexes.json b/firestore.indexes.json
index e0cee632..0a8b14bd 100644
--- a/firestore.indexes.json
+++ b/firestore.indexes.json
@@ -559,6 +559,28 @@
"queryScope": "COLLECTION_GROUP"
}
]
+ },
+ {
+ "collectionGroup": "bets",
+ "fieldPath": "id",
+ "indexes": [
+ {
+ "order": "ASCENDING",
+ "queryScope": "COLLECTION"
+ },
+ {
+ "order": "DESCENDING",
+ "queryScope": "COLLECTION"
+ },
+ {
+ "arrayConfig": "CONTAINS",
+ "queryScope": "COLLECTION"
+ },
+ {
+ "order": "ASCENDING",
+ "queryScope": "COLLECTION_GROUP"
+ }
+ ]
}
]
}
diff --git a/web/lib/firebase/bets.ts b/web/lib/firebase/bets.ts
index c442ff73..6fc29d24 100644
--- a/web/lib/firebase/bets.ts
+++ b/web/lib/firebase/bets.ts
@@ -4,6 +4,13 @@ import {
query,
where,
orderBy,
+ QueryConstraint,
+ limit,
+ startAfter,
+ doc,
+ getDocs,
+ getDoc,
+ DocumentSnapshot,
} from 'firebase/firestore'
import { uniq } from 'lodash'
@@ -78,6 +85,43 @@ export async function getUserBets(
.catch((reason) => reason)
}
+export async function getBets(options: {
+ userId?: string
+ contractId?: string
+ before?: string
+ limit: number
+}) {
+ const { userId, contractId, before } = options
+
+ const queryParts: QueryConstraint[] = [
+ orderBy('createdTime', 'desc'),
+ limit(options.limit),
+ ]
+ if (userId) {
+ queryParts.push(where('userId', '==', userId))
+ }
+ if (before) {
+ let beforeSnap: DocumentSnapshot
+ if (contractId) {
+ beforeSnap = await getDoc(
+ doc(db, 'contracts', contractId, 'bets', before)
+ )
+ } else {
+ beforeSnap = (
+ await getDocs(
+ query(collectionGroup(db, 'bets'), where('id', '==', before))
+ )
+ ).docs[0]
+ }
+ queryParts.push(startAfter(beforeSnap))
+ }
+
+ const querySource = contractId
+ ? collection(db, 'contracts', contractId, 'bets')
+ : collectionGroup(db, 'bets')
+ return await getValues