|
| 1 | +import { TxStatus } from '@shapeshiftoss/unchained-client' |
| 2 | +import { bnOrZero } from '@shapeshiftoss/utils' |
| 3 | +import type { Result } from '@sniptt/monads' |
| 4 | + |
| 5 | +import { getDefaultSlippageDecimalPercentageForSwapper } from '../../constants' |
| 6 | +import type { |
| 7 | + CommonTradeQuoteInput, |
| 8 | + GetTradeRateInput, |
| 9 | + GetUnsignedAptosTransactionArgs, |
| 10 | + SwapErrorRight, |
| 11 | + SwapperApi, |
| 12 | + SwapperDeps, |
| 13 | + TradeQuote, |
| 14 | + TradeRate, |
| 15 | + TradeStatus, |
| 16 | +} from '../../types' |
| 17 | +import { SwapperName } from '../../types' |
| 18 | +import { checkAptosSwapStatus, isExecutableTradeQuote } from '../../utils' |
| 19 | +import { getPanoraTradeData } from './swapperApi/getPanoraTradeData' |
| 20 | +import { getTradeQuote } from './swapperApi/getTradeQuote' |
| 21 | +import { getTradeRate } from './swapperApi/getTradeRate' |
| 22 | + |
| 23 | +export const panoraApi: SwapperApi = { |
| 24 | + getTradeQuote: ( |
| 25 | + input: CommonTradeQuoteInput, |
| 26 | + deps: SwapperDeps, |
| 27 | + ): Promise<Result<TradeQuote[], SwapErrorRight>> => { |
| 28 | + return getTradeQuote(input, deps) |
| 29 | + }, |
| 30 | + |
| 31 | + getTradeRate: ( |
| 32 | + input: GetTradeRateInput, |
| 33 | + deps: SwapperDeps, |
| 34 | + ): Promise<Result<TradeRate[], SwapErrorRight>> => { |
| 35 | + return getTradeRate(input, deps) |
| 36 | + }, |
| 37 | + |
| 38 | + getUnsignedAptosTransaction: async ({ |
| 39 | + stepIndex, |
| 40 | + tradeQuote, |
| 41 | + from, |
| 42 | + assertGetAptosChainAdapter, |
| 43 | + config, |
| 44 | + }: GetUnsignedAptosTransactionArgs) => { |
| 45 | + if (!isExecutableTradeQuote(tradeQuote)) throw new Error('Unable to execute a trade rate quote') |
| 46 | + |
| 47 | + const step = tradeQuote.steps[stepIndex ?? 0] |
| 48 | + if (!step) throw new Error(`No step at index ${stepIndex}`) |
| 49 | + |
| 50 | + const { accountNumber, sellAsset, buyAsset, sellAmountIncludingProtocolFeesCryptoBaseUnit } = |
| 51 | + step |
| 52 | + |
| 53 | + const adapter = assertGetAptosChainAdapter(sellAsset.chainId) |
| 54 | + |
| 55 | + // Re-fetch quote from Panora to get fresh txData for execution |
| 56 | + const slippagePercentage = bnOrZero( |
| 57 | + tradeQuote.slippageTolerancePercentageDecimal ?? |
| 58 | + getDefaultSlippageDecimalPercentageForSwapper(SwapperName.Panora), |
| 59 | + ) |
| 60 | + .times(100) |
| 61 | + .toNumber() |
| 62 | + |
| 63 | + const tradeDataResult = await getPanoraTradeData({ |
| 64 | + sellAsset, |
| 65 | + buyAsset, |
| 66 | + sellAmountIncludingProtocolFeesCryptoBaseUnit, |
| 67 | + receiveAddress: from, |
| 68 | + affiliateBps: tradeQuote.affiliateBps, |
| 69 | + slippagePercentage, |
| 70 | + config, |
| 71 | + }) |
| 72 | + |
| 73 | + if (tradeDataResult.isErr()) { |
| 74 | + throw new Error(`Failed to get Panora trade data: ${tradeDataResult.unwrapErr().message}`) |
| 75 | + } |
| 76 | + |
| 77 | + const { txData } = tradeDataResult.unwrap() |
| 78 | + |
| 79 | + return adapter.buildEntryFunctionApiTransaction({ |
| 80 | + from, |
| 81 | + accountNumber: accountNumber ?? 0, |
| 82 | + data: { |
| 83 | + function: txData.function as `${string}::${string}::${string}`, |
| 84 | + typeArguments: txData.type_arguments, |
| 85 | + functionArguments: txData.arguments, |
| 86 | + }, |
| 87 | + }) |
| 88 | + }, |
| 89 | + |
| 90 | + getAptosTransactionFees: ({ tradeQuote, stepIndex }: GetUnsignedAptosTransactionArgs) => { |
| 91 | + if (!isExecutableTradeQuote(tradeQuote)) throw new Error('Unable to execute a trade rate quote') |
| 92 | + |
| 93 | + const step = tradeQuote.steps[stepIndex ?? 0] |
| 94 | + if (!step) throw new Error('Missing step') |
| 95 | + if (!step.feeData.networkFeeCryptoBaseUnit) { |
| 96 | + throw new Error('Missing network fee in quote') |
| 97 | + } |
| 98 | + return Promise.resolve(step.feeData.networkFeeCryptoBaseUnit) |
| 99 | + }, |
| 100 | + |
| 101 | + checkTradeStatus: ({ swap, assertGetAptosChainAdapter }): Promise<TradeStatus> => { |
| 102 | + if (!swap?.sellTxHash) { |
| 103 | + return Promise.resolve({ |
| 104 | + status: TxStatus.Unknown, |
| 105 | + buyTxHash: undefined, |
| 106 | + message: undefined, |
| 107 | + }) |
| 108 | + } |
| 109 | + |
| 110 | + return checkAptosSwapStatus({ |
| 111 | + txHash: swap.sellTxHash, |
| 112 | + address: swap.receiveAddress, |
| 113 | + assertGetAptosChainAdapter, |
| 114 | + }) |
| 115 | + }, |
| 116 | +} |
0 commit comments