Skip to content

Commit cf8f1c3

Browse files
authored
Merge pull request #8 from hyperweb-io/fix/stake-tokens-cannot-stake
fix: cannot stake action
2 parents 1e3fae6 + 7edfc8d commit cf8f1c3

File tree

2 files changed

+55
-43
lines changed

2 files changed

+55
-43
lines changed

examples/stake-tokens/components/staking/DelegateModal.tsx

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
import { useState } from 'react';
2-
import { cosmos } from 'interchain-query';
3-
import { StdFee } from '@interchainjs/cosmos-types/types';
4-
import { useChain } from '@interchain-kit/react';
5-
import BigNumber from 'bignumber.js';
1+
import { useState } from "react";
2+
import { cosmos } from "interchain-query";
3+
import { StdFee } from "@interchainjs/cosmos-types/types";
4+
import { useChain } from "@interchain-kit/react";
5+
import BigNumber from "bignumber.js";
66
import {
77
BasicModal,
88
StakingDelegate,
99
Box,
1010
Button,
1111
Callout,
1212
Text,
13-
} from '@interchain-ui/react';
14-
import { assetLists } from '@chain-registry/v2'
15-
13+
} from "@interchain-ui/react";
14+
import { assetLists } from "@chain-registry/v2";
15+
import { useToast } from "@/hooks/useToast";
1616
import {
1717
type ExtendedValidator as Validator,
1818
formatValidatorMetaInfo,
1919
getAssetLogoUrl,
2020
isGreaterThanZero,
2121
shiftDigits,
2222
calcDollarValue,
23-
} from '@/utils';
24-
import { getCoin, getExponent } from '@/config';
25-
import { Prices, UseDisclosureReturn, useTx } from '@/hooks';
23+
} from "@/utils";
24+
import { getCoin, getExponent } from "@/config";
25+
import { Prices, UseDisclosureReturn, useTx } from "@/hooks";
2626

2727
const { delegate } = cosmos.staking.v1beta1.MessageComposer.fromPartial;
2828

