Skip to content

Commit

Permalink
feat: Improve ODOS types
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmDreamy committed Jan 5, 2025
1 parent 6b26de8 commit 42df774
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions utils/odos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@ import ky from "ky";
import * as R from "remeda";
import type { Address, Hex } from "viem";

export type OdosQuoteParameters = {
export type OdosQuoteParameters<
I extends Array<{ tokenAddress: Address; amount: bigint }>,
O extends Array<{ tokenAddress: Address; proportion: number }>,
> = {
endpoint: string;
chainId: number;
inputTokens: Array<{ tokenAddress: Address; amount: bigint }>;
outputTokens: Array<{ tokenAddress: Address; proportion: number }>;
inputTokens: [...I];
outputTokens: [...O];
userAddr: Address;
slippageLimitPercent?: number;
disableRFQs?: boolean;
compact?: boolean;
simple?: boolean;
};

export async function odosQuote(quoteParameters: OdosQuoteParameters) {
export async function odosQuote<
I extends Array<{ tokenAddress: Address; amount: bigint }>,
O extends Array<{ tokenAddress: Address; proportion: number }>,
>(quoteParameters: OdosQuoteParameters<I, O>) {
const odosApi = ky.extend({
prefixUrl: quoteParameters.endpoint,
});
Expand Down Expand Up @@ -44,10 +50,20 @@ export async function odosQuote(quoteParameters: OdosQuoteParameters) {
blockNumber: number;
gasEstimate: number;
gasEstimateValue: number;
inputTokens: Array<{ tokenAddress: Address; amount: `${bigint}` }>;
outputTokens: Array<{ tokenAddress: Address; amount: `${bigint}` }>;
inputTokens: {
[Index in keyof I]: {
tokenAddress: Lowercase<I[Index]["tokenAddress"]>;
amount: `${bigint}`;
};
};
outputTokens: {
[Index in keyof O]: {
tokenAddress: Lowercase<O[Index]["tokenAddress"]>;
amount: `${bigint}`;
};
};
netOutValue: number;
outValues: number[];
outValues: Array<`${number}`>;
transaction: {
gas: number;
gasPrice: number;
Expand All @@ -62,11 +78,15 @@ export async function odosQuote(quoteParameters: OdosQuoteParameters) {

return {
...assembledQuote,
inputTokens: assembledQuote.inputTokens.map(({ tokenAddress, amount }) => ({
tokenAddress,
amount: BigInt(amount),
})),
outputTokens: assembledQuote.outputTokens.map(
inputTokens: R.map(
assembledQuote.inputTokens,
({ tokenAddress, amount }) => ({
tokenAddress,
amount: BigInt(amount),
}),
),
outputTokens: R.map(
assembledQuote.outputTokens,
({ tokenAddress, amount }) => ({
tokenAddress,
amount: BigInt(amount),
Expand Down

0 comments on commit 42df774

Please sign in to comment.