Skip to content

Commit

Permalink
Merge branch 'main' into FSP-110_sol-staking_fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
icfor committed Apr 12, 2024
2 parents 7bc02b6 + 20003ca commit a3e36b4
Show file tree
Hide file tree
Showing 10 changed files with 1,033 additions and 26 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
"type-check": "tsc"
},
"dependencies": {
"@cosmjs/amino": "^0.32.2",
"@cosmjs/encoding": "^0.32.2",
"@cosmjs/proto-signing": "^0.32.2",
"@cosmjs/stargate": "^0.32.2",
"@emotion/react": "^11.11.4",
"@emotion/server": "^11.11.0",
"@emotion/styled": "^11.11.0",
"@keplr-wallet/cosmos": "^0.12.76",
"@keplr-wallet/proto-types": "^0.12.76",
"@keplr-wallet/types": "^0.12.70",
"@mui/icons-material": "^5.15.11",
"@mui/lab": "^5.0.0-alpha.166",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import type {
Account,
StakingNetworkInfo,
} from "@src/screens/staking/lib/staking_sdk/core";
import { unsupportedLedgerNetworks } from "@src/screens/staking/lib/staking_sdk/core/cosmos";
import { formatCoin } from "@src/screens/staking/lib/staking_sdk/formatters";
import type { StakeAccount } from "@src/screens/staking/lib/staking_sdk/staking_client_types";
import {
Expand Down Expand Up @@ -344,6 +345,10 @@ const PopOver = ({
<HighlightButton
className={styles.stake}
data-test="popover-stake-button"
disabled={
stakingNetworkId &&
unsupportedLedgerNetworks.has(stakingNetworkId)
}
onClick={() => {
if (!hasNetworkSupportedWallet) {
setSelectedAccount(
Expand Down
9 changes: 9 additions & 0 deletions src/screens/staking/lib/staking_sdk/core/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export enum CoinDenom {
AKT = "AKT",
ATOM = "ATOM",
DYDX = "DYDX",
DYM = "DYM",
INJ = "INJ",
ISLM = "ISLM",
KAVA = "KAVA",
OSMO = "OSMO",
PICA = "PICA",
Expand All @@ -36,6 +39,9 @@ export enum StakingNetworkId {
CosmosHub = "cosmoshub-4",
CosmosHubTestnet = "theta-testnet-001",
DyDx = "dydx-mainnet-1",
Dymension = "dymension_1100-1",
Injective = "injective-1",
IslamicCoin = "haqq_11235-1",
Kava = "kava_2222-10",
KavaTestnet = "kava_2221-16000",
Osmosis = "osmosis-1",
Expand All @@ -53,6 +59,9 @@ export const mainNetworkDenom: Record<StakingNetworkId, CoinDenom | null> = {
[StakingNetworkId.CosmosHub]: CoinDenom.ATOM,
[StakingNetworkId.CosmosHubTestnet]: CoinDenom.ATOM,
[StakingNetworkId.DyDx]: CoinDenom.DYDX,
[StakingNetworkId.Dymension]: CoinDenom.DYM,
[StakingNetworkId.Injective]: CoinDenom.INJ,
[StakingNetworkId.IslamicCoin]: CoinDenom.ISLM,
[StakingNetworkId.Kava]: CoinDenom.KAVA,
[StakingNetworkId.KavaTestnet]: CoinDenom.KAVA,
[StakingNetworkId.Osmosis]: CoinDenom.OSMO,
Expand Down
12 changes: 12 additions & 0 deletions src/screens/staking/lib/staking_sdk/core/cosmos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import {
testnetNetworks,
} from "./base";

export const ethermintNetworks = new Set([
StakingNetworkId.Dymension,
StakingNetworkId.Injective,
StakingNetworkId.IslamicCoin,
]);

export const keplrNetworks = new Set<StakingNetworkId>(
[
StakingNetworkId.Akash,
Expand All @@ -14,6 +20,9 @@ export const keplrNetworks = new Set<StakingNetworkId>(
StakingNetworkId.CosmosHub,
StakingNetworkId.CosmosHubTestnet,
StakingNetworkId.DyDx,
StakingNetworkId.Dymension,
StakingNetworkId.IslamicCoin,
StakingNetworkId.Injective,
StakingNetworkId.Kava,
StakingNetworkId.KavaTestnet,
StakingNetworkId.Osmosis,
Expand All @@ -28,6 +37,7 @@ export const keplrNonNativeChains = new Set([
StakingNetworkId.CelestiaTestnet,
StakingNetworkId.ComposableFinance,
StakingNetworkId.CosmosHubTestnet,
StakingNetworkId.IslamicCoin,
StakingNetworkId.KavaTestnet,
StakingNetworkId.StargazeTestnet,
]);
Expand All @@ -44,3 +54,5 @@ export const cosmosStakingNetworks = new Set([
...Array.from(cosmosWallets.has(WalletId.Keplr) ? keplrNetworks : []),
...Array.from(cosmosWallets.has(WalletId.Leap) ? leapNetworks : []),
]);

export const unsupportedLedgerNetworks = new Set<StakingNetworkId>([]);
3 changes: 3 additions & 0 deletions src/screens/staking/lib/staking_sdk/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export const networkIdToNetworkKey: Record<StakingNetworkId, NetworkKey> = {
[StakingNetworkId.CosmosHub]: "cosmos",
[StakingNetworkId.CosmosHubTestnet]: "cosmos-testnet",
[StakingNetworkId.DyDx]: "dydx",
[StakingNetworkId.Dymension]: "dymension",
[StakingNetworkId.Injective]: "injective",
[StakingNetworkId.IslamicCoin]: "islamic_coin",
[StakingNetworkId.Kava]: "kava",
[StakingNetworkId.KavaTestnet]: "kava-testnet",
[StakingNetworkId.Osmosis]: "osmosis",
Expand Down
3 changes: 3 additions & 0 deletions src/screens/staking/lib/staking_sdk/gecko_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export const geckoClient = {
[CoinDenom.AKT]: "akash-network",
[CoinDenom.ATOM]: "cosmos",
[CoinDenom.DYDX]: "dydx",
[CoinDenom.DYM]: "dymension",
[CoinDenom.INJ]: "injective",
[CoinDenom.ISLM]: "islamic-coin",
[CoinDenom.KAVA]: "kava",
[CoinDenom.OSMO]: "osmosis",
[CoinDenom.PICA]: "picasso",
Expand Down
18 changes: 18 additions & 0 deletions src/screens/staking/lib/staking_sdk/utils/coins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export const networkToUnnormalisedDenom = {
[StakingNetworkId.CosmosHub]: "UATOM",
[StakingNetworkId.CosmosHubTestnet]: "UATOM",
[StakingNetworkId.DyDx]: "ADYDX",
[StakingNetworkId.Dymension]: "ADYM",
[StakingNetworkId.Injective]: "inj", // This is important to be lowercase, since inj != INJ
[StakingNetworkId.IslamicCoin]: "AISLM",
[StakingNetworkId.Kava]: "UKAVA",
[StakingNetworkId.KavaTestnet]: "UKAVA",
[StakingNetworkId.Osmosis]: "UOSMO",
Expand All @@ -37,9 +40,14 @@ const aExp = 18;
const pExp = 12;
const nExp = 9;
const uExp = 6;
const exp0 = 0;

const denomMap: Record<DenomToNormalise, [CoinDenom, number]> = {
ADYDX: [CoinDenom.DYDX, aExp],
ADYM: [CoinDenom.DYM, aExp],
AISLM: [CoinDenom.ISLM, aExp],
// Because inj != INJ, this needs to keep the lower case, it is handled when
inj: [CoinDenom.INJ, exp0],
LAMPORTS: [CoinDenom.SOL, nExp],
PPICA: [CoinDenom.PICA, pExp],
UAKT: [CoinDenom.AKT, uExp],
Expand All @@ -51,6 +59,16 @@ const denomMap: Record<DenomToNormalise, [CoinDenom, number]> = {
};

export const normaliseCoin = (coin: Coin): Coin => {
// Special case where INJ != inj
if (coin.denom === "inj") {
return {
amount: new BigNumber(coin.amount)
.div(new BigNumber(10).pow(18))
.toString(),
denom: CoinDenom.INJ,
};
}

const compared = coin.denom?.toUpperCase();

const parseNum = (exp: number) =>
Expand Down
81 changes: 67 additions & 14 deletions src/screens/staking/lib/staking_sdk/wallet_operations/cosmos.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { EncodeObject } from "@cosmjs/proto-signing";
import { SigningStargateClient } from "@cosmjs/stargate";
import type {
MsgDelegateEncodeObject,
MsgUndelegateEncodeObject,
MsgWithdrawDelegatorRewardEncodeObject,
StdFee,
Event as TxEvent,
} from "@cosmjs/stargate";
import { SigningStargateClient } from "@cosmjs/stargate";
import type { AccountData } from "@keplr-wallet/types";
import { MsgWithdrawDelegatorReward } from "cosmjs-types/cosmos/distribution/v1beta1/tx";
import {
Expand All @@ -26,9 +26,11 @@ import { networksWithStaking } from "../core";
import type { Coin, StakingNetworkId } from "../core/base";
import { WalletId } from "../core/base";
import {
ethermintNetworks,
keplrNetworks,
keplrNonNativeChains,
leapNetworks,
unsupportedLedgerNetworks,
} from "../core/cosmos";
import { stakingClient } from "../staking_client";
import { addToConnectedWallets, getConnectedWallets } from "../utils/storage";
Expand All @@ -40,6 +42,7 @@ import type {
WalletOperationResult,
} from "./base";
import { ClaimRewardsError, StakeError, UnstakeError } from "./base";
import { signAndBroadcastEthermint } from "./cosmos/ethermint_utils";

type TxEventType = "delegate" | "unbond" | "withdraw_rewards";

Expand Down Expand Up @@ -67,14 +70,14 @@ const getCosmosFee = async ({
memo,
msgs,
}: FeeOpts) => {
const gasEstimate = await client
.simulate(account.address, msgs, memo)
.catch((err) => {
// eslint-disable-next-line no-console
console.log("debug: wallet_operations.ts: Estimate error", err);
const gasEstimate = ethermintNetworks.has(account.networkId)
? 0
: await client.simulate(account.address, msgs, memo).catch((err) => {
// eslint-disable-next-line no-console
console.log("debug: wallet_operations.ts: Estimate error", err);

return 0;
});
return 0;
});

// This is a factor to increase the gas fee, since the estimate can be a
// bit short in some cases (especially for the last events)
Expand Down Expand Up @@ -193,6 +196,20 @@ export const stakeAmountCosmos = ({
}) satisfies Record<CosmosError, WalletOperationResult<StakeError>>
)[getCosmosError(err)];

if (ethermintNetworks.has(account.networkId) && info.ethAccount) {
return signAndBroadcastEthermint(
networkInfo.rpc,
account,
info.ethAccount,
client,
[msgAny],
fee,
memo,
)
.then(handleSuccess)
.catch(handleError);
}

return client
.signAndBroadcast(account.address, [msgAny], fee, memo)
.then((result) => handleSuccess(result?.events))
Expand Down Expand Up @@ -274,6 +291,20 @@ export const claimRewardsCosmos = async ({
>
)[getCosmosError(err)];

if (ethermintNetworks.has(account.networkId) && info.ethAccount) {
return signAndBroadcastEthermint(
networkInfo.rpc,
account,
info.ethAccount,
client,
[msgAny],
fee,
"",
)
.then(handleSuccess)
.catch(handleError);
}

return client
.signAndBroadcast(account.address, [msgAny], fee)
.then((result) => handleSuccess(result?.events))
Expand Down Expand Up @@ -371,6 +402,20 @@ export const unstakeCosmos = async (
}) satisfies Record<CosmosError, WalletOperationResult<UnstakeError>>
)[getCosmosError(err)];

if (ethermintNetworks.has(opts.account.networkId) && info.ethAccount) {
return signAndBroadcastEthermint(
networkInfo.rpc,
opts.account,
info.ethAccount,
client,
[msgAny],
fee,
opts.memo,
)
.then(handleSuccess)
.catch(handleError);
}

return client
.signAndBroadcast(opts.account.address, [msgAny], fee, opts.memo)
.then((result) => handleSuccess(result?.events))
Expand Down Expand Up @@ -405,9 +450,17 @@ export const tryToConnectKeplr = async (
await keplr.enable([network]).catch(() => null);
}, Promise.resolve());

const handleError = (err: unknown) => {
const handleError = (network: StakingNetworkId) => (err: unknown) => {
// eslint-disable-next-line no-console
console.log("debug: index.tsx: err", err);
console.log("debug: index.tsx: err", network, err);

if (
(err as any)?.message?.includes(
"Ledger is unsupported for this chain",
)
) {
unsupportedLedgerNetworks.add(network);
}

return [] as Account[];
};
Expand Down Expand Up @@ -445,7 +498,7 @@ export const tryToConnectKeplr = async (
.keplr!.getOfflineSigner(network)
.getAccounts()
.then(parseAccounts(network))
.catch(handleError);
.catch(handleError(network));

return {
accounts,
Expand Down Expand Up @@ -509,9 +562,9 @@ export const tryToConnectLeap = async (
try {
await window.leap.enable(chainsToConnect);

const handleError = (err: unknown) => {
const handleError = (network: StakingNetworkId) => (err: unknown) => {
// eslint-disable-next-line no-console
console.log("debug: index.tsx: err", err);
console.log("debug: index.tsx: err", network, err);

return [] as Account[];
};
Expand Down Expand Up @@ -549,7 +602,7 @@ export const tryToConnectLeap = async (
.leap!.getOfflineSigner(network)
.getAccounts()
.then(parseAccounts(network))
.catch(handleError);
.catch(handleError(network));

return {
accounts,
Expand Down
Loading

0 comments on commit a3e36b4

Please sign in to comment.