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

refactor: checkCost function #34

Merged
merged 3 commits into from
Sep 13, 2023
Merged
Changes from 1 commit
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
16 changes: 12 additions & 4 deletions src/modules/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,19 @@ 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);
const billingConfig = (await this.getBillingConfig(appName)).service;
const serviceBillingConfig = billingConfig.default;
// TODO(woojae): calculate cost more accurately
let serviceBillingConfig = billingConfig.service.default;
if(billingConfig.service[serviceName]) {
serviceBillingConfig = billingConfig.service[serviceName];
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