-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from ainize-team/feat/woojae/modelController
Feat/woojae/model controller
- Loading branch information
Showing
16 changed files
with
226 additions
and
353 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
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,61 @@ | ||
import { SetOperation } from "@ainblockchain/ain-js/lib/types"; | ||
import { Request } from "express"; | ||
import { getChangeBalanceOp, getResponseOp, getWriteHistoryOp } from "./utils/operator"; | ||
import { HISTORY_TYPE, RESPONSE_STATUS, deposit, request, response } from "./types/type"; | ||
import { buildTxBody } from "./utils/builder"; | ||
import AinModule from "./ain"; | ||
|
||
export default class internal { | ||
private ain = AinModule.getInstance(); | ||
async handleDeposit(req: Request) { | ||
const { requesterAddress, appName, transferKey, transferValue } = this.getDataFromDepositRequest(req); | ||
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; | ||
} | ||
|
||
private getDataFromDepositRequest(req: Request) { | ||
if(!req.body.valuePath[1] || !req.body.valuePath[4] || !req.body.value) { | ||
throw new Error("Not from deposit request"); | ||
} | ||
const depositData: deposit = { | ||
transferKey: req.body.valuePath[4], | ||
transferValue: req.body.value, | ||
appName: req.body.valuePath[1], | ||
requesterAddress: req.body.auth.addr, | ||
} | ||
return depositData; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.