Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/main/.env.development
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ VITE_TRSRY_ADDR=7L53bUTBopuwFt3mKUfmkzgGLayYa1Yvn1hAg9v5UMrQzTfh
VITE_EVM_CHAIN_ID=222222
VITE_DISPLAY_ASSET_ID="10"
VITE_HSM_ENABLED=false
VITE_DRY_RUN_ENABLED=false
VITE_DRY_RUN_ENABLED=true
2 changes: 1 addition & 1 deletion apps/main/.env.production
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ VITE_TRSRY_ADDR=7L53bUTBopuwFt3mKUfmkzgGLayYa1Yvn1hAg9v5UMrQzTfh
VITE_EVM_CHAIN_ID=222222
VITE_DISPLAY_ASSET_ID="10"
VITE_HSM_ENABLED=false
VITE_DRY_RUN_ENABLED=false
VITE_DRY_RUN_ENABLED=true
30 changes: 16 additions & 14 deletions apps/main/src/api/dryRun.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
import { QUERY_KEY_BLOCK_PREFIX } from "@galacticcouncil/utils"
import { queryOptions } from "@tanstack/react-query"
import { Enum } from "polkadot-api"

import { decodeTx } from "@/modules/transactions/review/ReviewTransactionJsonView/ReviewTransactionJsonView.utils"
import { AnyPapiTx } from "@/modules/transactions/types"
import { AnyTransaction } from "@/modules/transactions/types"
import { isPapiTransaction } from "@/modules/transactions/utils/polkadot"
import { getPapiTransactionCallData } from "@/modules/transactions/utils/tx"
import { TProviderContext } from "@/providers/rpcProvider"

