Download api/models/Balance.js from Zhitomir/chat: direct link, hf CLI and curl.
- Browser
- Download file 1.03 kB
-
https://huggingface.co/spaces/Zhitomir/chat/resolve/main/api/models/Balance.js
- Command line
-
hf download hf://spaces/Zhitomir/chat/api/models/Balance.js
-
curl -L -o Balance.js https://huggingface.co/spaces/Zhitomir/chat/resolve/main/api/models/Balance.js
1.03 kB
| const mongoose = require('mongoose'); | |
| const balanceSchema = require('./schema/balance'); | |
| const { getMultiplier } = require('./tx'); | |
| const { logger } = require('~/config'); | |
| balanceSchema.statics.check = async function ({ | |
| user, | |
| model, | |
| endpoint, | |
| valueKey, | |
| tokenType, | |
| amount, | |
| endpointTokenConfig, | |
| }) { | |
| const multiplier = getMultiplier({ valueKey, tokenType, model, endpoint, endpointTokenConfig }); | |
| const tokenCost = amount * multiplier; | |
| const { tokenCredits: balance } = (await this.findOne({ user }, 'tokenCredits').lean()) ?? {}; | |
| logger.debug('[Balance.check]', { | |
| user, | |
| model, | |
| endpoint, | |
| valueKey, | |
| tokenType, | |
| amount, | |
| balance, | |
| multiplier, | |
| endpointTokenConfig: !!endpointTokenConfig, | |
| }); | |
| if (!balance) { | |
| return { | |
| canSpend: false, | |
| balance: 0, | |
| tokenCost, | |
| }; | |
| } | |
| logger.debug('[Balance.check]', { tokenCost }); | |
| return { canSpend: balance >= tokenCost, balance, tokenCost }; | |
| }; | |
| module.exports = mongoose.model('Balance', balanceSchema); | |