Skip to content

Commit

Permalink
Merge pull request #532 from logion-network/feature/fix-transfer-close
Browse files Browse the repository at this point in the history
Fix transfer and refactor close
  • Loading branch information
gdethier authored Nov 22, 2023
2 parents 117b9a3 + bb624ff commit 2455cc0
Show file tree
Hide file tree
Showing 8 changed files with 818 additions and 279 deletions.
2 changes: 1 addition & 1 deletion src/ExtrinsicSubmissionStateView.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useLogionChain } from './logion-chain/LogionChainContext';
import { useLogionChain } from './logion-chain';
import ExtrinsicSubmissionResult from './ExtrinsicSubmissionResult';

export interface Props {
Expand Down
126 changes: 126 additions & 0 deletions src/common/CommonContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export interface CommonContext {
viewer: Viewer;
setViewer: ((viewer: Viewer) => void) | null;
backendConfig: ((legalOfficerAddress: string | undefined) => BackendConfig);
expectNewTransactionState: ExpectNewTransactionState;
expectNewTransaction: () => void;
stopExpectNewTransaction: () => void;
}

interface FullCommonContext extends CommonContext {
Expand All @@ -49,6 +52,18 @@ const DEFAULT_BACKEND_CONFIG: BackendConfig = {
}
};

export enum ExpectNewTransactionStatus {
IDLE,
WAITING_NEW_TRANSACTION,
DONE
}

export interface ExpectNewTransactionState {
status: ExpectNewTransactionStatus;
minExpectedTransactions?: number;
refreshCount: number;
}

function initialContextValue(): FullCommonContext {
return {
dataAddress: null,
Expand All @@ -62,6 +77,12 @@ function initialContextValue(): FullCommonContext {
viewer: "User",
setViewer: null,
backendConfig: () => DEFAULT_BACKEND_CONFIG,
expectNewTransactionState: {
status: ExpectNewTransactionStatus.IDLE,
refreshCount: 0,
},
expectNewTransaction: () => {},
stopExpectNewTransaction: () => {},
}
}

Expand All @@ -81,6 +102,10 @@ type ActionType = 'FETCH_IN_PROGRESS'
| 'MUTATE_BALANCE_STATE'
| 'SET_SET_VIEWER'
| 'SET_VIEWER'
| 'EXPECT_NEW_TRANSACTION'
| 'SET_EXPECT_NEW_TRANSACTION'
| 'STOP_EXPECT_NEW_TRANSACTION'
| 'SET_STOP_EXPECT_NEW_TRANSACTION'
;

interface Action {
Expand All @@ -101,8 +126,13 @@ interface Action {
viewer?: Viewer;
setViewer?: (viewer: Viewer) => void;
backendConfig?: ((legalOfficerAddress: string | undefined) => BackendConfig);
expectNewTransaction?: () => void;
stopExpectNewTransaction?: () => void;
}

const MAX_REFRESH_COUNT = 12;
const REFRESH_PERIOD_MS = 3000;

const reducer: Reducer<FullCommonContext, Action> = (state: FullCommonContext, action: Action): FullCommonContext => {
switch (action.type) {
case 'FETCH_IN_PROGRESS':
Expand All @@ -116,13 +146,24 @@ const reducer: Reducer<FullCommonContext, Action> = (state: FullCommonContext, a
if(action.dataAddress === state.dataAddress) {
const nodesUp = action.nodesUp !== undefined ? action.nodesUp : state.nodesUp;
const nodesDown = action.nodesDown !== undefined ? action.nodesDown : state.nodesDown;
const expectNewTransactionState = buildNextExpectNewTransactionState(state.expectNewTransactionState, state.balanceState);
if(expectNewTransactionState.status === ExpectNewTransactionStatus.WAITING_NEW_TRANSACTION) {
console.log(`Scheduling retry #${expectNewTransactionState.refreshCount} (${state.balanceState!.transactions?.length} < ${state.expectNewTransactionState.minExpectedTransactions!})...`);
window.setTimeout(() => {
console.log(`Try #${ expectNewTransactionState.refreshCount }...`);
state.refresh(false);
}, REFRESH_PERIOD_MS);
} else {
console.log(`Stopped polling after ${state.expectNewTransactionState.refreshCount} retries (${state.balanceState?.transactions.length} >= ${state.expectNewTransactionState.minExpectedTransactions!})`);
}
return {
...state,
balanceState: action.balanceState,
availableLegalOfficers: action.availableLegalOfficers!,
backendConfig: action.backendConfig!,
nodesUp,
nodesDown,
expectNewTransactionState,
};
} else {
return state;
Expand Down Expand Up @@ -162,12 +203,71 @@ const reducer: Reducer<FullCommonContext, Action> = (state: FullCommonContext, a
...state,
viewer: action.viewer!,
};
case 'SET_EXPECT_NEW_TRANSACTION':
return {
...state,
expectNewTransaction: action.expectNewTransaction!,
}
case 'EXPECT_NEW_TRANSACTION':
if(state.expectNewTransactionState.status === ExpectNewTransactionStatus.IDLE || state.expectNewTransactionState.status === ExpectNewTransactionStatus.DONE) {
window.setTimeout(() => {
console.log(`Try #1...`);
state.refresh(false);
}, REFRESH_PERIOD_MS);
return {
...state,
expectNewTransactionState: {
status: ExpectNewTransactionStatus.WAITING_NEW_TRANSACTION,
minExpectedTransactions: state.balanceState ? state.balanceState.transactions.length + 1 : 1,
refreshCount: 1,
},
}
} else {
return state;
}
case 'SET_STOP_EXPECT_NEW_TRANSACTION':
return {
...state,
stopExpectNewTransaction: action.stopExpectNewTransaction!,
};
case 'STOP_EXPECT_NEW_TRANSACTION':
return {
...state,
expectNewTransactionState: {
status: ExpectNewTransactionStatus.IDLE,
minExpectedTransactions: undefined,
refreshCount: 0,
},
};
default:
/* istanbul ignore next */
throw new Error(`Unknown type: ${action.type}`);
}
}

function buildNextExpectNewTransactionState(current: ExpectNewTransactionState, balanceState?: BalanceState): ExpectNewTransactionState {
if(current.status === ExpectNewTransactionStatus.WAITING_NEW_TRANSACTION) {
if(balanceState
&& (balanceState.transactions.length >= current.minExpectedTransactions!
|| current.refreshCount >= MAX_REFRESH_COUNT)) {
return {
status: ExpectNewTransactionStatus.DONE,
minExpectedTransactions: undefined,
refreshCount: 0,
};
} else {
const refreshCount = current.refreshCount + 1;
return {
status: ExpectNewTransactionStatus.WAITING_NEW_TRANSACTION,
refreshCount,
minExpectedTransactions: current.minExpectedTransactions,
};
}
} else {
return current;
}
}

export function CommonContextProvider(props: Props) {
const { api, client, accounts } = useLogionChain();
const [ contextValue, dispatch ] = useReducer(reducer, initialContextValue());
Expand Down Expand Up @@ -296,6 +396,32 @@ export function CommonContextProvider(props: Props) {
}
}, [ contextValue ]);

const expectNewTransaction = useCallback(() => {
dispatch({ type: "EXPECT_NEW_TRANSACTION" });
}, [ ]);

useEffect(() => {
if(contextValue.expectNewTransaction !== expectNewTransaction) {
dispatch({
type: "SET_EXPECT_NEW_TRANSACTION",
expectNewTransaction,
})
}
}, [ contextValue.expectNewTransaction, expectNewTransaction ]);

const stopExpectNewTransaction = useCallback(() => {
dispatch({ type: "STOP_EXPECT_NEW_TRANSACTION" });
}, [ ]);

useEffect(() => {
if(contextValue.stopExpectNewTransaction !== stopExpectNewTransaction) {
dispatch({
type: "SET_STOP_EXPECT_NEW_TRANSACTION",
stopExpectNewTransaction,
})
}
}, [ contextValue.stopExpectNewTransaction, stopExpectNewTransaction ]);

return (
<CommonContextObject.Provider value={contextValue}>
{props.children}
Expand Down
Loading

0 comments on commit 2455cc0

Please sign in to comment.