Skip to content

Commit

Permalink
feat: 🎨 added function to check is vesting avalable or not
Browse files Browse the repository at this point in the history
  • Loading branch information
sadiabbasi committed Jan 10, 2025
1 parent 5497546 commit bdf5364
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
26 changes: 13 additions & 13 deletions src/containers/AssetManagerContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<AccountTokenDetails>({} as AccountTokenDetails);
const errorStore = useErrorStore();
Expand All @@ -106,36 +113,29 @@ 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 {
const assetData = await getAssetDetails(chain);

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 {
Expand All @@ -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', {
Expand Down
20 changes: 11 additions & 9 deletions src/utils/chain/antelope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -360,7 +361,6 @@ export class AntelopeToken extends AbstractToken implements IToken {
}

async getBalance(account?: AntelopeAccount): Promise<IAsset> {
return new Asset(this, new Decimal(0));
const contractAccount = this.getContractAccount();

if (!contractAccount) throw new Error('Token has no contract account');
Expand All @@ -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));
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down
18 changes: 16 additions & 2 deletions src/utils/chain/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ export interface IToken {
getBalance(account?: IAccount): Promise<IAsset>;
getUsdValue(account?: IAccount): Promise<number>;
isTransferable(): boolean;
isVesting(): boolean;
eq(other: IToken): boolean;
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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() &&
Expand Down

0 comments on commit bdf5364

Please sign in to comment.