Skip to content

Commit

Permalink
✨ (context-module) [DSDK-325]: Add uniswap loader (#675)
Browse files Browse the repository at this point in the history
  • Loading branch information
aussedatlo authored Feb 14, 2025
2 parents 2ed9e4d + 70b7d2c commit cafa997
Show file tree
Hide file tree
Showing 21 changed files with 1,789 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-seas-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/device-management-kit": patch
---

Use unknown type for HexaString typeguard
5 changes: 5 additions & 0 deletions .changeset/strong-rocks-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/context-module": minor
---

Add uniswap loader
11 changes: 11 additions & 0 deletions packages/device-management-kit/src/api/utils/HexaString.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ describe("HexaString", () => {
// THEN
expect(result).toBeFalsy();
});

it.each([123, [], {}, null, undefined, true])(
"should return false for invalid input %p",
(value) => {
// WHEN
const result = isHexaString(value);

// THEN
expect(result).toBeFalsy();
},
);
});

describe("hexaStringToBuffer function", () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/device-management-kit/src/api/utils/HexaString.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type HexaString = `0x${string}`;

export const isHexaString = (value: string): value is HexaString => {
return /^0x[0-9a-fA-F]*$/.test(value);
export const isHexaString = (value: unknown): value is HexaString => {
return typeof value === "string" && /^0x[0-9a-fA-F]*$/.test(value);
};

export const hexaStringToBuffer = (value: string): Uint8Array | null => {
Expand Down
5 changes: 5 additions & 0 deletions packages/signer/context-module/src/DefaultContextModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { type TransactionContextLoader } from "./transaction/domain/TransactionC
import { type TrustedNameContextLoader } from "./trusted-name/domain/TrustedNameContextLoader";
import { typedDataTypes } from "./typed-data/di/typedDataTypes";
import type { TypedDataContextLoader } from "./typed-data/domain/TypedDataContextLoader";
import { uniswapTypes } from "./uniswap/di/uniswapTypes";
import { type UniswapContextLoader } from "./uniswap/domain/UniswapContextLoader";
import { type ContextModule } from "./ContextModule";
import { makeContainer } from "./di";

Expand Down Expand Up @@ -54,6 +56,9 @@ export class DefaultContextModule implements ContextModule {
this._container.get<TransactionContextLoader>(
transactionTypes.TransactionContextLoader,
),
this._container.get<UniswapContextLoader>(
uniswapTypes.UniswapContextLoader,
),
];
}

Expand Down
2 changes: 2 additions & 0 deletions packages/signer/context-module/src/di.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { tokenModuleFactory } from "@/token/di/tokenModuleFactory";
import { transactionModuleFactory } from "@/transaction/di/transactionModuleFactory";
import { trustedNameModuleFactory } from "@/trusted-name/di/trustedNameModuleFactory";
import { typedDataModuleFactory } from "@/typed-data/di/typedDataModuleFactory";
import { uniswapModuleFactory } from "@/uniswap/di/uniswapModuleFactory";

type MakeContainerArgs = {
config: ContextModuleConfig;
Expand All @@ -26,6 +27,7 @@ export const makeContainer = ({ config }: MakeContainerArgs) => {
trustedNameModuleFactory(),
typedDataModuleFactory(),
nanoPkiModuleFactory(),
uniswapModuleFactory(),
);

return container;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const UNISWAP_PLUGIN_SIGNATURE =
"0x3044022014391e8f355867a57fe88f6a5a4dbcb8bf8f888a9db3ff3449caf72d120396bd02200c13d9c3f79400fe0aa0434ac54d59b79503c9964a4abc3e8cd22763e0242935";
export const UNISWAP_PLUGIN_NAME = "Uniswap";
46 changes: 46 additions & 0 deletions packages/signer/context-module/src/uniswap/constants/uniswap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { type HexaString } from "@ledgerhq/device-management-kit";

export const UNISWAP_EXECUTE_ABI = [
"function execute(bytes calldata commands, bytes[] calldata inputs, uint256 deadline) external payable",
];

export const UNISWAP_UNIVERSAL_ROUTER_ADDRESS =
"0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad";
export const UNISWAP_EXECUTE_SELECTOR = "0x3593564c";

export enum UniswapSupportedCommand {
V2_SWAP_EXACT_IN = "V2_SWAP_EXACT_IN",
V2_SWAP_EXACT_OUT = "V2_SWAP_EXACT_OUT",
V3_SWAP_EXACT_IN = "V3_SWAP_EXACT_IN",
V3_SWAP_EXACT_OUT = "V3_SWAP_EXACT_OUT",
WRAP_ETH = "WRAP_ETH",
UNWRAP_ETH = "UNWRAP_ETH",
PERMIT2_PERMIT = "PERMIT2_PERMIT",
PERMIT2_TRANSFER_FROM = "PERMIT2_TRANSFER_FROM",
PERMIT2_PERMIT_BATCH = "PERMIT2_PERMIT_BATCH",
PERMIT2_TRANSFER_FROM_BATCH = "PERMIT2_TRANSFER_FROM_BATCH",
PAY_PORTION = "PAY_PORTION",
SWEEP = "SWEEP",
}

export const UNISWAP_COMMANDS: Record<HexaString, UniswapSupportedCommand> = {
"0x08": UniswapSupportedCommand.V2_SWAP_EXACT_IN,
"0x09": UniswapSupportedCommand.V2_SWAP_EXACT_OUT,
"0x00": UniswapSupportedCommand.V3_SWAP_EXACT_IN,
"0x01": UniswapSupportedCommand.V3_SWAP_EXACT_OUT,
"0x0b": UniswapSupportedCommand.WRAP_ETH,
"0x0c": UniswapSupportedCommand.UNWRAP_ETH,
"0x0a": UniswapSupportedCommand.PERMIT2_PERMIT,
"0x0d": UniswapSupportedCommand.PERMIT2_TRANSFER_FROM,
"0x02": UniswapSupportedCommand.PERMIT2_PERMIT_BATCH,
"0x03": UniswapSupportedCommand.PERMIT2_TRANSFER_FROM_BATCH,
"0x06": UniswapSupportedCommand.PAY_PORTION,
"0x04": UniswapSupportedCommand.SWEEP,
};

export const UNISWAP_SWAP_COMMANDS: UniswapSupportedCommand[] = [
UniswapSupportedCommand.V2_SWAP_EXACT_IN,
UniswapSupportedCommand.V2_SWAP_EXACT_OUT,
UniswapSupportedCommand.V3_SWAP_EXACT_IN,
UniswapSupportedCommand.V3_SWAP_EXACT_OUT,
];
50 changes: 50 additions & 0 deletions packages/signer/context-module/src/uniswap/constants/weth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { type HexaString } from "@ledgerhq/device-management-kit";

export enum WETHSupportedChainIds {
ETHEREUM_MAINNET = 1,
ETHEREUM_GOERLI = 5,
ETHEREUM_SEPOLIA = 11155111,
ARBITRUM_ONE = 42161,
ARBITRUM_GOERLI = 421613,
AVALANCHE_C_CHAIN = 43114,
BSC = 56,
BASE = 8453,
BASE_GOERLI = 84531,
BLAST = 23888,
OPTIMISM = 10,
OPTIMISM_GOERLI = 420,
POLYGON = 137,
POLYGON_MUMBAI = 80001,
}

export type WETHSupportedChainId = keyof typeof WETH_ADDRESS_BY_CHAIN_ID;

export const WETH_ADDRESS_BY_CHAIN_ID: Record<
WETHSupportedChainIds,
HexaString
> = {
[WETHSupportedChainIds.ETHEREUM_MAINNET]:
"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
[WETHSupportedChainIds.ETHEREUM_GOERLI]:
"0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6",
[WETHSupportedChainIds.ETHEREUM_SEPOLIA]:
"0xfFf9976782d46CC05630D1f6eBAb18b2324d6B14",
[WETHSupportedChainIds.ARBITRUM_ONE]:
"0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
[WETHSupportedChainIds.ARBITRUM_GOERLI]:
"0xe39Ab88f8A4777030A534146A9Ca3B52bd5D43A3",
[WETHSupportedChainIds.AVALANCHE_C_CHAIN]:
"0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7",
[WETHSupportedChainIds.BSC]: "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c",
[WETHSupportedChainIds.BASE]: "0x4200000000000000000000000000000000000006",
[WETHSupportedChainIds.BASE_GOERLI]:
"0x44D627f900da8AdaC7561bD73aA745F132450798",
[WETHSupportedChainIds.BLAST]: "0x4300000000000000000000000000000000000004",
[WETHSupportedChainIds.OPTIMISM]:
"0x4200000000000000000000000000000000000006",
[WETHSupportedChainIds.OPTIMISM_GOERLI]:
"0x4200000000000000000000000000000000000006",
[WETHSupportedChainIds.POLYGON]: "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270",
[WETHSupportedChainIds.POLYGON_MUMBAI]:
"0x9c3C9283D3e44854697Cd22D3Faa240Cfb032889",
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { type HexaString } from "@ledgerhq/device-management-kit";

export interface AbiDecoderDataSource {
decode(types: string[], data: HexaString): unknown[];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { type HexaString } from "@ledgerhq/device-management-kit";

import { type UniswapSupportedCommand } from "@/uniswap/constants/uniswap";

export interface CommandDecoderDataSource {
decode(
command: UniswapSupportedCommand,
input: HexaString,
chainId: number,
): HexaString[];
}
Loading

0 comments on commit cafa997

Please sign in to comment.