Skip to content

Commit

Permalink
[Issue-173] Fetch mythical balance from client
Browse files Browse the repository at this point in the history
  • Loading branch information
saltict committed Nov 21, 2024
1 parent cef6345 commit 1bc83b4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
// Copyright 2019-2022 @subwallet/extension-ui authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { BookaSdk } from '@subwallet/extension-koni-ui/connector/booka/sdk';
import { MythicalWallet } from '@subwallet/extension-koni-ui/connector/booka/types';
import { ThemeProps } from '@subwallet/extension-koni-ui/types';
import { toDisplayNumber } from '@subwallet/extension-koni-ui/utils';
import React from 'react';
import React, { useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import styled from 'styled-components';

type Props = ThemeProps;
const apiSDK = BookaSdk.instance;

const Component = ({ className }: Props): React.ReactElement => {
const { t } = useTranslation();
const [mythicalWallet, setMythicalWallet] = React.useState<MythicalWallet>(apiSDK.getMythicalWallet());

useEffect(() => {
const walletSub = apiSDK.subscribeMythicalWallet().subscribe((data) => {
setMythicalWallet(data);
});

return () => {
walletSub.unsubscribe();
};
}, []);

return (
<div className={className}>
<div className='__info-item'>
<div className='__info-label'>{t('Your Wallet')}</div>
<div className='__info-value __wallet-address-wrapper'>
<div className='__wallet-address'>XG35NNH7TR</div>
<div className='__wallet-address'>${mythicalWallet?.address}</div>

<button className={'__copy-button'}>
<svg
Expand All @@ -39,7 +53,7 @@ const Component = ({ className }: Props): React.ReactElement => {
<div className='__info-item'>
<div className='__info-label'>{t('Current Balance')}</div>
<div className='__info-value __token-value-wrapper'>
<span className={'__token-value'}>{toDisplayNumber(7712762)}</span>
<span className={'__token-value'}>{toDisplayNumber(mythicalWallet?.balanceInMyth)}</span>
<span className={'__token-symbol'}>&nbsp;Myth</span>
</div>
</div>
Expand Down
35 changes: 34 additions & 1 deletion packages/extension-koni-ui/src/connector/booka/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { GameState } from '@playnation/game-sdk/dist/types';
import { SWStorage } from '@subwallet/extension-base/storage';
import { createPromiseHandler, detectTranslate } from '@subwallet/extension-base/utils';
import { AppMetadata, MetadataHandler } from '@subwallet/extension-koni-ui/connector/booka/metadata';
import { AccountRankType, Achievement, AirdropCampaign, AirdropEligibility, AirdropRaffle, AirdropRewardHistoryLog, BookaAccount, ClaimableAchievement, EnergyConfig, Game, GameEvent, GameInventoryItem, GameItem, GamePlay, LeaderboardPerson, LeaderboardResult, NFLRivalCard, RankInfo, ReferralData, Task, TaskCategory } from '@subwallet/extension-koni-ui/connector/booka/types';
import { AccountRankType, Achievement, AirdropCampaign, AirdropEligibility, AirdropRaffle, AirdropRewardHistoryLog, BookaAccount, ClaimableAchievement, EnergyConfig, Game, GameEvent, GameInventoryItem, GameItem, GamePlay, LeaderboardPerson, LeaderboardResult, MythicalWallet, NFLRivalCard, RankInfo, ReferralData, Task, TaskCategory } from '@subwallet/extension-koni-ui/connector/booka/types';
import { TelegramConnector } from '@subwallet/extension-koni-ui/connector/telegram';
import { signRaw } from '@subwallet/extension-koni-ui/messaging';
import { populateTemplateString } from '@subwallet/extension-koni-ui/utils';
Expand All @@ -16,6 +16,7 @@ import { BehaviorSubject } from 'rxjs';

export const DEFAULT_INIT_DATA = process.env.DEFAULT_INIT_DATA;
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';
const storage = SWStorage.instance;
const telegramConnector = TelegramConnector.instance;
Expand Down Expand Up @@ -80,6 +81,10 @@ export class BookaSdk {
private dailyRewardAchievementsSubject = new BehaviorSubject<Achievement[]>([]);
private inviteAchievementsSubject = new BehaviorSubject<Achievement[]>([]);
private claimableAchievementSubject = new BehaviorSubject<ClaimableAchievement>({});
private mythicalWalletSubject = new BehaviorSubject<MythicalWallet>({
address: '',
balanceInMyth: '0'
});

// Special cases
// Check if the account is banned
Expand Down Expand Up @@ -1044,6 +1049,34 @@ export class BookaSdk {
return this.airdropCampaignSubject;
}

async fetchMythicalBalance (token: string) {
const request = await fetch(`${MYTHICAL_API_HOST}/player/wallet-balance`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
}
});

if (request.status === 200 || request.status === 304) {
const balance = (await request.json()) as unknown as MythicalWallet;

this.mythicalWalletSubject.next(balance);

return balance;
} else {
return undefined;
}
}

getMythicalWallet () {
return this.mythicalWalletSubject.value;
}

subscribeMythicalWallet () {
return this.mythicalWalletSubject;
}

// Singleton
private static _instance: BookaSdk;

Expand Down
5 changes: 5 additions & 0 deletions packages/extension-koni-ui/src/connector/booka/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,3 +556,8 @@ export interface NFLRivalCard {
jump: number;
carry: number;
}

export interface MythicalWallet {
address: string,
balanceInMyth: string
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ export const AuthenticationMythProvider = ({ children }: AuthenticationMythProvi
// authContext.logIn();
// }

bookaSDK.fetchNFLRivalCardList(authContext.token).catch(console.error);
if (authContext.token) {
bookaSDK.fetchMythicalBalance(authContext.token).catch(console.error);
bookaSDK.fetchNFLRivalCardList(authContext.token).catch(console.error);
}
}, [authContext, authContext.token]);

const onLoginWithMythAccount = useCallback(() => {
Expand Down

0 comments on commit 1bc83b4

Please sign in to comment.