Skip to content

Commit

Permalink
[Issue-249] Update and fix log problems
Browse files Browse the repository at this point in the history
  • Loading branch information
saltict committed Jan 10, 2025
1 parent b1bcf16 commit d86ed52
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 31 deletions.
73 changes: 46 additions & 27 deletions packages/extension-koni-ui/src/connector/booka/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { BehaviorSubject } from 'rxjs';
import { stringToU8a, u8aToString } from '@polkadot/util';

export const DEFAULT_INIT_DATA = process.env.DEFAULT_INIT_DATA;
export const DEBUG_REPORT_URL = process.env.DEBUG_REPORT_URL || '';
export const GAME_API_HOST = process.env.GAME_API_HOST || 'https://game-api.anhmtv.xyz';
export const MYTHICAL_API_HOST = process.env.MYTHICAL_API_HOST || 'https://nflrivals.client.mythical.dev';
export const TELEGRAM_WEBAPP_LINK = process.env.TELEGRAM_WEBAPP_LINK || 'Playnation_bot/app';
Expand Down Expand Up @@ -91,16 +92,27 @@ export function getRewardStatus (status: RewardStatus) {

const metadataHandler = MetadataHandler.instance;

interface DebugData {
id: string;
datas: { type: string, input: any }[];
errors: { type: string, input: any }[];
}

// Todo: Create env to point or disable debugs
const DebugLogHandler = {
debugUrl: 'https://mythical-debug.playnation.app/debug',
debugUrl: DEBUG_REPORT_URL,
debugData: {
id: '_none_',
datas: [],
errors: []
},
} as DebugData,
debugLazy: undefined,
initHandler: createPromiseHandler<void>(),
initDebugLog: async () => {
if (!DebugLogHandler.debugUrl) {
return;
}

const { datas, errors } = DebugLogHandler.debugData;

const initLogData = {
Expand All @@ -123,49 +135,55 @@ const DebugLogHandler = {
if (rs.status > 400) {
console.error('Failed to push debug log', rs);
} else {
const rsData = await rs.json();
const rsData = (await rs.json()) as DebugData;

DebugLogHandler.debugData.id = rsData.id;
}

DebugLogHandler.initHandler.resolve();
},
sendTimeout: undefined,
sendDebugLog: (type: string, input: any, isError?: boolean) => {
sendDebugLog: (type: string, input: unknown, isError?: boolean) => {
if (!DebugLogHandler.debugUrl) {
return;
}

DebugLogHandler.sendTimeout && clearTimeout(DebugLogHandler.sendTimeout);
const debugUrl = DebugLogHandler.debugUrl;
const { datas, errors } = DebugLogHandler.debugData;

if (isError) {
// @ts-ignore
errors.push({
type, input
type,
input
});
} else {
// @ts-ignore
datas.push({
type, input
type,
input
});
}

// @ts-ignore
DebugLogHandler.sendTimeout = setTimeout(async () => {
await DebugLogHandler.initHandler.promise;
const id = DebugLogHandler.debugData.id;

const rs = await fetch(`${debugUrl}/${id}`, {
method: 'PUT',
body: JSON.stringify({
datas,
errors
})
});
DebugLogHandler.sendTimeout = setTimeout(() => {
(async () => {
await DebugLogHandler.initHandler.promise;
const id = DebugLogHandler.debugData.id;

const rs = await fetch(`${debugUrl}/${id}`, {
method: 'PUT',
body: JSON.stringify({
datas,
errors
})
});

if (rs.status > 400) {
console.error('Failed to push debug log', rs);
}
if (rs.status > 400) {
console.error('Failed to push debug log', rs);
}

DebugLogHandler.sendTimeout = undefined;
DebugLogHandler.sendTimeout = undefined;
})().catch(console.error);
}, 300);
}
};
Expand Down Expand Up @@ -212,7 +230,6 @@ export class BookaSdk {
this.initMetadataHandling();
const version = localStorage.getItem('cache-version');

// Todo #249: Try to reset cache
if (cacheVersion === version) {
const account = parseCache<BookaAccount>(CACHE_KEYS.account);
const taskCategoryList = parseCache<TaskCategory[]>(CACHE_KEYS.taskCategoryList);
Expand Down Expand Up @@ -373,7 +390,7 @@ export class BookaSdk {

const data = await response.json() as T;

this.pushDebugLog(url, { request: body, response: '__OK__' });
this.pushDebugLog(url, { request: body as string, response: '__OK__' });

return data;
}
Expand Down Expand Up @@ -568,6 +585,7 @@ export class BookaSdk {

async fetchMythicalBalance (token?: string) {
await this.waitForSync;

try {
const rs = await this.postRequest<MythicalWallet>(`${GAME_API_HOST}/api/mythical-account/fetch`, { token: token });

Expand Down Expand Up @@ -831,7 +849,7 @@ export class BookaSdk {
errors: []
};

pushDebugLog (type: string, input: any, isError?: boolean) {
pushDebugLog (type: string, input: unknown, isError?: boolean) {
DebugLogHandler.sendDebugLog(type, input, isError);
}

Expand Down Expand Up @@ -892,7 +910,7 @@ export class BookaSdk {
}
}

async updateAccountAddress(address: string) {
async updateAccountAddress (address: string) {
const initData = telegramConnector.initData || DEFAULT_INIT_DATA;
const currentAddress = this.account?.info.address;

Expand All @@ -907,6 +925,7 @@ export class BookaSdk {

try {
const account = await this.postRequest<BookaAccount>(`${GAME_API_HOST}/api/account/login`, syncData);

account && this.accountSubject.next(account);
} catch (e) {
console.error('Error in updateAccountAddress:', e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const startData = Telegram.WebApp.initDataUnsafe;
const linkSDK = new TelegramBotLink(config as LinkConfig);
const telegramConnector = TelegramConnector.instance;

// Todo #249: Kiểm tra khả năng không chạy vào login khi nào?
export const AuthenticationMythProvider = ({ children }: AuthenticationMythProviderProps) => {
const [account, setAccount] = useState<AccountPublicInfo>({} as AccountPublicInfo);
const [mythicalWallet, setMythicalWallet] = useState<MythicalWallet>(bookaSDK.getMythicalWallet());
Expand All @@ -71,7 +70,6 @@ export const AuthenticationMythProvider = ({ children }: AuthenticationMythProvi
bookaSDK.pushDebugLog('fetch_data_with_token', { token: authContext?.token?.length });
bookaSDK.fetchNFLRivalCardList(authContext.token).catch(console.error);
bookaSDK.fetchMythicalBalance(authContext.token).catch(console.error);

}, [authContext.token]);

useEffect(() => {
Expand All @@ -84,7 +82,7 @@ export const AuthenticationMythProvider = ({ children }: AuthenticationMythProvi
return () => {
unsub.unsubscribe();
};
}, []);
}, [mythicalWallet]);

const onLoginWithMythAccount = useCallback(() => {
localStorage.setItem(LOCAL_LOGGED_IN_PROMISE_KEY, 'login');
Expand Down Expand Up @@ -185,7 +183,6 @@ export const AuthenticationMythProvider = ({ children }: AuthenticationMythProvi
};
});

// Todo #249: Problems may be from here
if (linkData.link_address && !isSameAddress(bookaSDK.account?.info.address || '', linkData.link_address)) {
bookaSDK.pushDebugLog('on_link_data', linkData);
onLoginWithTelegramAccount(linkData.link_address).catch(console.error);
Expand Down
1 change: 1 addition & 0 deletions packages/webapp/webpack.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const _additionalEnv = {
INFURA_API_KEY: JSON.stringify(process.env.INFURA_API_KEY),
INFURA_API_KEY_SECRET: JSON.stringify(process.env.INFURA_API_KEY_SECRET),
DEFAULT_INIT_DATA: JSON.stringify(process.env.DEFAULT_INIT_DATA),
DEBUG_REPORT_URL: JSON.stringify(process.env.DEBUG_REPORT_URL),
GAME_API_HOST: JSON.stringify(process.env.GAME_API_HOST),
TELEGRAM_WEBAPP_LINK: JSON.stringify(process.env.TELEGRAM_WEBAPP_LINK),
AUTHENTICATE_CLIENT_ID: JSON.stringify(process.env.AUTHENTICATE_CLIENT_ID),
Expand Down

0 comments on commit d86ed52

Please sign in to comment.