Skip to content

Commit

Permalink
remove unused keplr-wallet deps
Browse files Browse the repository at this point in the history
  • Loading branch information
steezeburger committed Dec 11, 2024
1 parent ebf7194 commit 5d4b7e6
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 1,613 deletions.
1,666 changes: 128 additions & 1,538 deletions web/package-lock.json

Large diffs are not rendered by default.

12 changes: 4 additions & 8 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
"private": true,
"dependencies": {
"@cosmjs/launchpad": "^0.27.1",
"@cosmjs/math": "^0.32.4",
"@cosmjs/stargate": "^0.32.3",
"@cosmos-kit/keplr": "^2.14.1",
"@cosmos-kit/leap": "^2.14.1",
"@cosmos-kit/react": "^2.20.1",
"@creativebulma/bulma-tooltip": "^1.2.0",
"@keplr-wallet/common": "^0.12.22",
"@keplr-wallet/cosmos": "^0.12.22",
"@keplr-wallet/hooks": "^0.12.22",
"@keplr-wallet/stores": "^0.12.22",
"@keplr-wallet/types": "^0.12.22",
"@keplr-wallet/unit": "^0.12.22",
"@rainbow-me/rainbowkit": "^2.2.0",
"@tanstack/react-query": "^5.61.0",
"@types/node": "^16.18.56",
Expand All @@ -36,7 +32,8 @@
"uuid": "^9.0.1",
"viem": "^2.21.49",
"wagmi": "^2.13.0",
"web-vitals": "^2.1.4"
"web-vitals": "^2.1.4",
"webpack": "^5.97.1"
},
"overrides": {
"react-scripts": {
Expand Down Expand Up @@ -78,7 +75,6 @@
"buffer": "^6.0.3",
"react-app-rewired": "^2.2.1",
"source-map-explorer": "^2.5.3",
"stream-browserify": "^3.0.0",
"webpack": "^5.97.1"
"stream-browserify": "^3.0.0"
}
}
19 changes: 5 additions & 14 deletions web/src/components/DepositCard/DepositCard.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import type React from "react";
import { useCallback, useEffect, useMemo, useState } from "react";

import { Dec, DecUtils } from "@keplr-wallet/unit";
import AnimatedArrowSpacer from "components/AnimatedDownArrowSpacer/AnimatedDownArrowSpacer";
import Dropdown from "components/Dropdown/Dropdown";
import { useConfig } from "config";
import {
AddERC20ToWalletButton,
useEvmChainSelection,
} from "features/EthWallet";
import {
padDecimal,
sendIbcTransfer,
useIbcChainSelection,
} from "features/KeplrWallet";
import { sendIbcTransfer, useIbcChainSelection } from "features/KeplrWallet";
import { NotificationType, useNotifications } from "features/Notifications";
import { Decimal } from "@cosmjs/math";

