Skip to content

Commit

Permalink
Update domain name for transaction-api, add a new function "getSmartT…
Browse files Browse the repository at this point in the history
…ransactionByMinedTxHash" (#314)
  • Loading branch information
dan437 authored Apr 10, 2024
1 parent 0baaed5 commit 235f3fc
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 8 deletions.
42 changes: 42 additions & 0 deletions src/SmartTransactionsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,48 @@ describe('SmartTransactionsController', () => {
});
});

describe('getSmartTransactionByMinedTxHash', () => {
it('retrieves a smart transaction by a mined tx hash', () => {
const { smartTransactionsState } = smartTransactionsController.state;
const successfulSmartTransaction = createStateAfterSuccess()[0];
smartTransactionsController.update({
smartTransactionsState: {
...smartTransactionsState,
smartTransactions: {
[CHAIN_IDS.ETHEREUM]: [
successfulSmartTransaction,
] as SmartTransaction[],
},
},
});
const smartTransaction =
smartTransactionsController.getSmartTransactionByMinedTxHash(
successfulSmartTransaction.statusMetadata.minedHash,
);
expect(smartTransaction).toStrictEqual(successfulSmartTransaction);
});

it('returns undefined if there is no smart transaction found by tx hash', () => {
const { smartTransactionsState } = smartTransactionsController.state;
const successfulSmartTransaction = createStateAfterSuccess()[0];
smartTransactionsController.update({
smartTransactionsState: {
...smartTransactionsState,
smartTransactions: {
[CHAIN_IDS.ETHEREUM]: [
successfulSmartTransaction,
] as SmartTransaction[],
},
},
});
const smartTransaction =
smartTransactionsController.getSmartTransactionByMinedTxHash(
'nonStxTxHash',
);
expect(smartTransaction).toBeUndefined();
});
});

describe('isNewSmartTransaction', () => {
it('returns true if it is a new STX', () => {
const actual =
Expand Down
33 changes: 26 additions & 7 deletions src/SmartTransactionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -852,22 +852,41 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
}
}

getTransactions({
addressFrom,
status,
}: {
addressFrom: string;
status: SmartTransactionStatuses;
}): SmartTransaction[] {
#getCurrentSmartTransactions(): SmartTransaction[] {
const { smartTransactions } = this.state.smartTransactionsState;
const { chainId } = this.config;
const currentSmartTransactions = smartTransactions?.[chainId];
if (!currentSmartTransactions || currentSmartTransactions.length === 0) {
return [];
}
return currentSmartTransactions;
}

getTransactions({
addressFrom,
status,
}: {
addressFrom: string;
status: SmartTransactionStatuses;
}): SmartTransaction[] {
const currentSmartTransactions = this.#getCurrentSmartTransactions();
return currentSmartTransactions.filter((stx) => {
return stx.status === status && stx.txParams?.from === addressFrom;
});
}

getSmartTransactionByMinedTxHash(
txHash: string | undefined,
): SmartTransaction | undefined {
if (!txHash) {
return undefined;
}
const currentSmartTransactions = this.#getCurrentSmartTransactions();
return currentSmartTransactions.find((smartTransaction) => {
return (
smartTransaction.statusMetadata?.minedHash?.toLowerCase() ===
txHash.toLowerCase()
);
});
}
}
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const API_BASE_URL = 'https://transaction.metaswap.codefi.network';
export const API_BASE_URL = 'https://transaction.api.cx.metamask.io';
export const CHAIN_IDS = {
ETHEREUM: '0x1',
GOERLI: '0x5',
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ export type SmartTransactionsStatus = {
minedHash: string;
minedTx: SmartTransactionMinedTx;
isSettled: boolean;
duplicated?: boolean;
timedOut?: boolean;
proxied?: boolean;
};

export type SmartTransaction = {
Expand Down

0 comments on commit 235f3fc

Please sign in to comment.