-
Notifications
You must be signed in to change notification settings - Fork 63
[LW-10504]add timeout transaction inspectors #1313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
przemyslaw-wlodek
merged 1 commit into
master
from
feat/lw-10504-add-timeout-transaction-inspectors
Jun 21, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,17 @@ | ||
/* eslint-disable promise/param-names */ | ||
|
||
import { TimeoutError } from '../errors'; | ||
|
||
export const promiseTimeout = <T>(promise: Promise<T>, timeout: number) => { | ||
let timeoutId: NodeJS.Timeout; | ||
|
||
return Promise.race([ | ||
promise, | ||
new Promise<T>( | ||
(_, reject) => | ||
(timeoutId = setTimeout(() => reject(new TimeoutError('Failed to resolve the promise in time')), timeout)) | ||
) | ||
]).finally(() => { | ||
if (timeoutId) clearTimeout(timeoutId); | ||
}); | ||
}; |
This file contains hidden or 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 hidden or 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 hidden or 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,41 @@ | ||
import * as Cardano from '../Cardano'; | ||
import { AssetInfo } from '../Asset'; | ||
import { AssetProvider } from '../Provider'; | ||
import { Logger } from 'ts-log'; | ||
import { Milliseconds } from './time'; | ||
import { promiseTimeout } from './promiseTimeout'; | ||
|
||
type TryGetAssetInfosProps = { | ||
assetIds: Cardano.AssetId[]; | ||
assetProvider: AssetProvider; | ||
timeout: Milliseconds; | ||
logger: Logger; | ||
}; | ||
|
||
export const tryGetAssetInfos = async ({ assetIds, assetProvider, logger, timeout }: TryGetAssetInfosProps) => { | ||
try { | ||
return await promiseTimeout( | ||
assetProvider.getAssets({ | ||
assetIds, | ||
extraData: { nftMetadata: true, tokenMetadata: true } | ||
}), | ||
timeout | ||
); | ||
} catch (error) { | ||
logger.error('Error: Failed to retrieve assets', error); | ||
|
||
return assetIds.map<AssetInfo>((assetId) => { | ||
const policyId = Cardano.AssetId.getPolicyId(assetId); | ||
const name = Cardano.AssetId.getAssetName(assetId); | ||
|
||
return { | ||
assetId, | ||
fingerprint: Cardano.AssetFingerprint.fromParts(policyId, name), | ||
name, | ||
policyId, | ||
quantity: 0n, | ||
supply: 0n | ||
}; | ||
}); | ||
} | ||
}; |
This file contains hidden or 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,26 @@ | ||
import * as Cardano from '../../src/Cardano'; | ||
import { Asset, AssetProvider, HealthCheckResponse } from '../../src'; | ||
|
||
export const createMockInputResolver = ( | ||
historicalTxs: Cardano.HydratedTx[], | ||
resolveDelay = 0 | ||
): Cardano.InputResolver => ({ | ||
async resolveInput(input: Cardano.TxIn) { | ||
const tx = historicalTxs.find((historicalTx) => historicalTx.id === input.txId); | ||
|
||
if (!tx || tx.body.outputs.length <= input.index) return Promise.resolve(null); | ||
|
||
return await new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(tx.body.outputs[input.index]); | ||
}, resolveDelay); | ||
}); | ||
} | ||
}); | ||
|
||
export const createMockAssetProvider = (assets: Asset.AssetInfo[]): AssetProvider => ({ | ||
getAsset: async ({ assetId }) => | ||
assets.find((asset) => asset.assetId === assetId) ?? Promise.reject('Asset not found'), | ||
getAssets: async ({ assetIds }) => assets.filter((asset) => assetIds.includes(asset.assetId)), | ||
healthCheck: async () => Promise.resolve({} as HealthCheckResponse) | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.