export const papiDryRunErrorQuery = (
{ papi, dryRunErrorDecoder, papiCompatibilityToken }: TProviderContext,
address: string,
tx: AnyPapiTx,
tx: AnyTransaction,
debug?: boolean,
) =>
queryOptions({
queryKey: [
QUERY_KEY_BLOCK_PREFIX,
"dryRun",
"papi",
address,
getPapiTransactionCallData(tx, papiCompatibilityToken),
],
queryFn: async () => {
try {
const json = decodeTx(tx)
if (!isPapiTransaction(tx)) {
return null
}

const rawOrigin = Enum("Signed", address)
const origin = Enum("system", rawOrigin)

const result = await papi.apis.DryRunApi.dry_run_call(
{
type: "system",
value: {
type: "Signed",
value: address,
},
},
origin,
// @ts-expect-error contains structured call data
json,
tx.decodedCall,
1,
)

console.log(result)
if (!result.success || result.value.execution_result.success) {
return null
}
Expand All @@ -44,6 +44,8 @@ export const papiDryRunErrorQuery = (
result.value.execution_result.value.error,
)

const json = decodeTx(tx)

if (debug && error) {
console.log(new Date().toLocaleTimeString(), error.name, json)
}
Expand Down
85 changes: 10 additions & 75 deletions apps/main/src/api/trade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { QUERY_KEY_BLOCK_PREFIX } from "@galacticcouncil/utils"
import { QueryKey, queryOptions } from "@tanstack/react-query"
import Big from "big.js"

import { papiDryRunErrorQuery } from "@/api/dryRun"
import { getTimeFrameMillis } from "@/components/TimeFrame/TimeFrame.utils"
import { ENV } from "@/config/env"
import {
DcaFormValues,
DcaOrdersMode,
Expand Down Expand Up @@ -84,24 +82,17 @@ export const bestSellTxQuery = (
type BestSellWithTxArgs = BestSellArgs & {
readonly slippage: number
readonly address: string
readonly dryRun?: boolean
}

export const bestSellWithTxQuery = (
rpc: TProviderContext,
{ slippage, address, dryRun, ...bestSellArgs }: BestSellWithTxArgs,
{ slippage, address, ...bestSellArgs }: BestSellWithTxArgs,
) => {
const { queryClient } = rpc
const bestSell = bestSellQuery(rpc, bestSellArgs)

return queryOptions({
queryKey: [
QUERY_KEY_BLOCK_PREFIX,
bestSell.queryKey,
slippage,
address,
dryRun,
],
queryKey: [QUERY_KEY_BLOCK_PREFIX, bestSell.queryKey, slippage, address],
queryFn: async () => {
const swap = await queryClient.ensureQueryData(bestSell)

Expand All @@ -117,17 +108,9 @@ export const bestSellWithTxQuery = (
? await queryClient.ensureQueryData(txQuery)
: null

const dryRunError =
tx && dryRun && ENV.VITE_DRY_RUN_ENABLED
? await queryClient.ensureQueryData(
papiDryRunErrorQuery(rpc, address, tx, bestSellArgs.debug),
)
: null

return {
swap,
tx,
dryRunError,
}
},
enabled: bestSell.enabled as boolean,
Expand Down Expand Up @@ -189,7 +172,6 @@ type BestSellTwapWithTxArgs = BestSellTwapArgs & {
readonly slippage: number
readonly address: string
readonly maxRetries: number
readonly dryRun?: boolean
}

export const bestSellTwapWithTxQuery = (
Expand All @@ -198,7 +180,6 @@ export const bestSellTwapWithTxQuery = (
slippage,
maxRetries,
address,
dryRun,
...bestSellTwapArgs
}: BestSellTwapWithTxArgs,
enabled = true,
Expand All @@ -213,7 +194,6 @@ export const bestSellTwapWithTxQuery = (
slippage,
maxRetries,
address,
dryRun,
],
queryFn: async () => {
const twap = await queryClient.ensureQueryData(bestSellTwap)
Expand All @@ -231,14 +211,7 @@ export const bestSellTwapWithTxQuery = (
? await queryClient.ensureQueryData(txQuery)
: null

const dryRunError =
tx && dryRun && ENV.VITE_DRY_RUN_ENABLED
? await queryClient.ensureQueryData(
papiDryRunErrorQuery(rpc, address, tx),
)
: null

return { twap, tx, dryRunError }
return { twap, tx }
},
enabled: enabled && (bestSellTwap.enabled as boolean),
})
Expand Down Expand Up @@ -303,24 +276,17 @@ export const bestBuyTxQuery = (
type BestBuyWithTxArgs = BestBuyArgs & {
readonly slippage: number
readonly address: string
readonly dryRun?: boolean
}

export const bestBuyWithTxQuery = (
rpc: TProviderContext,
{ slippage, address, dryRun, ...bestBuyArgs }: BestBuyWithTxArgs,
{ slippage, address, ...bestBuyArgs }: BestBuyWithTxArgs,
) => {
const { queryClient } = rpc
const bestBuy = bestBuyQuery(rpc, bestBuyArgs)

return queryOptions({
queryKey: [
QUERY_KEY_BLOCK_PREFIX,
bestBuy.queryKey,
slippage,
address,
dryRun,
],
queryKey: [QUERY_KEY_BLOCK_PREFIX, bestBuy.queryKey, slippage, address],
queryFn: async () => {
const swap = await queryClient.ensureQueryData(bestBuy)

Expand All @@ -336,17 +302,9 @@ export const bestBuyWithTxQuery = (
? await queryClient.ensureQueryData(txQuery)
: null

const dryRunError =
tx && dryRun && ENV.VITE_DRY_RUN_ENABLED
? await queryClient.ensureQueryData(
papiDryRunErrorQuery(rpc, address, tx, bestBuyArgs.debug),
)
: null

return {
swap,
tx,
dryRunError,
}
},
enabled: bestBuy.enabled as boolean,
Expand Down Expand Up @@ -408,18 +366,11 @@ type BestBuyTwapWithTxArgs = BestBuyTwapArgs & {
readonly slippage: number
readonly address: string
readonly maxRetries: number
readonly dryRun?: boolean
}

export const bestBuyTwapWithTxQuery = (
rpc: TProviderContext,
{
slippage,
maxRetries,
address,
dryRun,
...bestBuyTwapArgs
}: BestBuyTwapWithTxArgs,
{ slippage, maxRetries, address, ...bestBuyTwapArgs }: BestBuyTwapWithTxArgs,
enabled = true,
) => {
const { queryClient } = rpc
Expand All @@ -432,7 +383,6 @@ export const bestBuyTwapWithTxQuery = (
slippage,
maxRetries,
address,
dryRun,
],
queryFn: async () => {
const twap = await queryClient.ensureQueryData(bestBuyTwap)
Expand All @@ -450,14 +400,7 @@ export const bestBuyTwapWithTxQuery = (
? await queryClient.ensureQueryData(txQuery)
: null

const dryRunError =
tx && dryRun && ENV.VITE_DRY_RUN_ENABLED
? await queryClient.ensureQueryData(
papiDryRunErrorQuery(rpc, address, tx),
)
: null

return { twap, tx, dryRunError }
return { twap, tx }
},
enabled: enabled && (bestBuyTwap.enabled as boolean),
})
Expand Down Expand Up @@ -541,18 +484,17 @@ type DcaTradeOrderArgs = {
readonly slippage: number
readonly maxRetries: number
readonly address: string
readonly dryRun?: boolean
}

export const dcaTradeOrderQuery = (
rpc: TProviderContext,
{ form, slippage, maxRetries, address, dryRun }: DcaTradeOrderArgs,
{ form, slippage, maxRetries, address }: DcaTradeOrderArgs,
) => {
const { queryClient } = rpc
const dcaOrder = dcaOrderQuery(rpc, form)

return queryOptions({
queryKey: [...dcaOrder.queryKey, slippage, maxRetries, address, dryRun],
queryKey: [...dcaOrder.queryKey, slippage, maxRetries, address],
queryFn: async () => {
const order = await queryClient.ensureQueryData(dcaOrder)

Expand All @@ -571,14 +513,7 @@ export const dcaTradeOrderQuery = (
? await queryClient.ensureQueryData(txQuery)
: null

const dryRunError =
orderTx && dryRun && ENV.VITE_DRY_RUN_ENABLED
? await queryClient.ensureQueryData(
papiDryRunErrorQuery(rpc, address, orderTx),
)
: null

return { order, orderTx, dryRunError }
return { order, orderTx }
},
enabled: dcaOrder.enabled as boolean,
})
Expand Down
22 changes: 3 additions & 19 deletions apps/main/src/api/xcm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import { secondsToMilliseconds } from "date-fns"
import { useEffect, useRef, useState } from "react"

import { TProviderContext, useRpcProvider } from "@/providers/rpcProvider"
import { useRpcProvider } from "@/providers/rpcProvider"

export const useCrossChainConfig = () => {
const { sdk } = useRpcProvider()
Expand Down Expand Up @@ -199,34 +199,18 @@ export const xcmTransferReportQuery = (
})

export const xcmTransferCallQuery = (
{ dryRunErrorDecoder }: TProviderContext,
transfer: Transfer | null,
amount: string,
transferArgs: XcmTransferArgs,
dryRun?: boolean,
) =>
queryOptions({
enabled: !!transfer && !!amount,
placeholderData: keepPreviousData,
queryKey: ["xcm", "call", amount, transferArgs, dryRun],
queryKey: ["xcm", "call", amount, transferArgs],
queryFn: async () => {
if (!transfer) throw new Error("Invalid transfer")
const call = await transfer.buildCall(amount)

const dryRunError = await (async () => {
if (!dryRun) {
return null
}

const result = await call?.dryRun()

if (result?.error) {
return await dryRunErrorDecoder.parseError(result.error)
}

return null
})()

return { call, dryRunError }
return { call }
},
})
1 change: 1 addition & 0 deletions apps/main/src/i18n/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@
"transaction.alert.pendingDispatchPermit": "Permit transaction is pending. Please wait.",
"transaction.alert.sellAll": "You are selling your entire balance of {{ value, currency }}.",
"transaction.alert.acceptRisk": "I accept the risk involved.",
"transaction.alert.dryRunWarning": "Transaction might fail. Reason: {{reason}}",
"transaction.batch.warning": "Your transaction would exhaust block limit, we need to split it into multiple transactions",
"transaction.batch.step.label": "Transaction {{ index }}",
"transaction.error.unsupportedTransaction": "Unsupported transaction or signer type",
Expand Down
7 changes: 1 addition & 6 deletions apps/main/src/modules/trade/swap/sections/DCA/Dca.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export const Dca: FC = () => {
const {
order,
orderTx,
dryRunError,
healthFactor: initialHealthFactor,
isLoading,
} = useDcaTradeOrder(form)
Expand Down Expand Up @@ -150,11 +149,7 @@ export const Dca: FC = () => {
}
isLoading={isLoading}
/>
<DcaErrors
priceImpact={order?.tradeImpactPct ?? 0}
errors={errors}
dryRunError={dryRunError}
/>
<DcaErrors priceImpact={order?.tradeImpactPct ?? 0} errors={errors} />
<DcaWarnings
isFormValid={isFormValid}
order={order}
Expand Down
Loading
Loading