Skip to content

Commit

Permalink
Merge pull request #34 from ainize-team/refactor/woojae/checkCostAndB…
Browse files Browse the repository at this point in the history
…alance

refactor: checkCost function
  • Loading branch information
akastercomcom authored Sep 13, 2023
2 parents 778b297 + cdbd125 commit 521b049
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/modules/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,18 @@ export default class App extends ModuleBase {
*/
async checkCostAndBalance(appName: string, serviceName: string, value: string, requesterAddress?: string) {
requesterAddress = requesterAddress ? requesterAddress : this.getDefaultAccount().address;
const billingConfig = await this.getBillingConfig(appName);
// TODO(woojae): calculate cost more accurately
let serviceBillingConfig = billingConfig.service.default;
if(billingConfig.service[serviceName]) {
serviceBillingConfig = billingConfig.service[serviceName];
const billingConfig = (await this.getBillingConfig(appName)).service;
const serviceBillingConfig = billingConfig.default;
if(billingConfig[serviceName]) {
if(billingConfig[serviceName].costPerToken) {
serviceBillingConfig.costPerToken = billingConfig[serviceName].costPerToken;
}
if(billingConfig[serviceName].minCost) {
serviceBillingConfig.minCost = billingConfig[serviceName].minCost;
}
if(billingConfig[serviceName].maxCost) {
serviceBillingConfig.maxCost = billingConfig[serviceName].maxCost;
}
}
const token = value.split(' ').length;
let cost = token * serviceBillingConfig.costPerToken;
Expand Down

0 comments on commit 521b049

Please sign in to comment.