From d67cf6601cd2e398d258bc2dde3961e15ed0f52b Mon Sep 17 00:00:00 2001 From: Yoojin Ko Date: Wed, 20 Sep 2023 16:41:52 +0900 Subject: [PATCH 1/2] feat: add txResultWrapper --- src/ain.ts | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/ain.ts b/src/ain.ts index 1b56323..c271369 100644 --- a/src/ain.ts +++ b/src/ain.ts @@ -1,6 +1,7 @@ import Ain from "@ainblockchain/ain-js"; import { getBlockChainEndpoint } from "./constants"; import { TransactionBody } from "@ainblockchain/ain-util"; +import { txResult } from "./types/type"; // NOTE(yoojin): Plz suggest a good name. export default class AinModule { @@ -63,7 +64,7 @@ export default class AinModule { return await this.ain!.db.ref(path).getValue(); } - async sendTransaction(data: TransactionBody) { + private async _sendTransaction(data: TransactionBody) { this.checkAinInitiated(); return await this.ain!.sendTransaction(data); } @@ -78,4 +79,28 @@ export default class AinModule { this.checkAinInitiated(); return this.ain!.em; } + + private hasFailedOpResultList(result: txResult): boolean { + if (result.result_list) { + return Object.values(result.result_list).some( + (result: { code: number }) => result.code !== 0 + ); + } + return result.code !== 0; + } + + private handleTxResultWrapper(operation: Function) { + return async (args: any) => { + const res = await operation(args); + const { tx_hash, result } = res; + if (this.hasFailedOpResultList(result)) { + throw new Error( + `Failed to send transaction (${tx_hash}).\n Tx Result: ${JSON.stringify(result)}` + ); + } + return tx_hash; + } + } + + sendTransaction = this.handleTxResultWrapper(this._sendTransaction.bind(this)); } From 22efb6541b4e84a32ea3eb7df7ce0bea0f430f95 Mon Sep 17 00:00:00 2001 From: Yoojin Ko Date: Wed, 20 Sep 2023 16:42:00 +0900 Subject: [PATCH 2/2] fix: write rules --- src/constants.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index fcaeaab..8c56959 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -69,7 +69,7 @@ export const defaultAppRules = (appName: string): { [type: string]: { ref: strin ".rule": { write: "auth.addr === $userAddress && getValue(`/apps/" + `${appName}` + "/balance/` + $userAddress + `/balance`) !== null && " + - "(!util.isEmpty(getValue(`/apps/" + `${appName}` + "/billingConfig/minCost`))) && (getValue(`/apps/" + `${appName}` + "/balance/` + $userAddress + `/balance`) >= getValue(`/apps/" + `${appName}` + "/billingConfig/minCost`))" + "(getValue(`/apps/" + `${appName}` + "/balance/` + $userAddress + `/balance`) >= getValue(`/apps/" + `${appName}` + "/billingConfig/minCost`))" }, }, }, @@ -86,8 +86,8 @@ export const defaultAppRules = (appName: string): { [type: string]: { ref: strin value: { ".rule": { write: "util.isAppAdmin(`" + `${appName}` + "`, auth.addr, getValue) === true && util.isDict(newData) && util.isString(newData.depositAddress) && " + - "util.isDict(newData.service) && util.isDict(newData.service.default) && util.isNumber(newData.service.default.costPerToken) && util.isNumber(newData.service.default.minCost) && " + - "util.isEmpty(newData.service.default.maxCost) || (util.isNumber(newData.service.default.maxCost) && newData.service.default.maxCost >= newData.service.default.minCost)", + "util.isDict(newData) && util.isNumber(newData.costPerToken) && util.isNumber(newData.minCost) && " + + "util.isEmpty(newData.maxCost) || (util.isNumber(newData.maxCost) && newData.maxCost >= newData.minCost)", }, }, },