-
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(portfolio): added records type to balance manager
- Loading branch information
1 parent
80abaf8
commit 4cae501
Showing
8 changed files
with
229 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import {useTransfer} from '@yoroi/transfer' | ||
import {Portfolio, Transfer} from '@yoroi/types' | ||
import {getTotalUsedByOtherTargets} from './get-total-used-by-other-targets' | ||
|
||
export const useTokenQuantities = ({ | ||
balances, | ||
selectedTokenId, | ||
primaryBreakdown, | ||
targets, | ||
selectedTargetIndex, | ||
}: { | ||
targets: Readonly<Transfer.Targets> | ||
id: Portfolio.Token.Id | ||
balances: ReadonlyArray<Portfolio.Token.Amount> | ||
primaryBreakdown: Readonly<Portfolio.PrimaryBreakdown> | ||
selectedTargetIndex: number | ||
}) => { | ||
const initialQuantity = | ||
targets[selectedTargetIndex]?.entry.amounts[selectedTokenId]?.quantity ?? 0n | ||
|
||
const used = getTotalUsedByOtherTargets({ | ||
targets, | ||
selectedTokenId, | ||
selectedTargetIndex, | ||
}) | ||
|
||
const balance = balances.find((amount) => amount.info.id === selectedTokenId) | ||
const available = Quantities.diff(balance, used) | ||
|
||
const isPrimary = tokenId === wallet.primaryTokenInfo.id | ||
const primaryLocked = asQuantity( | ||
wallet.primaryBreakdown.lockedAsStorageCost.toString(), | ||
) | ||
const locked = isPrimary ? primaryLocked : Quantities.zero | ||
|
||
const spendable = Quantities.diff(available, locked) | ||
|
||
return { | ||
balance, | ||
used, | ||
available, | ||
initialQuantity, | ||
locked, | ||
spendable, | ||
} | ||
} | ||
|
||
export function getBalanceTransferBreakdown() {} |
135 changes: 135 additions & 0 deletions
135
packages/transfer/src/helpers/get-total-used-by-other-targets.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,135 @@ | ||
import {tokenBalanceMocks} from '@yoroi/portfolio' | ||
import {getTotalUsedByOtherTargets} from './get-total-used-by-other-targets' | ||
import {Transfer} from '@yoroi/types' | ||
|
||
describe('getTotalUsedByOtherTargets', () => { | ||
it('should return the total amount of tokens used by other targets', () => { | ||
const targets: Transfer.Target[] = [ | ||
{ | ||
receiver: { | ||
resolve: '', | ||
as: 'address', | ||
selectedNameServer: undefined, | ||
addressRecords: undefined, | ||
}, | ||
entry: { | ||
address: '', | ||
amounts: { | ||
[tokenBalanceMocks.ftNoTicker.info.id]: { | ||
...tokenBalanceMocks.ftNoTicker, | ||
quantity: 100n, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
receiver: { | ||
resolve: 'address2', | ||
as: 'address', | ||
addressRecords: undefined, | ||
selectedNameServer: undefined, | ||
}, | ||
entry: { | ||
address: 'address2', | ||
amounts: { | ||
[tokenBalanceMocks.ftNoTicker.info.id]: { | ||
...tokenBalanceMocks.ftNoTicker, | ||
quantity: 200n, | ||
}, | ||
}, | ||
}, | ||
}, | ||
] | ||
const selectedTargetIndex = 0 | ||
const selectedTokenId = tokenBalanceMocks.ftNoTicker.info.id | ||
|
||
const totalUsed = getTotalUsedByOtherTargets({ | ||
targets, | ||
selectedTargetIndex, | ||
selectedTokenId, | ||
}) | ||
|
||
expect(totalUsed).toBe(200n) | ||
}) | ||
|
||
it('should return 0 if there are no other targets', () => { | ||
const targets: Transfer.Target[] = [ | ||
{ | ||
receiver: { | ||
resolve: '', | ||
as: 'address', | ||
selectedNameServer: undefined, | ||
addressRecords: undefined, | ||
}, | ||
entry: { | ||
address: '', | ||
amounts: { | ||
[tokenBalanceMocks.ftNoTicker.info.id]: { | ||
...tokenBalanceMocks.ftNoTicker, | ||
quantity: 100n, | ||
}, | ||
}, | ||
}, | ||
}, | ||
] | ||
const selectedTargetIndex = 0 | ||
const selectedTokenId = tokenBalanceMocks.ftNoTicker.info.id | ||
|
||
const totalUsed = getTotalUsedByOtherTargets({ | ||
targets, | ||
selectedTargetIndex, | ||
selectedTokenId, | ||
}) | ||
|
||
expect(totalUsed).toBe(0n) | ||
}) | ||
|
||
it('should return 0 if the selected token is not used by other targets', () => { | ||
const targets: Transfer.Target[] = [ | ||
{ | ||
receiver: { | ||
resolve: '', | ||
as: 'address', | ||
selectedNameServer: undefined, | ||
addressRecords: undefined, | ||
}, | ||
entry: { | ||
address: '', | ||
amounts: { | ||
[tokenBalanceMocks.ftNoTicker.info.id]: { | ||
...tokenBalanceMocks.ftNoTicker, | ||
quantity: 100n, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
receiver: { | ||
resolve: 'address2', | ||
as: 'address', | ||
addressRecords: undefined, | ||
selectedNameServer: undefined, | ||
}, | ||
entry: { | ||
address: 'address2', | ||
amounts: { | ||
[tokenBalanceMocks.ftNoTicker.info.id]: { | ||
...tokenBalanceMocks.ftNoTicker, | ||
quantity: 200n, | ||
}, | ||
}, | ||
}, | ||
}, | ||
] | ||
const selectedTargetIndex = 0 | ||
const selectedTokenId = 'anyOther.token' | ||
|
||
const totalUsed = getTotalUsedByOtherTargets({ | ||
targets, | ||
selectedTargetIndex, | ||
selectedTokenId, | ||
}) | ||
|
||
expect(totalUsed).toBe(0n) | ||
}) | ||
}) |
26 changes: 26 additions & 0 deletions
26
packages/transfer/src/helpers/get-total-used-by-other-targets.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, Transfer} from '@yoroi/types' | ||
|
||
/** | ||
* @summary Returns the total amount of tokens used by other targets | ||
* @returns BigInt | ||
*/ | ||
export function getTotalUsedByOtherTargets({ | ||
targets, | ||
selectedTargetIndex, | ||
selectedTokenId, | ||
}: { | ||
targets: Readonly<Transfer.Targets> | ||
selectedTargetIndex: number | ||
selectedTokenId: Portfolio.Token.Id | ||
}) { | ||
const isNotTheSelectedTarget = (_: Transfer.Target, index: number) => | ||
index !== selectedTargetIndex | ||
|
||
return targets | ||
.filter(isNotTheSelectedTarget) | ||
.reduce( | ||
(acc, target) => | ||
acc + (target.entry.amounts[selectedTokenId]?.quantity ?? 0n), | ||
0n, | ||
) | ||
} |
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,9 @@ | ||
import {PortfolioTokenAmount} from './amount' | ||
import {PortfolioTokenId} from './token' | ||
|
||
export type PortfolioTokenBalances = { | ||
records: Readonly<Map<PortfolioTokenId, PortfolioTokenAmount>> | ||
all: ReadonlyArray<PortfolioTokenAmount> | ||
fts: ReadonlyArray<PortfolioTokenAmount> | ||
nfts: ReadonlyArray<PortfolioTokenAmount> | ||
} |
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