first steps of starting X-sendCoins

This commit is contained in:
Claus-Peter Huebner 2023-08-17 14:09:15 +02:00
parent 322327155d
commit 0c72fae9a2
4 changed files with 47 additions and 16 deletions

View File

@ -3,6 +3,9 @@ import { ArgsType, Field } from 'type-graphql'
@ArgsType()
export class TransactionSendArgs {
@Field(() => String)
communityIdentifier: string
@Field(() => String)
identifier: string

View File

@ -34,6 +34,7 @@ import { virtualLinkTransaction, virtualDecayTransaction } from '@/util/virtualT
import { BalanceResolver } from './BalanceResolver'
import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from './const/const'
import { isHomeCommunity } from './util/communities'
import { findUserByIdentifier } from './util/findUserByIdentifier'
import { getLastTransaction } from './util/getLastTransaction'
import { getTransactionList } from './util/getTransactionList'
@ -317,24 +318,32 @@ export class TransactionResolver {
@Authorized([RIGHTS.SEND_COINS])
@Mutation(() => Boolean)
async sendCoins(
@Args() { identifier, amount, memo }: TransactionSendArgs,
@Args() { communityIdentifier, identifier, amount, memo }: TransactionSendArgs,
@Ctx() context: Context,
): Promise<boolean> {
logger.info(`sendCoins(identifier=${identifier}, amount=${amount}, memo=${memo})`)
if (amount.lte(0)) {
throw new LogError('Amount to send must be positive', amount)
logger.info(
`sendCoins(communityIdentifier=${communityIdentifier}, identifier=${identifier}, amount=${amount}, memo=${memo})`,
)
if (!communityIdentifier || (await isHomeCommunity(communityIdentifier))) {
// processing a local sendCoins
if (amount.lte(0)) {
throw new LogError('Amount to send must be positive', amount)
}
const senderUser = getUser(context)
// validate recipient user
const recipientUser = await findUserByIdentifier(identifier)
if (!recipientUser) {
throw new LogError('The recipient user was not found', recipientUser)
}
await executeTransaction(amount, memo, senderUser, recipientUser)
logger.info('successful executeTransaction', amount, memo, senderUser, recipientUser)
} else {
// processing a x-community sendCoins
logger.debug('processing a x-community transaction...')
}
const senderUser = getUser(context)
// validate recipient user
const recipientUser = await findUserByIdentifier(identifier)
if (!recipientUser) {
throw new LogError('The recipient user was not found', recipientUser)
}
await executeTransaction(amount, memo, senderUser, recipientUser)
logger.info('successful executeTransaction', amount, memo, senderUser, recipientUser)
return true
}
}

View File

@ -0,0 +1,19 @@
import { Community as DbCommunity } from '@entity/Community'
export async function isHomeCommunity(communityIdentifier: string): Promise<boolean> {
const homeCommunity = await DbCommunity.findOneByOrFail({ foreign: false })
if (communityIdentifier === homeCommunity.name) {
return true
} else if (communityIdentifier === homeCommunity.communityUuid) {
return true
} else if (communityIdentifier === homeCommunity.url) {
return true
} else {
return false
}
}
export async function getCommunityUrl(communityIdentifier: string): Promise<string> {
const community = await DbCommunity.findOneByOrFail({ name: communityIdentifier })
return community.url
}

View File

@ -72,7 +72,7 @@ export const createUser = gql`
export const sendCoins = gql`
mutation($identifier: String!, $amount: Decimal!, $memo: String!) {
sendCoins(identifier: $identifier, amount: $amount, memo: $memo)
sendCoins(communityIdentifier: $communityIdentifier, identifier: $identifier, amount: $amount, memo: $memo)
}
`