Skip to content

Commit

Permalink
chore: modify polling and clean up tests (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
meppsilon authored Nov 5, 2021
1 parent 796dacd commit c46e762
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 48 deletions.
75 changes: 37 additions & 38 deletions src/SmartTransactionsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ jest.mock('ethers', () => ({

getTransactionReceipt = jest.fn(() => ({ blockNumber: '123' }));

getTransaction = jest.fn();
getTransaction = jest.fn(() => ({
maxFeePerGas: { toHexString: () => '0x123' },
maxPriorityFeePerGas: { toHexString: () => '0x123' },
}));

getBlock = jest.fn();
},
Expand Down Expand Up @@ -182,6 +185,14 @@ const createSuccessLivenessApiResponse = () => ({
lastBlock: 123456,
});

const testHistory = [
{
op: 'add',
path: '/swapTokenValue',
value: '0.001',
},
];

const ethereumChainIdDec = CHAIN_IDS_HEX_TO_DEC[CHAIN_IDS.ETHEREUM];

describe('SmartTransactionsController', () => {
Expand All @@ -207,6 +218,10 @@ describe('SmartTransactionsController', () => {
confirmExternalTransaction: confirmExternalMock,
},
});

jest
.spyOn(smartTransactionsController, 'checkPoll')
.mockImplementation(() => ({}));
});

afterEach(async () => {
Expand Down Expand Up @@ -242,40 +257,13 @@ describe('SmartTransactionsController', () => {
});

it('calls poll', () => {
const pollSpy = jest.spyOn(smartTransactionsController, 'poll');
const checkPollSpy = jest.spyOn(smartTransactionsController, 'checkPoll');
networkListener({ provider: { chainId: '2' } } as NetworkState);
expect(pollSpy).toHaveBeenCalled();
expect(checkPollSpy).toHaveBeenCalled();
});
});

describe('poll', () => {
it('is called with interval', async () => {
const interval = 35000;
const pollSpy = jest.spyOn(smartTransactionsController, 'poll');
const updateSmartTransactionsSpy = jest.spyOn(
smartTransactionsController,
'updateSmartTransactions',
);
expect(pollSpy).toHaveBeenCalledTimes(0);
expect(updateSmartTransactionsSpy).toHaveBeenCalledTimes(0);
networkListener({
provider: { chainId: CHAIN_IDS.ETHEREUM },
} as NetworkState);
expect(pollSpy).toHaveBeenCalledTimes(1);
expect(updateSmartTransactionsSpy).toHaveBeenCalledTimes(1);
await smartTransactionsController.stop();
jest.useFakeTimers();
await smartTransactionsController.poll(interval);
expect(pollSpy).toHaveBeenCalledTimes(2);
expect(updateSmartTransactionsSpy).toHaveBeenCalledTimes(2);
jest.advanceTimersByTime(interval);
expect(pollSpy).toHaveBeenCalledTimes(3);
expect(updateSmartTransactionsSpy).toHaveBeenCalledTimes(3);
await smartTransactionsController.stop();
jest.clearAllTimers();
jest.useRealTimers();
});

it('does not call updateSmartTransactions on unsupported networks', async () => {
const updateSmartTransactionsSpy = jest.spyOn(
smartTransactionsController,
Expand Down Expand Up @@ -350,10 +338,12 @@ describe('SmartTransactionsController', () => {
.get(`/networks/${ethereumChainIdDec}/batchStatus?uuids=uuid1`)
.reply(200, pendingBatchStatusApiResponse);
await smartTransactionsController.fetchSmartTransactionsStatus(uuids);
const pendingState = createStateAfterPending()[0];
const pendingTransaction = { ...pendingState, history: [pendingState] };
expect(smartTransactionsController.state).toStrictEqual({
smartTransactionsState: {
smartTransactions: {
[CHAIN_IDS.ETHEREUM]: createStateAfterPending(),
[CHAIN_IDS.ETHEREUM]: [pendingTransaction],
},
userOptIn: undefined,
},
Expand All @@ -376,14 +366,14 @@ describe('SmartTransactionsController', () => {
.get(`/networks/${ethereumChainIdDec}/batchStatus?uuids=uuid2`)
.reply(200, successBatchStatusApiResponse);
await smartTransactionsController.fetchSmartTransactionsStatus(uuids);
expect(
smartTransactionsController.state.smartTransactionsState,
).toStrictEqual({
const successState = createStateAfterSuccess()[0];
const successTransaction = { ...successState, history: [successState] };
expect(smartTransactionsController.state).toStrictEqual({
smartTransactionsState: {
smartTransactions: {
[CHAIN_IDS.ETHEREUM]: [
...createStateAfterPending(),
...createStateAfterSuccess(),
...[successTransaction],
],
},
userOptIn: undefined,
Expand All @@ -405,7 +395,10 @@ describe('SmartTransactionsController', () => {

describe('updateSmartTransaction', () => {
it('updates smart transaction based on uuid', () => {
const pendingStx = createStateAfterPending()[0];
const pendingStx = {
...createStateAfterPending()[0],
history: testHistory,
};
const { smartTransactionsState } = smartTransactionsController.state;
smartTransactionsController.update({
smartTransactionsState: {
Expand Down Expand Up @@ -435,7 +428,10 @@ describe('SmartTransactionsController', () => {
smartTransactionsController,
'confirmSmartTransaction',
);
const pendingStx = createStateAfterPending()[0];
const pendingStx = {
...createStateAfterPending()[0],
history: testHistory,
};
smartTransactionsController.update({
smartTransactionsState: {
...smartTransactionsState,
Expand All @@ -457,7 +453,10 @@ describe('SmartTransactionsController', () => {

describe('confirmSmartTransaction', () => {
it('calls confirm external transaction', async () => {
const successfulStx = createStateAfterSuccess()[0];
const successfulStx = {
...createStateAfterSuccess()[0],
history: testHistory,
};
await smartTransactionsController.confirmSmartTransaction(
successfulStx as SmartTransaction,
);
Expand Down
30 changes: 20 additions & 10 deletions src/SmartTransactionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,24 @@ export default class SmartTransactionsController extends BaseController<
const { chainId } = newProvider;
this.configure({ chainId });
this.initializeSmartTransactionsForChainId();
this.poll();
this.checkPoll(this.state);
this.ethersProvider = new ethers.providers.Web3Provider(provider);
});

this.poll();
this.subscribe((currentState: any) => this.checkPoll(currentState));
}

checkPoll(state: any) {
const { smartTransactions } = state.smartTransactionsState;
const currentSmartTransactions = smartTransactions[this.config.chainId];
const pendingTransactions = currentSmartTransactions?.filter(
isSmartTransactionPending,
);
if (!this.timeoutHandle && pendingTransactions?.length > 0) {
this.poll();
} else if (this.timeoutHandle && pendingTransactions?.length === 0) {
this.stop();
}
}

initializeSmartTransactionsForChainId() {
Expand Down Expand Up @@ -158,8 +171,8 @@ export default class SmartTransactionsController extends BaseController<
return;
}
await safelyExecute(() => this.updateSmartTransactions());
this.timeoutHandle = setTimeout(() => {
this.poll(this.config.interval);
this.timeoutHandle = setInterval(() => {
safelyExecute(() => this.updateSmartTransactions());
}, this.config.interval);
}

Expand Down Expand Up @@ -243,13 +256,11 @@ export default class SmartTransactionsController extends BaseController<
const currentSmartTransactions = smartTransactions?.[chainId];

const transactionsToUpdate: string[] = currentSmartTransactions
.filter((smartTransaction) => isSmartTransactionPending(smartTransaction))
.filter(isSmartTransactionPending)
.map((smartTransaction) => smartTransaction.uuid);

if (transactionsToUpdate.length > 0) {
this.fetchSmartTransactionsStatus(transactionsToUpdate);
} else {
this.stop();
}
}

Expand All @@ -260,8 +271,8 @@ export default class SmartTransactionsController extends BaseController<
txHash,
);
const transaction = await this.ethersProvider.getTransaction(txHash);
const maxFeePerGas = transaction.maxFeePerGas.toHexString();
const maxPriorityFeePerGas = transaction.maxPriorityFeePerGas.toHexString();
const maxFeePerGas = transaction.maxFeePerGas?.toHexString();
const maxPriorityFeePerGas = transaction.maxPriorityFeePerGas?.toHexString();
if (transactionReceipt?.blockNumber) {
const blockData = await this.ethersProvider.getBlock(
transactionReceipt?.blockNumber,
Expand Down Expand Up @@ -434,7 +445,6 @@ export default class SmartTransactionsController extends BaseController<
});

setTimeout(() => {
console.log('reset cancellable');
this.updateSmartTransaction({
uuid: data.uuid,
cancellable: false,
Expand Down

0 comments on commit c46e762

Please sign in to comment.