export default function DepositCard(): React.ReactElement {
const { evmChains, ibcChains } = useConfig();
Expand Down Expand Up @@ -194,15 +190,10 @@ export default function DepositCard(): React.ReactElement {
setIsAnimating(true);

try {
// must left pad the amount with 0 if it starts with a dot because
// keplr's regex for a decimal is ^-?\d+.?\d*$ so it requires a leading digit
const amountStrPadded = padDecimal(amount);
const formattedAmount = DecUtils.getTenExponentN(
const formattedAmount = Decimal.fromUserInput(
amount,
selectedIbcCurrency.coinDecimals,
)
.mul(new Dec(amountStrPadded))
.truncate()
.toString();
).atomics;

const signer = await getCosmosSigningClient();
await sendIbcTransfer(
Expand Down
3 changes: 1 addition & 2 deletions web/src/features/KeplrWallet/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useIbcChainSelection } from "./hooks/useIbcChainSelection";
import { sendIbcTransfer } from "./services/ibc";
import { padDecimal } from "./utils/utils";

export { padDecimal, sendIbcTransfer, useIbcChainSelection };
export { sendIbcTransfer, useIbcChainSelection };
15 changes: 9 additions & 6 deletions web/src/features/KeplrWallet/services/ibc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { SigningStargateClient } from "@cosmjs/stargate";
import { Dec } from "@keplr-wallet/unit";
import type { IbcChainInfo, IbcCurrency } from "config";
import { Decimal } from "@cosmjs/math";
import { osmosis } from "osmojs";
import type { IbcChainInfo, IbcCurrency } from "config";
import { nowPlusMinutesInNano } from "../utils/utils.ts";

/**
* Send an IBC transfer from the selected chain to the recipient address.
Expand Down Expand Up @@ -47,8 +48,7 @@ export const sendIbcTransfer = async (
sender: sender,
memo: memo,
receiver: currency.sequencerBridgeAccount,
// 10 minutes from now, in nanoseconds
timeoutTimestamp: BigInt(Date.now() + 600_000) * BigInt(1_000_000),
timeoutTimestamp: nowPlusMinutesInNano(10),
},
};

Expand Down Expand Up @@ -85,8 +85,11 @@ export const getBalanceFromChain = async (
}

// convert to display amount using decimal places
const amount = new Dec(balance.balance.amount, currency.coinDecimals);
return `${amount.toString(2)} ${currency.coinDenom}`;
const amount = Decimal.fromAtomics(
balance.balance.amount,
currency.coinDecimals,
);
return `${amount.toString()} ${currency.coinDenom}`;
} catch (error) {
console.error("Failed to fetch balance:", error);
throw error;
Expand Down
56 changes: 23 additions & 33 deletions web/src/features/KeplrWallet/utils/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,35 @@
import { padDecimal } from "./utils"; // adjust import path as needed
import { nowPlusMinutesInNano } from "./utils";

describe("padDecimal", () => {
test("adds leading zero to strings starting with decimal point", () => {
expect(padDecimal(".5")).toBe("0.5");
expect(padDecimal(".123")).toBe("0.123");
expect(padDecimal(".0")).toBe("0.0");
describe("nowPlusMinutesInNano", () => {
beforeEach(() => {
jest.spyOn(Date, "now").mockImplementation(() => 1600000000000); // 2020-09-13T12:26:40.000Z
});

test("does not modify strings that do not start with decimal point", () => {
expect(padDecimal("1.5")).toBe("1.5");
expect(padDecimal("10.123")).toBe("10.123");
expect(padDecimal("0.5")).toBe("0.5");
expect(padDecimal("123")).toBe("123");
afterEach(() => {
jest.restoreAllMocks();
});

test("handles edge cases", () => {
// Empty string
expect(padDecimal("")).toBe("");
test("converts 5 minutes to expected nanoseconds from now", () => {
const result = nowPlusMinutesInNano(5);

// Just a decimal point
expect(padDecimal(".")).toBe("0.");
// assert the result is a BigInt
expect(typeof result).toBe("bigint");

// Multiple decimal points
expect(padDecimal(".1.2")).toBe("0.1.2");

// Leading zeros
expect(padDecimal("00.5")).toBe("00.5");

// Negative numbers
expect(padDecimal("-.5")).toBe("-.5");
expect(padDecimal("-0.5")).toBe("-0.5");
// convert result back to milliseconds and check the time difference
const resultInMs = Number(result / BigInt(1_000_000));
const expectedMs = Date.now() + 5 * 60 * 1000;
expect(resultInMs).toBe(expectedMs);
});

test("preserves string format without modifying actual numbers", () => {
// Scientific notation
expect(padDecimal("1e-10")).toBe("1e-10");

// Very long decimals
expect(padDecimal(".12345678901234567890")).toBe("0.12345678901234567890");
test("maintains millisecond precision when converting to nanoseconds", () => {
const result = nowPlusMinutesInNano(1);
// check that the last 6 digits (nanosecond portion) are zeros
expect(result % BigInt(1_000_000)).toBe(BigInt(0));
});

// Trailing zeros
expect(padDecimal(".500")).toBe("0.500");
test("handles zero minutes", () => {
const result = nowPlusMinutesInNano(0);
const expectedMs = Date.now();
expect(Number(result / BigInt(1_000_000))).toBe(expectedMs);
});
});
7 changes: 3 additions & 4 deletions web/src/features/KeplrWallet/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* Pad decimal with 0 if it starts with a dot
* @param str
* Returns now plus the given minutes, in nanoseconds
*/
export function padDecimal(str: string) {
return str.startsWith(".") ? `0${str}` : str;
export function nowPlusMinutesInNano(minutes: number): bigint {
return BigInt(Date.now() + minutes * 60 * 1000) * BigInt(1_000_000);
}
8 changes: 0 additions & 8 deletions web/src/react-app-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
/// <reference types="react-scripts" />
import type { Keplr } from "@keplr-wallet/types";

declare global {
interface Window {
// window.keplr should be provided by the Keplr extension
keplr?: Keplr;
}
}

0 comments on commit 5d4b7e6

Please sign in to comment.