-
Notifications
You must be signed in to change notification settings - Fork 2
Return SwapperError object #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
890326f
Add SwapperError type and structured error responses
0xh3rman 17bf045
Update index.ts
0xh3rman 22947cc
return objectResponse for new version
0xh3rman 2ce06c1
Update index.ts
0xh3rman 563c1ce
Update swap error type for input amount
0xh3rman 4c819c7
Refactor Mayan provider and add error handling
0xh3rman fd38d27
Update provider.ts
0xh3rman c69696c
cleanup ErrorData
0xh3rman 6b85b6d
add error.ts and cleanup
0xh3rman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,35 @@ | ||
| export enum MayanErrorCode { | ||
| AmountTooSmall = "AMOUNT_TOO_SMALL", | ||
| } | ||
|
|
||
| export type ErrorData = { | ||
| code?: MayanErrorCode | string; | ||
| message?: string; | ||
| data?: unknown; | ||
| }; | ||
|
|
||
| export function toMayanError(error: unknown): Error { | ||
| const message = extractErrorMessage(error); | ||
| if (message) { | ||
| return new Error(message); | ||
| } | ||
| if (error instanceof Error) { | ||
| return error; | ||
| } | ||
| return new Error("Unknown Mayan error"); | ||
| } | ||
|
|
||
| function extractErrorMessage(error: unknown): string | undefined { | ||
| const payloadMessage = extractPayloadMessage(error); | ||
| if (payloadMessage) return payloadMessage; | ||
|
|
||
| if (error instanceof Error && error.message) return error.message; | ||
| return undefined; | ||
| } | ||
|
|
||
| function extractPayloadMessage(error: unknown): string | undefined { | ||
| if (!error || typeof error !== "object") return undefined; | ||
|
|
||
| const obj = error as Record<string, unknown>; | ||
| return typeof obj.message === "string" ? obj.message : undefined; | ||
| } |
This file contains hidden or 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,102 +1,3 @@ | ||
| import { fetchQuote, ChainName, QuoteParams, QuoteOptions, Quote as MayanQuote, ReferrerAddresses } from "@mayanfinance/swap-sdk"; | ||
| import { QuoteRequest, Quote, SwapQuoteData, AssetId, Chain } from "@gemwallet/types"; | ||
| import { Protocol } from "../protocol"; | ||
| import { buildEvmQuoteData, EMPTY_ADDRESS } from "./evm"; | ||
| import { buildSolanaQuoteData } from "./solana"; | ||
| import { buildSuiQuoteData } from "./sui"; | ||
| import { BigIntMath } from "../bigint_math"; | ||
| import { getReferrerAddresses } from "../referrer"; | ||
| import { SUI_COIN_TYPE } from "../chain/sui/constants"; | ||
|
|
||
| export class MayanProvider implements Protocol { | ||
| private solanaRpc: string; | ||
| private suiRpc: string; | ||
|
|
||
| constructor(solanaRpc: string, suiRpc: string) { | ||
| this.solanaRpc = solanaRpc; | ||
| this.suiRpc = suiRpc; | ||
| } | ||
|
|
||
| mapAssetToTokenId(asset: AssetId): string { | ||
| if (asset.isNative()) { | ||
| if (asset.chain === Chain.Sui) { | ||
| return SUI_COIN_TYPE; | ||
| } | ||
| return EMPTY_ADDRESS; | ||
| } | ||
| return asset.tokenId!; | ||
| } | ||
|
|
||
| mapChainToName(chain: Chain): ChainName { | ||
| switch (chain) { | ||
| case Chain.SmartChain: | ||
| return "bsc"; | ||
| case Chain.AvalancheC: | ||
| return "avalanche" as ChainName; | ||
| case Chain.Hyperliquid: | ||
| return "hyperevm" as ChainName; | ||
| default: | ||
| return chain as ChainName; | ||
| } | ||
| } | ||
|
|
||
| async get_quote(quoteRequest: QuoteRequest): Promise<Quote> { | ||
| const fromAsset = AssetId.fromString(quoteRequest.from_asset.id); | ||
| const toAsset = AssetId.fromString(quoteRequest.to_asset.id); | ||
| const referrerBps = quoteRequest.referral_bps; | ||
| const referrerAddresses = getReferrerAddresses() as ReferrerAddresses; | ||
|
|
||
| const params: QuoteParams = { | ||
| fromToken: this.mapAssetToTokenId(fromAsset), | ||
| toToken: this.mapAssetToTokenId(toAsset), | ||
| amountIn64: quoteRequest.from_value, | ||
| fromChain: this.mapChainToName(fromAsset.chain), | ||
| toChain: this.mapChainToName(toAsset.chain), | ||
| slippageBps: "auto", | ||
| referrer: referrerAddresses.solana!, | ||
| referrerBps, | ||
| } | ||
|
|
||
| // explicitly set which types of quotes we want to fetch | ||
| const options: QuoteOptions = { | ||
| "wormhole": true, | ||
| "swift": true, | ||
| "gasless": false, | ||
| "mctp": true, | ||
| "shuttle": false, | ||
| "fastMctp": true, | ||
| "onlyDirect": false, | ||
| } | ||
|
|
||
| const quotes = await fetchQuote(params, options); | ||
|
|
||
| if (!quotes || quotes.length === 0) { | ||
| throw new Error("No quotes available"); | ||
| } | ||
|
|
||
| const quote = quotes[0]; | ||
|
|
||
| const output_value = BigIntMath.parseDecimals(quote.expectedAmountOut, quote.toToken.decimals); | ||
| const output_min_value = BigIntMath.parseDecimals(quote.minAmountOut, quote.toToken.decimals); | ||
|
|
||
| return { | ||
| quote: quoteRequest, | ||
| output_value: output_value.toString(), | ||
| output_min_value: output_min_value.toString(), | ||
| eta_in_seconds: quote.etaSeconds, | ||
| route_data: quote | ||
| }; | ||
| } | ||
|
|
||
| async get_quote_data(quote: Quote): Promise<SwapQuoteData> { | ||
| const fromAsset = AssetId.fromString(quote.quote.from_asset.id); | ||
|
|
||
| if (fromAsset.chain === Chain.Solana) { | ||
| return buildSolanaQuoteData(quote.quote, quote.route_data as MayanQuote, this.solanaRpc); | ||
| } else if (fromAsset.chain === Chain.Sui) { | ||
| return buildSuiQuoteData(quote.quote, quote.route_data as MayanQuote, this.suiRpc); | ||
| } else { | ||
| return buildEvmQuoteData(quote.quote, quote.route_data as MayanQuote); | ||
| } | ||
| } | ||
| } | ||
| export { MayanProvider } from "./provider"; | ||
| export { MayanErrorCode } from "./error"; | ||
| export type { ErrorData } from "./error"; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.