Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge feature/20241101 into develop #141

Merged
merged 5 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions src/app/api/_data/blk-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
API3_ENABLED,
defaultBalance,
BACKEND_API_URL,
defaultBalances,
tokensByNetwork,
BACKEND_API_TOKEN,
nodeProviderUrlSepolia
Expand All @@ -19,17 +20,15 @@ import TokenPriceFeedsAbi from './_abis/TokenPriceFeedsAbi.json'

export async function transferAll(channelUserId: string, walletTo: string): Promise<boolean> {
try {
// TODO:
// const data = { channel_user_id: channelUserId, dst_address: walletTo }
// const response = await axios.post(`${BACKEND_API_URL}/withdraw_funds`, data)
const data = { channel_user_id: channelUserId, dst_address: walletTo }
await axios.post(`${BACKEND_API_URL}/withdraw_funds`, data)
return true
} catch (error) {
console.error('Error transfering all funds:', error)
throw error
return true // avoid transfer-all error
}
}
export async function getBalancesWithTotalsFromBackend(walletAddress: string): Promise<IBalances> {
let balances: IBalances
const cacheKey = `getBalancesWithTotalsFromBackend.${walletAddress}`
const fromCache = cache.get(cacheKey) as IBalances

Expand All @@ -38,36 +37,42 @@ export async function getBalancesWithTotalsFromBackend(walletAddress: string): P
return fromCache
}

let responseBalances: IBalances
try {
const response = await axios.get(`${BACKEND_API_URL}/balance/${walletAddress}`, {
headers: {
Authorization: `Bearer ${BACKEND_API_TOKEN}`
}
})
const data = response.data as IBalances
responseBalances = response.data
} catch (error) {
console.error('Error fetching balance from backend:', error)
responseBalances = defaultBalances
}

// Convert keys to lowercase
const normalizedTotals = Object.keys(data.totals).reduce(
// Convert keys to lowercase
try {
const normalizedTotals = Object.keys(responseBalances.totals).reduce(
(acc, key) => {
const lowerKey = key.toLowerCase() as CurrencyKey
if (['usd', 'ars', 'brl', 'uyu'].includes(lowerKey)) {
acc[lowerKey] = data.totals[key as CurrencyKey]
acc[lowerKey] = responseBalances.totals[key as CurrencyKey]
}
return acc
},
{} as Record<CurrencyKey, number>
)

balances = {
...data,
const balances: IBalances = {
...responseBalances,
totals: normalizedTotals
}

cache.set(cacheKey, balances, 60) // 1 minute
return balances
} catch (error) {
console.error('Error fetching balance from backend:', error)
throw error
console.error('Error formatting balances totals:', error)
return defaultBalances
}
}

Expand Down
16 changes: 15 additions & 1 deletion src/config-global.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Network } from './types/networks'
import { IBalance, IBalances } from './types/wallet'

// ----------------------------------------------------------------------

Expand Down Expand Up @@ -56,7 +57,7 @@ export const STORAGE_KEY_SETTINGS = 'settings'
export const CONTACT_EMAIL = '[email protected]'
export const GET_BALANCES_FROM_BACKEND = true

export const defaultBalance = {
export const defaultBalance: IBalance = {
network: '',
token: '',
balance: 0,
Expand All @@ -68,6 +69,19 @@ export const defaultBalance = {
}
}

export const defaultBalances: IBalances = {
balances: [defaultBalance],
totals: {
usd: 0,
ars: 0,
brl: 0,
uyu: 0
},
certificates: [],
wallet: '',
message: ''
}

export const tokensByNetwork: { [networkKey: string]: Network } = {
scroll_testnet: {
config: {
Expand Down
2 changes: 2 additions & 0 deletions src/types/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export type IBalances = {
wallet: string
balances: IBalance[]
totals: Record<CurrencyKey, number>
message?: string
certificates?: []
}

export type INFTMetadata = {
Expand Down