Skip to content

Commit 235f3fc

Browse files
authored
Update domain name for transaction-api, add a new function "getSmartTransactionByMinedTxHash" (#314)
1 parent 0baaed5 commit 235f3fc

File tree

4 files changed

+72
-8
lines changed

4 files changed

+72
-8
lines changed

src/SmartTransactionsController.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,48 @@ describe('SmartTransactionsController', () => {
11861186
});
11871187
});
11881188

1189+
describe('getSmartTransactionByMinedTxHash', () => {
1190+
it('retrieves a smart transaction by a mined tx hash', () => {
1191+
const { smartTransactionsState } = smartTransactionsController.state;
1192+
const successfulSmartTransaction = createStateAfterSuccess()[0];
1193+
smartTransactionsController.update({
1194+
smartTransactionsState: {
1195+
...smartTransactionsState,
1196+
smartTransactions: {
1197+
[CHAIN_IDS.ETHEREUM]: [
1198+
successfulSmartTransaction,
1199+
] as SmartTransaction[],
1200+
},
1201+
},
1202+
});
1203+
const smartTransaction =
1204+
smartTransactionsController.getSmartTransactionByMinedTxHash(
1205+
successfulSmartTransaction.statusMetadata.minedHash,
1206+
);
1207+
expect(smartTransaction).toStrictEqual(successfulSmartTransaction);
1208+
});
1209+
1210+
it('returns undefined if there is no smart transaction found by tx hash', () => {
1211+
const { smartTransactionsState } = smartTransactionsController.state;
1212+
const successfulSmartTransaction = createStateAfterSuccess()[0];
1213+
smartTransactionsController.update({
1214+
smartTransactionsState: {
1215+
...smartTransactionsState,
1216+
smartTransactions: {
1217+
[CHAIN_IDS.ETHEREUM]: [
1218+
successfulSmartTransaction,
1219+
] as SmartTransaction[],
1220+
},
1221+
},
1222+
});
1223+
const smartTransaction =
1224+
smartTransactionsController.getSmartTransactionByMinedTxHash(
1225+
'nonStxTxHash',
1226+
);
1227+
expect(smartTransaction).toBeUndefined();
1228+
});
1229+
});
1230+
11891231
describe('isNewSmartTransaction', () => {
11901232
it('returns true if it is a new STX', () => {
11911233
const actual =

src/SmartTransactionsController.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -852,22 +852,41 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
852852
}
853853
}
854854

855-
getTransactions({
856-
addressFrom,
857-
status,
858-
}: {
859-
addressFrom: string;
860-
status: SmartTransactionStatuses;
861-
}): SmartTransaction[] {
855+
#getCurrentSmartTransactions(): SmartTransaction[] {
862856
const { smartTransactions } = this.state.smartTransactionsState;
863857
const { chainId } = this.config;
864858
const currentSmartTransactions = smartTransactions?.[chainId];
865859
if (!currentSmartTransactions || currentSmartTransactions.length === 0) {
866860
return [];
867861
}
862+
return currentSmartTransactions;
863+
}
868864

865+
getTransactions({
866+
addressFrom,
867+
status,
868+
}: {
869+
addressFrom: string;
870+
status: SmartTransactionStatuses;
871+
}): SmartTransaction[] {
872+
const currentSmartTransactions = this.#getCurrentSmartTransactions();
869873
return currentSmartTransactions.filter((stx) => {
870874
return stx.status === status && stx.txParams?.from === addressFrom;
871875
});
872876
}
877+
878+
getSmartTransactionByMinedTxHash(
879+
txHash: string | undefined,
880+
): SmartTransaction | undefined {
881+
if (!txHash) {
882+
return undefined;
883+
}
884+
const currentSmartTransactions = this.#getCurrentSmartTransactions();
885+
return currentSmartTransactions.find((smartTransaction) => {
886+
return (
887+
smartTransaction.statusMetadata?.minedHash?.toLowerCase() ===
888+
txHash.toLowerCase()
889+
);
890+
});
891+
}
873892
}

src/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const API_BASE_URL = 'https://transaction.metaswap.codefi.network';
1+
export const API_BASE_URL = 'https://transaction.api.cx.metamask.io';
22
export const CHAIN_IDS = {
33
ETHEREUM: '0x1',
44
GOERLI: '0x5',

src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ export type SmartTransactionsStatus = {
6767
minedHash: string;
6868
minedTx: SmartTransactionMinedTx;
6969
isSettled: boolean;
70+
duplicated?: boolean;
71+
timedOut?: boolean;
72+
proxied?: boolean;
7073
};
7174

7275
export type SmartTransaction = {

0 commit comments

Comments
 (0)