Skip to content

Commit

Permalink
fix: reset cursor when opening transaction history views (#2060)
Browse files Browse the repository at this point in the history
Co-authored-by: Glitch <66949816+glitch-txs@users.noreply.github.com>
  • Loading branch information
tomiir and glitch-txs committed Mar 27, 2024
1 parent 39c78b8 commit 087725a
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/core/src/controllers/AccountController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export const AccountController = {

resetAccount() {
state.isConnected = false
state.currentTab = 0
state.caipAddress = undefined
state.address = undefined
state.balance = undefined
Expand All @@ -107,6 +108,6 @@ export const AccountController = {
state.profileImage = undefined
state.addressExplorerUrl = undefined
state.smartAccountDeployed = undefined
state.currentTab = 0
state.tokenBalance = []
}
}
5 changes: 5 additions & 0 deletions packages/core/src/controllers/TransactionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export const TransactionsController = {
SnackController.showError('Failed to fetch transactions')
state.loading = false
state.empty = true
state.next = undefined
}
},

Expand Down Expand Up @@ -125,6 +126,10 @@ export const TransactionsController = {
})
},

clearCursor() {
state.next = undefined
},

resetTransactions() {
state.transactions = []
state.transactionsByYear = {}
Expand Down
16 changes: 14 additions & 2 deletions packages/core/tests/controllers/AccountController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const profileImage = 'https://ipfs.com/0x123.png'
// -- Tests --------------------------------------------------------------------
describe('AccountController', () => {
it('should have valid default state', () => {
expect(AccountController.state).toEqual({ isConnected: false })
expect(AccountController.state).toEqual({ isConnected: false, tokenBalance: [], currentTab: 0 })
})

it('should update state correctly on setIsConnected()', () => {
Expand Down Expand Up @@ -43,6 +43,18 @@ describe('AccountController', () => {

it('should update state correctly on resetAccount()', () => {
AccountController.resetAccount()
expect(AccountController.state).toEqual({ isConnected: false })
expect(AccountController.state).toEqual({
isConnected: false,
currentTab: 0,
caipAddress: undefined,
address: undefined,
balance: undefined,
balanceSymbol: undefined,
profileName: undefined,
profileImage: undefined,
addressExplorerUrl: undefined,
smartAccountDeployed: undefined,
tokenBalance: []
})
})
})
9 changes: 7 additions & 2 deletions packages/core/tests/controllers/ConnectorController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ const metamaskConnector = {
type: 'INJECTED',
info: { rdns: 'io.metamask.com' }
} as const

const zerionConnector = {
id: 'ecc4036f814562b41a5268adc86270fba1365471402006302e70169465b7ac18',
type: 'INJECTED'
} as const
// -- Tests --------------------------------------------------------------------
describe('ConnectorController', () => {
it('should have valid default state', () => {
Expand Down Expand Up @@ -37,8 +40,10 @@ describe('ConnectorController', () => {
})

it('should return the correct connector on getConnector', () => {
expect(ConnectorController.getConnector('walletConnect', '')).toBe(walletConnectConnector)
ConnectorController.addConnector(zerionConnector)
expect(ConnectorController.getConnector('walletConnect', '')).toBe(undefined)
expect(ConnectorController.getConnector('', 'io.metamask.com')).toBe(metamaskConnector)
expect(ConnectorController.getConnector(zerionConnector.id, '')).toBeUndefined()
expect(ConnectorController.getConnector('unknown', '')).toBeUndefined()
})
})
27 changes: 27 additions & 0 deletions packages/core/tests/controllers/TransactionsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,31 @@ describe('TransactionsController', () => {
}
})
})

it('should clear cursor correctly', async () => {
// Mock fetch transactions
const fetchTransactions = vi
.spyOn(BlockchainApiController, 'fetchTransactions')
.mockResolvedValue({
data: [],
next: 'cursor'
})

// Fetch transactions
await TransactionsController.fetchTransactions('0x123')
expect(TransactionsController.state.next).toBe('cursor')

TransactionsController.clearCursor()
expect(TransactionsController.state.next).toBeUndefined()

// Fetch transactions again
await TransactionsController.fetchTransactions('0x123')
expect(fetchTransactions).toHaveBeenCalledWith({
account: '0x123',
projectId,
cursor: undefined,
onramp: undefined
})
expect(TransactionsController.state.next).toBe('cursor')
})
})
1 change: 1 addition & 0 deletions packages/scaffold/src/partials/w3m-activity-list/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class W3mActivityList extends LitElement {
// -- Lifecycle ----------------------------------------- //
public constructor() {
super()
TransactionsController.clearCursor()
this.unsubscribe.push(
...[
AccountController.subscribe(val => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class W3mOnRampActivityView extends LitElement {
})
]
)
TransactionsController.clearCursor()
this.fetchTransactions()
}

Expand Down

0 comments on commit 087725a

Please sign in to comment.