Skip to content

Commit

Permalink
Merge pull request #34 from compolabs/feat/add-readable-errors
Browse files Browse the repository at this point in the history
[1460] Added human readable errors
  • Loading branch information
EchoDex authored Aug 21, 2024
2 parents 9054c4a + 92719e2 commit 9400456
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 43 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@compolabs/spark-orderbook-ts-sdk",
"version": "1.5.0",
"version": "1.5.1",
"type": "module",
"main": "./dist/index.сjs",
"module": "./dist/index.js",
Expand Down
8 changes: 2 additions & 6 deletions src/SparkOrderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import {

import BN from "./utils/BN";
import { NETWORK_ERROR, NetworkError } from "./utils/NetworkError";
import {
BETA_CONTRACT_ADDRESSES,
DEFAULT_GAS_LIMIT_MULTIPLIER,
DEFAULT_GAS_PRICE,
} from "./constants";
import { DEFAULT_GAS_LIMIT_MULTIPLIER, DEFAULT_GAS_PRICE } from "./constants";
import { IndexerApi } from "./IndexerApi";
import {
ActiveOrderReturn,
Expand Down Expand Up @@ -51,7 +47,7 @@ export class SparkOrderbook {

constructor(params: SparkParams) {
this.options = {
contractAddresses: params.contractAddresses ?? BETA_CONTRACT_ADDRESSES,
contractAddresses: params.contractAddresses,
wallet: params.wallet,
gasPrice: params.gasPrice ?? DEFAULT_GAS_PRICE,
gasLimitMultiplier:
Expand Down
21 changes: 0 additions & 21 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,4 @@
import { OrderbookContracts } from "src/interface";

export const DEFAULT_DECIMALS = 9;

export const DEFAULT_GAS_PRICE = "1";
export const DEFAULT_GAS_LIMIT_MULTIPLIER = "2";

export const BETA_CONTRACT_ADDRESSES: OrderbookContracts = {
market: "0x08ca18ed550d6229f001641d43aac58e00f9eb7e25c9bea6d33716af61e43b2a",
orderbook:
"0x8fa518228af2d06fc495faa51fea2f670c793fee747b50c9637220e35e8ddca0",
tokenFactory:
"0x3141a3f11e3f784364d57860e3a4dcf9b73d42e23fd49038773cefb09c633348",
pyth: "0x3cd5005f23321c8ae0ccfa98fb07d9a5ff325c483f21d2d9540d6897007600c9",
};

export const EXPLORER_URL = "https://app.fuel.network/";

export const TESTNET_NETWORK = {
name: "Fuel",
url: "https://testnet.fuel.network/v1/graphql",
};

export const TESTNET_INDEXER_URL =
"https://indexer.bigdevenergy.link/029c37c/v1/graphql";
16 changes: 2 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
import { BETA_TOKENS } from "./constants/tokens";
import BN from "./utils/BN";
import {
BETA_CONTRACT_ADDRESSES,
EXPLORER_URL,
TESTNET_INDEXER_URL,
TESTNET_NETWORK,
} from "./constants";
import { getHumanReadableError } from "./utils/getHumanReadableError";
import { SparkOrderbook } from "./SparkOrderbook";

export default SparkOrderbook;

export {
BETA_CONTRACT_ADDRESSES,
BETA_TOKENS,
BN,
EXPLORER_URL,
TESTNET_INDEXER_URL,
TESTNET_NETWORK,
};
export { BETA_TOKENS, BN, getHumanReadableError };

export * from "./interface";
export * from "./types/market";
Expand Down
2 changes: 1 addition & 1 deletion src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface GraphClientConfig {
export interface SparkParams {
networkUrl: string;
indexerConfig: GraphClientConfig;
contractAddresses?: OrderbookContracts;
contractAddresses: OrderbookContracts;
wallet?: WalletLocked | WalletUnlocked;
gasPrice?: string;
gasLimitMultiplier?: string;
Expand Down
64 changes: 64 additions & 0 deletions src/utils/getHumanReadableError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {
AccountErrorInput,
AssetErrorInput,
AuthErrorInput,
MatchErrorInput,
OrderErrorInput,
ValueErrorInput,
} from "src/types/market/MarketContractAbi";

type EnsureArray<T> = T extends any[] ? T : [T];

type HumanReadableMessages = {
[K in keyof AccountErrorInput]: (args: AccountErrorInput[K]) => string;
} & {
[K in keyof Required<OrderErrorInput>]: (
args: EnsureArray<OrderErrorInput[K]>,
) => string;
} & {
[K in keyof Required<MatchErrorInput>]: (
args: EnsureArray<MatchErrorInput[K]>,
) => string;
} & {
[K in keyof typeof AssetErrorInput]: () => string;
} & {
[K in keyof typeof AuthErrorInput]: () => string;
} & {
[K in keyof Required<ValueErrorInput>]: (
args: EnsureArray<ValueErrorInput[K]>,
) => string;
};

const humanReadableMessages: HumanReadableMessages = {
InsufficientBalance: (args) =>
`Insufficient balance. Available: ${args[0]?.toString()}, Required: ${args[1]?.toString()}.`,
InvalidAsset: () => `The asset provided is invalid.`,
InvalidFeeAsset: () => `The fee asset provided is invalid.`,
Unauthorized: () => `You are not authorized to perform this action.`,
CantMatch: () => `Can't match.`,
CantMatchMany: () => `Cannot match many.`,
CantFulfillMany: () => `Cannot fulfill many.`,
OrderNotFound: (args) => `Order with ID ${args[0]} was not found.`,
PriceTooSmall: (args) =>
`The price ${args[0]?.toString()} is too small. It should be at least ${args[1]?.toString()}.`,
ZeroOrderAmount: () => `Order amount cannot be zero.`,
ZeroLockAmount: () => `Lock amount cannot be zero.`,
FailedToRemove: (args) => `Failed to remove order with ID ${args[0]}.`,
InvalidAmount: () => `Invalid amount provided.`,
InvalidSlippage: () => `Invalid slippage provided.`,
InvalidArrayLength: () => `Invalid array length.`,
InvalidFeeAmount: (args) =>
`Fee ${args[0]?.toString()} is too low. Minimum fee required is ${args[1]?.toString()}.`,
};

export const getHumanReadableError = <K extends keyof HumanReadableMessages>(
error: Record<K, any[]>,
): string => {
const errorType = Object.keys(error)[0] as K;
const args = error[errorType] ?? [];
const msgFn = humanReadableMessages[errorType]! as unknown as (
args: any[],
) => string;

return msgFn(args) ?? "";
};

0 comments on commit 9400456

Please sign in to comment.