Skip to content

Commit e8899f0

Browse files
feat(coin-evm): implement validateIntent
1 parent 6c584be commit e8899f0

File tree

6 files changed

+994
-3
lines changed

6 files changed

+994
-3
lines changed

.changeset/perfect-icons-type.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ledgerhq/coin-evm": minor
3+
---
4+
5+
feat(coin-evm): implement `validateIntent`

libs/coin-modules/coin-evm/src/api/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
listOperations,
2828
getBalance,
2929
getSequence,
30+
validateIntent,
3031
} from "../logic/index";
3132

3233
export function createApi(config: EvmConfig, currencyId: CryptoCurrencyId): Api {
@@ -64,8 +65,7 @@ export function createApi(config: EvmConfig, currencyId: CryptoCurrencyId): Api
6465
throw new Error("getRewards is not supported");
6566
},
6667
getSequence: (address: string): Promise<number> => getSequence(currency, address),
67-
validateIntent(_intent: TransactionIntent): Promise<TransactionValidation> {
68-
throw new Error("validateIntent is not supported");
69-
},
68+
validateIntent: (intent: TransactionIntent): Promise<TransactionValidation> =>
69+
validateIntent(currency, intent),
7070
};
7171
}

libs/coin-modules/coin-evm/src/logic/common.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,58 @@
11
import { ethers } from "ethers";
22
import { TransactionTypes } from "ethers/lib/utils";
3+
import { FeeEstimation } from "@ledgerhq/coin-framework/api/types";
34
import ERC20ABI from "../abis/erc20.abi.json";
5+
import { ApiFeeData, ApiGasOptions } from "../types";
6+
7+
export function isApiGasOptions(options: unknown): options is ApiGasOptions {
8+
if (!options || typeof options !== "object") return false;
9+
10+
return (
11+
"slow" in options &&
12+
isApiFeeData(options.slow) &&
13+
"medium" in options &&
14+
isApiFeeData(options.medium) &&
15+
"fast" in options &&
16+
isApiFeeData(options.fast)
17+
);
18+
}
19+
20+
export function isApiFeeData(fees: unknown): fees is ApiFeeData {
21+
if (!fees || typeof fees !== "object") return false;
22+
23+
const isBigIntOrNull = (value: unknown): boolean => value === null || typeof value === "bigint";
24+
25+
return (
26+
"maxFeePerGas" in fees &&
27+
isBigIntOrNull(fees.maxFeePerGas) &&
28+
"maxPriorityFeePerGas" in fees &&
29+
isBigIntOrNull(fees.maxPriorityFeePerGas) &&
30+
"gasPrice" in fees &&
31+
isBigIntOrNull(fees.gasPrice) &&
32+
"nextBaseFee" in fees &&
33+
isBigIntOrNull(fees.nextBaseFee)
34+
);
35+
}
36+
37+
type LegacyFeeEstimation = FeeEstimation & { parameters: { gasPrice: bigint } };
38+
type Eip1559FeeEstimation = FeeEstimation & {
39+
parameters: { maxFeePerGas: bigint; maxPriorityFeePerGas: bigint };
40+
};
41+
42+
export function isLegacyFeeEstimation(
43+
estimation: FeeEstimation,
44+
): estimation is LegacyFeeEstimation {
45+
return typeof estimation.parameters?.gasPrice === "bigint";
46+
}
47+
48+
export function isEip1559FeeEstimation(
49+
estimation: FeeEstimation,
50+
): estimation is Eip1559FeeEstimation {
51+
return (
52+
typeof estimation.parameters?.maxFeePerGas === "bigint" &&
53+
typeof estimation.parameters?.maxPriorityFeePerGas === "bigint"
54+
);
55+
}
456

557
export function getTransactionType(intentType: string): TransactionTypes {
658
if (!["send-legacy", "send-eip1559"].includes(intentType)) {

libs/coin-modules/coin-evm/src/logic/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export * from "./estimateFees";
66
export * from "./listOperations";
77
export * from "./craftTransaction";
88
export * from "./getSequence";
9+
export * from "./validateIntent";

0 commit comments

Comments
 (0)