Created
August 8, 2024 11:01
-
-
Save vibern0/c83f266ea43f447f6c9dde3733b79ff1 to your computer and use it in GitHub Desktop.
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
| export const getQuote = async ( | |
| address: Address, | |
| amount: number, | |
| fromAssetTypeId: AssetTypeIdentifier, | |
| toAssetTypeId: AssetTypeIdentifier | |
| ) => { | |
| if (!amount) { | |
| throw new Error('Amount must be defined and bigger than 0'); | |
| } | |
| if (fromAssetTypeId === toAssetTypeId) { | |
| throw new Error('You can not swap to the same token'); | |
| } | |
| const fromAssetType = ASSET_TYPES_ENTITIES[fromAssetTypeId]; | |
| if (!fromAssetType.swap) { | |
| throw new Error('Unsupported from token'); | |
| } | |
| const toAssetType = ASSET_TYPES_ENTITIES[toAssetTypeId]; | |
| if (!toAssetType.swap) { | |
| throw new Error('Unsupported to token'); | |
| } | |
| const sellToken = fromAssetType.contractAddress | |
| ? fromAssetType.contractAddress | |
| : fromAssetType.include | |
| ? ASSET_TYPES_ENTITIES[fromAssetType.include].contractAddress | |
| : undefined; | |
| if (!sellToken) { | |
| throw new Error('Undefined sell token'); | |
| } | |
| const buyToken = toAssetType.contractAddress | |
| ? toAssetType.contractAddress | |
| : toAssetType.include | |
| ? ASSET_TYPES_ENTITIES[toAssetType.include].contractAddress | |
| : undefined; | |
| if (!buyToken) { | |
| throw new Error('Undefined buy token'); | |
| } | |
| return await orderBookApi.getQuote({ | |
| sellToken, | |
| buyToken, | |
| from: address, | |
| receiver: address, | |
| sellAmountBeforeFee: (amount * 10 ** 18).toString(), // 0.4 WETH | |
| kind: OrderQuoteSideKindSell.SELL | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment