Skip to content

Commit

Permalink
Merge pull request #141 from P4-Games/feature/20241101
Browse files Browse the repository at this point in the history
merge feature/20241101 into develop
  • Loading branch information
dappsar authored Nov 1, 2024
2 parents 9f507a1 + 7c70014 commit b5d9b8b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
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

0 comments on commit b5d9b8b

Please sign in to comment.