-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(billing): implement balance refresh
refs #247
- Loading branch information
1 parent
164d86b
commit a4fac9f
Showing
17 changed files
with
3,745 additions
and
3,579 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { AllowanceHttpService } from "@akashnetwork/http-sdk"; | ||
import { container } from "tsyringe"; | ||
|
||
import { apiNodeUrl } from "@src/utils/constants"; | ||
|
||
container.register(AllowanceHttpService, { useValue: new AllowanceHttpService({ baseURL: apiNodeUrl }) }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import "./user-wallet-schema.provider"; | ||
import "./config.provider"; | ||
import "./http-sdk.provider"; | ||
|
||
export * from "./user-wallet-schema.provider"; | ||
export * from "./config.provider"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
apps/api/src/billing/services/balances/balances.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { AllowanceHttpService } from "@akashnetwork/http-sdk"; | ||
import { singleton } from "tsyringe"; | ||
|
||
import { BillingConfig, InjectBillingConfig } from "@src/billing/providers"; | ||
import { UserWalletInput, UserWalletOutput, UserWalletRepository } from "@src/billing/repositories"; | ||
import { MasterWalletService } from "@src/billing/services"; | ||
|
||
@singleton() | ||
export class BalancesService { | ||
constructor( | ||
@InjectBillingConfig() private readonly config: BillingConfig, | ||
private readonly userWalletRepository: UserWalletRepository, | ||
private readonly masterWalletService: MasterWalletService, | ||
private readonly allowanceHttpService: AllowanceHttpService | ||
) {} | ||
|
||
async updateUserWalletLimits(userWallet: UserWalletOutput) { | ||
const [feeLimit, deploymentLimit] = await Promise.all([this.calculateFeeLimit(userWallet), this.calculateDeploymentLimit(userWallet)]); | ||
|
||
const update: Partial<UserWalletInput> = {}; | ||
|
||
const feeLimitStr = feeLimit.toString(); | ||
|
||
if (userWallet.feeAllowance !== feeLimitStr) { | ||
update.feeAllowance = feeLimitStr; | ||
} | ||
|
||
const deploymentLimitStr = deploymentLimit.toString(); | ||
|
||
if (userWallet.deploymentAllowance !== deploymentLimitStr) { | ||
update.deploymentAllowance = deploymentLimitStr; | ||
} | ||
|
||
if (Object.keys(update).length > 0) { | ||
await this.userWalletRepository.updateById(userWallet.id, update); | ||
} | ||
} | ||
|
||
private async calculateFeeLimit(userWallet: UserWalletOutput) { | ||
const feeAllowance = await this.allowanceHttpService.getFeeAllowancesForGrantee(userWallet.address); | ||
const masterWalletAddress = await this.masterWalletService.getFirstAddress(); | ||
|
||
return feeAllowance.reduce((acc, allowance) => { | ||
if (allowance.granter !== masterWalletAddress) { | ||
return acc; | ||
} | ||
|
||
return allowance.allowance.spend_limit.reduce((acc, { denom, amount }) => { | ||
if (denom !== this.config.TRIAL_ALLOWANCE_DENOM) { | ||
return acc; | ||
} | ||
|
||
return acc + parseInt(amount); | ||
}, 0); | ||
}, 0); | ||
} | ||
|
||
private async calculateDeploymentLimit(userWallet: UserWalletOutput) { | ||
const deploymentAllowance = await this.allowanceHttpService.getDeploymentAllowancesForGrantee(userWallet.address); | ||
const masterWalletAddress = await this.masterWalletService.getFirstAddress(); | ||
|
||
return deploymentAllowance.reduce((acc, allowance) => { | ||
if (allowance.granter !== masterWalletAddress || allowance.authorization.spend_limit.denom !== this.config.TRIAL_ALLOWANCE_DENOM) { | ||
return acc; | ||
} | ||
|
||
return acc + parseInt(allowance.authorization.spend_limit.amount); | ||
}, 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.