Skip to content

Commit

Permalink
Merge pull request #19 from Abracadabra-money/feature/improve-odos-types
Browse files Browse the repository at this point in the history
feat: Improve ODOS types
  • Loading branch information
0xmDreamy authored Jan 5, 2025
2 parents 6b26de8 + 34aea60 commit 0b6f683
Showing 1 changed file with 45 additions and 16 deletions.
61 changes: 45 additions & 16 deletions utils/odos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,32 @@ 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 }>,
C extends number,
> = {
endpoint: string;
chainId: number;
inputTokens: Array<{ tokenAddress: Address; amount: bigint }>;
outputTokens: Array<{ tokenAddress: Address; proportion: number }>;
chainId: C;
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 }>,
C extends number,
>(quoteParameters: OdosQuoteParameters<I, O, C>) {
const odosApi = ky.extend({
prefixUrl: quoteParameters.endpoint,
});

const { pathId } = await odosApi
const { inValues, pathId } = await odosApi
.post("sor/quote/v2", {
json: {
...R.omit(quoteParameters, ["endpoint"]),
Expand All @@ -31,7 +39,10 @@ export async function odosQuote(quoteParameters: OdosQuoteParameters) {
),
},
})
.json<{ pathId: string }>();
.json<{
inValues: { [Index in keyof I]: number };
pathId: string;
}>();

const assembledQuote = await odosApi
.post("sor/assemble", {
Expand All @@ -41,13 +52,24 @@ export async function odosQuote(quoteParameters: OdosQuoteParameters) {
},
})
.json<{
deprecated: string | null;
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: { [Index in keyof O]: `${number}` };
transaction: {
gas: number;
gasPrice: number;
Expand All @@ -56,22 +78,29 @@ export async function odosQuote(quoteParameters: OdosQuoteParameters) {
from: Address;
data: Hex;
nonce: number;
chainId: number;
chainId: C;
};
simulation: null;
}>();

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),
}),
),
inValues,
outValues: R.map(assembledQuote.outValues, (value) => Number(value)),
transaction: {
...assembledQuote.transaction,
value: BigInt(assembledQuote.transaction.value),
Expand Down

0 comments on commit 0b6f683

Please sign in to comment.