@@ -56,6 +56,7 @@ export const DelegateModal = ({
5656
modalTitle?: string;
5757
showDescription?: boolean;
5858
}) => {
59+
const { toast } = useToast();
5960
const { isOpen, onClose } = modalControl;
6061
const { address } = useChain(chainName);
6162

@@ -93,17 +94,26 @@ export const DelegateModal = ({
9394
maxAmountAndFee &&
9495
new BigNumber(amount).isEqualTo(maxAmountAndFee.maxAmount);
9596

96-
await tx([msg], {
97-
fee: isMaxAmountAndFeeExists ? maxAmountAndFee.fee : null,
98-
onSuccess: () => {
99-
setMaxAmountAndFee(undefined);
100-
closeOuterModal && closeOuterModal();
101-
updateData();
102-
onModalClose();
103-
},
104-
});
105-
106-
setIsDelegating(false);
97+
try {
98+
await tx([msg], {
99+
fee: isMaxAmountAndFeeExists ? maxAmountAndFee.fee : null,
100+
onSuccess: () => {
101+
setMaxAmountAndFee(undefined);
102+
closeOuterModal && closeOuterModal();
103+
updateData();
104+
onModalClose();
105+
},
106+
});
107+
} catch (error) {
108+
toast({
109+
type: "error",
110+
title: "Error while delegating",
111+
description: error instanceof Error ? error.message : "Unknown error",
112+
});
113+
console.log("error while delegating", error);
114+
} finally {
115+
setIsDelegating(false);
116+
}
107117
};
108118

109119
const handleMaxClick = async () => {
@@ -126,19 +136,19 @@ export const DelegateModal = ({
126136
});
127137

128138
const assetList = assetLists.find((asset) => asset.chainName === chainName);
129-
const denom = assetList?.assets[0].base!
130-
const denomUnit = assetList?.assets[0].denomUnits[0]
139+
const denom = assetList?.assets[0].base!;
140+
const denomUnit = assetList?.assets[0].denomUnits[0];
131141

132142
try {
133143
const fee = {
134144
amount: [
135145
{
136146
denom: denomUnit?.denom!,
137-
amount: (BigInt(10 ** (denomUnit?.exponent || 6)) / 10n).toString()
138-
}
147+
amount: (BigInt(10 ** (denomUnit?.exponent || 6)) / 10n).toString(),
148+
},
139149
],
140-
gas: '800000'
141-
}
150+
gas: "800000",
151+
};
142152
const feeAmount = new BigNumber(fee.amount[0].amount).shiftedBy(-exp);
143153
const balanceAfterFee = new BigNumber(balance)
144154
.minus(feeAmount)
@@ -172,11 +182,11 @@ export const DelegateModal = ({
172182

173183
return (
174184
<BasicModal
175-
title={modalTitle || 'Delegate'}
185+
title={modalTitle || "Delegate"}
176186
isOpen={isOpen}
177187
onClose={onModalClose}
178188
>
179-
<Box width={{ mobile: '100%', tablet: '$containerSm' }}>
189+
<Box width={{ mobile: "100%", tablet: "$containerSm" }}>
180190
<StakingDelegate
181191
header={{
182192
title: selectedValidator.name,
@@ -186,12 +196,12 @@ export const DelegateModal = ({
186196
headerExtra={headerExtra}
187197
delegationItems={[
188198
{
189-
label: 'Your Delegation',
199+
label: "Your Delegation",
190200
tokenAmount: selectedValidator.delegation,
191201
tokenName: coin.symbol,
192202
},
193203
{
194-
label: 'Available to Delegate',
204+
label: "Available to Delegate",
195205
tokenAmount: balance,
196206
tokenName: coin.symbol,
197207
},
@@ -212,7 +222,7 @@ export const DelegateModal = ({
212222
},
213223
partials: [
214224
{
215-
label: '1/2',
225+
label: "1/2",
216226
onClick: () => {
217227
const newAmount = new BigNumber(balance)
218228
.dividedBy(2)
@@ -221,7 +231,7 @@ export const DelegateModal = ({
221231
},
222232
},
223233
{
224-
label: '1/3',
234+
label: "1/3",
225235
onClick: () => {
226236
const newAmount = new BigNumber(balance)
227237
.dividedBy(3)
@@ -231,7 +241,7 @@ export const DelegateModal = ({
231241
},
232242
},
233243
{
234-
label: 'Max',
244+
label: "Max",
235245
onClick: handleMaxClick,
236246
isLoading: isSimulating,
237247
},

examples/stake-tokens/components/staking/StakingSection.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { useChain } from '@interchain-kit/react';
2-
import { Box, Spinner, Text } from '@interchain-ui/react';
3-
import { WalletState } from '@interchain-kit/core'
1+
import { useChain } from "@interchain-kit/react";
2+
import { Box, Spinner, Text } from "@interchain-ui/react";
3+
import { WalletState } from "@interchain-kit/core";
44

5-
import Overview from './Overview';
6-
import { MyValidators } from './MyValidators';
7-
import { AllValidators } from './AllValidators';
8-
import { useStakingData, useValidatorLogos } from '@/hooks';
5+
import Overview from "./Overview";
6+
import { MyValidators } from "./MyValidators";
7+
import { AllValidators } from "./AllValidators";
8+
import { useStakingData, useValidatorLogos } from "@/hooks";
99

1010
export const StakingSection = ({ chainName }: { chainName: string }) => {
1111
const { status } = useChain(chainName);
@@ -15,6 +15,8 @@ export const StakingSection = ({ chainName }: { chainName: string }) => {
1515
data?.allValidators || []
1616
);
1717

18+
const isChainDataLoading = isFetchingLogos || !data;
19+
1820
return (
1921
<Box my="$16">
2022
{status !== WalletState.Connected ? (
@@ -35,7 +37,7 @@ export const StakingSection = ({ chainName }: { chainName: string }) => {
3537
justifyContent="center"
3638
alignItems="center"
3739
>
38-
<Spinner size="$7xl" />
40+
<Spinner size="$7xl" color="$primary" />
3941
</Box>
4042
) : (
4143
<>

0 commit comments

Comments
 (0)