-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from compolabs/feat/add-readable-errors
[1460] Added human readable errors
- Loading branch information
Showing
6 changed files
with
70 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) ?? ""; | ||
}; |