Skip to content

Commit

Permalink
fix(wallet): await on get balance
Browse files Browse the repository at this point in the history
  • Loading branch information
dragosp1011 committed Sep 30, 2024
1 parent 8024a9f commit 9c86c69
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 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 @@ -274,14 +274,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
2 changes: 1 addition & 1 deletion packages/wallet/backend/src/gatehub/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export interface IWalletBalance {
interface IVault {
uuid: string
name: string
assetCode: string
asset_code: string
createdAt: string
updatedAt: string
}
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 9c86c69

Please sign in to comment.