Skip to content

Commit

Permalink
🐛 fix user balance fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Burg committed Sep 4, 2023
1 parent 8f45968 commit 7108c8f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
16 changes: 5 additions & 11 deletions batcher-ui/src/commands/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import { Cmd } from 'redux-loop';
import { scaleAmountDown, storeBalances } from '../utils/utils';
import { getBalances } from '../utils/utils';
import { gotUserBalances } from '../actions';
import * as api from '@tzkt/sdk-api';

const fetchUserBalancesCmd = (userAddress?: string) => {
return Cmd.run(
async () => {
if (!userAddress) return Promise.reject('No address !');
const rawBalances = await api.tokensGetTokenBalances({
account: {
eq: userAddress,
},
});

return storeBalances(rawBalances).map(b => ({
...b,
balance: scaleAmountDown(b.balance, b.decimals),
}));
return getBalances(
process.env.NEXT_PUBLIC_BATCHER_CONTRACT_HASH || '',
userAddress
);
},
{
successActionCreator: gotUserBalances,
Expand Down
2 changes: 1 addition & 1 deletion batcher-ui/src/types/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export enum PriceStrategy {

// ------ BATCHER STORAGE REPRESENTATION ------ //

type TokenNames = 'tzBTC' | 'EURL' | 'USDT';
export type TokenNames = 'tzBTC' | 'EURL' | 'USDT';
type SwapNames = 'tzBTC/USDT' | 'tzBTC/EURL';

type Swap = {
Expand Down
42 changes: 26 additions & 16 deletions batcher-ui/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import {
CurrentSwap,
Deposit,
HoldingsState,
Token,
TokenNames,
VolumesState,
VolumesStorage,
batchIsCleared,
} from '../types';
import { Batch } from 'src/types/events';
import { NetworkType } from '@airgap/beacon-sdk';
import * as api from '@tzkt/sdk-api';

export const scaleAmountDown = (amount: number, decimals: number) => {
const scale = 10 ** -decimals;
Expand Down Expand Up @@ -94,24 +97,31 @@ export type Balances = {
// TODO: need to configure token available in Batcher
export const TOKENS = ['USDT', 'EURL', 'TZBTC'];

/**
* Use to convert balances raw JSON from TZKT API to smooth Object
*/
export const toUserBalances = (rawBalances: any[]): Balances => {
return rawBalances.map(rawB => ({
name: rawB.token.metadata.symbol,
balance: rawB.balance,
decimals: rawB.token.metadata.decimals,
}));
};

export const filterBalances = (balances: Balances): Balances => {
return balances.filter(b => TOKENS.includes(b.name.toUpperCase()));
export const getBalances = async (
address: string,
userAddress: string
): Promise<Balances> => {
const storage = await getStorageByAddress(address);
const validTokens: Record<TokenNames, Token> = storage['valid_tokens'];
const rawBalances = await api.tokensGetTokenBalances({
account: {
eq: userAddress,
},
});
return Object.values(validTokens).map(token => {
const balance = rawBalances.find(
b => b.token?.contract?.address === token.address
)?.balance;
return {
name: token.name,
decimals: token.decimals,
balance: balance
? scaleAmountDown(parseFloat(balance), token.decimals)
: 0,
};
});
};

export const storeBalances = (balances: any[]) =>
filterBalances(toUserBalances(balances));

// ----- STORAGE ------

export const getStorageByAddress = (address: string): Promise<any> =>
Expand Down

0 comments on commit 7108c8f

Please sign in to comment.