Skip to content

Commit

Permalink
Fix typo, extract constants
Browse files Browse the repository at this point in the history
  • Loading branch information
danielailie committed Nov 7, 2024
1 parent a564486 commit e3ab881
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ export class TokenTransfer {
}

export class TokenComputer {
TOKEN_RANDOM_SEQUENCE_LENGTH = 6;
constructor() {}

isFungible(token: Token): boolean {
Expand All @@ -249,24 +250,25 @@ export class TokenComputer {

extractIdentifierFromExtendedIdentifier(identifier: string): string {
const parts = identifier.split("-");
const { prefix, ticker, randomnes } = this.splitIdentifierIntoComponent(parts);
const { prefix, ticker, randomSequence } = this.splitIdentifierIntoComponents(parts);

Check failure on line 253 in src/tokens.ts

View workflow job for this annotation

GitHub Actions / integration_tests

Argument of type 'string[]' is not assignable to parameter of type 'string'.

this.checkIfExtendedIdentifierWasProvided(parts);
this.ensureTokenTickerValidity(ticker);
this.checkLengthOfRandomSequence(randomnes);
this.checkLengthOfRandomSequence(randomSequence);
if (prefix) {
this.checkLengthOfPrefix(prefix);
return prefix + "-" + ticker + "-" + randomnes;
return prefix + "-" + ticker + "-" + randomSequence;
}
return ticker + "-" + randomnes;
return ticker + "-" + randomSequence;
}

splitIdentifierIntoComponent(parts: string[]): { prefix: any; ticker: any; randomnes: any } {
if (parts.length >= 3 && parts[2].length === 6) {
return { prefix: parts[0], ticker: parts[1], randomnes: parts[2] };
private splitIdentifierIntoComponents(identifier: string): { prefix: any; ticker: any; randomSequence: any } {
const parts = identifier.split("-");
if (parts.length >= 3 && parts[2].length === this.TOKEN_RANDOM_SEQUENCE_LENGTH) {
return { prefix: parts[0], ticker: parts[1], randomSequence: parts[2] };
}

return { prefix: null, ticker: parts[0], randomnes: parts[1] };
return { prefix: null, ticker: parts[0], randomSequence: parts[1] };
}

private checkIfExtendedIdentifierWasProvided(tokenParts: string[]): void {
Expand All @@ -284,18 +286,17 @@ export class TokenComputer {
}

private checkLengthOfRandomSequence(randomSequence: string): void {
const TOKEN_RANDOM_SEQUENCE_LENGTH = 6;

if (randomSequence.length !== TOKEN_RANDOM_SEQUENCE_LENGTH) {
if (randomSequence.length !== this.TOKEN_RANDOM_SEQUENCE_LENGTH) {
throw new ErrInvalidTokenIdentifier(
"The identifier is not valid. The random sequence does not have the right length",
);
}
}

private checkLengthOfPrefix(prefix: string): void {
const TOKEN_PREFIX_LENGTH = 4;
if (prefix.length > TOKEN_PREFIX_LENGTH) {
const MAX_TOKEN_PREFIX_LENGTH = 4;
const MIN_TOKEN_PREFIX_LENGTH = 1;
if (prefix.length < MIN_TOKEN_PREFIX_LENGTH || prefix.length > MAX_TOKEN_PREFIX_LENGTH) {
throw new ErrInvalidTokenIdentifier(
"The identifier is not valid. The prefix does not have the right length",
);
Expand Down

0 comments on commit e3ab881

Please sign in to comment.