Skip to content

Commit c6c247c

Browse files
committed
Cache interface refactor balances
1 parent 905ab4c commit c6c247c

File tree

5 files changed

+40
-21
lines changed

5 files changed

+40
-21
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "verto-cache-interface",
3-
"version": "1.2.5",
3+
"version": "1.2.6",
44
"description": "A communication package with Verto Cache System",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/calls/fetch-balance-by-user-addr.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ export const fetchBalanceByUserAddress = async (contractId: string, userAddress:
1616
if(stateKeys.length <= 0) {
1717
return undefined;
1818
}
19-
19+
20+
console.warn(`fetchBalanceByUserAddress does not return 'username'`);
21+
22+
// @ts-ignore
2023
return {
2124
name: contractMetadata.name,
2225
ticker: contractMetadata.ticker,

src/calls/fetch-balance-by-username.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
import {fetchUserMetadataByUsername} from "./fetch-user-metadata-by-username";
22
import {UserBalance} from "./types/user-balance-model";
33
import {fetchBalancesForAddress} from "./fetch-balances-for-address";
4+
import {cacheApiBaseRequest} from "./cache-api-base-request";
45

56
/**
67
* Fetches all the balances given a username.
78
* @param username
89
* @param tokenType Type contract with balances to be fetched, for example: 'art'
10+
* @param manual Whether it should fetch by each or by cache
911
*/
10-
export const fetchBalancesByUsername = async (username: string, tokenType?: string) => {
11-
const userMetadata = await fetchUserMetadataByUsername(username);
12-
if(userMetadata) {
13-
const addresses = userMetadata.addresses || [];
14-
let response: Array<UserBalance> = [];
12+
export const fetchBalancesByUsername = async (username: string, tokenType?: string, manual?: boolean) => {
13+
let response: Array<UserBalance> = [];
14+
if(manual) {
15+
const userMetadata = await fetchUserMetadataByUsername(username);
16+
if (userMetadata) {
17+
const addresses = userMetadata.addresses || [];
1518

16-
for (const address of addresses) {
17-
const balancesInAddress = await fetchBalancesForAddress(address, tokenType);
18-
response = [
19-
...response,
20-
...balancesInAddress
21-
];
22-
}
19+
for (const address of addresses) {
20+
const balancesInAddress = await fetchBalancesForAddress(address, tokenType);
21+
response = [
22+
...response,
23+
...balancesInAddress
24+
];
25+
}
2326

27+
return response;
28+
}
29+
} else {
30+
const data = (await cacheApiBaseRequest<{ entities: Array<UserBalance>}>(`users/balances/${username}?username=true`))?.data;
31+
response = [...(data?.entities || [])];
2432
return response;
2533
}
2634

src/calls/fetch-balances-for-address.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
import {UserBalance} from "./types/user-balance-model";
22
import {fetchBalanceByUserAddress} from "./fetch-balance-by-user-addr";
33
import {fetchContractsInUser} from "./fetch-contracts-in-user";
4+
import {cacheApiBaseRequest} from "./cache-api-base-request";
45

56
/**
67
* Fetches all the balances for a given address.
78
* This code execute a look-up in all the contracts that are known to the user.
89
* @param userAddress
910
* @param tokenType Whether to filter by an specific token type.
11+
* @param manual Whether it should fetch by each or by cache
1012
*/
11-
export const fetchBalancesForAddress = async (userAddress: string, tokenType?: string): Promise<Array<UserBalance>> => {
12-
const contracts = await fetchContractsInUser(userAddress) || [];
13-
13+
export const fetchBalancesForAddress = async (userAddress: string, tokenType?: string, manual?: boolean): Promise<Array<UserBalance>> => {
1414
let response: Array<UserBalance> = [];
15-
for (let contractId of contracts) {
16-
const balance = await fetchBalanceByUserAddress(contractId, userAddress);
17-
if(balance) {
18-
response.push(balance);
15+
if(manual) {
16+
const contracts = await fetchContractsInUser(userAddress) || [];
17+
for (let contractId of contracts) {
18+
const balance = await fetchBalanceByUserAddress(contractId, userAddress);
19+
if (balance) {
20+
response.push(balance);
21+
}
1922
}
23+
} else {
24+
const data = (await cacheApiBaseRequest<{ entities: Array<UserBalance>}>(`users/balances/${userAddress}`))?.data;
25+
response = [...(data?.entities || [])];
2026
}
2127

28+
2229
if(tokenType) {
2330
response = response.filter((item) => item.type?.toLowerCase() === tokenType.toLowerCase());
2431
}

src/calls/types/user-balance-model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ export interface UserBalance {
55
balance: number;
66
contractId: string;
77
userAddress: string;
8+
username: string;
89
type?: string;
910
}

0 commit comments

Comments
 (0)