-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(wallet-mobile): wip wiring up portoflio
- Loading branch information
1 parent
c601197
commit d46d28d
Showing
37 changed files
with
545 additions
and
386 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
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
31 changes: 31 additions & 0 deletions
31
apps/wallet-mobile/src/features/Portfolio/common/helpers/filterBySyncEvent.test.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,31 @@ | ||
import {Portfolio} from '@yoroi/types' | ||
|
||
import {filterBySyncEvent} from './filterBySyncEvent' | ||
|
||
describe('filterBySyncEvent', () => { | ||
it('should return true if the event is a sync event', () => { | ||
const event: Portfolio.Event.BalanceManager = { | ||
on: Portfolio.Event.ManagerOn.Sync, | ||
mode: 'all', | ||
sourceId: '1', | ||
} | ||
expect(filterBySyncEvent(event)).toBe(true) | ||
}) | ||
|
||
it('should return false if the event is not a sync event', () => { | ||
const event: Portfolio.Event.BalanceManager = { | ||
on: Portfolio.Event.ManagerOn.Hydrate, | ||
sourceId: '1', | ||
} | ||
expect(filterBySyncEvent(event)).toBe(false) | ||
}) | ||
|
||
it('should work with TokenManager events as well', () => { | ||
const event: Portfolio.Event.TokenManager = { | ||
on: Portfolio.Event.ManagerOn.Sync, | ||
ids: [], | ||
sourceId: '1', | ||
} | ||
expect(filterBySyncEvent(event)).toBe(true) | ||
}) | ||
}) |
5 changes: 5 additions & 0 deletions
5
apps/wallet-mobile/src/features/Portfolio/common/helpers/filterBySyncEvent.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,5 @@ | ||
import {Portfolio} from '@yoroi/types' | ||
|
||
export function filterBySyncEvent({on}: Portfolio.Event.BalanceManager | Portfolio.Event.TokenManager) { | ||
return on === Portfolio.Event.ManagerOn.Sync | ||
} |
16 changes: 16 additions & 0 deletions
16
apps/wallet-mobile/src/features/Portfolio/common/hooks/useBalances.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,16 @@ | ||
import {useObservableValue} from '@yoroi/common' | ||
import * as React from 'react' | ||
import {filter} from 'rxjs' | ||
|
||
import {YoroiWallet} from '../../../../yoroi-wallets/cardano/types' | ||
import {filterBySyncEvent as isSyncEvent} from '../helpers/filterBySyncEvent' | ||
|
||
export const useBalances = ({wallet}: {wallet: YoroiWallet}) => { | ||
const observable$ = React.useMemo(() => wallet.balance$.pipe(filter(isSyncEvent)), [wallet]) | ||
const executor = React.useCallback(() => wallet.balances, [wallet]) | ||
|
||
return useObservableValue({ | ||
observable$, | ||
executor, | ||
}) | ||
} |
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
File renamed without changes.
16 changes: 16 additions & 0 deletions
16
apps/wallet-mobile/src/features/Portfolio/common/hooks/usePrimaryBalance.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,16 @@ | ||
import {useObservableValue} from '@yoroi/common' | ||
import * as React from 'react' | ||
import {filter} from 'rxjs' | ||
|
||
import {YoroiWallet} from '../../../../yoroi-wallets/cardano/types' | ||
import {filterBySyncEvent as isSyncEvent} from '../helpers/filterBySyncEvent' | ||
|
||
export const usePrimaryBalance = ({wallet}: {wallet: YoroiWallet}) => { | ||
const observable$ = React.useMemo(() => wallet.balance$.pipe(filter(isSyncEvent)), [wallet]) | ||
const executor = React.useCallback(() => wallet.primaryBalance, [wallet]) | ||
|
||
return useObservableValue({ | ||
observable$, | ||
executor, | ||
}) | ||
} |
16 changes: 16 additions & 0 deletions
16
apps/wallet-mobile/src/features/Portfolio/common/hooks/usePrimaryBreakdown.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,16 @@ | ||
import {useObservableValue} from '@yoroi/common' | ||
import * as React from 'react' | ||
import {filter} from 'rxjs' | ||
|
||
import {YoroiWallet} from '../../../../yoroi-wallets/cardano/types' | ||
import {filterBySyncEvent as isSyncEvent} from '../helpers/filterBySyncEvent' | ||
|
||
export const usePrimaryBreakdown = ({wallet}: {wallet: YoroiWallet}) => { | ||
const observable$ = React.useMemo(() => wallet.balance$.pipe(filter(isSyncEvent)), [wallet]) | ||
const executor = React.useCallback(() => wallet.primaryBreakdown, [wallet]) | ||
|
||
return useObservableValue({ | ||
observable$, | ||
executor, | ||
}) | ||
} |
49 changes: 0 additions & 49 deletions
49
apps/wallet-mobile/src/features/Portfolio/common/transformers.ts
This file was deleted.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
...wallet-mobile/src/features/Portfolio/common/transformers/toBalanceManagerSyncArgs.test.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,41 @@ | ||
import {RawUtxo} from '../../../../yoroi-wallets/types' | ||
import {toBalanceManagerSyncArgs} from './toBalanceManagerSyncArgs' | ||
|
||
describe('toBalanceManagerSyncArgs', () => { | ||
const policyId = new Array(56).fill('1').join('') | ||
it('should calculate primaryStated and secondaryBalances correctly', () => { | ||
const rawUtxos: RawUtxo[] = [ | ||
{ | ||
amount: '100', | ||
receiver: '', | ||
tx_hash: '', | ||
tx_index: 0, | ||
utxo_id: '', | ||
assets: [ | ||
{assetId: `${policyId}.DEAD`, amount: '50', policyId, name: 'DEAD'}, | ||
{assetId: `${policyId}.DEADFEED`, amount: '30', policyId, name: 'DEADFEED'}, | ||
], | ||
}, | ||
{ | ||
receiver: '', | ||
tx_hash: '', | ||
tx_index: 0, | ||
utxo_id: '', | ||
amount: '200', | ||
assets: [ | ||
{assetId: `${policyId}.DEAD`, amount: '70', policyId, name: 'DEAD'}, | ||
{assetId: `${policyId}.3031`, amount: '80', policyId, name: '3031'}, | ||
], | ||
}, | ||
] | ||
const lockedAsStorageCost = 10n | ||
|
||
const result = toBalanceManagerSyncArgs(rawUtxos, lockedAsStorageCost) | ||
|
||
expect(result.primaryStated.totalFromTxs).toBe(300n) | ||
expect(result.primaryStated.lockedAsStorageCost).toBe(10n) | ||
expect(result.secondaryBalances.get(`${policyId}.DEAD`)?.balance).toBe(120n) | ||
expect(result.secondaryBalances.get(`${policyId}.DEADFEED`)?.balance).toBe(30n) | ||
expect(result.secondaryBalances.get(`${policyId}.3031`)?.balance).toBe(80n) | ||
}) | ||
}) |
26 changes: 26 additions & 0 deletions
26
apps/wallet-mobile/src/features/Portfolio/common/transformers/toBalanceManagerSyncArgs.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,26 @@ | ||
import {Portfolio} from '@yoroi/types' | ||
|
||
import {RawUtxo} from '../../../../yoroi-wallets/types' | ||
|
||
export function toBalanceManagerSyncArgs(rawUtxos: RawUtxo[], lockedAsStorageCost: bigint) { | ||
let primaryTokenBalance = 0n | ||
const secondaries = new Map<Portfolio.Token.Id, Omit<Portfolio.Token.Balance, 'info'>>() | ||
for (const utxo of rawUtxos) { | ||
primaryTokenBalance += BigInt(utxo.amount) | ||
for (const record of utxo.assets) { | ||
const tokenId: Portfolio.Token.Id = `${record.policyId}.${record.name}` | ||
const balance = (secondaries.get(tokenId)?.balance ?? 0n) + BigInt(record.amount) | ||
secondaries.set(tokenId, { | ||
balance, | ||
}) | ||
} | ||
} | ||
|
||
return { | ||
primaryStated: { | ||
totalFromTxs: primaryTokenBalance, | ||
lockedAsStorageCost, | ||
}, | ||
secondaryBalances: new Map(secondaries), | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
.../wallet-mobile/src/features/Portfolio/common/transformers/toChainSupportedNetwork.test.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,21 @@ | ||
import {Chain} from '@yoroi/types' | ||
|
||
import {toChainSupportedNetwork} from './toChainSupportedNetwork' | ||
|
||
describe('toChainSupportedNetwork', () => { | ||
it('should return Chain.Network.Main for networkId 0', () => { | ||
expect(toChainSupportedNetwork(0)).toBe(Chain.Network.Main) | ||
}) | ||
|
||
it('should return Chain.Network.Main for networkId 1', () => { | ||
expect(toChainSupportedNetwork(1)).toBe(Chain.Network.Main) | ||
}) | ||
|
||
it('should return Chain.Network.Sancho for networkId 450', () => { | ||
expect(toChainSupportedNetwork(450)).toBe(Chain.Network.Sancho) | ||
}) | ||
|
||
it('should return Chain.Network.Preprod for unknown networkId', () => { | ||
expect(toChainSupportedNetwork(999 as never)).toBe(Chain.Network.Preprod) | ||
}) | ||
}) |
15 changes: 15 additions & 0 deletions
15
apps/wallet-mobile/src/features/Portfolio/common/transformers/toChainSupportedNetwork.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,15 @@ | ||
import {Chain} from '@yoroi/types' | ||
|
||
import {NetworkId} from '../../../../yoroi-wallets/types' | ||
|
||
export function toChainSupportedNetwork(networkId: NetworkId): Chain.SupportedNetworks { | ||
switch (networkId) { | ||
case 0: | ||
case 1: | ||
return Chain.Network.Main | ||
case 450: | ||
return Chain.Network.Sancho | ||
default: | ||
return Chain.Network.Preprod | ||
} | ||
} |
Oops, something went wrong.