Skip to content

Commit

Permalink
chore(transfer) changed the allocated behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
stackchain committed May 4, 2024
1 parent 82d7c10 commit 1ec97fc
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ describe('targetGetAllocatedToOthersByToken', () => {
},
]
const targetIndex = 0
const tokenId = tokenBalanceMocks.ftNoTicker.info.id

const totalUsed = targetGetAllocatedToOthersByToken({
targets,
targetIndex,
tokenId,
})

expect(totalUsed).toBe(200n)
expect(totalUsed).toEqual(
new Map([
['14696a4676909f4e3cb1f2e60e2e08e5abed70caf5c02699be971139.3032', 200n],
]),
)
})

it('should return 0 if there are no other targets', () => {
Expand All @@ -73,18 +75,16 @@ describe('targetGetAllocatedToOthersByToken', () => {
},
]
const targetIndex = 0
const tokenId = tokenBalanceMocks.ftNoTicker.info.id

const totalUsed = targetGetAllocatedToOthersByToken({
targets,
targetIndex,
tokenId,
})

expect(totalUsed).toBe(0n)
expect(totalUsed).toEqual(new Map())
})

it('should return 0 if the selected token is not used by other targets', () => {
it('should return 0 if empty on other targets', () => {
const targets: Transfer.Target[] = [
{
receiver: {
Expand All @@ -103,6 +103,55 @@ describe('targetGetAllocatedToOthersByToken', () => {
},
},
},
{
receiver: {
resolve: '',
as: 'address',
selectedNameServer: undefined,
addressRecords: undefined,
},
entry: {
address: '',
amounts: {
[tokenBalanceMocks.ftNoTicker.info.id]: {
...tokenBalanceMocks.ftNoTicker,
quantity: 0n,
},
},
},
},
]
const targetIndex = 0

const totalUsed = targetGetAllocatedToOthersByToken({
targets,
targetIndex,
})

expect(totalUsed).toEqual(
new Map([[tokenBalanceMocks.ftNoTicker.info.id, 0n]]),
)
})

it('should return the other tokens by other targets', () => {
const targets: Transfer.Target[] = [
{
receiver: {
resolve: '',
as: 'address',
selectedNameServer: undefined,
addressRecords: undefined,
},
entry: {
address: '',
amounts: {
[tokenBalanceMocks.primaryETH.info.id]: {
...tokenBalanceMocks.ftNoTicker,
quantity: 100n,
},
},
},
},
{
receiver: {
resolve: 'address2',
Expand All @@ -122,14 +171,16 @@ describe('targetGetAllocatedToOthersByToken', () => {
},
]
const targetIndex = 0
const tokenId = 'anyOther.token'

const totalUsed = targetGetAllocatedToOthersByToken({
targets,
targetIndex,
tokenId,
})

expect(totalUsed).toBe(0n)
expect(totalUsed).toEqual(
new Map([
['14696a4676909f4e3cb1f2e60e2e08e5abed70caf5c02699be971139.3032', 200n],
]),
)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,33 @@ import {Portfolio, Transfer} from '@yoroi/types'

/**
* @summary Returns the total amount of tokens used by other targets
* @returns BigInt
* @returns Map<Portfolio.Token.Id, bigint>
*/
export function targetGetAllocatedToOthersByToken({
targets,
targetIndex,
tokenId,
}: {
targets: Readonly<Transfer.Targets>
targetIndex: number
tokenId: Portfolio.Token.Id
}) {
const isNotTheSelectedTarget = (_: Transfer.Target, index: number) =>
index !== targetIndex

return targets
.filter(isNotTheSelectedTarget)
.reduce(
(acc, target) => acc + (target.entry.amounts[tokenId]?.quantity ?? 0n),
0n,
)
const tokenAllocations = new Map<Portfolio.Token.Id, bigint>()

targets.filter(isNotTheSelectedTarget).forEach((target) => {
const amounts = target.entry.amounts

Object.keys(amounts).forEach((untypedTokenId) => {
const tokenId = untypedTokenId as Portfolio.Token.Id
const quantity = amounts[tokenId]?.quantity || 0n

const currentAllocation = tokenAllocations.get(tokenId) ?? 0n
const newAllocation = currentAllocation + quantity

tokenAllocations.set(tokenId, newAllocation)
})
})

return tokenAllocations
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,50 @@ describe('TransferAllocatedToOtherTargets', () => {
},
},
},
{
receiver: {
resolve: 'address2',
as: 'address',
addressRecords: undefined,
selectedNameServer: undefined,
},
entry: {
address: 'address2',
amounts: {
[tokenBalanceMocks.ftNoTicker.info.id]: {
...tokenBalanceMocks.ftNoTicker,
quantity: 50n,
},
[tokenBalanceMocks.primaryETH.info.id]: {
...tokenBalanceMocks.primaryETH,
quantity: 50n,
},
},
},
},
]

const result = targetGetAllocatedToOthers({targets})

expect(result.size).toEqual(2)
expect(result.get(0)?.get(tokenBalanceMocks.ftNoTicker.info.id)).toEqual({
allocated: 200n,
})
expect(result.get(1)?.get(tokenBalanceMocks.ftNoTicker.info.id)).toEqual({
allocated: 100n,
})
expect(result.size).toEqual(3)
expect(result).toEqual(
new Map([
[
0,
new Map([
[tokenBalanceMocks.ftNoTicker.info.id, 250n],
[tokenBalanceMocks.primaryETH.info.id, 50n],
]),
],
[
1,
new Map([
[tokenBalanceMocks.ftNoTicker.info.id, 150n],
[tokenBalanceMocks.primaryETH.info.id, 50n],
]),
],
[2, new Map([[tokenBalanceMocks.ftNoTicker.info.id, 300n]])],
]),
)
})
})
23 changes: 6 additions & 17 deletions packages/transfer/src/helpers/target-get-allocated-to-others.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Portfolio, Transfer} from '@yoroi/types'
import {Transfer} from '@yoroi/types'
import {freeze} from 'immer'

import {TransferAllocatedToOtherTargets} from '../types'
Expand All @@ -11,22 +11,11 @@ export function targetGetAllocatedToOthers({
}) {
const usedByOtherTargets: TransferAllocatedToOtherTargets = new Map()

targets.forEach((target, targetIndex) => {
Object.keys(target.entry.amounts).forEach((untypedTokenId) => {
const tokenId = untypedTokenId as Portfolio.Token.Id
const allocated = targetGetAllocatedToOthersByToken({
targets,
targetIndex,
tokenId,
})
const currentTarget = usedByOtherTargets.get(targetIndex) ?? new Map()

currentTarget.set(tokenId, {
allocated,
})

usedByOtherTargets.set(targetIndex, currentTarget)
})
targets.forEach((_, targetIndex) => {
usedByOtherTargets.set(
targetIndex,
targetGetAllocatedToOthersByToken({targets, targetIndex}),
)
})

return freeze(usedByOtherTargets, true)
Expand Down

0 comments on commit 1ec97fc

Please sign in to comment.