Skip to content

Commit

Permalink
fix: handling of AcrossApiSimulationError
Browse files Browse the repository at this point in the history
  • Loading branch information
dohaki committed Sep 25, 2024
1 parent 2effffe commit 354e810
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 43 deletions.
7 changes: 6 additions & 1 deletion packages/sdk/src/actions/getLimits.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { Address } from "viem";
import { Address, Hex } from "viem";
import { fetchAcrossApi, LoggerT } from "../utils";
import { MAINNET_API_URL } from "../constants";
import { Amount } from "../types";

type LimitsQueryParams = {
destinationChainId: number;
inputToken: Address;
outputToken: Address;
originChainId: number;
amount?: Amount;
message?: Hex;
recipient?: Address;
relayer?: Address;
};

export type GetLimitsParams = LimitsQueryParams & {
Expand Down
12 changes: 10 additions & 2 deletions packages/sdk/src/actions/getSuggestedFees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@ import { LoggerT, fetchAcrossApi } from "../utils";
import { Amount, Route } from "../types";
import { MAINNET_API_URL } from "../constants";

type SuggestedFeesQueryParams = Partial<Omit<Route, "originChainId">> &
Pick<Route, "originChainId"> & {
type SuggestedFeesQueryParams = Partial<
Omit<Route, "inputTokenSymbol" | "outputTokenSymbol" | "isNative">
> &
Pick<
Route,
| "originChainId"
| "destinationChainId"
| "inputTokenSymbol"
| "outputTokenSymbol"
> & {
amount: Amount;
recipient?: Address;
message?: string;
Expand Down
11 changes: 8 additions & 3 deletions packages/sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ export class AcrossClient {
) {
const { simulationId, simulationUrl } = await this.simulateTxOnTenderly(
{
networkId: params.originChainId.toString(),
networkId: params.destinationChainId.toString(),
to: e.transaction.to,
data: e.transaction.data,
from: e.transaction.from,
Expand All @@ -296,7 +296,12 @@ export class AcrossClient {

async getLimits(params: Omit<GetLimitsParams, "apiUrl" | "logger">) {
try {
return getLimits({ ...params, apiUrl: this.apiUrl, logger: this.logger });
const limits = await getLimits({
...params,
apiUrl: this.apiUrl,
logger: this.logger,
});
return limits;
} catch (e) {
if (
this.tenderlySimOnError &&
Expand All @@ -305,7 +310,7 @@ export class AcrossClient {
) {
const { simulationId, simulationUrl } = await this.simulateTxOnTenderly(
{
networkId: params.originChainId.toString(),
networkId: params.destinationChainId.toString(),
to: e.transaction.to,
data: e.transaction.data,
from: e.transaction.from,
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export class AcrossApiError extends HttpError {
) {
super(
{
name: "AcrossApiError",
...params,
name: params.name ?? "AcrossApiError",
},
opts,
);
Expand Down
65 changes: 29 additions & 36 deletions packages/sdk/src/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ export function isOk(res: Response) {

function makeFetcher(
name: string,
apiErrorHandler?: (
response: Response,
data: any,
url: string,
) => Promise<void>,
apiErrorHandler?: (response: Response, data: any, url: string) => void,
) {
return async <ResBody, ReqParams = {}>(
apiUrl: string,
Expand Down Expand Up @@ -92,43 +88,40 @@ function makeFetcher(
};
}

export const fetchAcrossApi = makeFetcher(
"Across API",
async (res, data, url) => {
// Check for Across API errors
if (
typeof data === "object" &&
data !== null &&
"type" in data &&
data.type === "AcrossApiError"
) {
const acrossApiError = data as unknown as {
message: string;
code: AcrossErrorCodeType;
transaction: {
from: Address;
to: Address;
data: Hex;
};
export const fetchAcrossApi = makeFetcher("Across API", (res, data, url) => {
// Check for Across API errors
if (
typeof data === "object" &&
data !== null &&
"type" in data &&
data.type === "AcrossApiError"
) {
const acrossApiError = data as unknown as {
message: string;
code: AcrossErrorCodeType;
transaction: {
from: Address;
to: Address;
data: Hex;
};
};

if (acrossApiError.code === "SIMULATION_ERROR") {
throw new AcrossApiSimulationError({
message: acrossApiError.message,
url,
transaction: acrossApiError.transaction,
});
}

throw new AcrossApiError({
status: res.status,
if (acrossApiError.code === "SIMULATION_ERROR") {
throw new AcrossApiSimulationError({
message: acrossApiError.message,
url,
code: acrossApiError.code,
transaction: acrossApiError.transaction,
});
}
},
);

throw new AcrossApiError({
status: res.status,
message: acrossApiError.message,
url,
code: acrossApiError.code,
});
}
});

export const fetchIndexerApi = makeFetcher(
"Indexer API",
Expand Down

0 comments on commit 354e810

Please sign in to comment.