Skip to content

Commit

Permalink
refactor: split util file.
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyoung-an committed Jun 3, 2024
1 parent f7d11d2 commit 648f1ce
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 99 deletions.
7 changes: 4 additions & 3 deletions src/ai/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,\
Expand All @@ -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 } };
}
Expand Down
21 changes: 10 additions & 11 deletions src/ai/assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -40,8 +41,6 @@ import {
validateServerConfigurationForObject,
validateToken,
} from '../utils/validator';
import { Path } from '../utils/path';
import { getEnv } from '../utils/env';

enum Role {
OWNER = 'owner',
Expand Down Expand Up @@ -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 };
Expand Down Expand Up @@ -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 };
Expand Down Expand Up @@ -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 };
Expand Down
16 changes: 5 additions & 11 deletions src/ai/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -27,7 +22,6 @@ import {
validateThread,
validateToken,
} from '../utils/validator';
import { Path } from '../utils/path';

/**
* This class supports create messages within threads.\
Expand Down Expand Up @@ -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 };
}
Expand Down Expand Up @@ -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 };
}
Expand Down
25 changes: 12 additions & 13 deletions src/ai/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 };
}
Expand Down Expand Up @@ -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 };
}
Expand Down Expand Up @@ -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 };
}
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/factoryBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand Down
4 changes: 2 additions & 2 deletions src/nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.\
Expand Down Expand Up @@ -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)}`);
}

Expand Down
5 changes: 5 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './env';
export * from './path';
export * from './transaction';
export * from './util';
export * from './validator';
58 changes: 0 additions & 58 deletions src/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 648f1ce

Please sign in to comment.