-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat(minifront): #1149: add priority sorting to the assets table * chore: changeset * fix(minifront): #1149: update after the merge * feat(services, minifront): #1149: assign priorityScores in services package * fix(minifront): #1149: update priority score sorting * chore: changesets * fix(services): #1149: update scoring assignment
- Loading branch information
Showing
12 changed files
with
185 additions
and
42 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@penumbra-zone/services': minor | ||
--- | ||
|
||
Add priority scores to the asset metadata |
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 @@ | ||
--- | ||
'minifront': patch | ||
--- | ||
|
||
Add priority sorting to the assets table |
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 was deleted.
Oops, something went wrong.
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,23 @@ | ||
import { BalancesResponse } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/view/v1/view_pb'; | ||
import { | ||
getMetadataFromBalancesResponseOptional, | ||
getAmount, | ||
} from '@penumbra-zone/getters/balances-response'; | ||
import { multiplyAmountByNumber, joinLoHiAmount } from '@penumbra-zone/types/amount'; | ||
|
||
export const sortByPriorityScore = (a: BalancesResponse, b: BalancesResponse) => { | ||
const aScore = getMetadataFromBalancesResponseOptional(a)?.priorityScore ?? 1n; | ||
const bScore = getMetadataFromBalancesResponseOptional(b)?.priorityScore ?? 1n; | ||
|
||
const aAmount = getAmount.optional()(a); | ||
const bAmount = getAmount.optional()(b); | ||
|
||
const aPriority = aAmount | ||
? joinLoHiAmount(multiplyAmountByNumber(aAmount, Number(aScore))) | ||
: aScore; | ||
const bPriority = bAmount | ||
? joinLoHiAmount(multiplyAmountByNumber(bAmount, Number(bScore))) | ||
: bScore; | ||
|
||
return Number(bPriority - aPriority); | ||
}; |
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
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
67 changes: 67 additions & 0 deletions
67
packages/services/src/view-service/util/asset-priority-score.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,67 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { getAssetPriorityScore } from './asset-priority-score'; | ||
import { | ||
AssetId, | ||
Metadata, | ||
} from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb'; | ||
import { base64ToUint8Array } from '@penumbra-zone/types/base64'; | ||
|
||
describe('getAssetPriorityScore', () => { | ||
const umTokenId = 'KeqcLzNx9qSH5+lcJHBB9KNW+YPrBk5dKzvPMiypahA='; | ||
const umToken = new AssetId({ inner: base64ToUint8Array(umTokenId) }); | ||
const delegationTokenId = '/5AHh95RAybBbUhQ5zXMWCvstH4rRK/5KMVIVGQltAw='; | ||
const usdcTokenId = 'A/8PdbaWqFds9NiYzmAN75SehGpkLwr7tgoVmwaIVgg='; | ||
|
||
it('returns 0 for the undefined metadata', () => { | ||
expect(getAssetPriorityScore(undefined, umToken)).toBe(0n); | ||
}); | ||
|
||
it('returns 10 for an unbonding token', () => { | ||
const metadata = new Metadata({ | ||
display: 'unbonding_start_at_100_penumbravalid1abc123', | ||
}); | ||
|
||
expect(getAssetPriorityScore(metadata, umToken)).toBe(10n); | ||
}); | ||
|
||
it('returns 20 for a delegation token', () => { | ||
const metadata = new Metadata({ | ||
display: | ||
'delegation_penumbravalid1sqwq8p8fqxx4aflthtwmu6kte8je7sh4tj7pyd82qpvdap5ajgrsv0q0ja', | ||
penumbraAssetId: { | ||
inner: base64ToUint8Array(delegationTokenId), | ||
}, | ||
}); | ||
|
||
expect(getAssetPriorityScore(metadata, umToken)).toBe(20n); | ||
}); | ||
|
||
it('returns 30 for an auction token', () => { | ||
const metadata = new Metadata({ | ||
display: 'auctionnft_0_pauctid1abc123', | ||
}); | ||
|
||
expect(getAssetPriorityScore(metadata, umToken)).toBe(30n); | ||
}); | ||
|
||
it('returns 40 for an token within registry', () => { | ||
const metadata = new Metadata({ | ||
display: 'transfer/channel-7/usdc', | ||
penumbraAssetId: { | ||
inner: base64ToUint8Array(usdcTokenId), | ||
}, | ||
}); | ||
|
||
expect(getAssetPriorityScore(metadata, umToken)).toBe(40n); | ||
}); | ||
|
||
it('returns 50 for the UM token', () => { | ||
const metadata = new Metadata({ | ||
penumbraAssetId: { | ||
inner: base64ToUint8Array(umTokenId), | ||
}, | ||
}); | ||
|
||
expect(getAssetPriorityScore(metadata, umToken)).toBe(50n); | ||
}); | ||
}); |
50 changes: 50 additions & 0 deletions
50
packages/services/src/view-service/util/asset-priority-score.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,50 @@ | ||
import { assetPatterns } from '@penumbra-zone/types/assets'; | ||
import { | ||
AssetId, | ||
Metadata, | ||
} from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb'; | ||
|
||
/** | ||
* Assigns a priority score to an asset based on its metadata. | ||
* The higher the score, the earlier the asset will be displayed in the UI. | ||
* - UM → 50 da | ||
* - Normal and IBC denoms → 40 | ||
* - Auctions → 30 | ||
* - Delegations → 20 | ||
* - Unbondings, proposals, voting receipts → 10 | ||
* - Unknown → 0 | ||
* | ||
* If a user has the balance of the asset, then the balance amount should be multiplied by this score in a sorting function. | ||
* | ||
* @param metadata {Metadata} – Asset metadata to assign the priority score | ||
* @param nativeTokenId {AssetId} – AssetId of the native chain token (UM) | ||
*/ | ||
export const getAssetPriorityScore = ( | ||
metadata: Metadata | undefined, | ||
nativeTokenId: AssetId, | ||
): bigint => { | ||
if (!metadata) return 0n; | ||
|
||
if (metadata.penumbraAssetId?.equals(nativeTokenId)) { | ||
return 50n; | ||
} | ||
|
||
if (assetPatterns.ibc.matches(metadata.display)) return 40n; | ||
|
||
if ( | ||
assetPatterns.auctionNft.matches(metadata.display) || | ||
assetPatterns.lpNft.matches(metadata.display) | ||
) | ||
return 30n; | ||
|
||
if (assetPatterns.delegationToken.matches(metadata.display)) return 20n; | ||
|
||
if ( | ||
assetPatterns.unbondingToken.matches(metadata.display) || | ||
assetPatterns.proposalNft.matches(metadata.display) || | ||
assetPatterns.votingReceipt.matches(metadata.display) | ||
) | ||
return 10n; | ||
|
||
return 40n; | ||
}; |