-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(HW-1044): Contracts store refactoring (#2260)
* feat(HW-1044): Fix mobx ts-ignores * feat(HW-1044): Contracts store * feat(HW-1044): Store contracts by id * feat(HW-1044): Check fetched contracts * feat(HW-1044): _searchContract * feat(HW-1044): Fix contract search function by using haqq address
- Loading branch information
1 parent
02742cb
commit e932aa0
Showing
33 changed files
with
382 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import {makeAutoObservable, runInAction} from 'mobx'; | ||
import {makePersistable} from 'mobx-persist-store'; | ||
|
||
import {AddressUtils} from '@app/helpers/address-utils'; | ||
import {Indexer, IndexerAddressesResponse} from '@app/services/indexer'; | ||
import {storage} from '@app/services/mmkv'; | ||
import {ChainId} from '@app/types'; | ||
|
||
import {ContractStoreData, IndexerContract} from './contract.types'; | ||
|
||
import {ALL_NETWORKS_ID, Provider} from '../provider'; | ||
|
||
class Contract { | ||
_data: ContractStoreData = {}; | ||
|
||
private _searchContract = (contractAddress: string) => { | ||
const fetchedContractFlatMap = Object.entries(this._data).flatMap( | ||
([_, v]) => Object.entries(v).flatMap(([__, c]) => c), | ||
); | ||
return ( | ||
fetchedContractFlatMap?.find(t => | ||
AddressUtils.equals(t.id, contractAddress), | ||
) ?? null | ||
); | ||
}; | ||
|
||
constructor(shouldSkipPersisting: boolean = false) { | ||
makeAutoObservable(this); | ||
if (!shouldSkipPersisting) { | ||
makePersistable(this, { | ||
name: this.constructor.name, | ||
properties: ['_data'], | ||
storage, | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Fetch contracts' information and store it into this._data | ||
* | ||
* @param contractAddresses Array of contract's addresses which shoul be fetched | ||
* @param chainId Chain id in which contracts will be fetched. If no chainId provided then contracts will be fetched for all chains | ||
*/ | ||
fetch = async (contractAddresses: string[], chainId?: ChainId) => { | ||
let contracts: IndexerAddressesResponse | null = null; | ||
|
||
if (!chainId) { | ||
const headers = Indexer.instance.getProvidersHeader( | ||
contractAddresses, | ||
Provider.getById(ALL_NETWORKS_ID), | ||
); | ||
contracts = await Indexer.instance.getAddresses(headers); | ||
} else { | ||
contracts = await Indexer.instance.getAddresses({ | ||
[chainId]: contractAddresses, | ||
}); | ||
} | ||
|
||
if (contracts) { | ||
runInAction(() => { | ||
this._data = Object.entries(contracts).reduce((acc, [cId, data]) => { | ||
const reducedContracts = data.reduce( | ||
(prev, contract) => ({ | ||
...prev, | ||
[contract.id]: contract, | ||
}), | ||
{} as Record<string, IndexerContract>, | ||
); | ||
|
||
return { | ||
...this._data, | ||
...acc, | ||
[cId]: { | ||
...this._data[cId], | ||
...reducedContracts, | ||
}, | ||
}; | ||
}, {} as ContractStoreData); | ||
}); | ||
} | ||
}; | ||
|
||
/** | ||
* Fetch contract information and update this._data with it | ||
* | ||
* @param contractAddress Contract's address which shoul be fetched | ||
* @param chainId Chain id in which contracts will be fetched. If no chainId provided then contracts will be fetched for all chains | ||
* @returns Contract for {contractAddress} | ||
*/ | ||
getById = async ( | ||
contractAddress: string, | ||
chainId?: ChainId, | ||
): Promise<IndexerContract | null> => { | ||
let contract: IndexerContract | null = null; | ||
const contractId = AddressUtils.toHaqq(contractAddress); | ||
|
||
if (!chainId) { | ||
// Check already fetched contracts | ||
contract = this._searchContract(contractId); | ||
|
||
// If fetched contract doesn't exists than fetch and find contract from all chains | ||
if (!contract) { | ||
const headers = Indexer.instance.getProvidersHeader( | ||
[contractAddress], | ||
Provider.getById(ALL_NETWORKS_ID), | ||
); | ||
const contracts = await Indexer.instance.getAddresses(headers); | ||
const contractFlatMap = Object.entries(contracts).flatMap( | ||
([_, v]) => v, | ||
); | ||
contract = contractFlatMap?.find(t => t.name) ?? null; | ||
} | ||
} else { | ||
contract = this._data[chainId][contractId] ?? null; | ||
if (!contract) { | ||
await this.fetch([contractAddress], chainId); | ||
contract = this._data[chainId][contractId] ?? null; | ||
} | ||
} | ||
|
||
return contract; | ||
}; | ||
} | ||
|
||
const instance = new Contract(Boolean(process.env.JEST_WORKER_ID)); | ||
export {instance as Contract}; |
Oops, something went wrong.