From 6d13e2d162314b7c427233e162f29750f098f807 Mon Sep 17 00:00:00 2001 From: Aleksandr Kraiz Date: Wed, 23 Aug 2023 22:10:53 +0400 Subject: [PATCH] Bridge: addressToAsset --- package.json | 2 +- src/Orion/bridge/index.ts | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index d95faaa..c7937e9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.19.67", + "version": "0.19.68", "description": "Orion Protocol SDK", "main": "./lib/index.cjs", "module": "./lib/index.js", diff --git a/src/Orion/bridge/index.ts b/src/Orion/bridge/index.ts index 15fba2f..ccadd14 100644 --- a/src/Orion/bridge/index.ts +++ b/src/Orion/bridge/index.ts @@ -10,6 +10,7 @@ import { BigNumber } from 'bignumber.js'; import generateSecret from '../../utils/generateSecret.js'; import { isPresent } from 'ts-is-present'; import { invariant } from '../../utils/invariant.js'; +import { simpleFetch } from 'simple-typed-fetch'; export const SECONDS_IN_DAY = 60 * 60 * 24; export const EXPIRATION_DAYS = 4; @@ -22,6 +23,12 @@ export default class Bridge { data: ExternalAtomicsData }>> = {}; + readonly ADDRESS_TO_ASSET_CACHE_TIME_MS = 5 * 60 * 1000; // 5 minutes + private addressToAsset: { + lastUpdate: number + data: Partial>>> + } = { lastUpdate: 0, data: {} }; + constructor( private readonly unitsArray: Unit[], ) {} @@ -46,11 +53,38 @@ export default class Bridge { } } + async getCombinedAddressToAsset() { + const { lastUpdate, data } = this.addressToAsset; + const cacheIsExpired = lastUpdate + this.EXTERNAL_ATOMICS_DATA_CACHE_TIME_MS < Date.now(); + if (!cacheIsExpired) return data; + const addressToAssetData: Partial>>> = {}; + await Promise.all(this.unitsArray.map(async (unit) => { + const { blockchainService, chainId } = unit; + const { assetToAddress } = await simpleFetch(blockchainService.getInfo)(); + Object.entries(assetToAddress).forEach(([asset, address]) => { + if (address !== undefined) { + const assetRecord = addressToAssetData[address]; + if (assetRecord !== undefined) { + assetRecord[chainId] = asset; + } else { + addressToAssetData[address] = { + [chainId]: asset, + }; + } + } + }); + })); + this.addressToAsset = { + lastUpdate: Date.now(), + data: addressToAssetData, + } + return addressToAssetData; + } + async combineLocalAndExternalData( walletAddress: string, localAtomicSwaps: AtomicSwapLocal[], transactions: TransactionInfo[], - combinedAddressToAsset: Partial>>>, ) { // Prepare transactions data const byTxHashMap = new Map(); @@ -78,6 +112,7 @@ export default class Bridge { // Combine local data and external data const bridgeHistory = await this.getHistory(walletAddress); + const combinedAddressToAsset = await this.getCombinedAddressToAsset(); const atomicSwapsMap = new Map(); Object.values(bridgeHistory) .filter(isPresent)