Skip to content

Commit

Permalink
Merge pull request #82 from terra-money/feature/fixes
Browse files Browse the repository at this point in the history
Feature/fixes
  • Loading branch information
simke9445 authored Sep 12, 2023
2 parents 09c0656 + 6660b82 commit 2542a16
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 140 deletions.
15 changes: 12 additions & 3 deletions apps/shared/libs/transactions/TransactionsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import thunk from 'redux-thunk';
import { transactionsReducer } from './transactionsReducer';
import { TxDispatch, trackTxAction } from './actions';
import { TxState } from './TxState';
import { createContext, useContext, useEffect, useMemo, useState } from 'react';
import { createContext, useContext, useEffect, useMemo, useRef, useState } from 'react';
import { LocalStorageTxStore } from './storage/LocalStorageTxStore';
import { createTxStoreMiddleware } from './storage';
import { pendingSubject, completedSubject, cancelledSubject, failedSubject } from './rx';
import { CompletedTransaction, FailedTransaction, PendingTransaction, TransactionStatus } from './types';
import { UIElementProps } from '../../components';
import { useChainSelector } from '../../hooks';
import { LCDClient } from '@terra-money/feather.js';

const storage = new LocalStorageTxStore('__tx_store');

Expand Down Expand Up @@ -56,7 +57,7 @@ const TransactionsProvider = (props: TransactionsProviderProps) => {

const [{ onPending, onCancelled, onCompleted, onFailed }, setEventHandlers] = useState<TxEventHandlers>({});

const { lcd } = useChainSelector();
const { lcd, selectedChainId } = useChainSelector();

const value = useThunkReducer(transactionsReducer, initialState, (state) => {
return {
Expand All @@ -66,13 +67,21 @@ const TransactionsProvider = (props: TransactionsProviderProps) => {
};
});

const lcdRef = useRef<LCDClient>(lcd);
const selectedChainIdRef = useRef<string>(selectedChainId);

useEffect(() => {
lcdRef.current = lcd;
selectedChainIdRef.current = selectedChainId;
}, [lcd, selectedChainId]);

// update the tracking status of the outstanding txs
useEffect(() => {
const [state, dispatch] = value;
if (state.initialized) {
state.transactions.forEach((transaction) => {
if (transaction.status === TransactionStatus.Pending) {
dispatch(trackTxAction(transaction.txHash, lcd));
dispatch(trackTxAction(transaction.txHash, lcdRef, selectedChainIdRef));
}
});
}
Expand Down
112 changes: 0 additions & 112 deletions apps/shared/libs/transactions/TxBuilder.ts

This file was deleted.

9 changes: 7 additions & 2 deletions apps/shared/libs/transactions/actions/addTxAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { ActionType, TxAsyncThunkAction } from '.';
import { TransactionPayload } from '../types';
import { trackTx } from './trackTx';

const addTxAction = (txHash: string, payload: TransactionPayload, lcd: LCDClient): TxAsyncThunkAction => {
const addTxAction = (
txHash: string,
payload: TransactionPayload,
lcdRef: React.MutableRefObject<LCDClient>,
chainIdRef: React.MutableRefObject<string>
): TxAsyncThunkAction => {
return async (dispatch, getState, args) => {
dispatch({
type: ActionType.Add,
Expand All @@ -13,7 +18,7 @@ const addTxAction = (txHash: string, payload: TransactionPayload, lcd: LCDClient
},
});

await trackTx(txHash, lcd, dispatch, getState, args);
await trackTx(txHash, lcdRef, dispatch, getState, args, chainIdRef);
};
};

Expand Down
13 changes: 8 additions & 5 deletions apps/shared/libs/transactions/actions/pollTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@ export class TxTimeoutError extends Error {
}

export const pollTx = async function (
lcd: LCDClient,
lcdRef: React.MutableRefObject<LCDClient>,
txHash: string,
cancellationToken: CancellationToken = None
cancellationToken: CancellationToken = None,
chainIdRef: React.MutableRefObject<string>
): Promise<TxInfo | Error> {
const timeout = Date.now() + 20 * 1000;

while (Date.now() < timeout && cancellationToken.cancelled() === false) {
try {
// TODO: temporary, updated with f(selected_chain, selected_network)
const resp = await lcd.tx.txInfo(txHash, 'pisco-1');
const lcd = lcdRef.current;
const chainId = chainIdRef.current;

const resp = await lcd.tx.txInfo(txHash, chainId);

if (resp.code !== 0) {
throw new TerraTxError(resp);
Expand All @@ -36,7 +39,7 @@ export const pollTx = async function (
return error;
}

if ([400, 404].includes(error.response.status)) {
if ([400, 404].includes(error?.response?.status)) {
// the tx was not yet found so try again after a delay
await sleep(500, cancellationToken);
continue;
Expand Down
7 changes: 4 additions & 3 deletions apps/shared/libs/transactions/actions/trackTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import { pollTx } from './pollTx';

const trackTx = async (
txHash: string,
lcd: LCDClient,
lcdRef: React.MutableRefObject<LCDClient>,
dispatch: TxDispatch,
getState: () => TxState,
args: TxThunkArgument
args: TxThunkArgument,
chainIdRef: React.MutableRefObject<string>
) => {
const notify = (subject: BehaviorSubject<Transaction>) => {
const transaction = find(getState(), txHash);
Expand All @@ -33,7 +34,7 @@ const trackTx = async (
}
});

const txInfoOrError = await pollTx(lcd, txHash, cancellationToken);
const txInfoOrError = await pollTx(lcdRef, txHash, cancellationToken, chainIdRef);

unsubscribe.unsubscribe();

Expand Down
8 changes: 6 additions & 2 deletions apps/shared/libs/transactions/actions/trackTxAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import { TransactionStatus } from '../types';
import { find } from '../utils/find';
import { trackTx } from './trackTx';

const trackTxAction = (txHash: string, lcd: LCDClient): TxAsyncThunkAction => {
const trackTxAction = (
txHash: string,
lcdRef: React.MutableRefObject<LCDClient>,
chainIdRef: React.MutableRefObject<string>
): TxAsyncThunkAction => {
return async (dispatch, getState, args) => {
const transaction = find(getState(), txHash);
if (transaction && transaction.status === TransactionStatus.Pending) {
await trackTx(txHash, lcd, dispatch, getState, args);
await trackTx(txHash, lcdRef, dispatch, getState, args, chainIdRef);
}
};
};
Expand Down
1 change: 0 additions & 1 deletion apps/shared/libs/transactions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ export * from './TransactionsProvider';
export * from './useTransaction';
export * from './useTransactions';
export * from './useTx';
export * from './TxBuilder';
export * from './useTransactionSubscribers';
15 changes: 13 additions & 2 deletions apps/shared/libs/transactions/useTx.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { CreateTxOptions } from '@terra-money/feather.js';
import { CreateTxOptions, LCDClient } from '@terra-money/feather.js';
import { useAsyncFn } from 'react-use';
import { useTransactionsContext } from '.';
import { addTxAction } from './actions';
import { PostResponse } from '@terra-money/wallet-kit';
import { TransactionPayload, TransactionStatus } from './types';
import { failedSubject } from './rx';
import { LocalWallet, useLocalWallet, useRefCallback } from '../../hooks';
import { useEffect, useRef } from 'react';

type TxOrFactory<Options> =
| CreateTxOptions
Expand All @@ -26,6 +27,16 @@ const useTx = <Options>(

const wallet = useLocalWallet();

const { chainId, lcd } = wallet;

const lcdRef = useRef<LCDClient>(lcd);
const chainIdRef = useRef<string>(chainId);

useEffect(() => {
lcdRef.current = lcd;
chainIdRef.current = chainId;
}, [lcd, chainId]);

const txCallback = useRefCallback(
async (options: Options) => {
if (wallet === undefined) {
Expand Down Expand Up @@ -67,7 +78,7 @@ const useTx = <Options>(
// however we are displaying a pending operation status so
// we really want the response to complete when the tx has been
// submitted to the mempool
const completion = dispatch(addTxAction(resp.txhash, payload, wallet.lcd));
const completion = dispatch(addTxAction(resp.txhash, payload, lcdRef, chainIdRef));

if (useTxOptions.waitForCompletion) {
await completion;
Expand Down
2 changes: 1 addition & 1 deletion apps/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@mui/material": "^5.10.2",
"@terra-money/feather.js": "^1.0.11",
"@terra-money/wallet-kit": "^1.0.11",
"@terra-money/warp-sdk": "^0.1.52",
"@terra-money/warp-sdk": "^0.1.53",
"assert": "^2.0.0",
"big.js": "^6.2.1",
"buffer": "^6.0.3",
Expand Down
2 changes: 1 addition & 1 deletion apps/warp-protocol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@popperjs/core": "^2.11.6",
"@terra-money/feather.js": "^1.0.11",
"@terra-money/wallet-kit": "^1.0.11",
"@terra-money/warp-sdk": "^0.1.52",
"@terra-money/warp-sdk": "^0.1.53",
"assert": "^2.0.0",
"big.js": "^6.2.1",
"brace": "^0.11.1",
Expand Down
2 changes: 1 addition & 1 deletion indexers/warp-protocol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@aws-sdk/client-dynamodb": "^3.159.0",
"@aws-sdk/util-dynamodb": "^3.159.0",
"@terra-money/feather.js": "^1.0.11",
"@terra-money/warp-sdk": "^0.1.52",
"@terra-money/warp-sdk": "^0.1.53",
"@types/node": "^16.11.56",
"axios": "^1.1.2",
"big.js": "^6.2.1",
Expand Down
14 changes: 7 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4322,7 +4322,7 @@ __metadata:
"@mui/material": ^5.10.2
"@terra-money/feather.js": ^1.0.11
"@terra-money/wallet-kit": ^1.0.11
"@terra-money/warp-sdk": ^0.1.52
"@terra-money/warp-sdk": ^0.1.53
"@testing-library/jest-dom": ^5.16.5
"@testing-library/react": ^13.3.0
"@testing-library/user-event": ^13.5.0
Expand Down Expand Up @@ -4468,9 +4468,9 @@ __metadata:
languageName: node
linkType: hard

"@terra-money/warp-sdk@npm:^0.1.52":
version: 0.1.52
resolution: "@terra-money/warp-sdk@npm:0.1.52"
"@terra-money/warp-sdk@npm:^0.1.53":
version: 0.1.53
resolution: "@terra-money/warp-sdk@npm:0.1.53"
dependencies:
"@terra-money/feather.js": ^1.0.11
"@types/node": ^16.11.56
Expand All @@ -4483,7 +4483,7 @@ __metadata:
lodash: ^4.17.21
pino: ^8.4.2
typescript: ^4.8.2
checksum: 3749d5f3d62a9b06f2e9f6ee00a61e4715b11bb75e93bf5bbb9b67aae1b620fd665e9dd312f3f9da1b0e916d8a0dcc68077d326e833f2fba2875232a94836968
checksum: 1758197ad3c8cee2f4b730015f5df1ac87472b37a2f4da7d53520e81d8330f59e5237445a0dd6afbe6d995efc5fb0f991340d3f3f60b1edbe45e807ade407fed
languageName: node
linkType: hard

Expand Down Expand Up @@ -5406,7 +5406,7 @@ __metadata:
"@popperjs/core": ^2.11.6
"@terra-money/feather.js": ^1.0.11
"@terra-money/wallet-kit": ^1.0.11
"@terra-money/warp-sdk": ^0.1.52
"@terra-money/warp-sdk": ^0.1.53
"@testing-library/jest-dom": ^5.16.5
"@testing-library/react": ^13.3.0
"@testing-library/user-event": ^13.5.0
Expand Down Expand Up @@ -5483,7 +5483,7 @@ __metadata:
"@aws-sdk/client-dynamodb": ^3.159.0
"@aws-sdk/util-dynamodb": ^3.159.0
"@terra-money/feather.js": ^1.0.11
"@terra-money/warp-sdk": ^0.1.52
"@terra-money/warp-sdk": ^0.1.53
"@types/d3-array": ^3.0.3
"@types/node": ^16.11.56
axios: ^1.1.2
Expand Down

0 comments on commit 2542a16

Please sign in to comment.