Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove hardcoded base token dependencies from the app #8160

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion packages/desktop/components/popups/NodeInfoPopup.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import { onMount } from 'svelte'
import { Button, Checkbox, CopyableBox, Spinner, Text } from 'shared/components'
import { formatNumber, localize } from '@core/i18n'
import { INode, INodeInfo } from '@core/network'
import { INode } from '@core/network'
import { INodeInfo } from '@iota/sdk/out/types'
import { closePopup } from '@auxiliary/popup'
import { showAppNotification } from '@auxiliary/notification'
import { resolveObjectPath, setClipboard } from '@core/utils'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ export function updateShimmerClaimingAccount(shimmerClaimingAccount: IShimmerCla
updateOnboardingProfile({ shimmerClaimingAccounts })
}

export function getOnboardingBaseToken(): IBaseToken {
export function getOnboardingBaseToken(): IBaseToken | undefined {
return get(onboardingProfile)?.network?.baseToken
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,83 +1,37 @@
import { COIN_TYPE, DEFAULT_BASE_TOKEN } from '../constants'
import { NetworkId, TokenSupply } from '../enums'
import { COIN_TYPE } from '../constants'
import { NetworkId } from '../enums'
import { IStardustNetworkMetadata } from '../interfaces'
import { NetworkMetadata } from '../types'
import { nodeInfoBaseToken, nodeInfoProtocol } from '../stores/node-info.store'
import { get } from 'svelte/store'

export const DEFAULT_NETWORK_METADATA: Readonly<{ [key in NetworkId]?: NetworkMetadata }> = {
[NetworkId.Shimmer]: <IStardustNetworkMetadata>{
id: NetworkId.Shimmer,
name: 'Shimmer',
coinType: COIN_TYPE[NetworkId.Shimmer],
protocol: {
version: 2,
networkName: 'shimmer',
bech32Hrp: 'smr',
minPowScore: 1500,
belowMaxDepth: 15,
rentStructure: {
vByteCost: 100,
vByteFactorData: 1,
vByteFactorKey: 10,
},
tokenSupply: TokenSupply.Shimmer,
},
baseToken: DEFAULT_BASE_TOKEN[NetworkId.Shimmer],
protocol: get(nodeInfoProtocol),
baseToken: get(nodeInfoBaseToken),
cpl121 marked this conversation as resolved.
Show resolved Hide resolved
},
[NetworkId.Testnet]: <IStardustNetworkMetadata>{
id: NetworkId.Testnet,
name: 'Testnet',
coinType: COIN_TYPE[NetworkId.Testnet],
protocol: {
version: 2,
networkName: 'testnet',
bech32Hrp: 'rms',
minPowScore: 1500,
belowMaxDepth: 15,
rentStructure: {
vByteCost: 100,
vByteFactorData: 1,
vByteFactorKey: 10,
},
tokenSupply: TokenSupply.Testnet,
},
baseToken: DEFAULT_BASE_TOKEN[NetworkId.Testnet],
protocol: get(nodeInfoProtocol),
baseToken: get(nodeInfoBaseToken),
},
[NetworkId.Iota]: <IStardustNetworkMetadata>{
id: NetworkId.Iota,
name: 'IOTA',
coinType: COIN_TYPE[NetworkId.Iota],
protocol: {
version: 2,
networkName: 'iota',
bech32Hrp: 'iota',
minPowScore: 1500,
belowMaxDepth: 15,
rentStructure: {
vByteCost: 250,
vByteFactorData: 1,
vByteFactorKey: 10,
},
tokenSupply: TokenSupply.Iota,
},
baseToken: DEFAULT_BASE_TOKEN[NetworkId.Iota],
protocol: get(nodeInfoProtocol),
baseToken: get(nodeInfoBaseToken),
},
[NetworkId.IotaAlphanet]: <IStardustNetworkMetadata>{
id: NetworkId.IotaAlphanet,
name: 'IOTA Alphanet',
coinType: COIN_TYPE[NetworkId.IotaAlphanet],
protocol: {
version: 2,
networkName: 'iota-alphanet-2',
bech32Hrp: 'atoi',
minPowScore: 1500,
belowMaxDepth: 15,
rentStructure: {
vByteCost: 250,
vByteFactorData: 1,
vByteFactorKey: 10,
},
tokenSupply: TokenSupply.Iota,
},
baseToken: DEFAULT_BASE_TOKEN[NetworkId.IotaAlphanet],
protocol: get(nodeInfoProtocol),
baseToken: get(nodeInfoBaseToken),
},
}
1 change: 0 additions & 1 deletion packages/shared/lib/core/network/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './coin-type.constant'
export * from './default-base-token.constant'
export * from './default-chain-configurations.constant'
export * from './default-max-parallel-api-requests'
export * from './default-network-metadata.constant'
Expand Down
1 change: 0 additions & 1 deletion packages/shared/lib/core/network/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ export * from './network-status.interface'
export * from './node-info-response.interface'
export * from './node.interface'
export * from './persisted-network.interface'
export * from './protocol.interface'
export * from './rent-structure.interface'
export * from './stardust-network-metadata.interface'
11 changes: 0 additions & 11 deletions packages/shared/lib/core/network/interfaces/protocol.interface.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IBaseToken } from '@core/wallet'
import { NetworkId } from '../enums'
import { IProtocol } from './protocol.interface'
import { INodeInfoProtocol } from '@iota/sdk'

/**
* Holds relevant data
Expand All @@ -11,6 +11,6 @@ export interface IStardustNetworkMetadata {
id: NetworkId
name: string
coinType: number
protocol: IProtocol
protocol: INodeInfoProtocol
baseToken: IBaseToken
}
6 changes: 5 additions & 1 deletion packages/shared/lib/core/network/stores/node-info.store.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { INodeInfo } from '@iota/sdk/out/types'
import { writable } from 'svelte/store'
import { writable, derived } from 'svelte/store'

export const nodeInfo = writable<INodeInfo | undefined>(undefined)

export function setNodeInfo(newNodeInfo: INodeInfo | undefined): void {
return nodeInfo.set(newNodeInfo)
}

export const nodeInfoBaseToken = derived(nodeInfo, ($nodeInfo) => $nodeInfo?.baseToken)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point of this store? You don't seem to be reading it from any svelte file so all the reactivity features it comes with are effectively not used at all, thus it seems more appropiate to me to just read the nodeInfo store


export const nodeInfoProtocol = derived(nodeInfo, ($nodeInfo) => $nodeInfo?.protocol)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not used

Suggested change
export const nodeInfoProtocol = derived(nodeInfo, ($nodeInfo) => $nodeInfo?.protocol)

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ export async function checkAndUpdateActiveProfileNetwork(): Promise<void> {
const $activeProfile = get(activeProfile)
const nodeInfoResponse = await getAndUpdateNodeInfo(true)
const networkId = $activeProfile?.network?.id
if (!networkId || networkId === NetworkId.Custom) {
if (
!networkId ||
networkId === NetworkId.Custom ||
!$activeProfile?.network?.protocol ||
!$activeProfile?.network?.baseToken
) {
const network = buildPersistedNetworkFromNodeInfoResponse(nodeInfoResponse)
network.chains = $activeProfile.network?.chains || []
updateActiveProfile({ network })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { INodeInfoBaseToken } from '@iota/sdk/out/types'
import { TokenStandard } from '../enums'

export interface IBaseToken {
export interface IBaseToken extends INodeInfoBaseToken {
standard: TokenStandard.BaseToken
name: string
tickerSymbol?: string
unit: string
subunit?: string | null
decimals: number
useMetricPrefix?: boolean
}
43 changes: 32 additions & 11 deletions packages/shared/lib/core/wallet/tests/convertToRawAmount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { convertToRawAmount } from '../utils'
import { TokenMetadata } from '../types'
import { TokenStandard } from '../enums'
import { MAX_SUPPORTED_DECIMALS } from '../constants'
import { DEFAULT_BASE_TOKEN, NetworkId } from '@core/network'
import { NetworkId } from '@core/network'
import { IBaseToken } from '../interfaces'

const WEB3_TOKEN_METADATA: TokenMetadata = {
name: 'RAWR',
Expand Down Expand Up @@ -35,6 +36,26 @@ const INVALID_TOKEN_METADATA: TokenMetadata = {
decimals: 0,
}

const DEFAULT_IOTA_BASE_TOKEN: IBaseToken = {
standard: TokenStandard.BaseToken,
name: 'IOTA',
tickerSymbol: 'IOTA',
unit: 'IOTA',
decimals: 6,
subunit: 'micro',
useMetricPrefix: false,
}

const DEFAULT_SHIMMER_BASE_TOKEN: IBaseToken = {
standard: TokenStandard.BaseToken,
name: 'Shimmer',
tickerSymbol: 'SMR',
unit: 'SMR',
decimals: 6,
subunit: 'glow',
useMetricPrefix: false,
}

describe('File: convertToRawAmount.ts', () => {
it('should return undefined if amount is empty', () => {
expect(convertToRawAmount('', INVALID_TOKEN_METADATA)).toStrictEqual(undefined)
Expand All @@ -43,40 +64,40 @@ describe('File: convertToRawAmount.ts', () => {
describe('given the tokenMetadata standard is BaseToken', () => {
describe("given useMetricPrefix is false (currently IOTA's case)", () => {
it("should return Big(amount) * decimal property if selectedUnit is unit and baseToken's decimal is less than MAX_SUPPORTED_DECIMALS", () => {
let value = convertToRawAmount('1', DEFAULT_BASE_TOKEN[NetworkId.Iota], 'IOTA')
expect(value).toStrictEqual(Big('1').mul(Big(10).pow(DEFAULT_BASE_TOKEN[NetworkId.Iota].decimals)))
let value = convertToRawAmount('1', DEFAULT_IOTA_BASE_TOKEN, 'IOTA')
expect(value).toStrictEqual(Big('1').mul(Big(10).pow(DEFAULT_IOTA_BASE_TOKEN.decimals)))
})
it("should return XXX if selectedUnit is unit and baseToken's decimals property is greater than MAX_SUPPORTED_DECIMALS", () => {
let value = convertToRawAmount('1', WEB3_TOKEN_METADATA, 'RAWR')
expect(value).toStrictEqual(Big('1').mul(Big(10).pow(MAX_SUPPORTED_DECIMALS)))
})
it('should return same Big(amount) if selectedUnit is subunit', () => {
expect(convertToRawAmount('1', DEFAULT_BASE_TOKEN[NetworkId.Iota], 'micro')).toStrictEqual(Big('1'))
expect(convertToRawAmount('1', DEFAULT_IOTA_BASE_TOKEN, 'micro')).toStrictEqual(Big('1'))
})
it('should return undefined if a unit is not provided', () => {
expect(convertToRawAmount('1', DEFAULT_BASE_TOKEN[NetworkId.Iota])).toStrictEqual(undefined)
expect(convertToRawAmount('1', DEFAULT_IOTA_BASE_TOKEN)).toStrictEqual(undefined)
})
it('should return undefined if provided unit does not match the tokenMetadata unit or subunit', () => {
expect(convertToRawAmount('1', DEFAULT_BASE_TOKEN[NetworkId.Iota], 'test')).toStrictEqual(undefined)
expect(convertToRawAmount('1', DEFAULT_IOTA_BASE_TOKEN, 'test')).toStrictEqual(undefined)
})
})
describe("given useMetricPrefix is false (currently Shimmer's case)", () => {
it("should return Big(amount) * decimal property if selectedUnit is unit and baseToken's decimal is less than MAX_SUPPORTED_DECIMALS", () => {
let value = convertToRawAmount('1', DEFAULT_BASE_TOKEN[NetworkId.Shimmer], 'SMR')
expect(value).toStrictEqual(Big('1').mul(Big(10).pow(DEFAULT_BASE_TOKEN[NetworkId.Shimmer].decimals)))
let value = convertToRawAmount('1', DEFAULT_SHIMMER_BASE_TOKEN, 'SMR')
expect(value).toStrictEqual(Big('1').mul(Big(10).pow(DEFAULT_SHIMMER_BASE_TOKEN.decimals)))
})
it("should return XXX if selectedUnit is unit and baseToken's decimals property is greater than MAX_SUPPORTED_DECIMALS", () => {
let value = convertToRawAmount('1', WEB3_TOKEN_METADATA, 'RAWR')
expect(value).toStrictEqual(Big('1').mul(Big(10).pow(MAX_SUPPORTED_DECIMALS)))
})
it('should return same Big(amount) if selectedUnit is subunit', () => {
expect(convertToRawAmount('1', DEFAULT_BASE_TOKEN[NetworkId.Shimmer], 'glow')).toStrictEqual(Big('1'))
expect(convertToRawAmount('1', DEFAULT_SHIMMER_BASE_TOKEN, 'glow')).toStrictEqual(Big('1'))
})
it('should return undefined if a unit is not provided', () => {
expect(convertToRawAmount('1', DEFAULT_BASE_TOKEN[NetworkId.Shimmer])).toStrictEqual(undefined)
expect(convertToRawAmount('1', DEFAULT_SHIMMER_BASE_TOKEN)).toStrictEqual(undefined)
})
it('should return undefined if provided unit does not match the tokenMetadata unit or subunit', () => {
expect(convertToRawAmount('1', DEFAULT_BASE_TOKEN[NetworkId.Shimmer], 'test')).toStrictEqual(undefined)
expect(convertToRawAmount('1', DEFAULT_SHIMMER_BASE_TOKEN, 'test')).toStrictEqual(undefined)
})
})
})
Expand Down
Loading