Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function (knex) {
return knex.schema.alterTable('accounts', (table) => {
table.dropColumn('assetId')
})
}

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function (knex) {
return knex.schema.alterTable('accounts', (table) => {
table.string('assetId')
})
}
1 change: 0 additions & 1 deletion packages/wallet/backend/seeds/01_users.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ exports.seed = async function (knex) {
id: randomUUID(),
name: 'ILP Account',
userId: user.id,
assetId: randomUUID(),
assetCode: 'EUR',
assetScale: 2,
balance: 100000n,
Expand Down
4 changes: 2 additions & 2 deletions packages/wallet/backend/src/account/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ export class AccountController implements IAccountController {
try {
const userId = req.session.user.id
const {
body: { name, assetId }
body: { name, assetCode }
} = await validate(accountSchema, req)

const createAccountResult = await this.accountService.createAccount({
userId,
name,
assetId
assetCode
})

res.status(200).json(toSuccessResponse(createAccountResult))
Expand Down
1 change: 0 additions & 1 deletion packages/wallet/backend/src/account/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export class Account extends BaseModel {
public name!: string
public balance!: bigint
public gateHubWalletId!: string
public readonly assetId!: string
public readonly assetCode!: string
public readonly assetScale!: number
public readonly userId!: string
Expand Down
21 changes: 9 additions & 12 deletions packages/wallet/backend/src/account/service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Account } from './model'
import { User } from '@/user/model'
import { RafikiClient } from '@/rafiki/rafiki-client'
import { transformBalance } from '@/utils/helpers'
import { Amount } from '@/rafiki/service'
import { BadRequest, Conflict, NotFound } from '@shared/backend'
Expand All @@ -9,11 +8,12 @@ import { GateHubClient } from '@/gatehub/client'
import { v4 as uuid } from 'uuid'
import { MANUAL_NETWORK, TransactionTypeEnum } from '@/gatehub/consts'
import { WalletAddress } from '../walletAddress/model'
import { AssetService } from '@/asset/service'

type CreateAccountArgs = {
userId: string
name: string
assetId: string
assetCode: string
isDefaultCardsAccount?: boolean
cardId?: string
}
Expand All @@ -38,7 +38,7 @@ interface IAccountService {
export class AccountService implements IAccountService {
constructor(
private gateHubClient: GateHubClient,
private rafikiClient: RafikiClient
private assetService: AssetService
) {}

public async createAccount(args: CreateAccountArgs): Promise<Account> {
Expand All @@ -51,11 +51,7 @@ export class AccountService implements IAccountService {
`An account with the name '${args.name}' already exists`
)
}
const asset = await this.rafikiClient.getAssetById(args.assetId)

if (!asset) {
throw new NotFound()
}
const asset = await this.assetService.getAssetByCode(args.assetCode)

const existingAssetAccount = await Account.query()
.where('assetCode', asset.code)
Expand Down Expand Up @@ -93,7 +89,6 @@ export class AccountService implements IAccountService {
name: args.name,
userId: args.userId,
assetCode: asset.code,
assetId: args.assetId,
assetScale: asset.scale,
gateHubWalletId,
cardId: args.cardId
Expand Down Expand Up @@ -259,16 +254,18 @@ export class AccountService implements IAccountService {
isDefaultCardsAccount = false,
cardId?: string
): Promise<Account | undefined> {
const asset = (await this.rafikiClient.listAssets({ first: 100 })).find(
(asset) => asset.code === 'EUR' && asset.scale === DEFAULT_ASSET_SCALE
const asset = await this.assetService.getAssetByCode(
'EUR',
DEFAULT_ASSET_SCALE
)

if (!asset) {
return
}
const account = await this.createAccount({
name,
userId,
assetId: asset.id,
assetCode: asset.code,
isDefaultCardsAccount,
cardId
})
Expand Down
2 changes: 1 addition & 1 deletion packages/wallet/backend/src/account/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { z } from 'zod'
export const accountSchema = z.object({
body: z.object({
name: z.string(),
assetId: z.string().uuid()
assetCode: z.string()
})
})

Expand Down
2 changes: 1 addition & 1 deletion packages/wallet/backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ export class App {
router.post('/outgoing-payments', isAuth, outgoingPaymentController.create)

// asset
router.get('/assets', isAuth, assetController.listAll)
router.get('/assets', isAuth, assetController.list)

// grant
router.get('/grants', isAuth, grantController.list)
Expand Down
22 changes: 4 additions & 18 deletions packages/wallet/backend/src/asset/controller.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,22 @@
import { NextFunction, Request } from 'express'
import { RafikiClient } from '@/rafiki/rafiki-client'
import { RhyzaAdminClient } from '@/rhyza/admin-client'
import { Controller, toSuccessResponse } from '@shared/backend'
import { AssetResponse } from '@wallet/shared'

interface IAssetController {
list: Controller<AssetResponse[]>
listAll: Controller<Pick<AssetResponse, 'code' | 'scale'>[]>
list: Controller<Pick<AssetResponse, 'code' | 'scale'>[]>
}

export class AssetController implements IAssetController {
constructor(private rafikiClient: RafikiClient) {}
constructor(private rhyzaAdminClient: RhyzaAdminClient) {}

list = async (
_req: Request,
res: CustomResponse<AssetResponse[]>,
next: NextFunction
) => {
try {
const assets = await this.rafikiClient.listAssets({ first: 100 })
res.json(toSuccessResponse(assets))
} catch (e) {
next(e)
}
}

listAll = async (
_req: Request,
res: CustomResponse<Pick<AssetResponse, 'code' | 'scale'>[]>,
next: NextFunction
) => {
try {
const assets = await this.rafikiClient.listRhyzaAssets()
const assets = await this.rhyzaAdminClient.listAssets()
res.json(toSuccessResponse(assets))
} catch (e) {
next(e)
Expand Down
24 changes: 24 additions & 0 deletions packages/wallet/backend/src/asset/service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { RhyzaAdminClient } from '@/rhyza/admin-client'
import { Asset } from '@/rhyza/types'
import { NotFound } from '@shared/backend'

interface IAssetService {
getAssetByCode: (assetCode: string, scale?: number) => Promise<Asset>
}

export class AssetService implements IAssetService {
constructor(private rhyzaAdminClient: RhyzaAdminClient) {}

async getAssetByCode(assetCode: string, scale?: number): Promise<Asset> {
const assets = await this.rhyzaAdminClient.listAssets()
const asset = assets.find(
scale
? (asset) => asset.code === assetCode && asset.scale === scale
: (asset) => asset.code === assetCode
)
if (!asset) {
throw new NotFound(`Asset ${assetCode} not found`)
}
return asset
}
}
3 changes: 3 additions & 0 deletions packages/wallet/backend/src/createContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import { TerminalService } from '@/terminal/service'
import { AdminNotificationController } from '@/admin-notification/controller'
import { AdminNotificationService } from '@/admin-notification/service'
import { HsmAtallaService } from '@/hsm/atalla/service'
import { AssetService } from './asset/service'

export interface Cradle {
env: Env
Expand Down Expand Up @@ -96,6 +97,7 @@ export interface Cradle {
userController: UserController
authController: AuthController
assetController: AssetController
assetService: AssetService
accountController: AccountController
transactionController: TransactionController
incomingPaymentController: IncomingPaymentController
Expand Down Expand Up @@ -180,6 +182,7 @@ export async function createContainer(
userController: asClass(UserController).singleton(),
authController: asClass(AuthController).singleton(),
assetController: asClass(AssetController).singleton(),
assetService: asClass(AssetService).singleton(),
accountController: asClass(AccountController).singleton(),
transactionController: asClass(TransactionController).singleton(),
incomingPaymentController: asClass(IncomingPaymentController).singleton(),
Expand Down
9 changes: 4 additions & 5 deletions packages/wallet/backend/src/incomingPayment/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import axios from 'axios'
import { Env } from '@/config/env'
import { NotFound } from '@shared/backend'
import { PaymentDetailsResponse } from '@wallet/shared'
import { AssetService } from '@/asset/service'

interface IIncomingPaymentService {
create: (
Expand Down Expand Up @@ -49,6 +50,7 @@ export class IncomingPaymentService implements IIncomingPaymentService {
constructor(
private accountService: AccountService,
private rafikiClient: RafikiClient,
private assetService: AssetService,
private env: Env
) {}

Expand All @@ -65,14 +67,11 @@ export class IncomingPaymentService implements IIncomingPaymentService {
throw new NotFound()
}

const { assetId } = await this.accountService.findAccountById(
const { assetCode } = await this.accountService.findAccountById(
existingWalletAddress.accountId,
userId
)
const asset = await this.rafikiClient.getAssetById(assetId)
if (!asset) {
throw new NotFound()
}
const asset = await this.assetService.getAssetByCode(assetCode)

let expiryDate: Date | undefined

Expand Down
16 changes: 7 additions & 9 deletions packages/wallet/backend/src/quote/service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { AccountService } from '@/account/service'
import { IncomingPaymentService } from '@/incomingPayment/service'
import { WalletAddress } from '@/walletAddress/model'
import { Asset, Quote } from '@/rafiki/backend/generated/graphql'
import { Quote } from '@/rafiki/backend/generated/graphql'
import { RafikiClient } from '@/rafiki/rafiki-client'
import { incomingPaymentRegexp, transformBalance } from '@/utils/helpers'
import { WalletAddressService } from '@/walletAddress/service'
import { QuoteWithFees } from './controller'
import { RatesService } from '@/rates/service'
import { BadRequest, NotFound } from '@shared/backend'
import { BadRequest } from '@shared/backend'
import { AssetService } from '@/asset/service'

interface IQuoteService {
create: (params: CreateQuoteParams) => Promise<Quote>
Expand Down Expand Up @@ -35,7 +36,8 @@ export class QuoteService implements IQuoteService {
private rafikiClient: RafikiClient,
private incomingPaymentService: IncomingPaymentService,
private ratesService: RatesService,
private walletAddressService: WalletAddressService
private walletAddressService: WalletAddressService,
private assetService: AssetService
) {}

async create(params: CreateQuoteParams): Promise<QuoteWithFees> {
Expand All @@ -47,16 +49,12 @@ export class QuoteService implements IQuoteService {
throw new BadRequest('Invalid wallet address')
}

const { assetId, assetCode } = await this.accountService.findAccountById(
const { assetCode } = await this.accountService.findAccountById(
existingWalletAddress.accountId,
params.userId
)

let asset: Pick<Asset, 'scale' | 'code'> | undefined =
await this.rafikiClient.getAssetById(assetId)
if (!asset) {
throw new NotFound()
}
let asset = await this.assetService.getAssetByCode(assetCode)

const account = await this.accountService.getAccountById(
params.userId,
Expand Down
25 changes: 0 additions & 25 deletions packages/wallet/backend/src/rafiki/rafiki-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import {
CreateReceiverMutationVariables,
DepositLiquidityMutation,
DepositLiquidityMutationVariables,
GetAssetQuery,
GetAssetQueryVariables,
GetAssetsQuery,
GetAssetsQueryVariables,
GetQuoteQuery,
Expand Down Expand Up @@ -53,7 +51,6 @@ import {
} from './backend/generated/graphql'
import {
createAssetMutation,
getAssetQuery,
getAssetsQuery
} from './backend/request/asset.request'
import { getIncomingPaymentQuery } from './backend/request/incoming-payment.request'
Expand Down Expand Up @@ -90,7 +87,6 @@ import { replaceIlpDev } from '@/utils/helpers'
interface IRafikiClient {
createAsset(code: string, scale: number): Promise<Asset>
listAssets(args?: QueryAssetsArgs): Promise<Asset[]>
getAssetById(id: string): Promise<Asset>
getRafikiAsset(assetCode: string): Promise<Asset | undefined>
}

Expand Down Expand Up @@ -134,27 +130,6 @@ export class RafikiClient implements IRafikiClient {
return response.assets.edges.map((el) => el.node as Asset)
}

public async listRhyzaAssets(): Promise<
{
code: string
scale: number
}[]
> {
return [
{ code: 'USD', scale: 2 },
{ code: 'EUR', scale: 2 }
]
}

public async getAssetById(id: string): Promise<Asset> {
const response = await this.backendGraphQLClient.request<
GetAssetQuery,
GetAssetQueryVariables
>(getAssetQuery, { id })

return response.asset as Asset
}

public async createReceiver(params: CreateReceiverParams): Promise<Receiver> {
const walletAddressUrl = replaceIlpDev(params.walletAddressUrl)
const input: CreateReceiverInput = {
Expand Down
13 changes: 13 additions & 0 deletions packages/wallet/backend/src/rhyza/admin-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ interface WalletAddressCreateResponse {
address: string
}

interface ListAssetsResponse {
assets: {
code: string
scale: number
}[]
}

export class RhyzaAdminClient {
constructor(private http: HttpClient) {}

Expand All @@ -38,4 +45,10 @@ export class RhyzaAdminClient {
)
return { id: response.id, address: response.address }
}

async listAssets(): Promise<Asset[]> {
const response = await this.http.get<ListAssetsResponse>('/assets')

return response.assets.map(({ code, scale }) => ({ code, scale }))
}
}
Loading
Loading