Skip to content

Commit b778821

Browse files
authored
support outputAmount (#222)
* support outputAmount Signed-off-by: Gerhard Steenkamp <[email protected]> * bump package Signed-off-by: Gerhard Steenkamp <[email protected]> * add changeset Signed-off-by: Gerhard Steenkamp <[email protected]> * use boolean Signed-off-by: Gerhard Steenkamp <[email protected]> * use allowUnmatchedDecimals in unit test Signed-off-by: Gerhard Steenkamp <[email protected]> --------- Signed-off-by: Gerhard Steenkamp <[email protected]>
1 parent c5010eb commit b778821

File tree

8 files changed

+68
-10
lines changed

8 files changed

+68
-10
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@across-protocol/app-sdk": patch
3+
---
4+
5+
Add support for outputAmount field returned by suggested fees. this allows the safe handling of routes where input and output tokens do not have the same decimals

packages/sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@across-protocol/app-sdk",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"main": "./dist/index.js",
55
"type": "module",
66
"description": "The official SDK for integrating Across bridge into your dapp.",

packages/sdk/src/actions/getLimits.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ type LimitsQueryParams = {
3030
* [Optional] The relayer address to simulate fill with. Defaults to the Across relayer.
3131
*/
3232
relayer?: Address;
33+
/**
34+
* [Optional] Caller specifies whether to includes routes where input token
35+
* and output token do not have the same decimals
36+
*/
37+
allowUnmatchedDecimals?: boolean;
3338
};
3439

3540
/**

packages/sdk/src/actions/getQuote.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ export async function getQuote(params: GetQuoteParams): Promise<Quote> {
158158
message,
159159
logger,
160160
apiUrl,
161+
allowUnmatchedDecimals: true,
161162
});
162163

163164
logger?.debug("fees", fees);

packages/sdk/src/actions/getSuggestedFees.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ export type SuggestedFeesQueryParams = {
4545
* [Optional] Whether to skip the amount limit check. Defaults to `false`.
4646
*/
4747
skipAmountLimit?: boolean;
48+
/**
49+
* [Optional] Caller specifies whether to includes routes where input token
50+
* and output token do not have the same decimals
51+
*/
52+
allowUnmatchedDecimals?: boolean;
4853
};
4954

5055
/**
@@ -146,6 +151,18 @@ export type GetSuggestedFeesReturnType = {
146151
maxDepositShortDelay: bigint;
147152
recommendedDepositInstant: bigint;
148153
};
154+
inputToken: {
155+
address: Address;
156+
symbol: string;
157+
decimals: number;
158+
chainId: number;
159+
};
160+
outputToken: {
161+
address: Address;
162+
symbol: string;
163+
decimals: number;
164+
chainId: number;
165+
};
149166
};
150167

151168
/**
@@ -165,19 +182,16 @@ export async function getSuggestedFees({
165182
logger,
166183
);
167184

168-
const parsed = parseSuggestedFees(data);
169-
return {
170-
...parsed,
171-
outputAmount: BigInt(params.amount) - BigInt(data.totalRelayFee.total),
172-
};
185+
return parseSuggestedFees(data);
173186
}
174187

175188
export function parseSuggestedFees(
176189
raw: SuggestedFeesApiResponse,
177-
): Omit<GetSuggestedFeesReturnType, "outputAmount"> {
190+
): GetSuggestedFeesReturnType {
178191
return {
179192
// ensure even unformatted values get passed through
180193
...raw,
194+
outputAmount: BigInt(raw.outputAmount),
181195
estimatedFillTimeSec: raw.estimatedFillTimeSec,
182196
capitalFeePct: Number(raw.capitalFeePct),
183197
capitalFeeTotal: BigInt(raw.capitalFeeTotal),
@@ -217,5 +231,17 @@ export function parseSuggestedFees(
217231
maxDepositShortDelay: BigInt(raw.limits.maxDepositShortDelay),
218232
recommendedDepositInstant: BigInt(raw.limits.recommendedDepositInstant),
219233
},
234+
inputToken: {
235+
address: raw.inputToken.address as Address,
236+
symbol: raw.inputToken.symbol,
237+
decimals: raw.inputToken.decimals,
238+
chainId: raw.inputToken.chainId,
239+
},
240+
outputToken: {
241+
address: raw.outputToken.address as Address,
242+
symbol: raw.outputToken.symbol,
243+
decimals: raw.outputToken.decimals,
244+
chainId: raw.outputToken.chainId,
245+
},
220246
};
221247
}

packages/sdk/src/api/suggested-fees.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ export const suggestedFeesResponseJsonSchema = z.object({
4747
recommendedDepositInstant: bigNumberString,
4848
}),
4949
fillDeadline: numericString,
50+
outputAmount: bigNumberString,
51+
inputToken: z.object({
52+
address: ethereumAddress,
53+
symbol: z.string(),
54+
decimals: z.number(),
55+
chainId: z.number(),
56+
}),
57+
outputToken: z.object({
58+
address: ethereumAddress,
59+
symbol: z.string(),
60+
decimals: z.number(),
61+
chainId: z.number(),
62+
}),
5063
});
5164

5265
export type SuggestedFeesApiResponse = z.infer<

packages/sdk/src/utils/fetch.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function buildSearchParams<
2424
for (const key in params) {
2525
const value = params[key];
2626

27-
if (!value) {
27+
if (!isDefined(value)) {
2828
continue;
2929
}
3030

@@ -45,6 +45,10 @@ export function isOk(res: Response) {
4545
return false;
4646
}
4747

48+
export function isDefined<T>(value: T): value is NonNullable<T> {
49+
return value !== undefined && value !== null ? true : false;
50+
}
51+
4852
function makeFetcher(
4953
name: string,
5054
apiErrorHandler?: (

packages/sdk/test/unit/api/suggested-fees.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { getSuggestedFees } from "../../../src/actions/getSuggestedFees.js";
1+
import {
2+
getSuggestedFees,
3+
GetSuggestedFeesParams,
4+
} from "../../../src/actions/getSuggestedFees.js";
25
import { suggestedFeesResponseJsonSchema } from "../../../src/api/suggested-fees.js";
36
import { MAINNET_API_URL } from "../../../src/constants/index.js";
47
import { buildSearchParams } from "../../../src/utils/fetch.js";
@@ -11,7 +14,8 @@ const params = {
1114
outputToken: "0x4200000000000000000000000000000000000006",
1215
destinationChainId: 10,
1316
amount: "2000000000000000000",
14-
} as const;
17+
allowUnmatchedDecimals: true,
18+
} as const satisfies GetSuggestedFeesParams;
1519

1620
describe("suggested-fees", async () => {
1721
// first test the raw response from the API.

0 commit comments

Comments
 (0)