diff --git a/src/containers/AssetManagerContainer.tsx b/src/containers/AssetManagerContainer.tsx index 72119169..46c5db97 100644 --- a/src/containers/AssetManagerContainer.tsx +++ b/src/containers/AssetManagerContainer.tsx @@ -89,6 +89,13 @@ const formatBalance = (balance: IAsset) => { return balance.toString().split(' ')[0]; }; +const getTotalBalance = (availableBalance: IAsset, vestedBalance: IAsset) => { + const availableBalanceValue = formatBalance(availableBalance); + const vestedBalanceValue = formatBalance(vestedBalance); + + return new Decimal(availableBalanceValue).add(vestedBalanceValue); +}; + const AssetManagerContainer = ({ navigation, chain }: Props) => { const [asset, setAsset] = useState({} as AccountTokenDetails); const errorStore = useErrorStore(); @@ -106,6 +113,8 @@ const AssetManagerContainer = ({ navigation, chain }: Props) => { const token = chain.getNativeToken() as PangeaVestedToken; const symbol = chain.getNativeToken().getSymbol(); + const isVestingAvailable = chain.getNativeToken().isVesting(); + useEffect(() => { const fetchAssetDetails = async () => { try { @@ -113,29 +122,20 @@ const AssetManagerContainer = ({ navigation, chain }: Props) => { setAsset(assetData); - if (symbol === 'LEOS') { + if (isVestingAvailable) { const account = AntelopeAccount.fromAccount(PangeaMainnetChain, assetData.account); const vestedBalance = await token.getVestedTotalBalance(account); const availableBalance = await token.getAvailableBalance(account); - const availableBalanceValue = formatBalance(availableBalance); - const vestedBalanceUsd = await vestedBalance.getUsdValue(); - const vestedBalanceValue = formatBalance(vestedBalance); - const totalBalance = new Decimal(availableBalanceValue).add(vestedBalanceValue); + const totalBalance = getTotalBalance(availableBalance, vestedBalance); const usdPriceValue = await chain.getNativeToken().getUsdPrice(); - console.log( - 'vestedBalance1111', - totalBalance.toString(), - formatBalance(availableBalance), - formatBalance(vestedBalance) - ); setBalance({ totalBalance: totalBalance.toString() + ' ' + symbol, totalBalanceUsd: totalBalance.toNumber() * usdPriceValue, availableBalance: availableBalance.toString(), availableBalanceUsd: await availableBalance.getUsdValue(), vestedBalance: vestedBalance.toString(), - vestedBalanceUsd, + vestedBalanceUsd: await vestedBalance.getUsdValue(), }); setShowVesting(true); } else { @@ -157,7 +157,7 @@ const AssetManagerContainer = ({ navigation, chain }: Props) => { const interval = setInterval(fetchAssetDetails, 10000); return () => clearInterval(interval); - }, [chain, token, symbol, errorStore]); + }, [chain, token, symbol, errorStore, isVestingAvailable]); const redirectVestedAsset = () => { navigation.navigate('VestedAssets', { diff --git a/src/utils/chain/antelope.ts b/src/utils/chain/antelope.ts index 52b11857..a1854b59 100644 --- a/src/utils/chain/antelope.ts +++ b/src/utils/chain/antelope.ts @@ -334,9 +334,10 @@ export class AntelopeToken extends AbstractToken implements IToken { precision: number, logoUrl: string, coinmarketCapId: string, - transferable = true + transferable = true, + vestingAvailable = false ) { - super(name, symbol, precision, chain, logoUrl, transferable); + super(name, symbol, precision, chain, logoUrl, transferable, vestingAvailable); this.coinmarketCapId = coinmarketCapId; } @@ -360,7 +361,6 @@ export class AntelopeToken extends AbstractToken implements IToken { } async getBalance(account?: AntelopeAccount): Promise { - return new Asset(this, new Decimal(0)); const contractAccount = this.getContractAccount(); if (!contractAccount) throw new Error('Token has no contract account'); @@ -379,8 +379,6 @@ export class AntelopeToken extends AbstractToken implements IToken { this.getSymbol() ); - if (!assets) return new Asset(this, new Decimal(0)); - const asset = assets.find((asset) => asset.symbol.toString() === this.toAntelopeSymbol().toString()); if (!asset) return new Asset(this, new Decimal(0)); @@ -475,7 +473,8 @@ export const LEOSToken = new PangeaVestedToken( 6, 'https://github.com/Tonomy-Foundation/documentation/blob/master/images/logos/LEOS%20256x256.png?raw=true', 'leos', - false + false, + true ); export const LEOSTestnetToken = new PangeaVestedToken( @@ -485,7 +484,8 @@ export const LEOSTestnetToken = new PangeaVestedToken( 6, 'https://github.com/Tonomy-Foundation/documentation/blob/master/images/logos/LEOS%20256x256.png?raw=true', 'leos-testnet', - false + false, + true ); export const LEOSStagingToken = new PangeaVestedToken( @@ -495,7 +495,8 @@ export const LEOSStagingToken = new PangeaVestedToken( 6, 'https://github.com/Tonomy-Foundation/documentation/blob/master/images/logos/LEOS%20256x256.png?raw=true', 'leos-staging', - false + false, + true ); export const LEOSLocalToken = new PangeaVestedToken( @@ -505,7 +506,8 @@ export const LEOSLocalToken = new PangeaVestedToken( 6, 'https://github.com/Tonomy-Foundation/documentation/blob/master/images/logos/LEOS%20256x256.png?raw=true', 'leos-local', - false + false, + true ); export const EOSJungleChain = new AntelopeChain( diff --git a/src/utils/chain/types.ts b/src/utils/chain/types.ts index d94c6cbb..e9f0165a 100644 --- a/src/utils/chain/types.ts +++ b/src/utils/chain/types.ts @@ -243,6 +243,7 @@ export interface IToken { getBalance(account?: IAccount): Promise; getUsdValue(account?: IAccount): Promise; isTransferable(): boolean; + isVesting(): boolean; eq(other: IToken): boolean; } @@ -254,14 +255,24 @@ export abstract class AbstractToken implements IToken { protected chain: IChain; protected logoUrl: string; protected transferable = true; - - constructor(name: string, symbol: string, precision: number, chain: IChain, logoUrl: string, transferable = true) { + protected vestingAvailable = false; + + constructor( + name: string, + symbol: string, + precision: number, + chain: IChain, + logoUrl: string, + transferable = true, + vestingAvailable = false + ) { this.name = name; this.symbol = symbol; this.precision = precision; this.chain = chain; this.logoUrl = logoUrl; this.transferable = transferable; + this.vestingAvailable = vestingAvailable; } setAccount(account: IAccount): IToken { @@ -291,6 +302,9 @@ export abstract class AbstractToken implements IToken { isTransferable(): boolean { return this.transferable; } + isVesting(): boolean { + return this.vestingAvailable; + } eq(other: IToken): boolean { return ( this.getName() === other.getName() &&