-
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.
- Loading branch information
1 parent
bbe4147
commit 48672b2
Showing
13 changed files
with
151 additions
and
304 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
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() { | ||
this.ain = AinModule.getInstance(); | ||
} | ||
async handleDeposit(req: Request) { | ||
const transferKey = req.body.valuePath[4]; | ||
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.
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.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.