Skip to content

Commit

Permalink
feat: bring incoming payment and quote response type into shared pack…
Browse files Browse the repository at this point in the history
…age (#1395)

* - Bring transfer response type into shared package

* fix fetch wallet types
  • Loading branch information
rico191013 authored Jul 9, 2024
1 parent 4c52eee commit 461523a
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 52 deletions.
11 changes: 3 additions & 8 deletions packages/wallet/backend/src/incomingPayment/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@ import {
} from '@/incomingPayment/validation'
import { IncomingPaymentService } from '@/incomingPayment/service'
import { Controller, toSuccessResponse } from '@shared/backend'

export interface PaymentDetails {
value: number
description?: string
assetCode: string
}
import { PaymentDetailsResponse } from '@wallet/shared'

interface IIncomingPaymentController {
create: Controller<{ url: string }>
getPaymentDetailsByUrl: Controller<PaymentDetails>
getPaymentDetailsByUrl: Controller<PaymentDetailsResponse>
}

export class IncomingPaymentController implements IIncomingPaymentController {
Expand Down Expand Up @@ -47,7 +42,7 @@ export class IncomingPaymentController implements IIncomingPaymentController {

getPaymentDetailsByUrl = async (
req: Request,
res: CustomResponse<PaymentDetails>,
res: CustomResponse<PaymentDetailsResponse>,
next: NextFunction
) => {
try {
Expand Down
6 changes: 3 additions & 3 deletions packages/wallet/backend/src/incomingPayment/service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AccountService } from '@/account/service'
import { PaymentDetails } from '@/incomingPayment/controller'
import { WalletAddress } from '@/walletAddress/model'
import { RafikiClient } from '@/rafiki/rafiki-client'
import { transformAmount } from '@/utils/helpers'
Expand All @@ -8,6 +7,7 @@ import { add, Duration } from 'date-fns'
import axios from 'axios'
import { Env } from '@/config/env'
import { NotFound } from '@shared/backend'
import { PaymentDetailsResponse } from '@wallet/shared'

interface IIncomingPaymentService {
create: (
Expand All @@ -16,7 +16,7 @@ interface IIncomingPaymentService {
amount: number,
description: string
) => Promise<string>
getPaymentDetailsByUrl: (url: string) => Promise<PaymentDetails>
getPaymentDetailsByUrl: (url: string) => Promise<PaymentDetailsResponse>
}

interface CreateReceiverParams {
Expand Down Expand Up @@ -93,7 +93,7 @@ export class IncomingPaymentService implements IIncomingPaymentService {
return response.id
}

async getPaymentDetailsByUrl(url: string): Promise<PaymentDetails> {
async getPaymentDetailsByUrl(url: string): Promise<PaymentDetailsResponse> {
const receiver = await this.rafikiClient.getReceiverById(url)
const asset = {
scale:
Expand Down
5 changes: 3 additions & 2 deletions packages/wallet/backend/src/quote/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { QuoteService } from './service'
import { quoteSchema } from './validation'
import { createExchangeQuoteSchema } from '@/account/validation'
import { Controller, toSuccessResponse } from '@shared/backend'
import { QuoteResponse } from '@wallet/shared'

interface IQuoteController {
create: Controller<Quote>
Expand All @@ -19,7 +20,7 @@ export class QuoteController implements IQuoteController {

create = async (
req: Request,
res: CustomResponse<QuoteWithFees | Quote>,
res: CustomResponse<QuoteResponse>,
next: NextFunction
) => {
try {
Expand All @@ -44,7 +45,7 @@ export class QuoteController implements IQuoteController {

createExchangeQuote = async (
req: Request,
res: CustomResponse<QuoteWithFees | Quote>,
res: CustomResponse<QuoteResponse>,
next: NextFunction
) => {
try {
Expand Down
3 changes: 2 additions & 1 deletion packages/wallet/frontend/src/components/ExchangeRate.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { getCurrencySymbol } from '@/utils/helpers'
import { memo } from 'react'
import { SimpleArrow } from './icons/Arrow'
import { AssetOP, ExchangeRates } from '@/lib/api/asset'
import { ExchangeRates } from '@/lib/api/asset'
import { AssetOP } from '@wallet/shared'

type ExchangeRateProps = {
convertAmount: number
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Quote } from '@/lib/api/transfers'
import { useOnboardingContext } from '@/lib/context/onboarding'
import { Button } from '@/ui/Button'
import { formatAmount, getFee } from '@/utils/helpers'
import { Dialog, Transition } from '@headlessui/react'
import { Fragment } from 'react'
import { PaperPlane } from '../icons/PaperPlane'
import { QuoteResponse } from '@wallet/shared'

type QuoteDialogProps = {
onClose: () => void
onAccept: () => void
quote: Quote
quote: QuoteResponse
type: string
receiverName?: string
}
Expand Down
5 changes: 3 additions & 2 deletions packages/wallet/frontend/src/lib/api/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import {
type ErrorResponse,
type SuccessResponse
} from '../httpClient'
import { acceptQuoteSchema, Quote } from './transfers'
import { acceptQuoteSchema } from './transfers'
import {
createAccountSchema,
fundAccountSchema,
withdrawFundsSchema,
exchangeAssetSchema
} from '@wallet/shared'
import { QuoteResponse } from '@wallet/shared'
import { WalletAddressResponse } from '@wallet/shared/src'

export type Account = {
Expand Down Expand Up @@ -44,7 +45,7 @@ type WithdrawFundsError = ErrorResponse<WithdrawFundsArgs | undefined>
type WithdrawFundsResponse = SuccessResponse | WithdrawFundsError

type ExchangeArgs = z.infer<typeof exchangeAssetSchema>
type QuoteResult = SuccessResponse<Quote>
type QuoteResult = SuccessResponse<QuoteResponse>
type ExchangeResponse = QuoteResult | ErrorResponse<ExchangeArgs | undefined>

type AcceptQuoteArgs = z.infer<typeof acceptQuoteSchema>
Expand Down
5 changes: 0 additions & 5 deletions packages/wallet/frontend/src/lib/api/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ import {
} from '../httpClient'
import { AssetResponse, RatesResponse } from '@wallet/shared'

export type AssetOP = {
assetCode: string
assetScale: number
}

export type ExchangeRates = Record<string, number>

type ListAssetsResult = SuccessResponse<AssetResponse[]>
Expand Down
23 changes: 3 additions & 20 deletions packages/wallet/frontend/src/lib/api/transfers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
type ErrorResponse,
type SuccessResponse
} from '../httpClient'
import { AssetOP } from '@/lib/api/asset'
import { PaymentDetailsResponse, QuoteResponse } from '@wallet/shared'

export const sendSchema = z.object({
walletAddressId: z
Expand Down Expand Up @@ -80,30 +80,13 @@ export const requestSchema = z
}
})

type PaymentDetails = {
assetCode: string
description?: string
value: number
}

type AmountProps = AssetOP & {
value: string
}

interface Expiration {
value: number
unit: string
}

export interface Quote {
id: string
receiveAmount: AmountProps
debitAmount: AmountProps
fee?: AmountProps
}

type SendArgs = z.infer<typeof sendSchema>
type QuoteResult = SuccessResponse<Quote>
type QuoteResult = SuccessResponse<QuoteResponse>
type SendError = ErrorResponse<SendArgs | undefined>
type SendResponse = QuoteResult | SendError

Expand All @@ -116,7 +99,7 @@ type RequestResult = SuccessResponse<{ url: string }>
type RequestError = ErrorResponse<RequestArgs | undefined>
type RequestResponse = RequestResult | RequestError

type IncomingPaymentDetailsResult = SuccessResponse<PaymentDetails>
type IncomingPaymentDetailsResult = SuccessResponse<PaymentDetailsResponse>
type IncomingPaymentDetailsResponse =
| IncomingPaymentDetailsResult
| ErrorResponse
Expand Down
6 changes: 3 additions & 3 deletions packages/wallet/frontend/src/lib/api/walletAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {
} from '../httpClient'
import {
ListWalletAddressesResponse,
WalletAddressResponse
} from '@wallet/shared/src'
import { WalletAddressOP } from '@wallet/shared'
WalletAddressResponse,
WalletAddressOP
} from '@wallet/shared'

export const createWalletAddressSchema = z.object({
walletAddressName: z.string().toLowerCase().min(3, {
Expand Down
3 changes: 2 additions & 1 deletion packages/wallet/frontend/src/pages/exchange.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
} from 'next/types'
import { Account, accountService } from '@/lib/api/account'
import { formatAmount, getCurrencySymbol, getObjectKeys } from '@/utils/helpers'
import { AssetOP, assetService, ExchangeRates } from '@/lib/api/asset'
import { assetService, ExchangeRates } from '@/lib/api/asset'
import { Controller } from 'react-hook-form'
import { NextPageWithLayout } from '@/lib/types/app'
import { ErrorDialog } from '@/components/dialogs/ErrorDialog'
Expand All @@ -25,6 +25,7 @@ import { QuoteDialog } from '@/components/dialogs/QuoteDialog'
import { balanceState } from '@/lib/balance'
import { useSnapshot } from 'valtio'
import { exchangeAssetSchema } from '@wallet/shared'
import { AssetOP } from '@wallet/shared'

type ExchangeAssetProps = InferGetServerSidePropsType<typeof getServerSideProps>
const ExchangeAssetPage: NextPageWithLayout<ExchangeAssetProps> = ({
Expand Down
2 changes: 1 addition & 1 deletion packages/wallet/frontend/src/pages/transfer/request.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import { Label } from '@/ui/forms/Label'
import { FieldError } from '@/ui/forms/FieldError'
import { useSnapshot } from 'valtio'
import { balanceState } from '@/lib/balance'
import { AssetOP } from '@wallet/shared'
import { useOnboardingContext } from '@/lib/context/onboarding'
import { AssetOP } from '@/lib/api/asset'

type SelectTimeUnitOption = Omit<SelectOption, 'value'> & {
value: TimeUnit
Expand Down
3 changes: 2 additions & 1 deletion packages/wallet/frontend/src/pages/transfer/send.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ import {
} from '@/utils/constants'
import { useOnboardingContext } from '@/lib/context/onboarding'
import { QuoteDialog } from '@/components/dialogs/QuoteDialog'
import { AssetOP, assetService, ExchangeRates } from '@/lib/api/asset'
import { assetService, ExchangeRates } from '@/lib/api/asset'
import { ExchangeRate } from '@/components/ExchangeRate'
import { useSnapshot } from 'valtio'
import { balanceState } from '@/lib/balance'
import { AssetOP } from '@wallet/shared'

type SendProps = InferGetServerSidePropsType<typeof getServerSideProps>

Expand Down
6 changes: 3 additions & 3 deletions packages/wallet/frontend/src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { cx, CxOptions } from 'class-variance-authority'
import { Quote } from '../lib/api/transfers'
import { twMerge } from 'tailwind-merge'
import { AssetOP } from '@/lib/api/asset'
import { AssetOP } from '@wallet/shared'
import { QuoteResponse } from '@wallet/shared'

/**
* `getObjectKeys` should be used only when we have additional knowledge.
Expand Down Expand Up @@ -73,7 +73,7 @@ export const formatDate = ({
})
}

export const getFee = (quote: Quote): FormattedAmount => {
export const getFee = (quote: QuoteResponse): FormattedAmount => {
if (quote.fee) {
return formatAmount({
assetCode: quote.fee.assetCode,
Expand Down
5 changes: 5 additions & 0 deletions packages/wallet/shared/src/types/incomingPayment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface PaymentDetailsResponse {
assetCode: string
description?: string
value: number
}
2 changes: 2 additions & 0 deletions packages/wallet/shared/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export * from './account'
export * from './asset'
export * from './rate'
export * from './qoute'
export * from './incomingPayment'
export * from './transaction'
export * from './grant'
export * from './walletAddress'
12 changes: 12 additions & 0 deletions packages/wallet/shared/src/types/qoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { AssetOP } from './asset'

type AmountProps = AssetOP & {
value: string
}

export interface QuoteResponse {
id: string
receiveAmount: AmountProps
debitAmount: AmountProps
fee?: AmountProps
}

0 comments on commit 461523a

Please sign in to comment.