Skip to content

Commit

Permalink
Broken: refactoring utility function location
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienGllmt committed Oct 29, 2023
1 parent ff80dd9 commit 45dacb7
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 55 deletions.
4 changes: 2 additions & 2 deletions packages/paima-sdk/paima-mw-core/src/wallets/algorand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PaimaMiddlewareErrorCode, buildEndpointErrorFxn } from '../errors';
import { AlgorandConnector } from '@paima/providers';
import { getGameName } from '../state';
import type { WalletMode } from './wallet-modes';
import { connectInjectedWallet } from './wallet-modes';
import { connectInjected } from './wallet-modes';

export async function algorandLoginWrapper(
loginInfo: LoginInfoMap[WalletMode.Algorand]
Expand All @@ -14,7 +14,7 @@ export async function algorandLoginWrapper(
gameName: getGameName(),
gameChainId: undefined, // Not needed because of batcher
};
const loginResult = await connectInjectedWallet(
const loginResult = await connectInjected(
'algorandLoginWrapper',
errorFxn,
PaimaMiddlewareErrorCode.ALGORAND_LOGIN,
Expand Down
4 changes: 2 additions & 2 deletions packages/paima-sdk/paima-mw-core/src/wallets/cardano.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { LoginInfoMap, OldResult, Result, Wallet } from '../types';
import { buildEndpointErrorFxn, PaimaMiddlewareErrorCode } from '../errors';
import type { WalletMode } from './wallet-modes';
import { connectInjectedWallet } from './wallet-modes';
import { connectInjected } from './wallet-modes';
import { CardanoConnector } from '@paima/providers';
import { getGameName } from '../state';

Expand All @@ -27,7 +27,7 @@ export async function cardanoLoginWrapper(
gameName: getGameName(),
gameChainId: undefined, // Not needed because of batcher
};
const loginResult = await connectInjectedWallet(
const loginResult = await connectInjected(
'cardanoLoginWrapper',
errorFxn,
PaimaMiddlewareErrorCode.CARDANO_LOGIN,
Expand Down
4 changes: 2 additions & 2 deletions packages/paima-sdk/paima-mw-core/src/wallets/evm/injected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import type { LoginInfoMap, OldResult, Result, Wallet } from '../../types';
import { updateFee } from '../../helpers/posting';

import { connectInjectedWallet } from '../wallet-modes';
import { connectInjected } from '../wallet-modes';
import type { WalletMode } from '../wallet-modes';
import { EvmInjectedConnector } from '@paima/providers';

Expand Down Expand Up @@ -92,7 +92,7 @@ export async function evmLoginWrapper(
gameName: getGameName(),
gameChainId: '0x' + getChainId().toString(16),
};
const loginResult = await connectInjectedWallet(
const loginResult = await connectInjected(
'evmLoginWrapper',
errorFxn,
PaimaMiddlewareErrorCode.EVM_LOGIN,
Expand Down
4 changes: 2 additions & 2 deletions packages/paima-sdk/paima-mw-core/src/wallets/polkadot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getGameName } from '../state';
import type { LoginInfoMap, Result, Wallet } from '../types';
import { PolkadotConnector } from '@paima/providers';
import type { WalletMode } from './wallet-modes';
import { connectInjectedWallet } from './wallet-modes';
import { connectInjected } from './wallet-modes';

export async function polkadotLoginWrapper(
loginInfo: LoginInfoMap[WalletMode.Polkadot]
Expand All @@ -14,7 +14,7 @@ export async function polkadotLoginWrapper(
gameName: getGameName(),
gameChainId: undefined, // Not needed because of batcher
};
const loginResult = await connectInjectedWallet(
const loginResult = await connectInjected(
'polkadotLoginWrapper',
errorFxn,
PaimaMiddlewareErrorCode.POLKADOT_LOGIN,
Expand Down
55 changes: 9 additions & 46 deletions packages/paima-sdk/paima-mw-core/src/wallets/wallet-modes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,14 @@ import type {
IProvider,
GameInfo,
EthersApi,
InjectionPreference,
WalletMode,
} from '@paima/providers';
import { WalletNotFound, UnsupportedWallet } from '@paima/providers';
import { WalletNotFound, UnsupportedWallet, connectInjectedWallet } from '@paima/providers';
import type { EndpointErrorFxn } from '../errors';
import type { Result } from '../types';
import type { PaimaMiddlewareErrorCode } from '../errors';
import { FE_ERR_SPECIFIC_WALLET_NOT_INSTALLED } from '../errors';
import assertNever from 'assert-never';

export const enum WalletMode {
NoWallet,
EvmInjected,
EvmEthers,
Cardano,
Polkadot,
Algorand,
}

export type InjectionPreference<T> =
| {
name: string;
}
| {
connection: ActiveConnection<T>;
};

export type BaseLoginInfo<Api> = {
preference?: InjectionPreference<Api>;
Expand All @@ -58,7 +42,7 @@ function getWalletName(info: BaseLoginInfo<unknown>): undefined | string {
}
return info.preference.connection.metadata.name;
}
export async function connectInjectedWallet<Api>(
export async function connectInjected<Api>(
typeName: string,
errorFxn: EndpointErrorFxn,
errorCode: PaimaMiddlewareErrorCode,
Expand All @@ -67,32 +51,11 @@ export async function connectInjectedWallet<Api>(
gameInfo: GameInfo
): Promise<Result<IProvider<Api>>> {
try {
if (loginInfo.preference == null) {
console.log(`${typeName} Attempting simple login`);
const provider = await connector.connectSimple(gameInfo);
return {
success: true,
result: provider,
};
} else if ('name' in loginInfo.preference) {
const walletName = loginInfo.preference.name;
console.log(`${typeName} Attempting to log into ${walletName}`);
const provider = await connector.connectNamed(gameInfo, walletName);
return {
success: true,
result: provider,
};
} else if ('connection' in loginInfo.preference) {
const walletName = loginInfo.preference.connection.metadata.name;
console.log(`${typeName} Attempting to log into ${walletName}`);
const provider = await connector.connectExternal(gameInfo, loginInfo.preference.connection);
return {
success: true,
result: provider,
};
} else {
assertNever(loginInfo.preference);
}
const provider = await connectInjectedWallet(typeName, loginInfo.preference, connector, gameInfo);
return {
success: true,
result: provider
};
} catch (err) {
if (err instanceof WalletNotFound || err instanceof UnsupportedWallet) {
return errorFxn(errorCode, undefined, FE_ERR_SPECIFIC_WALLET_NOT_INSTALLED);
Expand Down
3 changes: 2 additions & 1 deletion packages/paima-sdk/paima-providers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@truffle/hdwallet-provider": "^2.1.15",
"bech32": "^2.0.0",
"web3": "1.10.0",
"web3-utils": "1.10.0"
"web3-utils": "1.10.0",
"assert-never": "^1.2.1"
}
}
2 changes: 2 additions & 0 deletions packages/paima-sdk/paima-providers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ export * from './cardano';
export * from './evm/index';
export * from './polkadot';
export * from './errors';
export * from './utils';
export type * from './algorand';
export type * from './cardano';
export type * from './evm/index';
export type * from './polkadot';
export type * from './IProvider';
export type * from './utils';
60 changes: 60 additions & 0 deletions packages/paima-sdk/paima-providers/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import type { ActiveConnection, GameInfo, IConnector, IProvider } from './IProvider';
import assertNever from 'assert-never';
import { AlgorandConnector } from './algorand';
import { EvmInjectedConnector } from './evm';
import { CardanoConnector } from './cardano';
import { PolkadotConnector } from './polkadot';

export const enum WalletMode {
NoWallet,
EvmInjected,
EvmEthers,
Cardano,
Polkadot,
Algorand,
}

export type InjectionPreference<T> =
| {
name: string;
}
| {
connection: ActiveConnection<T>;
};

export async function allInjectedWallets(gameInfo: GameInfo): Promise<{
[WalletMode.EvmInjected]: ReturnType<typeof EvmInjectedConnector.getWalletOptions>,
[WalletMode.Cardano]: ReturnType<typeof CardanoConnector.getWalletOptions>,
[WalletMode.Polkadot]: Awaited<ReturnType<typeof PolkadotConnector.getWalletOptions>>,
[WalletMode.Algorand]: ReturnType<typeof AlgorandConnector.getWalletOptions>,
}> {
return {
[WalletMode.EvmInjected]: EvmInjectedConnector.getWalletOptions(),
[WalletMode.Cardano]: CardanoConnector.getWalletOptions(),
[WalletMode.Polkadot]: await PolkadotConnector.getWalletOptions(gameInfo.gameName),
[WalletMode.Algorand]: AlgorandConnector.getWalletOptions(),
}
}
export async function connectInjectedWallet<Api>(
typeName: string,
preference: undefined | InjectionPreference<Api>,
connector: IConnector<Api>,
gameInfo: GameInfo
): Promise<IProvider<Api>> {
if (preference == null) {
console.log(`${typeName} Attempting simple login`);
const provider = await connector.connectSimple(gameInfo);
return provider;
} else if ('name' in preference) {
const walletName = preference.name;
console.log(`${typeName} Attempting to log into ${walletName}`);
const provider = await connector.connectNamed(gameInfo, walletName);
return provider;
} else if ('connection' in preference) {
const walletName = preference.connection.metadata.name;
console.log(`${typeName} Attempting to log into ${walletName}`);
const provider = await connector.connectExternal(gameInfo, preference.connection);
return provider;
}
assertNever(preference);
}

0 comments on commit 45dacb7

Please sign in to comment.