Skip to content

Commit

Permalink
fix: remove exhange endpoint (#1926)
Browse files Browse the repository at this point in the history
  • Loading branch information
dragosp1011 authored Feb 25, 2025
1 parent 446c2b1 commit 98b93b8
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 147 deletions.
7 changes: 0 additions & 7 deletions packages/wallet/backend/src/account/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ export const accountSchema = z.object({
})
})

export const createExchangeQuoteSchema = z.object({
body: z.object({
assetCode: z.string(),
amount: z.number().positive()
})
})

export const fundSchema = z.object({
params: z.object({
accountId: z.string().uuid()
Expand Down
5 changes: 0 additions & 5 deletions packages/wallet/backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,6 @@ export class App {
router.post('/accounts', isAuth, accountController.createAccount)
router.get('/accounts', isAuth, accountController.listAccounts)
router.get('/accounts/:id', isAuth, accountController.getAccountById)
router.post(
'/accounts/:accountId/exchange',
isAuth,
quoteController.createExchangeQuote
)

// Fund account is possible only in sandbox
if (env.GATEHUB_ENV === 'sandbox') {
Expand Down
26 changes: 0 additions & 26 deletions packages/wallet/backend/src/quote/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { validate } from '@/shared/validate'
import type { NextFunction, Request } from 'express'
import { QuoteService } from './service'
import { quoteSchema } from './validation'
import { createExchangeQuoteSchema } from '@/account/validation'
import { Controller, toSuccessResponse } from '@shared/backend'
import { QuoteResponse } from '@wallet/shared'

Expand Down Expand Up @@ -42,29 +41,4 @@ export class QuoteController implements IQuoteController {
next(e)
}
}

createExchangeQuote = async (
req: Request,
res: CustomResponse<QuoteResponse>,
next: NextFunction
) => {
try {
const userId = req.session.user.id
const { accountId } = req.params
const {
body: { assetCode, amount }
} = await validate(createExchangeQuoteSchema, req)

const quote = await this.quoteService.createExchangeQuote({
userId,
accountId,
assetCode,
amount
})

res.status(200).json(toSuccessResponse(quote))
} catch (e) {
next(e)
}
}
}
111 changes: 2 additions & 109 deletions packages/wallet/backend/src/quote/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,14 @@ import { IncomingPaymentService } from '@/incomingPayment/service'
import { WalletAddress } from '@/walletAddress/model'
import { Asset, Quote } from '@/rafiki/backend/generated/graphql'
import { RafikiClient } from '@/rafiki/rafiki-client'
import {
incomingPaymentRegexp,
transformBalance,
urlToPaymentId
} from '@/utils/helpers'
import {
createWalletAddressIfFalsy,
WalletAddressService
} from '@/walletAddress/service'
import { incomingPaymentRegexp, transformBalance } from '@/utils/helpers'
import { WalletAddressService } from '@/walletAddress/service'
import { QuoteWithFees } from './controller'
import { RatesService } from '@/rates/service'
import { Account } from '@/account/model'
import { NodeCacheInstance } from '@/utils/helpers'
import { BadRequest, NotFound } from '@shared/backend'
type CreateExchangeQuote = {
userId: string
accountId: string
assetCode: string
amount: number
}

interface IQuoteService {
create: (params: CreateQuoteParams) => Promise<Quote>
createExchangeQuote: (args: CreateExchangeQuote) => Promise<QuoteWithFees>
}

type CreateQuoteParams = {
Expand All @@ -44,19 +28,6 @@ type ConvertParams = {
amount: bigint
}

const getAccountWithWalletAddressBy = async (where: Partial<Account>) => {
const account = await Account.query()
.where(where)
.withGraphFetched({ walletAddresses: true })
.modifyGraph('walletAddresses', (builder) => {
builder.where({ active: true }).orderBy('createdAt', 'ASC').limit(1)
})
.limit(1)
.first()

return account
}

export class QuoteService implements IQuoteService {
constructor(
private accountService: AccountService,
Expand Down Expand Up @@ -144,18 +115,6 @@ export class QuoteService implements IQuoteService {

value = convertedValue

//* This next check is for first-party transfers. Future Third party transfers will need to go through another flow.
// TODO: discuss if this check is required
// const assetList = await this.rafikiClient.listAssets({ first: 100 })
// asset = assetList.find(
// (a) => a.code === destinationWalletAddress.assetCode
// )
// if (!asset) {
// throw new BadRequest(
// 'Destination wallet address asset is not supported'
// )
// }

asset = {
code: assetDetails.assetCode,
scale: assetDetails.assetScale
Expand Down Expand Up @@ -227,70 +186,4 @@ export class QuoteService implements IQuoteService {
(Number(params.amount) * conversionRate.rates[params.to]).toFixed()
)
}

public async createExchangeQuote({
userId,
accountId,
assetCode,
amount
}: CreateExchangeQuote): Promise<QuoteWithFees> {
const accountFrom = await getAccountWithWalletAddressBy({
userId,
id: accountId
})

if (!accountFrom) {
throw new NotFound(`The source account does not exist for this user.`)
}

const rafikiAsset = await this.rafikiClient.getRafikiAsset(assetCode)
if (!rafikiAsset) {
throw new NotFound(
`Asset Code "${assetCode}" does not exist in Rafiki; Payment Pointer could not be automatically created.`
)
}

const senderPp = await createWalletAddressIfFalsy({
walletAddress: accountFrom.walletAddresses?.[0],
userId,
accountId: accountFrom.id,
publicName: `Exchange Payment Pointer (exchanging into ${assetCode})`,
walletAddressService: this.walletAddressService
})
const senderPpId = senderPp.id

let accountTo = await getAccountWithWalletAddressBy({ userId, assetCode })

if (!accountTo) {
accountTo = await this.accountService.createAccount({
name: `${assetCode} account`,
userId,
assetId: rafikiAsset.id
})
}

const receiverPp = await createWalletAddressIfFalsy({
walletAddress: accountTo.walletAddresses?.[0],
userId,
accountId: accountTo.id,
publicName: `${assetCode} Payment Pointer`,
walletAddressService: this.walletAddressService
})
const receiverPpUrl = receiverPp.url

const quote = await this.create({
userId,
walletAddressId: senderPpId,
amount,
description: 'Currency exchange',
isReceive: true,
receiver: receiverPpUrl
})

//OUTCOME
NodeCacheInstance.set(quote.id, true)
//INCOME
NodeCacheInstance.set(urlToPaymentId(quote.receiver), true)
return quote
}
}

0 comments on commit 98b93b8

Please sign in to comment.