Skip to content

Commit

Permalink
Merge pull request #35 from ainize-team/feature/yoojin/billing_config…
Browse files Browse the repository at this point in the history
…_rule

Fix write rules.
  • Loading branch information
yoojinko authored Sep 13, 2023
2 parents 521b049 + 92186d1 commit 4a89b80
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 20 deletions.
15 changes: 4 additions & 11 deletions src/modules/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ export default class App extends ModuleBase {
".rule": {
write:
"auth.addr === $userAddress && getValue(`/apps/" + `${appName}` + "/balance/` + $userAddress + `/balance`) !== null && " +
"((getValue(`/apps/" + `${appName}` + "/billingConfig/` + $serviceName) !== null) && (getValue(`/apps/" + `${appName}` + "/balance/` + $userAddress + `/balance`) >= getValue(`/apps/" + `${appName}` + "/billingConfig/` + $serviceName + `/minCost`)) || " +
"(!util.isEmpty(getValue(`/apps/" + `${appName}` + "/billingConfig/` + $serviceName + `/minCost`))) && (getValue(`/apps/" + `${appName}` + "/balance/` + $userAddress + `/balance`) >= getValue(`/apps/" + `${appName}` + "/billingConfig/` + $serviceName + `/minCost`)) || " +
"getValue(`/apps/" + `${appName}` + "/balance/` + $userAddress + `/balance`) >= getValue(`/apps/" + `${appName}` + "/billingConfig/service/default/minCost`)"
}
}
Expand All @@ -290,19 +290,12 @@ export default class App extends ModuleBase {
ref: Path.app(appName).billingConfig(),
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)",
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)",
}
}
},
billingConfigOfService: {
ref: Path.app(appName).billingConfigOfService("$serviceName"),
value: {
".rule": {
write: "util.isAppAdmin(`" + `${appName}` + "`, auth,addr, getValue) === true && util.isDict(newData) && util.isNumber(newData.minCost)",
}
}
}
}
}

Expand Down
13 changes: 5 additions & 8 deletions src/modules/moduleBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,22 @@ export default class ModuleBase {
return await this.ain.sendTransaction(txBody);
}

private getFailedOpResultList(result: txResult): opResult[] {
private hasFailedOpResultList(result: txResult): boolean {
if (result.result_list) {
return Object.values(result.result_list).filter(
return Object.values(result.result_list).some(
(result: { code: number }) => result.code !== 0
);
}
return [];
return result.code !== 0;
}

private handleTxResultWrapper(operation: Function) {
return async (args: any) => {
const res = await operation(args);
const { tx_hash, result } = res;
const failedOpResult = this.getFailedOpResultList(result);
if (failedOpResult.length > 0) {
const errorString = failedOpResult.map((value) => `\n code: ${value.code} - ${value.message}`);
console.log('failedOpResult :>> ', failedOpResult);
if (this.hasFailedOpResultList(result)) {
throw new Error(
`Failed to send transaction (${tx_hash}).` + errorString
`Failed to send transaction (${tx_hash}).\n Tx Result: ${JSON.stringify(result)}`
);
}
return tx_hash;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ export default class Wallet extends ModuleBase{
throw new Error ("You need to add account");
}
txBody.address = signerAddress;
return await this.ain.sendTransaction(txBody);
return await this.sendTransaction(txBody);
}
}

0 comments on commit 4a89b80

Please sign in to comment.