Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
199b892
commit c3300b0
Showing
11 changed files
with
177 additions
and
394 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
55 changes: 55 additions & 0 deletions
55
packages/transfer/src/helpers/target-get-allocated-in-others.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,55 @@ | ||
import {Transfer} from '@yoroi/types' | ||
import {tokenBalanceMocks} from '@yoroi/portfolio' | ||
|
||
import {targetGetUsedByOtherTargets} from './target-get-allocated-in-others' | ||
|
||
describe('targetGetUsedByOtherTargets()', () => { | ||
it('should calculate balances breakdown correctly', () => { | ||
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 result = targetGetUsedByOtherTargets({targets}) | ||
|
||
expect(result.size).toEqual(1) | ||
expect(result.get(0)?.get(tokenBalanceMocks.ftNoTicker.info.id)).toEqual({ | ||
used: 200n, | ||
}) | ||
expect(result.get(0)?.get(tokenBalanceMocks.ftNoTicker.info.id)).toEqual({ | ||
used: 100n, | ||
}) | ||
}) | ||
}) |
31 changes: 31 additions & 0 deletions
31
packages/transfer/src/helpers/target-get-allocated-in-others.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, Transfer} from '@yoroi/types' | ||
import {freeze} from 'immer' | ||
|
||
import {TransferUsedByOtherTargets} from '../types' | ||
import {targetGetTokenTotalUsedByOthers} from './target-get-token-allocated-to-others' | ||
|
||
export function targetGetUsedByOtherTargets({ | ||
targets, | ||
}: { | ||
targets: Readonly<Transfer.Targets> | ||
}) { | ||
const usedByOtherTargets: TransferUsedByOtherTargets = new Map() | ||
|
||
targets.forEach((target, targetIndex) => { | ||
Object.keys(target.entry.amounts).forEach((untypedTokenId) => { | ||
const tokenId = untypedTokenId as Portfolio.Token.Id | ||
const allocated = targetGetTokenTotalUsedByOthers({ | ||
targets, | ||
targetIndex, | ||
tokenId, | ||
}) | ||
const currentTarget = usedByOtherTargets.get(targetIndex) ?? new Map() | ||
|
||
currentTarget.set(tokenId, { | ||
allocated, | ||
}) | ||
}) | ||
}) | ||
|
||
return freeze(usedByOtherTargets, true) | ||
} |
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.