remove admin
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
import { getCsrfToken } from '@shared/api/csrf'
|
||||
import { tokenStore, refreshAccessToken } from '@shared/api/tokenStore'
|
||||
import {
|
||||
getOrganizationWallets,
|
||||
getOrganizationBalances,
|
||||
type OrgAmountRaw,
|
||||
type OrgWalletBalance,
|
||||
} from '@features/b2b'
|
||||
|
||||
const WALLET_API_URL = 'https://app.cryptowallet.elcsa.ru'
|
||||
|
||||
@@ -148,6 +154,94 @@ export async function getPortfolio(): Promise<PortfolioData> {
|
||||
return res.data
|
||||
}
|
||||
|
||||
// --- Legal-entity (b2b organizations) adapters ---
|
||||
// The b2b balances endpoint returns the same information as the cryptowallet
|
||||
// portfolio, but with snake_case keys, string-encoded numbers and an items[]
|
||||
// array instead of a perChain map. These adapters normalise it into the
|
||||
// existing PortfolioData / WalletAddress shapes so the wallet UI is reused as-is.
|
||||
|
||||
const NATIVE_SYMBOL_TO_CHAIN: Record<string, Chain> = {
|
||||
ETH: 'ETH',
|
||||
BNB: 'BSC',
|
||||
BTC: 'BTC',
|
||||
TRX: 'TRX',
|
||||
SOL: 'SOL',
|
||||
}
|
||||
|
||||
/** Normalise a b2b chain/native_symbol into our Chain enum, or null if unknown. */
|
||||
function normalizeChain(chain: string, nativeSymbol?: string): Chain | null {
|
||||
const upper = chain?.toUpperCase()
|
||||
if ((CHAINS as string[]).includes(upper)) return upper as Chain
|
||||
const bySymbol = nativeSymbol ? NATIVE_SYMBOL_TO_CHAIN[nativeSymbol.toUpperCase()] : undefined
|
||||
return bySymbol ?? null
|
||||
}
|
||||
|
||||
function parseNum(value: string | number | null | undefined): number {
|
||||
const n = typeof value === 'number' ? value : parseFloat(value ?? '')
|
||||
return Number.isFinite(n) ? n : 0
|
||||
}
|
||||
|
||||
function toFormattedAmount(a: OrgAmountRaw): FormattedAmount {
|
||||
return {
|
||||
raw: a.raw,
|
||||
formatted: a.formatted,
|
||||
decimals: a.decimals,
|
||||
usdPrice: parseNum(a.usd_price),
|
||||
usdValue: parseNum(a.usd_value),
|
||||
}
|
||||
}
|
||||
|
||||
function toPortfolioChain(item: OrgWalletBalance, chain: Chain): PortfolioChain {
|
||||
const tokens: Record<string, FormattedAmount> = {}
|
||||
for (const [symbol, amount] of Object.entries(item.tokens ?? {})) {
|
||||
tokens[symbol] = toFormattedAmount(amount)
|
||||
}
|
||||
return {
|
||||
chain,
|
||||
address: item.address,
|
||||
native: toFormattedAmount(item.native),
|
||||
tokens,
|
||||
totalUsd: parseNum(item.total_usd),
|
||||
stale: false,
|
||||
lastUpdated: 0,
|
||||
}
|
||||
}
|
||||
|
||||
export async function getOrgPortfolio(): Promise<PortfolioData> {
|
||||
const res = await getOrganizationBalances()
|
||||
const perChain = {} as Record<Chain, PortfolioChain>
|
||||
for (const item of res.items ?? []) {
|
||||
const chain = normalizeChain(item.chain, item.native_symbol)
|
||||
if (!chain) continue
|
||||
perChain[chain] = toPortfolioChain(item, chain)
|
||||
}
|
||||
return {
|
||||
totalUsd: parseNum(res.total_usd),
|
||||
hasErrors: !!res.has_errors,
|
||||
perChain,
|
||||
}
|
||||
}
|
||||
|
||||
const EMPTY_AMOUNT: FormattedAmount = { raw: '0', formatted: '0', decimals: 0, usdPrice: 0, usdValue: 0 }
|
||||
|
||||
export async function getOrgWalletBalance(chain: Chain): Promise<WalletBalanceData> {
|
||||
const portfolio = await getOrgPortfolio()
|
||||
const c = portfolio.perChain[chain]
|
||||
if (!c) return { chain, address: '', native: EMPTY_AMOUNT, tokens: {} }
|
||||
return { chain, address: c.address, native: c.native, tokens: c.tokens }
|
||||
}
|
||||
|
||||
export async function getOrgWalletAddresses(): Promise<WalletAddress[]> {
|
||||
const wallets = await getOrganizationWallets()
|
||||
const result: WalletAddress[] = []
|
||||
for (const w of wallets ?? []) {
|
||||
const chain = normalizeChain(w.chain)
|
||||
if (!chain) continue
|
||||
result.push({ chain, address: w.address, derivationPath: w.derivation_path })
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
export interface TokenInfo {
|
||||
chain: string
|
||||
symbol: string
|
||||
|
||||
Reference in New Issue
Block a user