Skip to content

Commit

Permalink
chore: add txStatuses to BridgeStatusController
Browse files Browse the repository at this point in the history
  • Loading branch information
infiniteflower committed Oct 16, 2024
1 parent dcdd07a commit d79e23d
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 25 deletions.
26 changes: 11 additions & 15 deletions app/scripts/controllers/bridge-status/bridge-status-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import {
BridgeStatusControllerState,
BridgeStatusControllerMessenger,
StatusRequest,
} from './types';
import { fetchBridgeTxStatus } from './utils';

Expand All @@ -24,15 +25,7 @@ export default class BridgeStatusController extends BaseController<
{ bridgeStatusState: BridgeStatusControllerState },
BridgeStatusControllerMessenger
> {
#provider: Provider;

constructor({
provider,
messenger,
}: {
provider: Provider;
messenger: BridgeStatusControllerMessenger;
}) {
constructor({ messenger }: { messenger: BridgeStatusControllerMessenger }) {
super({
name: BRIDGE_STATUS_CONTROLLER_NAME,
metadata,
Expand All @@ -45,9 +38,6 @@ export default class BridgeStatusController extends BaseController<
`${BRIDGE_STATUS_CONTROLLER_NAME}:getBridgeTxStatus`,
this.getBridgeTxStatus.bind(this),
);

// Assign vars
this.#provider = provider;
}

resetState = () => {
Expand All @@ -58,12 +48,18 @@ export default class BridgeStatusController extends BaseController<
});
};

getBridgeTxStatus = async () => {
getBridgeTxStatus = async (statusRequest: StatusRequest) => {
const { bridgeStatusState } = this.state;

const bridgeStatus = await fetchBridgeTxStatus();
const bridgeStatus = await fetchBridgeTxStatus(statusRequest);
this.update((_state) => {
_state.bridgeStatusState = { ...bridgeStatusState, bridgeStatus };
_state.bridgeStatusState = {
...bridgeStatusState,
txStatuses: {
...bridgeStatusState.txStatuses,
[statusRequest.srcTxHash]: bridgeStatus,
},
};
});
};
}
4 changes: 3 additions & 1 deletion app/scripts/controllers/bridge-status/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ import { BridgeStatusControllerState } from './types';
export const BRIDGE_STATUS_CONTROLLER_NAME = 'BridgeStatusController';

export const DEFAULT_BRIDGE_STATUS_CONTROLLER_STATE: BridgeStatusControllerState =
{};
{
txStatuses: {},
};
19 changes: 12 additions & 7 deletions app/scripts/controllers/bridge-status/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ export interface StatusRequest {
refuel?: boolean; //lifi
}

export interface Asset {
export type Asset = {
chainId: DecChainId;
address: string;
symbol: string;
name: string;
decimals: number;
icon?: string;
}
};

export interface ChainStatus {
export type ChainStatus = {
chainId: DecChainId;
txHash: string;
amount?: string;
token?: Asset;
}
};

export enum StatusTypes {
UNKNOWN = 'UNKNOWN',
Expand All @@ -57,15 +57,15 @@ export enum BridgeId {
STARGATE = 'stargate',
}

export interface StatusResponse {
export type StatusResponse = {
status: StatusTypes;
srcChain: ChainStatus;
destChain?: ChainStatus;
bridge?: BridgeId;
isExpectedToken?: boolean;
isUnrecognizedRouterAddress?: boolean;
refuel?: RefuelStatusResponse;
}
};

export enum FeeType {
METABRIDGE = 'metabridge',
Expand Down Expand Up @@ -135,7 +135,12 @@ export type BridgeHistoryItem = {
targetContractAddress?: string;
};

export type BridgeStatusControllerState = {};
// All fields need to be types not interfaces, same with their children fields
// o/w you get a type error
export type BridgeStatusControllerState = {
txStatuses: Record<string, StatusResponse>;
// txHistory: Record<string, BridgeHistoryItem>;
};

export enum BridgeStatusAction {
GET_BRIDGE_TX_STATUS = 'getBridgeTxStatus',
Expand Down
4 changes: 2 additions & 2 deletions app/scripts/controllers/bridge-status/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export const fetchBridgeTxStatus = async (statusRequest: StatusRequest) => {
// Convert to Extension format

// Return
console.log('fetchBridgeTxStatus', statusRequest);
return 'connected';
console.log('fetchBridgeTxStatus', { statusRequest, rawTxStatus });
return rawTxStatus;
};
28 changes: 28 additions & 0 deletions ui/ducks/bridge-status/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import BridgeStatusController from '../../../app/scripts/controllers/bridge-status/bridge-status-controller';
import {
BridgeStatusAction,
StatusRequest,
} from '../../../app/scripts/controllers/bridge-status/types';
import { forceUpdateMetamaskState } from '../../store/actions';
import { submitRequestToBackground } from '../../store/background-connection';
import { MetaMaskReduxDispatch } from '../../store/store';

const callBridgeStatusControllerMethod = <T extends any[]>(
bridgeAction: BridgeStatusAction,
args?: T,
) => {
return async (dispatch: MetaMaskReduxDispatch) => {
await submitRequestToBackground(bridgeAction, args);
await forceUpdateMetamaskState(dispatch);
};
};

export const getBridgeTxStatus = (statusRequest: StatusRequest) => {
return async (dispatch: MetaMaskReduxDispatch) => {
return dispatch(
callBridgeStatusControllerMethod<
Parameters<BridgeStatusController['getBridgeTxStatus']>
>(BridgeStatusAction.GET_BRIDGE_TX_STATUS, [statusRequest]),
);
};
};
19 changes: 19 additions & 0 deletions ui/ducks/bridge-status/selectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createSelector } from 'reselect';
import { BridgeStatusControllerState } from '../../../app/scripts/controllers/bridge-status/types';

export type BridgeStatusAppState = {
metamask: {
bridgeStatusState: BridgeStatusControllerState;
};
};

export const selectBridgeStatusState = (state: BridgeStatusAppState) =>
state.metamask.bridgeStatusState;

export const selectBridgeTxStatuses = createSelector(
[
selectBridgeStatusState,
(_: BridgeStatusAppState, txHash: string) => txHash,
],
(bridgeStatusState) => bridgeStatusState.txStatuses,
);

0 comments on commit d79e23d

Please sign in to comment.