track total deposits
This commit is contained in:
parent
dd2f271de8
commit
4fc0bcd8a9
|
@ -7,6 +7,7 @@ export type User = {
|
||||||
avatarUrl?: string
|
avatarUrl?: string
|
||||||
|
|
||||||
balance: number
|
balance: number
|
||||||
|
totalDeposits: number
|
||||||
totalPnLCached: number
|
totalPnLCached: number
|
||||||
creatorVolumeCached: number
|
creatorVolumeCached: number
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,6 +55,7 @@ export const createUser = functions
|
||||||
username,
|
username,
|
||||||
avatarUrl,
|
avatarUrl,
|
||||||
balance,
|
balance,
|
||||||
|
totalDeposits: balance,
|
||||||
createdTime: Date.now(),
|
createdTime: Date.now(),
|
||||||
totalPnLCached: 0,
|
totalPnLCached: 0,
|
||||||
creatorVolumeCached: 0,
|
creatorVolumeCached: 0,
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import * as admin from 'firebase-admin'
|
import * as admin from 'firebase-admin'
|
||||||
import * as _ from 'lodash'
|
import * as _ from 'lodash'
|
||||||
|
|
||||||
import { PrivateUser, User } from '../../../common/user'
|
import { PrivateUser, STARTING_BALANCE, User } from '../../../common/user'
|
||||||
|
|
||||||
// Generate your own private key, and set the path below:
|
// Generate your own private key, and set the path below:
|
||||||
// https://console.firebase.google.com/u/0/project/mantic-markets/settings/serviceaccounts/adminsdk
|
// https://console.firebase.google.com/u/0/project/mantic-markets/settings/serviceaccounts/adminsdk
|
||||||
|
@ -29,6 +29,15 @@ async function main() {
|
||||||
username,
|
username,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (user.totalDeposits === undefined) {
|
||||||
|
await firestore
|
||||||
|
.collection('users')
|
||||||
|
.doc(user.id)
|
||||||
|
.update({ totalDeposits: STARTING_BALANCE })
|
||||||
|
|
||||||
|
console.log('set starting balance for:', user.username)
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await firestore
|
await firestore
|
||||||
.collection('private-users')
|
.collection('private-users')
|
||||||
|
|
|
@ -129,7 +129,7 @@ const issueMoneys = async (session: any) => {
|
||||||
|
|
||||||
await firestore.collection('stripe-transactions').add(transaction)
|
await firestore.collection('stripe-transactions').add(transaction)
|
||||||
|
|
||||||
await payUser(userId, payout)
|
await payUser(userId, payout, true)
|
||||||
|
|
||||||
console.log('user', userId, 'paid M$', payout)
|
console.log('user', userId, 'paid M$', payout)
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,11 @@ export const getUserByUsername = async (username: string) => {
|
||||||
|
|
||||||
const firestore = admin.firestore()
|
const firestore = admin.firestore()
|
||||||
|
|
||||||
const updateUserBalance = (userId: string, delta: number) => {
|
const updateUserBalance = (
|
||||||
|
userId: string,
|
||||||
|
delta: number,
|
||||||
|
isDeposit = false
|
||||||
|
) => {
|
||||||
return firestore.runTransaction(async (transaction) => {
|
return firestore.runTransaction(async (transaction) => {
|
||||||
const userDoc = firestore.doc(`users/${userId}`)
|
const userDoc = firestore.doc(`users/${userId}`)
|
||||||
const userSnap = await transaction.get(userDoc)
|
const userSnap = await transaction.get(userDoc)
|
||||||
|
@ -51,15 +55,20 @@ const updateUserBalance = (userId: string, delta: number) => {
|
||||||
`User (${userId}) balance cannot be negative: ${newUserBalance}`
|
`User (${userId}) balance cannot be negative: ${newUserBalance}`
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (isDeposit) {
|
||||||
|
const newTotalDeposits = (user.totalDeposits || 0) + delta
|
||||||
|
transaction.update(userDoc, { totalDeposits: newTotalDeposits })
|
||||||
|
}
|
||||||
|
|
||||||
transaction.update(userDoc, { balance: newUserBalance })
|
transaction.update(userDoc, { balance: newUserBalance })
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export const payUser = (userId: string, payout: number) => {
|
export const payUser = (userId: string, payout: number, isDeposit = false) => {
|
||||||
if (!isFinite(payout) || payout <= 0)
|
if (!isFinite(payout) || payout <= 0)
|
||||||
throw new Error('Payout is not positive: ' + payout)
|
throw new Error('Payout is not positive: ' + payout)
|
||||||
|
|
||||||
return updateUserBalance(userId, payout)
|
return updateUserBalance(userId, payout, isDeposit)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const chargeUser = (userId: string, charge: number) => {
|
export const chargeUser = (userId: string, charge: number) => {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user