Skip to content

Commit

Permalink
fix: continue
Browse files Browse the repository at this point in the history
  • Loading branch information
NeOMakinG committed Nov 15, 2024
1 parent 8b1e162 commit dc473d2
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 42 deletions.
2 changes: 2 additions & 0 deletions packages/caip/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
10 changes: 10 additions & 0 deletions packages/chain-adapters/src/solana/SolanaChainAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,12 @@ export class ChainAdapter implements IChainAdapter<KnownChainIds.SolanaMainnet>
}
}

public async getTxStatus(tx: unchained.solana.Tx, pubkey: string): Promise<unchained.TxStatus> {
const parsedTx = await this.parseTx(tx, pubkey)

return parsedTx.status
}

private async parseTx(tx: unchained.solana.Tx, pubkey: string): Promise<Transaction> {
const { address: _, ...parsedTx } = await this.parser.parse(tx, pubkey)

Expand All @@ -530,4 +536,8 @@ export class ChainAdapter implements IChainAdapter<KnownChainIds.SolanaMainnet>
})),
}
}

get httpProvider(): unchained.solana.Api {
return this.providers.http
}
}
39 changes: 5 additions & 34 deletions packages/swapper/src/swappers/JupiterSwapper/endpoints.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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,
Expand Down Expand Up @@ -70,43 +67,17 @@ export const jupiterApi: SwapperApi = {
const adapter = assertGetSolanaChainAdapter(step.sellAsset.chainId)

const buildSwapTxInput: BuildSendApiTxInput<KnownChainIds.SolanaMainnet> = {
to: '',
from,
value: step.sellAmountIncludingProtocolFeesCryptoBaseUnit,
value: '0',
accountNumber: step.accountNumber,
chainSpecific: {
instructions: [...computeBudgetInstructions, ...setupInstructions, swapInstruction],
},
} as unknown as BuildSendApiTxInput<KnownChainIds.SolanaMainnet>
}

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,
}
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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(
Expand Down
7 changes: 3 additions & 4 deletions packages/swapper/src/swappers/JupiterSwapper/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -50,14 +50,13 @@ export const getJupiterQuote = ({
export const getJupiterSwapInstructions = ({
apiUrl,
fromAddress,
feeAccount,
rawQuote,
}: GetJupiterSwapArgs): Promise<
Result<AxiosResponse<SwapInstructionsResponse, any>, SwapErrorRight>
> =>
jupiterService.post<SwapInstructionsResponse>(`${apiUrl}/swap-instructions`, {
userPublicKey: fromAddress,
feeAccount,
feeAccount: SHAPESHIFT_SOLANA_FEE_ACCOUNT,
quoteResponse: rawQuote,
dynamicComputeUnitLimit: true,
prioritizationFeeLamports: 'auto',
Expand Down
3 changes: 2 additions & 1 deletion packages/swapper/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 42 additions & 1 deletion packages/swapper/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)
}
}
1 change: 1 addition & 0 deletions src/lib/tradeExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export class TradeExecution {
assertGetEvmChainAdapter,
assertGetUtxoChainAdapter,
assertGetCosmosSdkChainAdapter,
assertGetSolanaChainAdapter,
fetchIsSmartContractAddressQuery,
})

Expand Down

0 comments on commit dc473d2

Please sign in to comment.