Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions __tests__/storage/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
OutputValueType,
WALLET_FLAGS,
TokenVersion,
ApiVersion,
} from '../../src/types';

describe('handleStop', () => {
Expand Down Expand Up @@ -218,17 +219,39 @@ describe('config version', () => {
it('should get native token from version', async () => {
const store = new MemoryStore();
const storage = new Storage(store);
// No version set: should use DEFAULT_NATIVE_TOKEN_CONFIG
expect(storage.getNativeTokenData()).toEqual({
...DEFAULT_NATIVE_TOKEN_CONFIG,
uid: NATIVE_TOKEN_UID,
});

// Fullnode provides native_token without version field: should default to NATIVE
const version = { native_token: { name: 'Native', symbol: 'N' } };
storage.setApiVersion(version);
expect(storage.getNativeTokenData()).toEqual({
version: TokenVersion.NATIVE,
name: 'Native',
symbol: 'N',
uid: NATIVE_TOKEN_UID,
});

// Fullnode explicitly provides version: should preserve it
storage.setApiVersion({
native_token: { name: 'Hathor', symbol: 'HTR', version: TokenVersion.NATIVE },
} as ApiVersion);
expect(storage.getNativeTokenData().version).toBe(TokenVersion.NATIVE);

// Fullnode provides an unknown version: should forward it as-is
storage.setApiVersion({
native_token: { name: 'Hathor', symbol: 'HTR', version: 99 as number },
} as ApiVersion);
expect(storage.getNativeTokenData().version).toBe(99);

// native_token is null: should fall back to DEFAULT_NATIVE_TOKEN_CONFIG
storage.setApiVersion({ native_token: null } as ApiVersion);
expect(storage.getNativeTokenData().version).toBe(TokenVersion.NATIVE);

// Version reset to null: should fall back to DEFAULT_NATIVE_TOKEN_CONFIG
storage.setApiVersion(null);
expect(storage.getNativeTokenData()).toEqual({
...DEFAULT_NATIVE_TOKEN_CONFIG,
Expand All @@ -247,16 +270,19 @@ describe('config version', () => {
});
});

it('should save native token from version', async () => {
it('should save native token from version with version NATIVE', async () => {
const store = new MemoryStore();
const storage = new Storage(store);
await expect(storage.getToken(NATIVE_TOKEN_UID)).resolves.toEqual(null);
// Fullnode response without version field
const version = { native_token: { name: 'Native', symbol: 'N' } };
storage.setApiVersion(version);
await storage.saveNativeToken();
await expect(storage.getToken(NATIVE_TOKEN_UID)).resolves.toMatchObject({
const saved = await storage.getToken(NATIVE_TOKEN_UID);
expect(saved).toMatchObject({
name: 'Native',
symbol: 'N',
version: TokenVersion.NATIVE,
uid: NATIVE_TOKEN_UID,
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/storage/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
ILogger,
getDefaultLogger,
AuthorityType,
TokenVersion,
} from '../types';
import transactionUtils from '../utils/transaction';
import {
Expand Down Expand Up @@ -140,8 +141,7 @@ export class Storage implements IStorage {
*/
getNativeTokenData(): ITokenData {
const nativeToken = this.version?.native_token ?? DEFAULT_NATIVE_TOKEN_CONFIG;

return { ...nativeToken, uid: NATIVE_TOKEN_UID };
return { version: TokenVersion.NATIVE, ...nativeToken, uid: NATIVE_TOKEN_UID };
}

/**
Expand Down
Loading