Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(wallet): await on get balance #1657

Merged
merged 2 commits into from
Oct 1, 2024
Merged
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
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
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
}
}
}
Loading