Skip to content

Commit

Permalink
feat: lazy fetch contract data (#1944)
Browse files Browse the repository at this point in the history
Co-authored-by: iGroza <[email protected]>
  • Loading branch information
iGroza and iGroza authored Jun 7, 2024
1 parent e4c88c7 commit 37bcd0c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
32 changes: 31 additions & 1 deletion src/models/contracts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {makeAutoObservable} from 'mobx';
import {makePersistable} from 'mobx-persist-store';

import {AddressUtils} from '@app/helpers/address-utils';
import {Whitelist} from '@app/helpers/whitelist';
import {storage} from '@app/services/mmkv';
import {IContract, IToken, MobXStore} from '@app/types';

Expand All @@ -11,6 +13,7 @@ class ContractsStore implements MobXStore<IContract> {
* @value IContract
*/
data: Record<string, IContract> = {};
private _fetchInProgressMap: Record<string, boolean> = {};

constructor(shouldSkipPersisting: boolean = false) {
makeAutoObservable(this);
Expand Down Expand Up @@ -72,7 +75,13 @@ class ContractsStore implements MobXStore<IContract> {
}

getById(id: string) {
return this.data[id];
const result = this.data[id];

if (!result) {
this._fetchContractData(id);
}

return result;
}

update(id: string | undefined, item: Omit<Partial<IToken>, 'id'>) {
Expand All @@ -93,6 +102,27 @@ class ContractsStore implements MobXStore<IContract> {
};
return true;
}

private async _fetchContractData(address: string) {
try {
const isFetching = this._fetchInProgressMap[address];
if (isFetching || !AddressUtils.isValidAddress(address)) {
return;
}
this._fetchInProgressMap[address] = true;
const haqqAddress = AddressUtils.toHaqq(address);
const data = await Whitelist.verifyAddress(haqqAddress);

if (!data) {
return;
}

this.create(haqqAddress, data);
} catch (err) {
} finally {
this._fetchInProgressMap[address] = false;
}
}
}

const instance = new ContractsStore(Boolean(process.env.JEST_WORKER_ID));
Expand Down
15 changes: 1 addition & 14 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1371,20 +1371,7 @@ export enum AddressType {
unknown = 'unknown',
}

export interface VerifyAddressResponse {
id: string;
address_type: AddressType;
name?: string | null;
symbol?: string | null;
icon?: string | null;
decimals?: number | null;
is_erc20?: boolean | null;
is_erc721?: boolean | null;
is_erc1155?: boolean | null;
is_in_white_list?: boolean | null;
updated_at: string;
created_at: string;
}
export type VerifyAddressResponse = IContract;

export interface MobXStoreFromRealm {
realmSchemaName: string;
Expand Down

0 comments on commit 37bcd0c

Please sign in to comment.