Skip to content

Commit

Permalink
Merge pull request #43 from nabla-studio/DavideSegullo/refactor-utils
Browse files Browse the repository at this point in the history
Refactor Utils
  • Loading branch information
DavideSegullo authored Jan 3, 2024
2 parents c0afeda + c1b3b70 commit 6c64dca
Show file tree
Hide file tree
Showing 19 changed files with 379 additions and 213 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@
"react": "18.2.0",
"react-dom": "18.2.0",
"react-qr-code": "^2.0.12",
"semver": "^7.5.4",
"stargazejs": "^0.14.1",
"utf-8-validate": "^6.0.3",
"vue": "^3.3.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/suggest-chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import type { AssetLists, Chain } from '@nabla-studio/chain-registry';
export interface SuggestChain {
name: string;
chain: Chain;
assetList?: AssetLists;
assetList: AssetLists;
}
2 changes: 1 addition & 1 deletion packages/store/src/slices/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const createConnectSlice: StateCreator<
name: chain.chain_name,
assetList: get().assetsLists.find(
(list) => list.chain_name === chain.chain_name,
),
)!,
}));

return suggestChains(wallet.options.name, chains);
Expand Down
1 change: 0 additions & 1 deletion packages/wallets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"@cosmjs/proto-signing": "^0.31.3",
"cosmjs-types": "^0.8.0",
"@keplr-wallet/types": "^0.12.38",
"semver": "^7.5.4",
"long": "^5.2.3",
"@cosmostation/extension-client": "^0.1.15"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/wallets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export * from './cosmostation';
export * from './xdefi';
export * from './station';
export * from './wallet-connect';
export * from './utils';
export * from './types';
7 changes: 2 additions & 5 deletions packages/wallets/src/keplr/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { ExtensionWallet, assertIsDefined } from '@quirks/core';
import type { SignDoc } from 'cosmjs-types/cosmos/tx/v1beta1/tx';
import type { Keplr } from '@keplr-wallet/types';
import Long from 'long';
import { chainRegistryChainToKeplr } from './utils';
import { getChainInfo } from '../utils';

