From dc473d2daccf0c80522764b78a99a28770c9733c Mon Sep 17 00:00:00 2001 From: NeOMakinG <14963751+NeOMakinG@users.noreply.github.com> Date: Fri, 15 Nov 2024 17:46:59 +0100 Subject: [PATCH] fix: continue --- packages/caip/src/constants.ts | 2 + .../src/solana/SolanaChainAdapter.ts | 10 +++++ .../src/swappers/JupiterSwapper/endpoints.ts | 39 +++-------------- .../swapperApi/getTradeQuote.ts | 5 ++- .../swappers/JupiterSwapper/utils/helpers.ts | 7 ++- packages/swapper/src/types.ts | 3 +- packages/swapper/src/utils.ts | 43 ++++++++++++++++++- src/lib/tradeExecution.ts | 1 + 8 files changed, 68 insertions(+), 42 deletions(-) diff --git a/packages/caip/src/constants.ts b/packages/caip/src/constants.ts index 6ea029a0147..5e0f705bda7 100644 --- a/packages/caip/src/constants.ts +++ b/packages/caip/src/constants.ts @@ -16,6 +16,8 @@ export const arbitrumAssetId: AssetId = 'eip155:42161/slip44:60' export const arbitrumNovaAssetId: AssetId = 'eip155:42170/slip44:60' export const baseAssetId: AssetId = 'eip155:8453/slip44:60' export const solAssetId: AssetId = 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501' +export const wrappedSolAssetId: AssetId = + 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/token:So11111111111111111111111111111111111111112' export const foxatarAssetId: AssetId = 'eip155:137/erc721:0x2e727c425a11ce6b8819b3004db332c12d2af2a2' diff --git a/packages/chain-adapters/src/solana/SolanaChainAdapter.ts b/packages/chain-adapters/src/solana/SolanaChainAdapter.ts index 812f688d0f8..a28c666b62f 100644 --- a/packages/chain-adapters/src/solana/SolanaChainAdapter.ts +++ b/packages/chain-adapters/src/solana/SolanaChainAdapter.ts @@ -515,6 +515,12 @@ export class ChainAdapter implements IChainAdapter } } + public async getTxStatus(tx: unchained.solana.Tx, pubkey: string): Promise { + const parsedTx = await this.parseTx(tx, pubkey) + + return parsedTx.status + } + private async parseTx(tx: unchained.solana.Tx, pubkey: string): Promise { const { address: _, ...parsedTx } = await this.parser.parse(tx, pubkey) @@ -530,4 +536,8 @@ export class ChainAdapter implements IChainAdapter })), } } + + get httpProvider(): unchained.solana.Api { + return this.providers.http + } } diff --git a/packages/swapper/src/swappers/JupiterSwapper/endpoints.ts b/packages/swapper/src/swappers/JupiterSwapper/endpoints.ts index 667aaa12fd7..88a1f6d85f7 100644 --- a/packages/swapper/src/swappers/JupiterSwapper/endpoints.ts +++ b/packages/swapper/src/swappers/JupiterSwapper/endpoints.ts @@ -1,13 +1,11 @@ import type { BuildSendApiTxInput } from '@shapeshiftoss/chain-adapters' import type { SolanaSignTx } from '@shapeshiftoss/hdwallet-core' import type { KnownChainIds } from '@shapeshiftoss/types' -import { TxStatus } from '@shapeshiftoss/unchained-client' import type { AxiosError } from 'axios' -import type { InterpolationOptions } from 'node-polyglot' import type { GetUnsignedSolanaTransactionArgs } from '../../types' import { type SwapperApi } from '../../types' -import { isExecutableTradeQuote, isExecutableTradeStep } from '../../utils' +import { checkSolanaSwapStatus, isExecutableTradeQuote, isExecutableTradeStep } from '../../utils' import { getTradeQuote, getTradeRate } from './swapperApi/getTradeQuote' import { getJupiterSwapInstructions } from './utils/helpers' @@ -30,7 +28,6 @@ export const jupiterApi: SwapperApi = { if (!tradeQuote.rawQuote) throw Error('Missing raw quote') - // @TODO: add feeAccount const maybeSwapResponse = await getJupiterSwapInstructions({ apiUrl: jupiterUrl, fromAddress: from, @@ -70,43 +67,17 @@ export const jupiterApi: SwapperApi = { const adapter = assertGetSolanaChainAdapter(step.sellAsset.chainId) const buildSwapTxInput: BuildSendApiTxInput = { + to: '', from, - value: step.sellAmountIncludingProtocolFeesCryptoBaseUnit, + value: '0', accountNumber: step.accountNumber, chainSpecific: { instructions: [...computeBudgetInstructions, ...setupInstructions, swapInstruction], }, - } as unknown as BuildSendApiTxInput + } return (await adapter.buildSendApiTransaction(buildSwapTxInput)).txToSign }, - checkTradeStatus: (): Promise<{ - status: TxStatus - buyTxHash: string | undefined - message: string | [string, InterpolationOptions] | undefined - }> => { - try { - return { - buyTxHash: '', - status: TxStatus.Pending, - message: '', - } as unknown as Promise<{ - status: TxStatus - buyTxHash: string | undefined - message: string | [string, InterpolationOptions] | undefined - }> - } catch (e) { - console.error(e) - return { - buyTxHash: undefined, - status: TxStatus.Unknown, - message: undefined, - } as unknown as Promise<{ - status: TxStatus - buyTxHash: string | undefined - message: string | [string, InterpolationOptions] | undefined - }> - } - }, + checkTradeStatus: checkSolanaSwapStatus, } diff --git a/packages/swapper/src/swappers/JupiterSwapper/swapperApi/getTradeQuote.ts b/packages/swapper/src/swappers/JupiterSwapper/swapperApi/getTradeQuote.ts index 81879dd8e2a..4b189175c30 100644 --- a/packages/swapper/src/swappers/JupiterSwapper/swapperApi/getTradeQuote.ts +++ b/packages/swapper/src/swappers/JupiterSwapper/swapperApi/getTradeQuote.ts @@ -6,6 +6,7 @@ import { fromAssetId, solAssetId, toAssetId, + wrappedSolAssetId, } from '@shapeshiftoss/caip' import type { GetFeeDataInput } from '@shapeshiftoss/chain-adapters' import type { KnownChainIds } from '@shapeshiftoss/types' @@ -68,8 +69,8 @@ const _getTradeQuote = async ( const maybeQuoteResponse = await getJupiterQuote({ apiUrl: jupiterUrl, - sourceAsset: sellAsset.assetId, - destinationAsset: buyAsset.assetId, + sourceAsset: sellAsset.assetId === solAssetId ? wrappedSolAssetId : sellAsset.assetId, + destinationAsset: buyAsset.assetId === solAssetId ? wrappedSolAssetId : buyAsset.assetId, commissionBps: affiliateBps, amount: sellAmount, slippageBps: convertDecimalPercentageToBasisPoints( diff --git a/packages/swapper/src/swappers/JupiterSwapper/utils/helpers.ts b/packages/swapper/src/swappers/JupiterSwapper/utils/helpers.ts index d36e8b64944..a47346de9c8 100644 --- a/packages/swapper/src/swappers/JupiterSwapper/utils/helpers.ts +++ b/packages/swapper/src/swappers/JupiterSwapper/utils/helpers.ts @@ -13,6 +13,8 @@ export const isSupportedChainId = (chainId: ChainId): chainId is JupiterSupporte return jupiterSupportedChainIds.includes(chainId as JupiterSupportedChainId) } +const SHAPESHIFT_SOLANA_FEE_ACCOUNT = 'C7RTJbss7R1r7j8NUNYbasUXfbPJR99PMhqznvCiU43N' + type GetJupiterQuoteArgs = { apiUrl: string sourceAsset: string @@ -25,8 +27,6 @@ type GetJupiterQuoteArgs = { type GetJupiterSwapArgs = { apiUrl: string fromAddress: string - // @TODO: make this mandatory when we have the DAO fee account - feeAccount?: string rawQuote: unknown } @@ -50,14 +50,13 @@ export const getJupiterQuote = ({ export const getJupiterSwapInstructions = ({ apiUrl, fromAddress, - feeAccount, rawQuote, }: GetJupiterSwapArgs): Promise< Result, SwapErrorRight> > => jupiterService.post(`${apiUrl}/swap-instructions`, { userPublicKey: fromAddress, - feeAccount, + feeAccount: SHAPESHIFT_SOLANA_FEE_ACCOUNT, quoteResponse: rawQuote, dynamicComputeUnitLimit: true, prioritizationFeeLamports: 'auto', diff --git a/packages/swapper/src/types.ts b/packages/swapper/src/types.ts index fbd381c4e8f..9f7b2e7256e 100644 --- a/packages/swapper/src/types.ts +++ b/packages/swapper/src/types.ts @@ -410,7 +410,8 @@ export type CheckTradeStatusInput = { config: SwapperConfig } & EvmSwapperDeps & UtxoSwapperDeps & - CosmosSdkSwapperDeps + CosmosSdkSwapperDeps & + SolanaSwapperDeps // a result containing all routes that were successfully generated, or an error in the case where // no routes could be generated diff --git a/packages/swapper/src/utils.ts b/packages/swapper/src/utils.ts index 5e39875e6a5..a230fc30aa3 100644 --- a/packages/swapper/src/utils.ts +++ b/packages/swapper/src/utils.ts @@ -1,5 +1,12 @@ -import { type AccountId, type AssetId, type ChainId, fromAssetId } from '@shapeshiftoss/caip' +import { + type AccountId, + type AssetId, + type ChainId, + fromAccountId, + fromAssetId, +} from '@shapeshiftoss/caip' import type { EvmChainAdapter } from '@shapeshiftoss/chain-adapters' +import type { ChainAdapter as SolanaChainAdapter } from '@shapeshiftoss/chain-adapters/dist/solana/SolanaChainAdapter' import type { SolanaSignTx } from '@shapeshiftoss/hdwallet-core' import type { Asset } from '@shapeshiftoss/types' import { evm, TxStatus } from '@shapeshiftoss/unchained-client' @@ -318,3 +325,37 @@ export const isToken = (assetId: AssetId) => { } export const isExecutableTradeStep = (step: TradeQuoteStep): step is ExecutableTradeStep => step.accountNumber !== undefined + +export const checkSolanaSwapStatus = async ({ + txHash, + chainId, + accountId, + assertGetSolanaChainAdapter, +}: { + txHash: string + accountId: AccountId | undefined + chainId: ChainId + assertGetSolanaChainAdapter: (chainId: ChainId) => SolanaChainAdapter +}): Promise<{ + status: TxStatus + buyTxHash: string | undefined + message: string | [string, InterpolationOptions] | undefined +}> => { + try { + if (!accountId) throw new Error('Missing accountId') + + const account = fromAccountId(accountId).account + const adapter = assertGetSolanaChainAdapter(chainId) + const tx = await adapter.httpProvider.getTransaction({ txid: txHash }) + const status = await adapter.getTxStatus(tx, account) + + return { + status, + buyTxHash: txHash, + message: undefined, + } + } catch (e) { + console.error(e) + return createDefaultStatusResponse(txHash) + } +} diff --git a/src/lib/tradeExecution.ts b/src/lib/tradeExecution.ts index 71b591e87da..ff580245941 100644 --- a/src/lib/tradeExecution.ts +++ b/src/lib/tradeExecution.ts @@ -104,6 +104,7 @@ export class TradeExecution { assertGetEvmChainAdapter, assertGetUtxoChainAdapter, assertGetCosmosSdkChainAdapter, + assertGetSolanaChainAdapter, fetchIsSmartContractAddressQuery, })