Skip to content
This repository was archived by the owner on Aug 6, 2025. It is now read-only.
Merged
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
4 changes: 2 additions & 2 deletions src/features/gift/actors/giftMakerRootMachine.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { errors } from "@defuse-protocol/internal-utils"

Check failure on line 1 in src/features/gift/actors/giftMakerRootMachine.ts

View workflow job for this annotation

GitHub Actions / test / test

Module '"@defuse-protocol/internal-utils"' has no exported member 'errors'.
import {
type ActorRefFrom,
type DoneActorEvent,
Expand All @@ -19,7 +20,6 @@
WalletSignatureResult,
} from "../../../types/walletMessage"
import { assert } from "../../../utils/assert"
import { toError } from "../../../utils/errors"
import {
type Events as DepositedBalanceEvents,
depositedBalanceMachine,
Expand Down Expand Up @@ -218,7 +218,7 @@
},
actions: {
logError: (_, event: { error: unknown }) => {
const err = toError(event.error)
const err = errors.toError(event.error)
logger.error(err)
},
setError: assign({
Expand Down
7 changes: 2 additions & 5 deletions src/features/machines/depositMachine.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { errors } from "@defuse-protocol/internal-utils"

Check failure on line 1 in src/features/machines/depositMachine.ts

View workflow job for this annotation

GitHub Actions / test / test

Module '"@defuse-protocol/internal-utils"' has no exported member 'errors'.
import { assign, fromPromise, setup } from "xstate"
import { logger } from "../../logger"
import { emitEvent } from "../../services/emitter"
Expand Down Expand Up @@ -210,7 +211,7 @@
params: ({ event }) => {
return {
reason: "ERR_SUBMITTING_TRANSACTION",
error: toError(event.error),
error: errors.toError(event.error),
}
},
},
Expand Down Expand Up @@ -265,7 +266,3 @@
},
},
})

function toError(error: unknown): Error {
return error instanceof Error ? error : new Error("unknown error")
}
7 changes: 2 additions & 5 deletions src/features/machines/poaBridgeInfoActor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { errors } from "@defuse-protocol/internal-utils"

Check failure on line 1 in src/features/machines/poaBridgeInfoActor.ts

View workflow job for this annotation

GitHub Actions / test / test

Module '"@defuse-protocol/internal-utils"' has no exported member 'errors'.
import {
type ActorRefFrom,
type SnapshotFrom,
Expand Down Expand Up @@ -90,7 +91,7 @@
target: "error",
actions: {
type: "logError",
params: ({ event }) => toError(event.error),
params: ({ event }) => errors.toError(event.error),
},
},
},
Expand Down Expand Up @@ -137,7 +138,3 @@
}
)
}

function toError(error: unknown): Error {
return error instanceof Error ? error : new Error("unknown error")
}
6 changes: 3 additions & 3 deletions src/features/machines/signIntentMachine.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { errors } from "@defuse-protocol/internal-utils"

Check failure on line 1 in src/features/machines/signIntentMachine.ts

View workflow job for this annotation

GitHub Actions / test / test

Module '"@defuse-protocol/internal-utils"' has no exported member 'errors'.
import { assertEvent, assign, fromPromise, setup } from "xstate"
import { nearClient } from "../../constants/nearClient"
import {
Expand All @@ -11,7 +12,6 @@
WalletSignatureResult,
} from "../../types/walletMessage"
import { assert } from "../../utils/assert"
import { toError } from "../../utils/errors"
import { verifyWalletSignature } from "../../utils/verifyWalletSignature"
import {
type WalletErrorCode,
Expand Down Expand Up @@ -172,7 +172,7 @@
event.error,
"ERR_USER_DIDNT_SIGN"
),
error: toError(event.error),
error: errors.toError(event.error),
}
},
},
Expand Down Expand Up @@ -225,7 +225,7 @@
type: "setError",
params: ({ event }) => ({
reason: "ERR_CANNOT_VERIFY_SIGNATURE",
error: toError(event.error),
error: errors.toError(event.error),
}),
},
],
Expand Down
13 changes: 5 additions & 8 deletions src/features/machines/swapIntentMachine.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { FeeEstimation } from "@defuse-protocol/bridge-sdk"
import { errors } from "@defuse-protocol/internal-utils"

Check failure on line 2 in src/features/machines/swapIntentMachine.ts

View workflow job for this annotation

GitHub Actions / test / test

Module '"@defuse-protocol/internal-utils"' has no exported member 'errors'.
import { secp256k1 } from "@noble/curves/secp256k1"
import type { providers } from "near-api-js"
import { assign, fromPromise, setup } from "xstate"
Expand Down Expand Up @@ -424,7 +425,7 @@
event.error,
"ERR_USER_DIDNT_SIGN"
),
error: toError(event.error),
error: errors.toError(event.error),
}),
},
],
Expand Down Expand Up @@ -475,7 +476,7 @@
type: "setError",
params: ({ event }) => ({
reason: "ERR_CANNOT_VERIFY_SIGNATURE",
error: toError(event.error),
error: errors.toError(event.error),
}),
},
],
Expand Down Expand Up @@ -536,7 +537,7 @@
type: "setError",
params: ({ event }) => ({
reason: "ERR_PUBKEY_EXCEPTION",
error: toError(event.error),
error: errors.toError(event.error),
}),
},
],
Expand Down Expand Up @@ -589,7 +590,7 @@
type: "setError",
params: ({ event }) => ({
reason: "ERR_CANNOT_PUBLISH_INTENT",
error: toError(event.error),
error: errors.toError(event.error),
}),
},
],
Expand Down Expand Up @@ -662,10 +663,6 @@
},
})