export class KeplrWalletExtension extends ExtensionWallet<Keplr> {
constructor(options: WalletOptions) {
Expand Down Expand Up @@ -156,10 +156,7 @@ export class KeplrWalletExtension extends ExtensionWallet<Keplr> {
assertIsDefined(this.client);

for (const suggestion of suggestions) {
const suggestChain = chainRegistryChainToKeplr(
suggestion.chain,
suggestion.assetList ? [suggestion.assetList] : [],
);
const suggestChain = getChainInfo(suggestion.chain, suggestion.assetList);

await this.client.experimentalSuggestChain(suggestChain);
}
Expand Down
189 changes: 0 additions & 189 deletions packages/wallets/src/keplr/utils.ts

This file was deleted.

9 changes: 3 additions & 6 deletions packages/wallets/src/leap/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import type {
import { ExtensionWallet, assertIsDefined } from '@quirks/core';
import type { SignDoc } from 'cosmjs-types/cosmos/tx/v1beta1/tx';
import Long from 'long';
import { chainRegistryChainToKeplr } from '../keplr/utils';
import { Leap } from './types';
import { getChainInfo } from '../utils';
import type { Leap } from './types';

export class LeapWalletExtension extends ExtensionWallet<Leap> {
constructor(options: WalletOptions) {
Expand Down Expand Up @@ -160,10 +160,7 @@ export class LeapWalletExtension extends ExtensionWallet<Leap> {
assertIsDefined(this.client);

for (const suggestion of suggestions) {
const suggestChain = chainRegistryChainToKeplr(
suggestion.chain,
suggestion.assetList ? [suggestion.assetList] : [],
);
const suggestChain = getChainInfo(suggestion.chain, suggestion.assetList);

await this.client.experimentalSuggestChain(suggestChain);
}
Expand Down
7 changes: 2 additions & 5 deletions packages/wallets/src/station/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { ExtensionWallet, assertIsDefined } from '@quirks/core';
import type { SignDoc } from 'cosmjs-types/cosmos/tx/v1beta1/tx';
import type { Keplr } from '@keplr-wallet/types';
import Long from 'long';
import { chainRegistryChainToKeplr } from '../keplr/utils';
import { getChainInfo } from '../utils';

export class StationWalletExtension extends ExtensionWallet<Keplr> {
constructor(options: WalletOptions) {
Expand Down Expand Up @@ -156,10 +156,7 @@ export class StationWalletExtension extends ExtensionWallet<Keplr> {
assertIsDefined(this.client);

for (const suggestion of suggestions) {
const suggestChain = chainRegistryChainToKeplr(
suggestion.chain,
suggestion.assetList ? [suggestion.assetList] : [],
);
const suggestChain = getChainInfo(suggestion.chain, suggestion.assetList);

await this.client.experimentalSuggestChain(suggestChain);
}
Expand Down
1 change: 1 addition & 0 deletions packages/wallets/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './keplr';
7 changes: 7 additions & 0 deletions packages/wallets/src/types/keplr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Chain } from '@nabla-studio/chain-registry';

export interface ChainInfoOptions {
getRpcEndpoint: (chain: Chain) => string;
getRestEndpoint: (chain: Chain) => string;
getExplorer: (chain: Chain) => string;
}
32 changes: 32 additions & 0 deletions packages/wallets/src/utils/chain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { Bech32Config } from '@keplr-wallet/types';
import type { Chain } from '@nabla-studio/chain-registry';

export const getRpc = (chain: Chain): string =>
chain.apis?.rpc?.[0]?.address ?? '';

export const getRest = (chain: Chain): string =>
chain.apis?.rest?.[0]?.address ?? '';

export const getExplr = (chain: Chain): string =>
chain.explorers?.[0]?.url ?? '';

/**
* Returns bech32 address format as required by Keplr and other wallets
*
* @param prefix Chain bech32 prefix
* @returns a valid `Bech32Config`
*/
export const getBech32Config = (
prefix: string,
validatorPrefix: string = 'val',
consensusPrefix: string = 'cons',
publicPrefix: string = 'pub',
operatorPrefix: string = 'oper',
): Bech32Config => ({
bech32PrefixAccAddr: prefix,
bech32PrefixAccPub: `${prefix}${publicPrefix}`,
bech32PrefixValAddr: `${prefix}${validatorPrefix}${operatorPrefix}`,
bech32PrefixValPub: `${prefix}${validatorPrefix}${operatorPrefix}${publicPrefix}`,
bech32PrefixConsAddr: `${prefix}${validatorPrefix}${consensusPrefix}`,
bech32PrefixConsPub: `${prefix}${validatorPrefix}${consensusPrefix}${publicPrefix}`,
});
3 changes: 3 additions & 0 deletions packages/wallets/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './chain';
export * from './semver';
export * from './wallet';
40 changes: 40 additions & 0 deletions packages/wallets/src/utils/semver.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { satisfies, extractVer, parseVer } from './semver';

const validVer =
'osmosis-labs/[email protected]';
const validVer2 = 'osmosis-labs/[email protected]';
const invalidVer = 'osmosis-labs/cosmos-sdk';

const versionToCheck = '0.40.0';

test('extract valid version', () => {
expect(extractVer(validVer)).toBe('0.45.0');
});

test('extract invalid version', () => {
expect(extractVer(invalidVer)).toBe(null);
});

test('parse version', () => {
expect(parseVer(validVer)).toBe('0.45.0');
});

test('parse version with missing padding', () => {
expect(parseVer(validVer2)).toBe('0.45.0');
});

test('satisfies compare gte', () => {
expect(satisfies(versionToCheck, '>=0.40.0')).toBeTruthy();
});

test('satisfies compare lte', () => {
expect(satisfies(versionToCheck, '<=0.40.0')).toBeTruthy();
});

test('satisfies compare lte falsy', () => {
expect(satisfies(versionToCheck, '<=0.39.0')).toBeFalsy();
});

test('satisfies compare gte small version', () => {
expect(satisfies(versionToCheck, '>=0.4.0')).toBeTruthy();
});
Loading

0 comments on commit 6c64dca

Please sign in to comment.