From 648f1ce7941a019dca28c9027e32b037b77605b9 Mon Sep 17 00:00:00 2001 From: jiyoung Date: Mon, 3 Jun 2024 10:46:41 +0900 Subject: [PATCH] refactor: split util file. --- src/ai/ai.ts | 7 +++--- src/ai/assistant.ts | 21 ++++++++-------- src/ai/message.ts | 16 ++++--------- src/ai/thread.ts | 25 ++++++++++--------- src/factoryBase.ts | 2 +- src/nft.ts | 4 ++-- src/utils/index.ts | 5 ++++ src/utils/util.ts | 58 --------------------------------------------- 8 files changed, 39 insertions(+), 99 deletions(-) create mode 100644 src/utils/index.ts diff --git a/src/ai/ai.ts b/src/ai/ai.ts index 36ab2995..3acf907c 100644 --- a/src/ai/ai.ts +++ b/src/ai/ai.ts @@ -12,10 +12,11 @@ import { QueryParams, TokenStatus, } from '../types'; -import { buildSetTxBody, buildSetValueOp, sleep, sendTx } from '../utils/util'; +import { DEFAULT_AINIZE_SERVICE_NAME } from '../constants'; import { Path } from '../utils/path'; +import { buildSetTxBody, buildSetValueOp, sendTx } from '../utils/transaction'; +import { sleep } from '../utils/util'; import { validateObject, validateObjectOwner } from '../utils/validator'; -import { DEFAULT_AINIZE_SERVICE_NAME } from '../constants'; /** * This class manages ai configurations for AINFT object,\ @@ -40,7 +41,7 @@ export class Ai extends FactoryBase { await getService(this.ainize!, serviceName); // NOTE(jiyoung): check if the service is deployed on Ainize. const txBody = this.buildTxBodyForConfigureAi(objectId, serviceName, address); - const result = await sendTx(this.ain, txBody); + const result = await sendTx(txBody, this.ain); return { ...result, config: { name: serviceName } }; } diff --git a/src/ai/assistant.ts b/src/ai/assistant.ts index 718221f3..7c519a9e 100644 --- a/src/ai/assistant.ts +++ b/src/ai/assistant.ts @@ -22,16 +22,17 @@ import { THREAD_GC_NUM_SIBLINGS_DELETED, WHITELISTED_OBJECT_IDS, } from '../constants'; +import { getEnv } from '../utils/env'; +import { Path } from '../utils/path'; import { - buildSetTxBody, buildSetValueOp, - sendTx, buildSetWriteRuleOp, - buildSetOp, buildSetStateRuleOp, - getChecksumAddress, - getAssistant, -} from '../utils/util'; + buildSetOp, + buildSetTxBody, + sendTx, +} from '../utils/transaction'; +import { getChecksumAddress, getAssistant } from '../utils/util'; import { isObjectOwner, validateAssistant, @@ -40,8 +41,6 @@ import { validateServerConfigurationForObject, validateToken, } from '../utils/validator'; -import { Path } from '../utils/path'; -import { getEnv } from '../utils/env'; enum Role { OWNER = 'owner', @@ -104,7 +103,7 @@ export class Assistants extends FactoryBase { if (role === Role.OWNER) { const txBody = this.buildTxBodyForCreateAssistant(address, objectId, tokenId, data); - const result = await sendTx(this.ain, txBody); + const result = await sendTx(txBody, this.ain); return { ...result, assistant: data }; } else { return { assistant: data }; @@ -162,7 +161,7 @@ export class Assistants extends FactoryBase { if (role === Role.OWNER) { const txBody = this.buildTxBodyForUpdateAssistant(address, objectId, tokenId, data); - const result = await sendTx(this.ain, txBody); + const result = await sendTx(txBody, this.ain); return { ...result, assistant: data }; } else { return { assistant: data }; @@ -207,7 +206,7 @@ export class Assistants extends FactoryBase { if (role === Role.OWNER) { const txBody = this.buildTxBodyForDeleteAssistant(address, objectId, tokenId); - const result = await sendTx(this.ain, txBody); + const result = await sendTx(txBody, this.ain); return { ...result, delAssistant: data }; } else { return { delAssistant: data }; diff --git a/src/ai/message.ts b/src/ai/message.ts index 7fa5c860..10dc1990 100644 --- a/src/ai/message.ts +++ b/src/ai/message.ts @@ -11,14 +11,9 @@ import { MessageTransactionResult, MessageUpdateParams, } from '../types'; -import { - buildSetOp, - buildSetTxBody, - buildSetValueOp, - getAssistant, - getValue, - sendTx, -} from '../utils/util'; +import { Path } from '../utils/path'; +import { buildSetValueOp, buildSetOp, buildSetTxBody, sendTx } from '../utils/transaction'; +import { getAssistant, getValue } from '../utils/util'; import { validateAssistant, validateMessage, @@ -27,7 +22,6 @@ import { validateThread, validateToken, } from '../utils/validator'; -import { Path } from '../utils/path'; /** * This class supports create messages within threads.\ @@ -70,7 +64,7 @@ export class Messages extends FactoryBase { threadId, allMessages ); - const result = await sendTx(this.ain, txBody); + const result = await sendTx(txBody, this.ain); return { ...result, messages: allMessages }; } @@ -116,7 +110,7 @@ export class Messages extends FactoryBase { }); const txBody = await this.buildTxBodyForUpdateMessage(data, objectId, tokenId, address); - const result = await sendTx(this.ain, txBody); + const result = await sendTx(txBody, this.ain); return { ...result, message: data }; } diff --git a/src/ai/thread.ts b/src/ai/thread.ts index 0a96ab04..8dff9ac3 100644 --- a/src/ai/thread.ts +++ b/src/ai/thread.ts @@ -15,15 +15,9 @@ import { ThreadWithAssistant, ThreadWithMessages, } from '../types'; -import { - buildSetTxBody, - buildSetValueOp, - getAssistant, - getChecksumAddress, - getValue, - sendTx, -} from '../utils/util'; import { Path } from '../utils/path'; +import { buildSetValueOp, buildSetTxBody, sendTx } from '../utils/transaction'; +import { getAssistant, getValue } from '../utils/util'; import { validateAssistant, validateObject, @@ -84,7 +78,7 @@ export class Threads extends FactoryBase { }); const txBody = this.buildTxBodyForCreateThread(address, objectId, tokenId, data); - const result = await sendTx(this.ain, txBody); + const result = await sendTx(txBody, this.ain); return { ...result, thread: data }; } @@ -126,7 +120,7 @@ export class Threads extends FactoryBase { }); const txBody = await this.buildTxBodyForUpdateThread(address, objectId, tokenId, data); - const result = await sendTx(this.ain, txBody); + const result = await sendTx(txBody, this.ain); return { ...result, thread: data }; } @@ -163,7 +157,7 @@ export class Threads extends FactoryBase { }); const txBody = this.buildTxBodyForDeleteThread(address, objectId, tokenId, threadId); - const result = await sendTx(this.ain, txBody); + const result = await sendTx(txBody, this.ain); return { ...result, delThread: data }; } @@ -183,7 +177,12 @@ export class Threads extends FactoryBase { await validateThread(this.ain, objectId, tokenId, address, threadId); const appId = AinftObject.getAppId(objectId); - const threadPath = Path.app(appId).token(tokenId).ai().history(address).thread(threadId).value(); + const threadPath = Path.app(appId) + .token(tokenId) + .ai() + .history(address) + .thread(threadId) + .value(); const data = await this.ain.db.ref(threadPath).getValue(); const thread = { id: data.id, @@ -254,7 +253,7 @@ export class Threads extends FactoryBase { }); const txBody = this.buildTxBodyForCreateAndRunThread(address, objectId, tokenId, data); - const result = await sendTx(this.ain, txBody); + const result = await sendTx(txBody, this.ain); return { ...result, ...data }; // NOTE(jiyoung): example data diff --git a/src/factoryBase.ts b/src/factoryBase.ts index 7ec3f07e..e0177e25 100644 --- a/src/factoryBase.ts +++ b/src/factoryBase.ts @@ -3,7 +3,7 @@ import Ainize from '@ainize-team/ainize-js'; import stringify = require("fast-json-stable-stringify"); import axios, { AxiosRequestHeaders } from "axios"; import { HttpMethod, HttpMethodToAxiosMethod, SerializedMessage } from "./types"; -import { buildData, isJoiError, sleep } from './utils/util'; +import { buildData, isJoiError } from './utils/util'; import FormData from "form-data"; /** diff --git a/src/nft.ts b/src/nft.ts index e9313e9a..ae32e30e 100644 --- a/src/nft.ts +++ b/src/nft.ts @@ -10,7 +10,7 @@ import { } from './types'; import Ainft721Object from './ainft721Object'; import stringify from 'fast-json-stable-stringify'; -import { isTransactionSuccess } from './utils/util'; +import { isTxSuccess } from './utils/transaction'; /** * This class supports creating AINFT object, searching AINFTs and things about NFTs.\ @@ -46,7 +46,7 @@ export default class Nft extends FactoryBase { const { ainftObjectId, txBody } = await this.sendRequest(HttpMethod.POST, trailingUrl, body); const res = await this.ain.sendTransaction(txBody); - if (!isTransactionSuccess(res)) { + if (!isTxSuccess(res)) { throw Error(`App creation is failed. - ${JSON.stringify(res)}`); } diff --git a/src/utils/index.ts b/src/utils/index.ts new file mode 100644 index 00000000..c2de4ba0 --- /dev/null +++ b/src/utils/index.ts @@ -0,0 +1,5 @@ +export * from './env'; +export * from './path'; +export * from './transaction'; +export * from './util'; +export * from './validator'; diff --git a/src/utils/util.ts b/src/utils/util.ts index 29cddc6d..db2672af 100644 --- a/src/utils/util.ts +++ b/src/utils/util.ts @@ -37,64 +37,6 @@ export const buildData = ( return _data; }; -export const buildSetTxBody = ( - operation: SetOperation | SetMultiOperation, - address: string -): TransactionInput => { - return { - operation: operation, - address, - gas_price: MIN_GAS_PRICE, - nonce: -1, - }; -}; - -export const sendTx = async (ain: Ain, txBody: any) => { - const result = await ain.sendTransaction(txBody); - if (!isTransactionSuccess(result)) { - throw new Error(`Transaction failed: ${JSON.stringify(result)}`); - } - return result; -}; - -export function isTransactionSuccess(transactionResponse: any) { - const { result } = transactionResponse; - if (result.code && result.code !== 0) { - return false; - } - if (result.result_list) { - const results = Object.values(result.result_list); - return results.every((_result: any) => _result.code === 0); - } - return true; -} - -export const buildSetValueOp = (ref: string, value: any): SetOperation => ({ - type: 'SET_VALUE', - ref, - value, -}); - -export const buildSetWriteRuleOp = (ref: string, rule: any) => buildSetRuleOp(ref, { write: rule }); - -export const buildSetStateRuleOp = (ref: string, rule: any) => buildSetRuleOp(ref, { state: rule }); - -export const buildSetRuleOp = (ref: string, rule: { write?: any; state?: any }): SetOperation => ({ - type: 'SET_RULE', - ref, - value: { - '.rule': { - write: rule.write, - state: rule.state, - }, - }, -}); - -export const buildSetOp = (opList: any[]): SetMultiOperation => ({ - type: 'SET', - op_list: opList, -}); - export function sleep(ms: number) { return new Promise((resolve) => { setTimeout(resolve, ms);