function toError(error: unknown): Error {
return error instanceof Error ? error : new Error("unknown error")
}

function enqueueBetterQuote(
quotes: PriorityQueue<AggregatedQuote>,
originalQuote: AggregatedQuote,
Expand Down
4 changes: 2 additions & 2 deletions src/features/otcDesk/actors/otcMakerRootMachine.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { errors } from "@defuse-protocol/internal-utils"

Check failure on line 1 in src/features/otcDesk/actors/otcMakerRootMachine.ts

View workflow job for this annotation

GitHub Actions / test / test

Module '"@defuse-protocol/internal-utils"' has no exported member 'errors'.
import {
type ActorRefFrom,
type PromiseActorLogic,
Expand All @@ -16,7 +17,6 @@
WalletSignatureResult,
} from "../../../types/walletMessage"
import { assert } from "../../../utils/assert"
import { toError } from "../../../utils/errors"
import {
type Events as DepositedBalanceEvents,
depositedBalanceMachine,
Expand Down Expand Up @@ -129,7 +129,7 @@
},
actions: {
logError: (_, event: { error: unknown }) => {
const err = toError(event.error)
const err = errors.toError(event.error)
logger.error(err)
},
setError: assign({
Expand Down
52 changes: 0 additions & 52 deletions src/services/blockchainBalanceService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { base64 } from "@scure/base"
import { AccountLayout } from "@solana/spl-token"
import { Connection, PublicKey } from "@solana/web3.js"
import { Address as TonAddress, beginCell } from "@ton/ton"
Expand Down Expand Up @@ -67,57 +66,6 @@ export const getNearNep141Balance = async ({
}
}

export const getNearNep141StorageBalance = async ({
contractId,
accountId,
}: {
contractId: string
accountId: string
}): Promise<bigint> => {
try {
const args = { account_id: accountId }
const argsBase64 = Buffer.from(JSON.stringify(args)).toString("base64")

const response = await nearClient.query({
request_type: "call_function",
method_name: "storage_balance_of",
account_id: contractId,
args_base64: argsBase64,
finality: "optimistic",
})

const parsed = decodeQueryResult(
response,
v.union([v.null(), v.object({ total: v.string() })])
)

return BigInt(parsed?.total || "0")
} catch (err: unknown) {
throw new Error("Error fetching balance", { cause: err })
}
}

export const getNearNep141MinStorageBalance = async ({
contractId,
}: {
contractId: string
}): Promise<bigint> => {
const response = await nearClient.query({
request_type: "call_function",
method_name: "storage_balance_bounds",
account_id: contractId,
args_base64: base64.encode(new TextEncoder().encode(JSON.stringify({}))),
finality: "optimistic",
})

const parsed = decodeQueryResult(
response,
v.object({ min: v.string(), max: v.string() })
)

return BigInt(parsed.min)
}

export const getEvmNativeBalance = async ({
userAddress,
rpcUrl,
Expand Down
11 changes: 7 additions & 4 deletions src/services/nep141StorageService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { logger } from "../logger"
import type { BaseTokenInfo } from "../types/base"
import { isFungibleToken } from "../utils/token"
import {
getNearNep141MinStorageBalance,
getNearNep141StorageBalance,
} from "./blockchainBalanceService"
} from "@defuse-protocol/internal-utils"
import { nearClient } from "../constants/nearClient"
import { logger } from "../logger"
import type { BaseTokenInfo } from "../types/base"
import { isFungibleToken } from "../utils/token"

export type Output =
| {
Expand Down Expand Up @@ -42,10 +43,12 @@ export async function getNEP141StorageRequired({
await Promise.allSettled([
getNearNep141MinStorageBalance({
contractId: token.address,
nearProvider: nearClient,
}),
getNearNep141StorageBalance({
contractId: token.address,
accountId: userAccountId,
nearProvider: nearClient,
}),
])

Expand Down
11 changes: 3 additions & 8 deletions src/utils/errors.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
export function toError(error: unknown): Error {
if (error instanceof Error) return error
return new Error(
typeof error === "string" ? error : "An unexpected error occurred"
)
}
import { errors } from "@defuse-protocol/internal-utils"

Check failure on line 1 in src/utils/errors.ts

View workflow job for this annotation

GitHub Actions / test / test

Module '"@defuse-protocol/internal-utils"' has no exported member 'errors'.

export function hasMessage(
err: unknown,
Expand All @@ -14,7 +9,7 @@
): boolean {
if (!searchText) return false

const error = toError(err)
const error = errors.toError(err)
const search = options.ignoreCase ? searchText.toLowerCase() : searchText

const matches = (text: string) =>
Expand All @@ -37,7 +32,7 @@
// biome-ignore lint/suspicious/noExplicitAny: any is required for the constructor type
errorType: new (...args: any[]) => T
): T | null {
const error = toError(err)
const error = errors.toError(err)

if (error instanceof errorType) return error

Expand Down
Loading