Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement/live txs #129

Merged
merged 42 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
4352d18
added live balances
Dec 16, 2021
1550bfb
minor changes
Jan 4, 2022
93723c7
RIF Sockets Context
Jan 7, 2022
dadc239
URL based on platform and exporting rifWalletServicesUrl variable
Jan 7, 2022
419d36c
Adding socket.io-client
Jan 7, 2022
eabcc79
Wrapping app with RIFSocketsProvider
Jan 7, 2022
8513363
Implementing live prices
Jan 7, 2022
ceaf9be
Merge branch 'US-492' into feature/live-balances
agustin-v Jan 7, 2022
1d91593
Merge pull request #81 from rsksmart/feature/live-balances
agustin-v Jan 7, 2022
12f6f21
Lifting state for activities/transactions
Jan 7, 2022
375096c
Lifting balances state
Jan 7, 2022
590b735
Lint
Jan 10, 2022
fc06cb0
Fixing merging issues
Jan 18, 2022
fe290c1
Update src/core/setup.ts
agustin-v Jan 18, 2022
15e1ca9
Fixing merge issues
Jan 18, 2022
12a7ee2
Merge branch 'US-442' of github.com:rsksmart/swallet into US-442
Jan 18, 2022
292f0bd
Lint
Jan 19, 2022
b70a051
Fixing test
Jan 19, 2022
426a890
Move service url to config
ilanolkies Jan 19, 2022
be9731d
connected home with tokens and transactions state
Jan 20, 2022
5ac230a
Merge pull request #113 from rsksmart/feature/connect-landing
ilanolkies Jan 21, 2022
be32609
Merge branch 'US-492' into US-442
ilanolkies Jan 21, 2022
4e103c5
Merge pull request #100 from rsksmart/US-442
ilanolkies Jan 21, 2022
927a545
Merge branch 'develop' into US-492
ilanolkies Jan 21, 2022
0c35ac6
fixed tests and added polimophic socket class (#121)
chescalante Jan 26, 2022
278e36e
Moved subscriptions instance to Core + added ABI Enhancer after recei…
ilanolkies Jan 26, 2022
e8f4faf
Lint
ilanolkies Jan 26, 2022
1d4d723
Changing to new state structure
Jan 28, 2022
dca8cfd
Changing state structure to match enhanced transactions
Jan 28, 2022
c626713
Implementing top 5 enhanced transactions
Jan 28, 2022
65d43ca
Implementing FlatList component to render transactionsç
Jan 28, 2022
fa5e498
Implementing FlatList to implement infinite scrolling
Jan 28, 2022
429914f
Fixing merging issues
Jan 28, 2022
48c0d0f
Lint
Jan 28, 2022
d9db7c0
Fixing test
Jan 28, 2022
6c51f16
Merge branch 'develop' into enhancement/live-txs
Feb 4, 2022
c3d77e8
Removing data array from the state structure, because its duplicating…
Feb 4, 2022
e92d48a
Lint
Feb 4, 2022
d3b16d2
Sorting by timestamp
Feb 7, 2022
1c37661
Fixing sorting funciton
Feb 8, 2022
bb7d571
Changing filtering to use hash since its unique
Feb 9, 2022
a054897
Merge branch 'develop' into enhancement/live-txs
ilanolkies Feb 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/screens/activity/ActivityRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const styles = StyleSheet.create({
width: '100%',
borderBottomWidth: 1,
borderBottomColor: '#CCCCCC',
paddingVertical: 15,
paddingVertical: 30,
},
text: {
paddingTop: 10,
Expand Down
104 changes: 37 additions & 67 deletions src/screens/activity/ActivityScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useState } from 'react'
import { StyleSheet, View, ScrollView, Text } from 'react-native'
import React, { useState } from 'react'
import { FlatList, StyleSheet, View, Text } from 'react-native'

import { useSocketsState } from '../../subscriptions/RIFSockets'
import { useTranslation } from 'react-i18next'
Expand All @@ -18,9 +18,7 @@ import { ScreenWithWallet } from '../types'
import { ScreenProps } from '../../RootNavigation'
import { RIFWallet } from '../../lib/core'
import ActivityRow from './ActivityRow'
import { grid } from '../../styles/grid'
import { SquareButton } from '../../components/button/SquareButton'
import { Arrow, RefreshIcon } from '../../components/icons'
import { IActivity } from '../../subscriptions/types'

export interface IActivityTransaction {
originTransaction: IApiTransaction
Expand All @@ -41,13 +39,16 @@ export const ActivityScreen: React.FC<
ScreenProps<'Activity'> & ScreenWithWallet & ActivityScreenProps
> = ({ wallet, fetcher, abiEnhancer, navigation }) => {
const [info, setInfo] = useState('')
const { state, dispatch } = useSocketsState()
useState<TransactionsServerResponseWithActivityTransactions | null>(null)

const {
state: { transactions },
dispatch,
} = useSocketsState()

const { t } = useTranslation()

useEffect(() => {
fetchTransactionsPage()
}, [])
const hasTransactions =
transactions && transactions.activityTransactions!.length > 0

const fetchTransactionsPage = async ({
prev,
Expand All @@ -59,7 +60,6 @@ export const ActivityScreen: React.FC<
/*i18n.changeLanguage('es')*/
try {
setInfo(t('Loading transactions. Please wait...'))
dispatch({ type: 'newActivity', payload: null })

const fetchedTransactions: TransactionsServerResponseWithActivityTransactions =
await fetcher.fetchTransactionsByAddress(
Expand All @@ -82,76 +82,46 @@ export const ActivityScreen: React.FC<
}),
)

dispatch({ type: 'newActivity', payload: fetchedTransactions })
dispatch({
type: 'newTransactions',
payload: fetchedTransactions as IActivity,
})

setInfo('')
} catch (e: any) {
setInfo(t('Error reaching API: ') + e.message)
}
}

return (
<ScrollView style={styles.parent}>
<View>
<Text style={styles.header}>Activity</Text>
<View style={{ ...grid.row, ...styles.refreshButtonView }}>
<View style={{ ...grid.column4, ...styles.column }}>
<SquareButton
onPress={() =>
fetchTransactionsPage({ prev: state.activities?.prev })
}
disabled={!state.activities?.prev}
title="prev"
icon={
<Arrow
rotate={270}
color={state.activities?.prev ? '#66777E' : '#f1f1f1'}
/>
}
/>
</View>
<View style={{ ...grid.column4, ...styles.column }}>
<SquareButton
onPress={() => fetchTransactionsPage()}
title="refresh"
icon={<RefreshIcon width={50} height={50} color="#66777E" />}
/>
</View>
<View style={{ ...grid.column4, ...styles.column }}>
<SquareButton
onPress={() =>
fetchTransactionsPage({ next: state.activities?.next })
}
disabled={!state.activities?.next}
title="next"
icon={
<Arrow
rotate={90}
color={state.activities?.next ? '#66777E' : '#f1f1f1'}
/>
}
/>
</View>
</View>

{!!info && <Text testID="Info.Text">{info}</Text>}

{state.activities &&
state.activities.activityTransactions!.length > 0 &&
state.activities.activityTransactions!.map(
(activityTransaction: IActivityTransaction) => (
<ActivityRow
key={activityTransaction.originTransaction.hash}
activityTransaction={activityTransaction}
navigation={navigation}
/>
),
)}
</ScrollView>

{hasTransactions && (
<FlatList
data={transactions.activityTransactions}
initialNumToRender={10}
keyExtractor={item => item.originTransaction.hash}
onEndReached={() =>
fetchTransactionsPage({ next: transactions?.next })
}
onEndReachedThreshold={0.2}
onRefresh={fetchTransactionsPage}
refreshing={!!info}
renderItem={({ item }) => (
<ActivityRow activityTransaction={item} navigation={navigation} />
)}
style={styles.parent}
/>
)}
</View>
)
}

const styles = StyleSheet.create({
parent: {
paddingHorizontal: 20,
marginBottom: 30,
backgroundColor: '#ffffff',
},
refreshButtonView: {
Expand Down
2 changes: 1 addition & 1 deletion src/screens/home/ActivityComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const ActivityComponent: React.FC<Interface> = ({
}) => {
const { state } = useSocketsState()

const recent = state.transactions.slice(0, 5)
const recent = state.transactions.activityTransactions.slice(0, 5)

return (
<ScrollView style={styles.portfolio}>
Expand Down
72 changes: 39 additions & 33 deletions src/subscriptions/RIFSockets.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,48 +58,54 @@ describe('Live Subscriptions Context', () => {

test('expect useSubscription to dispatch new transaction and update state', () => {
const { result } = renderHook(() => useSocketsState(), { wrapper })
expect(result.current.state.transactions.length).toEqual(0)
expect(
result.current.state.transactions.activityTransactions.length,
).toEqual(0)
act(() => {
result.current.dispatch({
type: 'newTransaction',
payload: {
_id: '',
hash: '0x09073e82ebe101dce197731e2270bbad56b4da279edeeb5ed2671eccf723eec0',
nonce: 2452059,
blockHash: '',
blockNumber: 2452060,
transactionIndex: 1,
from: '0x0000000000000000000000000000000000000000',
to: '0x0000000000000000000000000000000001000008',
gas: 0,
gasPrice: '0x3938700',
value: '0xb1a2bc2ec50000',
input: '0x',
v: '',
r: '',
s: '',
timestamp: 1640036267,
receipt: {
transactionHash: '',
transactionIndex: 1,
originTransaction: {
_id: '',
hash: '0x09073e82ebe101dce197731e2270bbad56b4da279edeeb5ed2671eccf723eec0',
nonce: 2452059,
blockHash: '',
blockNumber: 2434238,
cumulativeGasUsed: 112668,
gasUsed: 21000,
contractAddress: null,
logs: [],
from: '',
to: '',
status: '0x1',
logsBloom: '',
blockNumber: 2452060,
transactionIndex: 1,
from: '0x0000000000000000000000000000000000000000',
to: '0x0000000000000000000000000000000001000008',
gas: 0,
gasPrice: '0x3938700',
value: '0xb1a2bc2ec50000',
input: '0x',
v: '',
r: '',
s: '',
timestamp: 1640036267,
receipt: {
transactionHash: '',
transactionIndex: 1,
blockHash: '',
blockNumber: 2434238,
cumulativeGasUsed: 112668,
gasUsed: 21000,
contractAddress: null,
logs: [],
from: '',
to: '',
status: '0x1',
logsBloom: '',
},
txType: 'normal',
txId: '',
},
txType: 'normal',
txId: '',
enhancedTransaction: undefined,
},
})
})

expect(result.current.state.transactions.length).toEqual(1)
expect(
result.current.state.transactions.activityTransactions.length,
).toEqual(1)
})
})
})
46 changes: 38 additions & 8 deletions src/subscriptions/RIFSockets.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
import React from 'react'
import { useSelectedWallet } from '../Context'
import { enhanceTransactionInput } from '../screens/activity/ActivityScreen'
import { filterEnhancedTransactions, sortEnhancedTransactions } from './utils'

import {
Action,
Dispatch,
IActivityTransaction,
State,
SubscriptionsProviderProps,
} from './types'
import { constants } from 'ethers'

import { Action, Dispatch, State, SubscriptionsProviderProps } from './types'
import { ITokenWithBalance } from '../lib/rifWalletServices/RIFWalletServicesTypes'
import { RIFWallet } from '../lib/core'

function liveSubscriptionsReducer(state: State, action: Action) {
const { type } = action

switch (action.type) {
case 'newActivity':
case 'newTransactions':
const sortedTxs: Array<IActivityTransaction> = [
...action.payload!.activityTransactions,
...state.transactions!.activityTransactions,
]
.sort(sortEnhancedTransactions)
.filter(filterEnhancedTransactions)

return {
...state,
activities: action.payload,
transactions: {
prev: action.payload.prev,
next: action.payload.next,
activityTransactions: sortedTxs,
},
}

case 'newBalance':
Expand All @@ -36,9 +55,19 @@ function liveSubscriptionsReducer(state: State, action: Action) {
}

case 'newTransaction':
const sortedTx: Array<IActivityTransaction> = [
action.payload,
...state.transactions!.activityTransactions,
]
.sort(sortEnhancedTransactions)
.filter(filterEnhancedTransactions)

return {
...state,
transactions: [action.payload, ...state.transactions],
transactions: {
...state.transactions,
activityTransactions: sortedTx,
},
}

case 'init':
Expand All @@ -54,8 +83,11 @@ function liveSubscriptionsReducer(state: State, action: Action) {

return {
...state,
transactions: [...action.payload.transactions],
balances: balancesInitial,
transactions: {
...state.transactions,
activityTransactions: action.payload.transactions,
},
}

default:
Expand All @@ -64,15 +96,13 @@ function liveSubscriptionsReducer(state: State, action: Action) {
}

const initialState = {
activities: {
transactions: {
activityTransactions: [],
data: [],
next: null,
prev: null,
},
prices: {},
balances: {},
transactions: [],
}
//TODO: Move this to the backend
const loadRBTCBalance = async (wallet: RIFWallet, dispatch: Dispatch) => {
Expand Down
Loading