增加一个get方式获取crypto价格的接口, 增加查询是否可购买国库模式eth的方法

This commit is contained in:
CounterFire2023 2023-07-19 17:50:40 +08:00
parent 6a5d256ebe
commit 907b30fe8b
3 changed files with 42 additions and 4 deletions

View File

@ -3,7 +3,6 @@ import BaseController, { ROLE_ANON } from 'common/base.controller'
import { ZError } from 'common/ZError'
import { role, router } from 'decorators/router'
import { createOrder, queryFiat, queryPrice, refreshToken } from 'service/alchemy.svr'
import { generateKVStr } from 'utils/net.util'
import { PayRecord, PayStatus } from 'modules/PayRecord'
import { PriceSvr } from 'service/price.svr'
import { reportPayResult } from 'service/game.svr'
@ -52,7 +51,7 @@ class AlchemyController extends BaseController {
throw new ZError(14, priceData.returnMsg || 'fetch price error')
}
logger.debug('pirce data::', JSON.stringify(priceData))
let amountEstimate = priceData.data.cryptoAmount
let amountEstimate = priceData.data.cryptoQuantity
let record = new PayRecord({
account: user.id,
address,
@ -123,6 +122,30 @@ class AlchemyController extends BaseController {
return { price: result }
}
@router('get /pay/alchemy/crypto_price')
async queryCryptoPriceGET(req, res) {
let { crypto, chain, currency, env } = req.params
if (!crypto || !chain) {
throw new ZError(11, 'token or network not found')
}
// ceg价格固定为0.1
if ((chain.toLowerCase() === 'agor' || chain.toLowerCase() === 'eth') && crypto.toLowerCase() === 'ceg') {
return { price: 0.1 }
}
if ((chain.toLowerCase() === 'agor' || chain.toLowerCase() === 'eth') && crypto.toLowerCase() === 'agor') {
crypto = 'ETH'
chain = 'ARBITRUM'
}
let data = {
crypto,
network: chain,
fiat: currency || 'USD',
}
let result = await new PriceSvr().fetchPrice(data)
return { price: result }
}
@router('get /pay/alchemy/fait_list')
async cryptoList(req, res) {
let result = await queryFiat()
@ -131,6 +154,20 @@ class AlchemyController extends BaseController {
}
return result.data
}
@router('get /pay/alchemy/can_i_buy_treasury')
async canIBuyEth(req, res) {
let user = req.user
let crypto = 'ETH'
let today = new Date()
today.setHours(0, 0, 0, 0)
let count = await PayRecord.countDocuments({
account: user.id,
crypto,
status: PayStatus.SUCCESS,
createdAt: { $gte: today },
})
return { result: count >= 3 ? 0 : 1 }
}
@role(ROLE_ANON)
@router('post /pay/alchemy/estimateGas')

View File

@ -177,9 +177,9 @@ class AlchemyOutController extends BaseController {
// 简单的比较一下, 如果差距太大, 就不处理
if (record.cryptoAmountEstimate) {
let diff = parseFloat(cryptoAmount) / parseFloat(record.cryptoAmountEstimate)
if (diff > 3 || diff < 0.5) {
if (diff > 3) {
new OrderCacheSvr().removeOrder(orderNo)
return errorRes('params mismatch, cryptoAmount too big or too small')
return errorRes('params mismatch, cryptoAmount too big')
}
}
record.cryptoAmount =

View File

@ -21,6 +21,7 @@ export enum PayStatus {
@dbconn('pay')
@index({ outOrderId: 1 }, { unique: true, partialFilterExpression: { outOrderId: { $exists: true } } })
@index({ account: 1, crypto: 1, status: 1, createdAt: 1 }, { unique: false })
@modelOptions({
schemaOptions: { collection: 'pay_record', timestamps: true },
options: { allowMixed: Severity.ALLOW },