-
Notifications
You must be signed in to change notification settings - Fork 0
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
Feat/woojae/model controller #42
Changes from 4 commits
0227acc
bbe4147
48672b2
9734673
e93ca76
9bb9a86
bfe339f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,16 @@ | ||
import { SetOperation } from "@ainblockchain/ain-js/lib/types"; | ||
import AinModule from "../ain"; | ||
import { Path } from "../constants"; | ||
import { getRequestDepositOp, getTransferOp } from "../utils/util"; | ||
import { buildSetOperation, buildTxBody } from "../utils/builder"; | ||
|
||
export default class ModelController { | ||
private static instance: ModelController | undefined; | ||
private ain = AinModule.getInstance(); | ||
static getInstance() { | ||
if(!ModelController.instance){ | ||
ModelController.instance = new ModelController(); | ||
|
||
} | ||
return ModelController.instance; | ||
} | ||
|
@@ -14,37 +22,64 @@ export default class ModelController { | |
|
||
//TODO(woojae): implement this | ||
async getInformation(modelName: string) { | ||
return await true; | ||
return await 'information of model'; | ||
} | ||
|
||
//TODO(woojae): implement this | ||
async calculateCost(modelName: string, requestData: string) { | ||
return await 0.3; | ||
const billingConfig = await this.ain.getValue(Path.app(modelName).billingConfig()); | ||
const token = requestData.split(' ').length; | ||
let cost = token * billingConfig.costPerToken; | ||
if (billingConfig.minCost && cost < billingConfig.minCost) { | ||
cost = billingConfig.minCost; | ||
} else if (billingConfig.maxCost && cost > billingConfig.maxCost) { | ||
cost = billingConfig.maxCost; | ||
} | ||
return cost; | ||
} | ||
|
||
//TODO(woojae): implement this | ||
async chargeCredit(modelName: string, amount: number) { | ||
return await true; | ||
this.isLoggedIn(); | ||
const transferKey = Date.now(); | ||
const userAddress = this.ain.getDefaultAccount()!.address; | ||
const depositAddress = await this.getDepositAddress(modelName); | ||
const op_list: SetOperation[] = [ | ||
getTransferOp(userAddress, depositAddress, transferKey.toString(), amount), | ||
getRequestDepositOp(modelName, userAddress, transferKey.toString(), amount) | ||
] | ||
const txBody = buildTxBody(op_list, transferKey); | ||
return this.ain.sendTransaction(txBody); | ||
} | ||
|
||
|
||
//TODO(woojae): implement this | ||
async withdrawCredit(modelName: string, amount: number) { | ||
return await true; | ||
} | ||
|
||
//TODO(woojae): implement this | ||
async getCreditBalance(modelName: string) { | ||
return await 0.3; | ||
this.isLoggedIn(); | ||
const userAddress = this.ain.getDefaultAccount()!.address; | ||
const balancePath = Path.app(modelName).balanceOfUser(userAddress); | ||
return await this.ain.getValue(balancePath); | ||
} | ||
|
||
//TODO(woojae): implement this | ||
async getCreditHistory(modelName: string) { | ||
return await true; | ||
this.isLoggedIn(); | ||
const userAddress = this.ain.getDefaultAccount()!.address; | ||
const creditHistoryPath = Path.app(modelName).historyOfUser(userAddress); | ||
return await this.ain.getValue(creditHistoryPath); | ||
} | ||
|
||
//TODO(woojae): implement this | ||
//TODO(woojae): connect with handler | ||
async use(modelName: string, requestData: string) { | ||
return await true; | ||
this.isLoggedIn(); | ||
const requestKey = Date.now(); | ||
const requesterAddress = this.ain.getDefaultAccount()!.address; | ||
const requestPath = Path.app(modelName).request(requesterAddress, requestKey); | ||
const requestOp = buildSetOperation("SET_VALUE", requestPath, {prompt: requestData}); | ||
const txBody = buildTxBody(requestOp); | ||
await this.ain.sendTransaction(txBody); | ||
return requestKey; | ||
} | ||
|
||
//TODO(woojae): implement this | ||
|
@@ -64,4 +99,14 @@ export default class ModelController { | |
async changeModelInfo(modelName: string, config: any) { | ||
return await true; | ||
} | ||
|
||
private async getDepositAddress(appName: string) { | ||
return await this.ain.getValue(Path.app(appName).billingConfig().defaultAddress); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 앗 수정했습니다. |
||
} | ||
|
||
private isLoggedIn() { | ||
if(!this.ain.getDefaultAccount()) | ||
throw new Error('Set defaultAccount First.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Login First? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 수정했습니다. |
||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { SetOperation } from "@ainblockchain/ain-js/lib/types"; | ||
import { Request } from "express"; | ||
import { getChangeBalanceOp, getResponseOp, getWriteHistoryOp } from "./utils/util"; | ||
import { HISTORY_TYPE, RESPONSE_STATUS, request, response } from "./types/type"; | ||
import { buildTxBody } from "./utils/builder"; | ||
import AinModule from "./ain"; | ||
import { Path } from "./constants"; | ||
|
||
export default class internal { | ||
private ain: AinModule; | ||
constructor() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor) constructor 없이 변수 선언부에 바로 할당해도 좋을 것 같습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 수정했습니다. |
||
this.ain = AinModule.getInstance(); | ||
} | ||
async handleDeposit(req: Request) { | ||
const transferKey = req.body.valuePath[4]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. request data 처럼 deposit data도 type 및 parsing func 있으면 더 좋을 것 같습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 반영했습니다. |
||
const transferValue = req.body.value; | ||
const appName = req.body.valuePath[1]; | ||
const requesterAddress = req.body.auth.addr; | ||
const ops: SetOperation[] = []; | ||
const changeBalanceOp = await getChangeBalanceOp(appName, requesterAddress, "INC_VALUE", transferValue); | ||
const writeHistoryOp = await getWriteHistoryOp(appName, requesterAddress, HISTORY_TYPE.DEPOSIT, transferValue, transferKey); | ||
ops.push(changeBalanceOp); | ||
ops.push(writeHistoryOp); | ||
const txBody = buildTxBody(ops); | ||
return await this.ain.sendTransaction(txBody); | ||
} | ||
|
||
async handleRequest(req: Request, cost: number, status: RESPONSE_STATUS, responseData: string) { | ||
const { requesterAddress, requestKey, appName } = this.getDataFromServiceRequest(req); | ||
const ops:SetOperation[] = []; | ||
const responseOp = getResponseOp(appName, requesterAddress, requestKey, status, responseData, cost); | ||
ops.push(responseOp); | ||
if(cost > 0) { | ||
const changeBalanceOp = getChangeBalanceOp(appName, requesterAddress, 'DEC_VALUE', cost); | ||
const writeHistoryOp = getWriteHistoryOp(appName, requesterAddress, HISTORY_TYPE.USAGE, cost, requestKey); | ||
ops.push(changeBalanceOp); | ||
ops.push(writeHistoryOp); | ||
} | ||
const txBody = buildTxBody(ops); | ||
return await this.ain.sendTransaction(txBody); | ||
} | ||
|
||
getDataFromServiceRequest(req: Request) { | ||
if(!req.body.valuePath[1] || !req.body.valuePath[3] || !req.body.valuePath[4] || !req.body.value.prompt) { | ||
throw new Error("Not from service request"); | ||
} | ||
const requestData: request = { | ||
appName: req.body.valuePath[1], | ||
requesterAddress: req.body.auth.addr, | ||
requestKey: req.body.valuePath[4], | ||
requestData: req.body.value.prompt, | ||
} | ||
return requestData; | ||
} | ||
}; |
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여러 함수에 쓰여서 ainize 에 getAddress 나 getUserAddress 추가해도 좋을 것 같습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ainize보단 ain에 추가하겠습니다