Skip to content

Commit

Permalink
feat: state mutation after successful transactions (#1047)
Browse files Browse the repository at this point in the history
* chore: allow transactions only when there is enough fee

* chore: state mutation after transactions
  • Loading branch information
Teja2045 authored Jan 3, 2024
1 parent f6f5f41 commit 0628160
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 12 deletions.
8 changes: 8 additions & 0 deletions frontend/src/store/features/bank/bankSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export const multiTxns = createAsyncThunk(
);
dispatch(setTxAndHash({ tx, hash: tx.transactionHash }));
if (result?.code === 0) {
dispatch(getBalances({ baseURL: rest, chainID, address }));
return fulfillWithValue({ txHash: result?.transactionHash });
} else {
return rejectWithValue(result?.rawLog);
Expand Down Expand Up @@ -136,6 +137,13 @@ export const txBankSend = createAsyncThunk(
);
dispatch(setTxAndHash({ tx, hash: tx.transactionHash }));
if (result?.code === 0) {
dispatch(
getBalances({
baseURL: data.basicChainInfo.rest,
chainID,
address: data.basicChainInfo.address,
})
);
return fulfillWithValue({ txHash: result?.transactionHash });
} else {
return rejectWithValue(result?.rawLog);
Expand Down
17 changes: 17 additions & 0 deletions frontend/src/store/features/distribution/distributionSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { TxStatus } from '@/types/enums';
import { GAS_FEE } from '@/utils/constants';
import { NewTransaction } from '@/utils/transaction';
import { addTransactions } from '../transactionHistory/transactionHistorySlice';
import { getBalances } from '../bank/bankSlice';

const initialState: DistributionStoreInitialState = {
chains: {},
Expand Down Expand Up @@ -69,6 +70,22 @@ export const txWithdrawAllRewards = createAsyncThunk(
);

if (result?.code === 0) {
dispatch(
getBalances({
baseURL: data.rest,
chainID: data.chainID,
address: data.address,
})
);

dispatch(
getDelegatorTotalRewards({
baseURL: data.rest,
address: data.address,
chainID: data.chainID,
denom: data.denom,
})
);
dispatch(
setTxAndHash({
hash: result?.transactionHash,
Expand Down
59 changes: 47 additions & 12 deletions frontend/src/store/features/staking/stakeSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { NewTransaction } from '@/utils/transaction';
import { addTransactions } from '../transactionHistory/transactionHistorySlice';
import { setError, setTxAndHash } from '../common/commonSlice';
import { Unbonding } from '@/txns/staking/unbonding';
import { getDelegatorTotalRewards } from '../distribution/distributionSlice';
import { getBalances } from '../bank/bankSlice';

interface Chain {
validators: Validators;
Expand Down Expand Up @@ -144,28 +146,25 @@ export const txRestake = createAsyncThunk(
data: TxReStakeInputs,
{ rejectWithValue, fulfillWithValue, dispatch }
) => {
const { chainID, address, rest, aminoConfig, prefix, cosmosAddress } =
data.basicChainInfo;
try {
const result = await signAndBroadcast(
data.basicChainInfo.chainID,
data.basicChainInfo.aminoConfig,
data.basicChainInfo.prefix,
chainID,
aminoConfig,
prefix,
data.msgs,
399999 + Math.ceil(399999 * 0.1 * (data.msgs?.length || 1)),
data.memo,
`${data.feeAmount}${data.denom}`,
data.basicChainInfo.rest,
rest,
data.feegranter?.length > 0 ? data.feegranter : undefined
);
const tx = NewTransaction(
result,
data.msgs,
data.basicChainInfo.chainID,
data.basicChainInfo.address
);
const tx = NewTransaction(result, data.msgs, chainID, address);
dispatch(
addTransactions({
chainID: data.basicChainInfo.chainID,
address: data.basicChainInfo.cosmosAddress,
chainID,
address: cosmosAddress,
transactions: [tx],
})
);
Expand All @@ -176,6 +175,21 @@ export const txRestake = createAsyncThunk(
})
);
if (result?.code === 0) {
dispatch(
getDelegatorTotalRewards({
baseURL: rest,
address: address,
chainID: chainID,
denom: data.denom,
})
);
dispatch(
getDelegations({
baseURL: rest,
address: address,
chainID: chainID,
})
);
return fulfillWithValue({ txHash: result?.transactionHash });
} else {
return rejectWithValue(result?.rawLog);
Expand Down Expand Up @@ -242,6 +256,13 @@ export const txDelegate = createAsyncThunk(
chainID: data.basicChainInfo.chainID,
})
);
dispatch(
getBalances({
baseURL: data.basicChainInfo.baseURL,
chainID: data.basicChainInfo.chainID,
address: data.delegator,
})
);
return fulfillWithValue({ txHash: result?.transactionHash });
} else {
return rejectWithValue(result?.rawLog);
Expand Down Expand Up @@ -367,6 +388,20 @@ export const txUnDelegate = createAsyncThunk(
);

if (result?.code === 0) {
dispatch(
getDelegations({
baseURL: data.basicChainInfo.rest,
address: data.basicChainInfo.address,
chainID: data.basicChainInfo.chainID,
})
);
dispatch(
getUnbonding({
baseURL: data.basicChainInfo.rest,
address: data.basicChainInfo.address,
chainID: data.basicChainInfo.chainID,
})
);
return fulfillWithValue({ txHash: result?.transactionHash });
} else {
return rejectWithValue(result?.rawLog);
Expand Down

1 comment on commit 0628160

@vercel
Copy link

@vercel vercel bot commented on 0628160 Jan 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

dev-resolute – ./frontend

dev-resolute-git-dev-my-apps-projects.vercel.app
dev-resolute-my-apps-projects.vercel.app
beta-resolute.vitwit.com

Please sign in to comment.