Skip to content

Commit

Permalink
fix(wallet): await on get balance (#1657)
Browse files Browse the repository at this point in the history
* fix(wallet): await on get balance

* fix: snake case vault fields
  • Loading branch information
dragosp1011 authored Oct 1, 2024
1 parent bef2be5 commit 5abc871
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
12 changes: 7 additions & 5 deletions packages/wallet/backend/src/account/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,12 @@ export class AccountService implements IAccountService {
const accounts = await query

if (!includeWalletAddress) {
accounts.forEach(async (acc) => {
const balance = await this.getAccountBalance(acc)
acc.balance = transformBalance(balance, acc.assetScale)
})
await Promise.all(
accounts.map(async (acc) => {
const balance = await this.getAccountBalance(acc)
acc.balance = transformBalance(balance, acc.assetScale)
})
)
}

return accounts
Expand Down Expand Up @@ -164,7 +166,7 @@ export class AccountService implements IAccountService {
account.gateHubWalletId
)
return Number(
balances.find((balance) => balance.vault.assetCode === account.assetCode)
balances.find((balance) => balance.vault.asset_code === account.assetCode)
?.total ?? 0
)
}
Expand Down
8 changes: 6 additions & 2 deletions packages/wallet/backend/src/gatehub/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,18 @@ export class GateHubClient {
}

async createTransaction(
body: ICreateTransactionRequest
body: ICreateTransactionRequest,
managedUserUuid?: string
): Promise<ICreateTransactionResponse> {
const url = `${this.apiUrl}/core/v1/transactions`

const response = await this.request<ICreateTransactionResponse>(
'POST',
url,
JSON.stringify(body)
JSON.stringify(body),
{
managedUserUuid
}
)

return response
Expand Down
6 changes: 3 additions & 3 deletions packages/wallet/backend/src/gatehub/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ export interface IWalletBalance {
interface IVault {
uuid: string
name: string
assetCode: string
createdAt: string
updatedAt: string
asset_code: string
created_at: string
updated_at: string
}

export interface IConnectUserToGatewayResponse {}
Expand Down
35 changes: 22 additions & 13 deletions packages/wallet/backend/src/rafiki/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,21 +247,27 @@ export class RafikiService implements IRafikiService {
const walletAddress = await this.getWalletAddress(wh)
const debitAmount = this.getAmountFromWebHook(wh)

const { gateHubWalletId: sendingWallet, userId } =
await this.getGateHubWalletAddress(walletAddress)
const {
gateHubWalletId: sendingWallet,
userId,
gateHubUserId
} = await this.getGateHubWalletAddress(walletAddress)

if (!this.validateAmount(debitAmount, wh.type)) {
return
}

await this.gateHubClient.createTransaction({
amount: this.amountToNumber(debitAmount),
vault_uuid: this.getVaultUuid(debitAmount.assetCode),
sending_address: sendingWallet,
receiving_address: this.env.GATEHUB_SETTLEMENT_WALLET_ADDRESS,
type: HOSTED_TRANSACTION_TYPE,
message: 'Transfer'
})
await this.gateHubClient.createTransaction(
{
amount: this.amountToNumber(debitAmount),
vault_uuid: this.getVaultUuid(debitAmount.assetCode),
sending_address: sendingWallet,
receiving_address: this.env.GATEHUB_SETTLEMENT_WALLET_ADDRESS,
type: HOSTED_TRANSACTION_TYPE,
message: 'Transfer'
},
gateHubUserId
)

if (wh.data.balance !== '0') {
await this.rafikiClient.withdrawLiqudity(wh.id)
Expand Down Expand Up @@ -365,17 +371,20 @@ export class RafikiService implements IRafikiService {
}

private async getGateHubWalletAddress(walletAddress: WalletAddress) {
const account = await Account.query().findById(walletAddress.accountId)
const account = await Account.query()
.findById(walletAddress.accountId)
.withGraphFetched('user')

if (!account || !account.gateHubWalletId) {
if (!account?.gateHubWalletId || !account.user?.gateHubUserId) {
throw new BadRequest(
'No account associated to the provided payment pointer'
)
}

return {
userId: account.userId,
gateHubWalletId: account.gateHubWalletId
gateHubWalletId: account.gateHubWalletId,
gateHubUserId: account.user.gateHubUserId
}
}
}

0 comments on commit 5abc871

Please sign in to comment.