first draft of simple tradingLevel checks

This commit is contained in:
Claus-Peter Huebner 2023-08-29 02:24:08 +02:00
parent 6acf2f3c5b
commit 9aedbea8f4
5 changed files with 50 additions and 24 deletions

View File

@ -6,8 +6,8 @@ import { backendLogger as logger } from '@/server/logger'
import { SendCoinsArgs } from './model/SendCoinsArgs'
import { revertSendCoins } from './query/revertSendCoins'
import { voteForSendCoins } from './query/voteForSendCoins'
import { settleSendCoins } from './query/settleSendCoins'
import { voteForSendCoins } from './query/voteForSendCoins'
// eslint-disable-next-line camelcase
export class SendCoinsClient {

View File

@ -55,6 +55,11 @@ const federation = {
FEDERATION_API: process.env.FEDERATION_API || '1_0',
FEDERATION_PORT: process.env.FEDERATION_PORT || 5010,
FEDERATION_COMMUNITY_URL: process.env.FEDERATION_COMMUNITY_URL || null,
FEDERATION_TRADING_LEVEL: {
RECEIVER_COMMUNITY_URL: 'https://stage3.gradido.net/api/',
SEND_COINS: true,
AMOUNT: 100,
},
}
const CONFIG = {

View File

@ -13,6 +13,7 @@ import Decimal from 'decimal.js-light'
import { fullName } from '@/graphql/util/fullName'
import { settlePendingReceiveTransaction } from '../util/settlePendingReceiveTransaction'
import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from '../const/const'
import { checkTradingLevel } from '@/graphql/util/checkTradingLevel'
@Resolver()
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@ -44,6 +45,11 @@ export class SendCoinsResolver {
communityReceiverIdentifier,
)
}
if (!(await checkTradingLevel(homeCom, amount))) {
throw new LogError(
`X-Com: configuration of Trading-Level doesn't permit requested x-com sendCoin action!`,
)
}
// second check if receiver user exists in this community
const receiverUser = await DbUser.findOneBy({ gradidoID: userReceiverIdentifier })
if (!receiverUser) {
@ -52,6 +58,14 @@ export class SendCoinsResolver {
homeCom.name,
)
}
if (
communitySenderIdentifier === communityReceiverIdentifier &&
userReceiverIdentifier === userSenderIdentifier
) {
throw new LogError(
`Sender and Recipient are the same: communityUUID=${communityReceiverIdentifier}, gradidoID=${userReceiverIdentifier}`,
)
}
if (memo.length < MEMO_MIN_CHARS) {
throw new LogError('Memo text is too short', memo.length)
}

View File

@ -29,43 +29,25 @@ export async function settlePendingReceiveTransaction(
try {
logger.info('X-Com: settlePendingReceiveTransaction:', homeCom, receiverUser, pendingTx)
// ensure that no other pendingTx with the same sender or recipient exists
const openSenderPendingTx = await DbPendingTransaction.count({
where: [
{ userGradidoID: pendingTx?.userGradidoID, state: PendingTransactionState.NEW },
{ linkedUserGradidoID: pendingTx?.linkedUserGradidoID, state: PendingTransactionState.NEW },
{ userGradidoID: pendingTx.userGradidoID, state: PendingTransactionState.NEW },
{ linkedUserGradidoID: pendingTx.linkedUserGradidoID!, state: PendingTransactionState.NEW },
],
})
const openReceiverPendingTx = await DbPendingTransaction.count({
where: [
{ userGradidoID: userReceiverIdentifier, state: PendingTransactionState.NEW },
{ linkedUserGradidoID: userReceiverIdentifier, state: PendingTransactionState.NEW },
{ userGradidoID: pendingTx.linkedUserGradidoID!, state: PendingTransactionState.NEW },
{ linkedUserGradidoID: pendingTx.userGradidoID, state: PendingTransactionState.NEW },
],
})
if (openSenderPendingTx > 1 || openReceiverPendingTx > 1) {
throw new LogError('There are more than 1 pending Transactions for Sender and/or Recipient')
}
if (
communityReceiverIdentifier === communitySenderIdentifier &&
communitySenderIdentifier === userSenderIdentifier
) {
throw new LogError('Sender and Recipient are the same user: ', userSenderName)
}
if (memo.length < MEMO_MIN_CHARS) {
throw new LogError('Memo text is too short', memo.length)
}
if (memo.length > MEMO_MAX_CHARS) {
throw new LogError('Memo text is too long', memo.length)
}
const recipientUser = await DbUser.findOneByOrFail({ gradidoID: userReceiverIdentifier })
const lastTransaction = await getLastTransaction(recipientUser.id)
const pendingTx = await DbPendingTransaction.findOneByOrFail({
userId: recipientUser.id,
userGradidoID: recipientUser.gradidoID,
})
if (lastTransaction?.id !== pendingTx.previous) {
throw new LogError(

View File

@ -0,0 +1,25 @@
import CONFIG from '@/config'
import { Community as DbCommunity } from '@entity/Community'
import { Decimal } from 'decimal.js-light'
import { federationLogger as logger } from '@/server/logger'
export async function checkTradingLevel(homeCom: DbCommunity, amount: Decimal): Promise<boolean> {
const tradingLevel = CONFIG.FEDERATION_TRADING_LEVEL
if (homeCom.url !== tradingLevel.RECEIVER_COMMUNITY_URL) {
logger.warn(
`X-Com: tradingLevel allows to receive coins only wiht url ${tradingLevel.RECEIVER_COMMUNITY_URL}`,
)
return false
}
if (!tradingLevel.SEND_COINS) {
logger.warn(`X-Com: tradingLevel disable general x-com sendcoin actions!`)
return false
}
if (new Decimal(tradingLevel.AMOUNT) < amount) {
logger.warn(
`X-Com: tradingLevel only allows to receive coins lower than amount of ${tradingLevel.AMOUNT}`,
)
return false
}
return true
}