-
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.
Update regex parsing + unbonding case
- Loading branch information
Showing
6 changed files
with
101 additions
and
58 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,32 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
import { Metadata } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb'; | ||
import { customizeSymbol } from './customize-symbol'; | ||
|
||
describe('Customizing metadata', () => { | ||
test('should work for delegation token', () => { | ||
const metadata = new Metadata({ | ||
display: | ||
'delegation_penumbravalid1fjuj67ayaqueqxg03d65ps5aah6m39u39qeacu3zv2cw3dzxssyq3yrcez', | ||
}); | ||
customizeSymbol(metadata); | ||
expect(metadata.symbol).toBe('Delegated UM (fjuj67ayaqueqxg03d65ps5aa...)'); | ||
}); | ||
|
||
test('should work for unbonding token', () => { | ||
const metadata = new Metadata({ | ||
display: | ||
'uunbonding_epoch_29_penumbravalid1fjuj67ayaqueqxg03d65ps5aah6m39u39qeacu3zv2cw3dzxssyq3yrcez', | ||
}); | ||
customizeSymbol(metadata); | ||
expect(metadata.symbol).toBe('Unbonding UM, epoch 29 (fjuj67ayaqueqxg03d65ps5aa...)'); | ||
}); | ||
|
||
test('should do nothing if no matches', () => { | ||
const metadata = new Metadata({ | ||
display: 'test_usd', | ||
symbol: 'usdc', | ||
}); | ||
customizeSymbol(metadata); | ||
expect(metadata.symbol).toBe('usdc'); | ||
}); | ||
}); |
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,27 @@ | ||
import { Metadata } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb'; | ||
import { | ||
assetPatterns, | ||
DelegationCaptureGroups, | ||
UnbondingCaptureGroups, | ||
} from '@penumbra-zone/constants'; | ||
|
||
const DELEGATION_SYMBOL_LENGTH = 50 - 'delegation_penumbravalid1'.length; | ||
const UNBONDING_SYMBOL_LENGTH = 41 - 'unbonding_epoch_'.length; | ||
|
||
// If the metadata is for a delegation or unbonding tokens, customize its symbol. | ||
// We can't trust the validator's self-described name, so use their validator ID (in metadata.display). | ||
export const customizeSymbol = (metadata: Metadata) => { | ||
const delegationMatch = assetPatterns.delegationToken.exec(metadata.display); | ||
if (delegationMatch) { | ||
const { id } = delegationMatch.groups as unknown as DelegationCaptureGroups; | ||
const shortenedId = id.slice(0, DELEGATION_SYMBOL_LENGTH); | ||
metadata.symbol = `Delegated UM (${shortenedId}...)`; | ||
} | ||
|
||
const unbondingMatch = assetPatterns.unbondingToken.exec(metadata.display); | ||
if (unbondingMatch) { | ||
const { id, epoch } = unbondingMatch.groups as unknown as UnbondingCaptureGroups; | ||
const shortenedId = id.slice(0, UNBONDING_SYMBOL_LENGTH); | ||
metadata.symbol = `Unbonding UM, epoch ${epoch} (${shortenedId}...)`; | ||
} | ||
}; |
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