-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(portfolio): base sync portfolio (#3195)
Signed-off-by: Juliano Lazzarotto <[email protected]> Co-authored-by: Michal S <[email protected]>
- Loading branch information
1 parent
da57048
commit 749f8ea
Showing
174 changed files
with
8,165 additions
and
308 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,13 @@ | ||
{ | ||
"java.configuration.updateBuildConfiguration": "disabled", | ||
"typescript.tsdk": "node_modules/typescript/lib" | ||
"typescript.tsdk": "node_modules/typescript/lib", | ||
"jestrunner.debugOptions": { | ||
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/react-scripts", | ||
"runtimeArgs": [ | ||
"test", | ||
"${fileBasename}", | ||
"--no-cache", | ||
"--color" | ||
] | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const nftCryptoKitty = { | ||
decimals: 0, | ||
ticker: 'CryptoKitty', | ||
name: 'CryptoKitty #1234', | ||
symbol: 'CK', | ||
status: 'normal', | ||
application: 'token', | ||
tag: '', | ||
reference: '0xabcdef1234567890.cryptokitty1234', | ||
website: 'https://www.cryptokitties.co', | ||
originalImage: 'https://cdn.example.com/ck-original1234.png', | ||
id: '14696a4676909f4e3cb1f2e60e2e08e5abed70caf5c02699be971139.43554259', | ||
fingerprint: 'asset1s7nlt45cc82upqewvjtgu7g97l7eg483c6wu75', | ||
nature: 'secondary', | ||
type: 'nft', | ||
} | ||
|
||
function generateTokenInfos(baseToken, count) { | ||
const tokens = [] | ||
for (let i = 0; i < count; i++) { | ||
// Clone the base token object to create a new token | ||
const id = `${baseToken.id.split('.')[0]}.${Buffer.from(String(i)).toString('hex')}` | ||
const name = `${baseToken.name.split(' ')[0]} #${i}` | ||
const reference = `${baseToken.reference.split('.')[0]}.${Buffer.from(String(i)).toString('hex')}` | ||
const fingerprint = `asset${i}s7nlt45cc82upqewvjtgu7g97l7eg483c6wu${i}` | ||
|
||
const newToken = {...baseToken, id, name, reference, fingerprint} | ||
|
||
tokens.push(newToken) | ||
} | ||
return tokens | ||
} | ||
|
||
const generatedTokens = generateTokenInfos(nftCryptoKitty, 50) | ||
|
||
const apiResponseTokenInfos = generatedTokens.reduce((acc, token, index) => { | ||
acc[token.id] = [200, token, `hash${index + 1}`, 3600] | ||
return acc | ||
}, {}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
apps/wallet-mobile/src/features/Portfolio/common/usePortfolioBalanceManager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import {mountMMKVStorage, observableStorageMaker} from '@yoroi/common' | ||
import {createPrimaryTokenInfo, portfolioBalanceManagerMaker, portfolioBalanceStorageMaker} from '@yoroi/portfolio' | ||
import {Portfolio} from '@yoroi/types' | ||
import * as React from 'react' | ||
|
||
import {YoroiWallet} from '../../../yoroi-wallets/cardano/types' | ||
|
||
export const usePortfolioBalanceManager = ({ | ||
tokenManager, | ||
walletId, | ||
}: { | ||
tokenManager: Portfolio.Manager.Token | ||
walletId: YoroiWallet['id'] | ||
}) => { | ||
return React.useMemo(() => { | ||
const balanceStorageMounted = mountMMKVStorage<Portfolio.Token.Id>({path: `balance/${walletId}/`}) | ||
const primaryBreakdownStorageMounted = mountMMKVStorage<Portfolio.Token.Id>({ | ||
path: `/primary-breakdown/${walletId}/`, | ||
}) | ||
|
||
const balanceStorage = portfolioBalanceStorageMaker({ | ||
balanceStorage: observableStorageMaker(balanceStorageMounted), | ||
primaryBreakdownStorage: observableStorageMaker(primaryBreakdownStorageMounted), | ||
}) | ||
|
||
const balanceManager = portfolioBalanceManagerMaker({ | ||
tokenManager, | ||
storage: balanceStorage, | ||
primaryToken: { | ||
info: primaryTokenInfo, | ||
discovery: { | ||
counters: { | ||
items: 0, | ||
supply: 0n, | ||
totalItems: 0, | ||
}, | ||
id: primaryTokenInfo.id, | ||
originalMetadata: { | ||
filteredMintMetadatum: null, | ||
referenceDatum: null, | ||
tokenRegistry: null, | ||
}, | ||
properties: {}, | ||
source: { | ||
decimals: Portfolio.Token.Source.Metadata, | ||
name: Portfolio.Token.Source.Metadata, | ||
ticker: Portfolio.Token.Source.Metadata, | ||
symbol: Portfolio.Token.Source.Metadata, | ||
image: Portfolio.Token.Source.Metadata, | ||
}, | ||
}, | ||
}, | ||
sourceId: walletId, | ||
}) | ||
|
||
balanceManager.hydrate() | ||
return { | ||
balanceManager, | ||
balanceStorage, | ||
} | ||
}, [tokenManager, walletId]) | ||
} | ||
|
||
const primaryTokenInfo = createPrimaryTokenInfo({ | ||
decimals: 6, | ||
name: 'ADA', | ||
ticker: 'ADA', | ||
symbol: '$', | ||
reference: '', | ||
tag: '', | ||
website: '', | ||
originalImage: '', | ||
}) |
27 changes: 27 additions & 0 deletions
27
apps/wallet-mobile/src/features/Portfolio/common/usePortfolioTokenManager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import {mountMMKVStorage, observableStorageMaker} from '@yoroi/common' | ||
import {portfolioApiMaker, portfolioTokenManagerMaker, portfolioTokenStorageMaker} from '@yoroi/portfolio' | ||
import {Chain, Portfolio} from '@yoroi/types' | ||
import * as React from 'react' | ||
|
||
export const usePortfolioTokenManager = ({network}: {network: Chain.Network}) => { | ||
return React.useMemo(() => { | ||
const tokenDiscoveryStorageMounted = mountMMKVStorage<Portfolio.Token.Id>({path: `${network}/token-discovery/`}) | ||
const tokenInfoStorageMounted = mountMMKVStorage<Portfolio.Token.Id>({path: `${network}/token-info/`}) | ||
|
||
const tokenStorage = portfolioTokenStorageMaker({ | ||
tokenDiscoveryStorage: observableStorageMaker(tokenDiscoveryStorageMounted), | ||
tokenInfoStorage: observableStorageMaker(tokenInfoStorageMounted), | ||
}) | ||
const api = portfolioApiMaker({ | ||
network, | ||
}) | ||
|
||
const tokenManager = portfolioTokenManagerMaker({ | ||
api, | ||
storage: tokenStorage, | ||
}) | ||
|
||
tokenManager.hydrate({sourceId: 'initial'}) | ||
return {tokenManager, tokenStorage} | ||
}, [network]) | ||
} |
127 changes: 127 additions & 0 deletions
127
apps/wallet-mobile/src/features/Portfolio/useCases/PortfolioScreen.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import {useNavigation} from '@react-navigation/native' | ||
import {useObserver} from '@yoroi/common' | ||
import {Chain} from '@yoroi/types' | ||
import * as React from 'react' | ||
import {Text, View} from 'react-native' | ||
import {FlatList} from 'react-native-gesture-handler' | ||
import {SafeAreaView} from 'react-native-safe-area-context' | ||
|
||
import {Button, Spacer} from '../../../components' | ||
import {useSelectedWallet} from '../../WalletManager/Context' | ||
import {usePortfolioBalanceManager} from '../common/usePortfolioBalanceManager' | ||
import {usePortfolioTokenManager} from '../common/usePortfolioTokenManager' | ||
|
||
export const PortfolioScreen = () => { | ||
const navigation = useNavigation() | ||
const wallet = useSelectedWallet() | ||
const {tokenManager, tokenStorage} = usePortfolioTokenManager({network: Chain.Network.Main}) | ||
const {balanceManager: bmW1, balanceStorage: bs1} = usePortfolioBalanceManager({ | ||
tokenManager, | ||
walletId: wallet.id, | ||
}) | ||
|
||
// wallet 2 for testing | ||
const {balanceManager: bmW2, balanceStorage: bs2} = usePortfolioBalanceManager({ | ||
tokenManager, | ||
walletId: 'wallet-2', | ||
}) | ||
const {data: balancesW2, isPending: isPendingW2} = useObserver({ | ||
observable: bmW2.observable, | ||
executor: () => bmW2.getBalances().all, | ||
}) | ||
// end of wallet 2 | ||
|
||
const {data: balances, isPending} = useObserver({ | ||
observable: bmW1.observable, | ||
executor: () => bmW1.getBalances().all, | ||
}) | ||
const opacity = isPending || isPendingW2 ? 0.5 : 1 | ||
|
||
const handleOnSync = () => { | ||
bmW1.sync({ | ||
primaryBalance: { | ||
balance: 1n, | ||
lockedInBuiltTxs: 2n, | ||
minRequiredByTokens: 0n, | ||
records: [], | ||
}, | ||
secondaryBalances: new Map([ | ||
['14696a4676909f4e3cb1f2e60e2e08e5abed70caf5c02699be971139.34', {balance: 2n, lockedInBuiltTxs: 0n}], | ||
['14696a4676909f4e3cb1f2e60e2e08e5abed70caf5c02699be971139.35', {balance: 3n, lockedInBuiltTxs: 0n}], | ||
['14696a4676909f4e3cb1f2e60e2e08e5abed70caf5c02699be971139.36', {balance: 4n, lockedInBuiltTxs: 0n}], | ||
]), | ||
}) | ||
|
||
bmW2.sync({ | ||
primaryBalance: { | ||
balance: 2n, | ||
lockedInBuiltTxs: 3n, | ||
minRequiredByTokens: 0n, | ||
records: [], | ||
}, | ||
secondaryBalances: new Map([ | ||
['14696a4676909f4e3cb1f2e60e2e08e5abed70caf5c02699be971139.3130', {balance: 222n, lockedInBuiltTxs: 0n}], | ||
['14696a4676909f4e3cb1f2e60e2e08e5abed70caf5c02699be971139.35', {balance: 223n, lockedInBuiltTxs: 0n}], | ||
['14696a4676909f4e3cb1f2e60e2e08e5abed70caf5c02699be971139.3131', {balance: 224n, lockedInBuiltTxs: 0n}], | ||
['14696a4676909f4e3cb1f2e60e2e08e5abed70caf5c02699be971139.3132', {balance: 224n, lockedInBuiltTxs: 0n}], | ||
]), | ||
}) | ||
} | ||
|
||
const handleOnReset = () => { | ||
bs1.clear() | ||
bs2.clear() | ||
tokenStorage.clear() | ||
navigation.goBack() | ||
} | ||
|
||
return ( | ||
<SafeAreaView edges={['bottom', 'top', 'left', 'right']}> | ||
<View style={{padding: 16}}> | ||
<Text>Portfolio playground</Text> | ||
|
||
<Spacer height={16} /> | ||
|
||
<Button title="sync" onPress={handleOnSync} shelleyTheme /> | ||
|
||
<Spacer height={16} /> | ||
|
||
<Button title="reset" onPress={handleOnReset} outlineShelley /> | ||
|
||
<Spacer height={16} /> | ||
|
||
<Text>w1 {wallet.id}</Text> | ||
|
||
<Text>all: {balances.length}</Text> | ||
|
||
<FlatList | ||
data={balances} | ||
keyExtractor={(item) => item.info.id} | ||
renderItem={({item}) => ( | ||
<View style={{padding: 8, backgroundColor: 'lightgray', marginVertical: 4, opacity}}> | ||
<Text>{item.info.name}</Text> | ||
|
||
<Text>{item.balance.toString()}</Text> | ||
</View> | ||
)} | ||
/> | ||
|
||
<Text>wallet 2</Text> | ||
|
||
<Text>all: {balances.length}</Text> | ||
|
||
<FlatList | ||
data={balancesW2} | ||
keyExtractor={(item) => item.info.id} | ||
renderItem={({item}) => ( | ||
<View style={{padding: 8, backgroundColor: 'lightgray', marginVertical: 4, opacity}}> | ||
<Text>{item.info.name}</Text> | ||
|
||
<Text>{item.balance.toString()}</Text> | ||
</View> | ||
)} | ||
/> | ||
</View> | ||
</SafeAreaView> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.