Skip to content

Commit

Permalink
perf: lazy load signer packages
Browse files Browse the repository at this point in the history
  • Loading branch information
RyukTheCoder authored and RanGojo committed Sep 18, 2024
1 parent b21df3d commit 7b88f18
Show file tree
Hide file tree
Showing 85 changed files with 270 additions and 199 deletions.
18 changes: 10 additions & 8 deletions examples/wallets-demo/src/components/List/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ function Item({
supportedChainsNames
).find((a) => a.accounts.find((b) => b.isConnected));

const signers = getSigners(type);
const isMatchedNetworkWithAccount = walletState.accounts.find((account) =>
account?.toLowerCase()?.includes(network?.toLowerCase())
);
Expand All @@ -125,13 +124,16 @@ function Item({
alert('Error in detecting tx type.');
return;
}
const result = signers
.getSigner(txType)
.signMessage('Hello World', address || 'meow', chainId);
result
.then((signature) => {
alert(signature);
})

getSigners(type)
.then(async (signers) =>
signers
.getSigner(txType)
.signMessage('Hello World', address || 'meow', chainId)
.then((signature) => {
alert(signature);
})
)
.catch((ex) => {
alert(
'Error' + `(${info.name}): ` + (ex.message || 'Failed to sign')
Expand Down
7 changes: 5 additions & 2 deletions queue-manager/rango-preset/src/actions/checkStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ async function checkTransactionStatus({
const txType = getCurrentStepTxType(currentStep);
const sourceWallet = getRelatedWallet(swap, currentStep);
if (txType && sourceWallet) {
signer = context.getSigners(sourceWallet.walletType).getSigner(txType);
signer = (await context.getSigners(sourceWallet.walletType)).getSigner(
txType
);
}
} catch (error) {
/*
Expand Down Expand Up @@ -289,7 +291,8 @@ async function checkApprovalStatus({
const txType = getCurrentStepTxType(currentStep);
const sourceWallet = getRelatedWallet(swap, currentStep);
if (txType && sourceWallet) {
signer = context.getSigners(sourceWallet.walletType).getSigner(txType);
const walletSigners = await context.getSigners(sourceWallet.walletType);
signer = walletSigners.getSigner(txType);
}
} catch (error) {
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,5 @@ export async function executeTransaction(
}

// All the conditions are met. We can safely send the tx to wallet for sign.
signTransaction(actions);
await signTransaction(actions);
}
13 changes: 9 additions & 4 deletions queue-manager/rango-preset/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -991,9 +991,9 @@ export function isRequiredWalletConnected(
return { ok: matched, reason: 'account_miss_match' };
}

export function signTransaction(
export async function signTransaction(
actions: ExecuterActions<SwapStorage, SwapActionTypes, SwapQueueContext>
): void {
): Promise<void> {
const { setTransactionDataByHash } = inMemoryTransactionsData();
const { getStorage, setStorage, failed, next, schedule, context } = actions;
const { meta, getSigners, isMobileWallet } = context;
Expand All @@ -1004,7 +1004,6 @@ export function signTransaction(
const sourceWallet = getRelatedWallet(swap, currentStep);
const mobileWallet = isMobileWallet(sourceWallet?.walletType);
const walletAddress = getCurrentAddressOf(swap, currentStep);
const walletSigners = getSigners(sourceWallet.walletType);
const currentStepBlockchain = getCurrentBlockchainOf(swap, currentStep);

const onFinish = () => {
Expand Down Expand Up @@ -1109,6 +1108,9 @@ export function signTransaction(
onFinish();
return;
}

const walletSigners = await getSigners(sourceWallet.walletType);

const signer = walletSigners.getSigner(txType);
signer.signAndSendTx(tx, walletAddress, chainId).then(
({ hash, response }) => {
Expand All @@ -1122,7 +1124,10 @@ export function signTransaction(
hash,
explorerUrl &&
(!response || (response && !response.hashRequiringUpdate))
? { url: explorerUrl, description: isApproval ? 'Approve' : 'Swap' }
? {
url: explorerUrl,
description: isApproval ? 'Approve' : 'Swap',
}
: undefined
);
// response used for evm transactions to get receipt and track replaced
Expand Down
2 changes: 1 addition & 1 deletion queue-manager/rango-preset/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export interface SwapQueueContext extends QueueContext {
meta: Meta;
wallets: Wallet | null;
providers: Providers;
getSigners: (type: WalletType) => SignerFactory;
getSigners: (type: WalletType) => Promise<SignerFactory>;
switchNetwork: (
wallet: WalletType,
network: Network
Expand Down
2 changes: 1 addition & 1 deletion wallets/core/src/legacy/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export interface WalletActions {
// Optional, but should be provided at the same time.
suggest?: Suggest;
switchNetwork?: SwitchNetwork;
getSigners: (provider: any) => SignerFactory;
getSigners: (provider: any) => Promise<SignerFactory>;
canSwitchNetworkTo?: CanSwitchNetwork;
canEagerConnect?: CanEagerConnect;
getWalletInfo(allBlockChains: BlockchainMeta[]): WalletInfo;
Expand Down
4 changes: 2 additions & 2 deletions wallets/core/src/legacy/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ class Wallet<InstanceType = any> {
}
}

getSigners(provider: any) {
return this.actions.getSigners(provider);
async getSigners(provider: any) {
return await this.actions.getSigners(provider);
}
getWalletInfo(allBlockChains: BlockchainMeta[]) {
return this.actions.getWalletInfo(allBlockChains);
Expand Down
2 changes: 1 addition & 1 deletion wallets/provider-argentx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const subscribe: Subscribe = ({ instance, state, updateAccounts }) => {

export const canSwitchNetworkTo: CanSwitchNetwork = () => false;

export const getSigners: (provider: any) => SignerFactory = signer;
export const getSigners: (provider: any) => Promise<SignerFactory> = signer;

export const canEagerConnect: CanEagerConnect = ({ instance }) =>
instance.isPreauthorized();
Expand Down
6 changes: 4 additions & 2 deletions wallets/provider-argentx/src/signer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { SignerFactory } from 'rango-types';

import { DefaultStarknetSigner } from '@rango-dev/signer-starknet';
import { DefaultSignerFactory, TransactionType as TxType } from 'rango-types';

export default function getSigners(provider: any): SignerFactory {
export default async function getSigners(
provider: any
): Promise<SignerFactory> {
const signers = new DefaultSignerFactory();
const { DefaultStarknetSigner } = await import('@rango-dev/signer-starknet');
signers.registerSigner(TxType.STARKNET, new DefaultStarknetSigner(provider));
return signers;
}
2 changes: 1 addition & 1 deletion wallets/provider-bitget/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export const canEagerConnect: CanEagerConnect = async ({ instance, meta }) => {
}
return Promise.resolve(false);
};
export const getSigners: (provider: any) => SignerFactory = signer;
export const getSigners: (provider: any) => Promise<SignerFactory> = signer;

export const getWalletInfo: (allBlockChains: BlockchainMeta[]) => WalletInfo = (
allBlockChains
Expand Down
8 changes: 5 additions & 3 deletions wallets/provider-bitget/src/signer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import type { SignerFactory } from 'rango-types';

import { DefaultEvmSigner } from '@rango-dev/signer-evm';
import { DefaultTronSigner } from '@rango-dev/signer-tron';
import { getNetworkInstance, Networks } from '@rango-dev/wallets-shared';
import { DefaultSignerFactory, TransactionType as TxType } from 'rango-types';

export default function getSigners(provider: any): SignerFactory {
export default async function getSigners(
provider: any
): Promise<SignerFactory> {
const ethProvider = getNetworkInstance(provider, Networks.ETHEREUM);
const tronProvider = getNetworkInstance(provider, Networks.TRON);
const signers = new DefaultSignerFactory();
const { DefaultEvmSigner } = await import('@rango-dev/signer-evm');
const { DefaultTronSigner } = await import('@rango-dev/signer-tron');
signers.registerSigner(TxType.EVM, new DefaultEvmSigner(ethProvider));
signers.registerSigner(TxType.TRON, new DefaultTronSigner(tronProvider));
return signers;
Expand Down
2 changes: 1 addition & 1 deletion wallets/provider-braavos/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const subscribe: Subscribe = ({ instance, state, updateAccounts }) => {

export const canSwitchNetworkTo: CanSwitchNetwork = () => false;

export const getSigners: (provider: any) => SignerFactory = signer;
export const getSigners: (provider: any) => Promise<SignerFactory> = signer;

export const canEagerConnect: CanEagerConnect = ({ instance }) =>
instance.isPreauthorized();
Expand Down
6 changes: 4 additions & 2 deletions wallets/provider-braavos/src/signer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { SignerFactory } from 'rango-types';

import { DefaultStarknetSigner } from '@rango-dev/signer-starknet';
import { DefaultSignerFactory, TransactionType as TxType } from 'rango-types';

export default function getSigners(provider: any): SignerFactory {
export default async function getSigners(
provider: any
): Promise<SignerFactory> {
const signers = new DefaultSignerFactory();
const { DefaultStarknetSigner } = await import('@rango-dev/signer-starknet');
signers.registerSigner(TxType.STARKNET, new DefaultStarknetSigner(provider));
return signers;
}
2 changes: 1 addition & 1 deletion wallets/provider-brave/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export const switchNetwork: SwitchNetwork = switchNetworkForEvm;

export const canSwitchNetworkTo: CanSwitchNetwork = canSwitchNetworkToEvm;

export const getSigners: (provider: any) => SignerFactory = signer;
export const getSigners: (provider: any) => Promise<SignerFactory> = signer;

export const canEagerConnect: CanEagerConnect = async ({ instance, meta }) => {
const evm_instance = chooseInstance(instance, meta, Networks.ETHEREUM);
Expand Down
8 changes: 5 additions & 3 deletions wallets/provider-brave/src/signer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import type { SignerFactory } from 'rango-types';

import { DefaultEvmSigner } from '@rango-dev/signer-evm';
import { DefaultSolanaSigner } from '@rango-dev/signer-solana';
import { getNetworkInstance, Networks } from '@rango-dev/wallets-shared';
import { DefaultSignerFactory, TransactionType as TxType } from 'rango-types';

export default function getSigners(provider: any): SignerFactory {
export default async function getSigners(
provider: any
): Promise<SignerFactory> {
const ethProvider = getNetworkInstance(provider, Networks.ETHEREUM);
const solProvider = getNetworkInstance(provider, Networks.SOLANA);
const signers = new DefaultSignerFactory();
const { DefaultEvmSigner } = await import('@rango-dev/signer-evm');
const { DefaultSolanaSigner } = await import('@rango-dev/signer-solana');
signers.registerSigner(TxType.EVM, new DefaultEvmSigner(ethProvider));
signers.registerSigner(TxType.SOLANA, new DefaultSolanaSigner(solProvider));
return signers;
Expand Down
2 changes: 1 addition & 1 deletion wallets/provider-clover/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const switchNetwork: SwitchNetwork = async (options) => {

export const canSwitchNetworkTo: CanSwitchNetwork = canSwitchNetworkToEvm;

export const getSigners: (provider: any) => SignerFactory = signer;
export const getSigners: (provider: any) => Promise<SignerFactory> = signer;

export const canEagerConnect: CanEagerConnect = async ({ instance, meta }) => {
const evm_instance = chooseInstance(instance, meta, Networks.ETHEREUM);
Expand Down
8 changes: 5 additions & 3 deletions wallets/provider-clover/src/signer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import type { SignerFactory } from 'rango-types';

import { DefaultEvmSigner } from '@rango-dev/signer-evm';
import { DefaultSolanaSigner } from '@rango-dev/signer-solana';
import { getNetworkInstance, Networks } from '@rango-dev/wallets-shared';
import { DefaultSignerFactory, TransactionType as TxType } from 'rango-types';

export default function getSigners(provider: any): SignerFactory {
export default async function getSigners(
provider: any
): Promise<SignerFactory> {
const ethProvider = getNetworkInstance(provider, Networks.ETHEREUM);
const solProvider = getNetworkInstance(provider, Networks.SOLANA);
const signers = new DefaultSignerFactory();
const { DefaultEvmSigner } = await import('@rango-dev/signer-evm');
const { DefaultSolanaSigner } = await import('@rango-dev/signer-solana');
signers.registerSigner(TxType.EVM, new DefaultEvmSigner(ethProvider));
signers.registerSigner(TxType.SOLANA, new DefaultSolanaSigner(solProvider));
return signers;
Expand Down
2 changes: 1 addition & 1 deletion wallets/provider-coin98/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const switchNetwork: SwitchNetwork = async (options) => {

export const canSwitchNetworkTo: CanSwitchNetwork = canSwitchNetworkToEvm;

export const getSigners: (provider: any) => SignerFactory = signer;
export const getSigners: (provider: any) => Promise<SignerFactory> = signer;

export const getWalletInfo: (allBlockChains: BlockchainMeta[]) => WalletInfo = (
allBlockChains
Expand Down
9 changes: 5 additions & 4 deletions wallets/provider-coin98/src/signer.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import type { SignerFactory } from 'rango-types';

import { DefaultEvmSigner } from '@rango-dev/signer-evm';
import { getNetworkInstance, Networks } from '@rango-dev/wallets-shared';
import { DefaultSignerFactory, TransactionType as TxType } from 'rango-types';

import { CustomSolanaSigner } from './solana-signer.js';

export default function getSigners(provider: any): SignerFactory {
export default async function getSigners(
provider: any
): Promise<SignerFactory> {
const ethProvider = getNetworkInstance(provider, Networks.ETHEREUM);
const solProvider = getNetworkInstance(provider, Networks.SOLANA);
const signers = new DefaultSignerFactory();
const { DefaultEvmSigner } = await import('@rango-dev/signer-evm');
const { CustomSolanaSigner } = await import('./solana-signer.js');
signers.registerSigner(TxType.EVM, new DefaultEvmSigner(ethProvider));
signers.registerSigner(TxType.SOLANA, new CustomSolanaSigner(solProvider));
return signers;
Expand Down
2 changes: 1 addition & 1 deletion wallets/provider-coinbase/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export const switchNetwork: SwitchNetwork = switchNetworkForEvm;

export const canSwitchNetworkTo: CanSwitchNetwork = canSwitchNetworkToEvm;

export const getSigners: (provider: any) => SignerFactory = signer;
export const getSigners: (provider: any) => Promise<SignerFactory> = signer;

export const canEagerConnect: CanEagerConnect = async ({ instance, meta }) => {
const evm_instance = chooseInstance(instance, meta, Networks.ETHEREUM);
Expand Down
8 changes: 5 additions & 3 deletions wallets/provider-coinbase/src/signer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import type { SignerFactory } from 'rango-types';

import { DefaultEvmSigner } from '@rango-dev/signer-evm';
import { DefaultSolanaSigner } from '@rango-dev/signer-solana';
import { getNetworkInstance, Networks } from '@rango-dev/wallets-shared';
import { DefaultSignerFactory, TransactionType as TxType } from 'rango-types';

export default function getSigners(provider: any): SignerFactory {
export default async function getSigners(
provider: any
): Promise<SignerFactory> {
const ethProvider = getNetworkInstance(provider, Networks.ETHEREUM);
const solProvider = getNetworkInstance(provider, Networks.SOLANA);
const signers = new DefaultSignerFactory();
const { DefaultEvmSigner } = await import('@rango-dev/signer-evm');
const { DefaultSolanaSigner } = await import('@rango-dev/signer-solana');
signers.registerSigner(TxType.EVM, new DefaultEvmSigner(ethProvider));
signers.registerSigner(TxType.SOLANA, new DefaultSolanaSigner(solProvider));
return signers;
Expand Down
2 changes: 1 addition & 1 deletion wallets/provider-cosmostation/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export const suggest: Suggest = async (options) => {
}
};

export const getSigners: (provider: any) => SignerFactory = signer;
export const getSigners: (provider: any) => Promise<SignerFactory> = signer;

export const canEagerConnect: CanEagerConnect = async ({ instance, meta }) => {
const evm_instance = chooseInstance(instance, meta, Networks.ETHEREUM);
Expand Down
8 changes: 5 additions & 3 deletions wallets/provider-cosmostation/src/signer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import type { SignerFactory } from 'rango-types';

import { DefaultCosmosSigner } from '@rango-dev/signer-cosmos';
import { DefaultEvmSigner } from '@rango-dev/signer-evm';
import { getNetworkInstance, Networks } from '@rango-dev/wallets-shared';
import { DefaultSignerFactory, TransactionType as TxType } from 'rango-types';

export default function getSigners(provider: any): SignerFactory {
export default async function getSigners(
provider: any
): Promise<SignerFactory> {
const ethProvider = getNetworkInstance(provider, Networks.ETHEREUM);
const cosmosProvider = getNetworkInstance(provider, Networks.COSMOS);
const signers = new DefaultSignerFactory();
const { DefaultEvmSigner } = await import('@rango-dev/signer-evm');
const { DefaultCosmosSigner } = await import('@rango-dev/signer-cosmos');
signers.registerSigner(TxType.EVM, new DefaultEvmSigner(ethProvider));
signers.registerSigner(
TxType.COSMOS,
Expand Down
2 changes: 1 addition & 1 deletion wallets/provider-default/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const switchNetwork: SwitchNetwork = switchNetworkForEvm;

export const canSwitchNetworkTo: CanSwitchNetwork = canSwitchNetworkToEvm;

export const getSigners: (provider: any) => SignerFactory = signer;
export const getSigners: (provider: any) => Promise<SignerFactory> = signer;

export const canEagerConnect: CanEagerConnect = canEagerlyConnectToEvm;

Expand Down
6 changes: 4 additions & 2 deletions wallets/provider-default/src/signer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { SignerFactory } from 'rango-types';

import { DefaultEvmSigner } from '@rango-dev/signer-evm';
import { DefaultSignerFactory, TransactionType as TxType } from 'rango-types';

export default function getSigners(provider: any): SignerFactory {
export default async function getSigners(
provider: any
): Promise<SignerFactory> {
const signers = new DefaultSignerFactory();
const { DefaultEvmSigner } = await import('@rango-dev/signer-evm');
signers.registerSigner(TxType.EVM, new DefaultEvmSigner(provider));
return signers;
}
2 changes: 1 addition & 1 deletion wallets/provider-enkrypt/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const switchNetwork: SwitchNetwork = switchNetworkForEvm;

export const canSwitchNetworkTo: CanSwitchNetwork = canSwitchNetworkToEvm;

export const getSigners: (provider: any) => SignerFactory = signer;
export const getSigners: (provider: any) => Promise<SignerFactory> = signer;

export const canEagerConnect: CanEagerConnect = canEagerlyConnectToEvm;

Expand Down
Loading

0 comments on commit 7b88f18

Please sign in to comment.