Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienGllmt committed Oct 29, 2023
1 parent 5ab6f42 commit ff80dd9
Show file tree
Hide file tree
Showing 19 changed files with 319 additions and 3,399 deletions.
3,345 changes: 48 additions & 3,297 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/paima-sdk/paima-mw-core/src/endpoints/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
localRemoteVersionsCompatible,
} from '../helpers/auxiliary-queries';
import { checkCardanoWalletStatus } from '../wallets/cardano';
import { checkEthWalletStatus } from '../wallets/evm';
import { checkEthWalletStatus } from '../wallets/evm/injected';
import { specificWalletLogin } from '../wallets/wallets';
import {
getEmulatedBlocksActive,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { SignFunction } from '../types';
import {
AlgorandConnector,
CardanoConnector,
EvmConnector,
EvmInjectedConnector,
PolkadotConnector,
} from '@paima/providers';
import { createMessageForBatcher, type BatchedSubunit } from '@paima/concise';
Expand All @@ -23,7 +23,7 @@ export function batchedToJsonString(b: BatchedSubunit): string {
function selectSignFunction(addressType: AddressType): SignFunction {
switch (addressType) {
case AddressType.EVM:
return EvmConnector.instance().getOrThrowProvider().signMessage;
return EvmInjectedConnector.instance().getOrThrowProvider().signMessage;
case AddressType.CARDANO:
return CardanoConnector.instance().getOrThrowProvider().signMessage;
case AddressType.POLKADOT:
Expand Down
8 changes: 4 additions & 4 deletions packages/paima-sdk/paima-mw-core/src/helpers/posting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
TruffleConnector,
AlgorandConnector,
CardanoConnector,
EvmConnector,
EvmInjectedConnector,
PolkadotConnector,
} from '@paima/providers';
import type { BatchedSubunit } from '@paima/concise';
Expand Down Expand Up @@ -115,14 +115,14 @@ export async function postConciselyEncodedData(gameInput: string): Promise<Resul
switch (postingMode) {
case PostingMode.UNBATCHED:
return await postString(
EvmConnector.instance().getOrThrowProvider().sendTransaction,
EvmConnector.instance().getOrThrowProvider().getAddress(),
EvmInjectedConnector.instance().getOrThrowProvider().sendTransaction,
EvmInjectedConnector.instance().getOrThrowProvider().getAddress(),
gameInput
);
case PostingMode.BATCHED_ETH:
return await buildBatchedSubunit(
AddressType.EVM,
EvmConnector.instance().getOrThrowProvider().getAddress(),
EvmInjectedConnector.instance().getOrThrowProvider().getAddress(),
gameInput
).then(submitToBatcher);
case PostingMode.BATCHED_CARDANO:
Expand Down
6 changes: 3 additions & 3 deletions packages/paima-sdk/paima-mw-core/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { PostingInfo, PostingModeString } from './types';
import {
AlgorandConnector,
CardanoConnector,
EvmConnector,
EvmInjectedConnector,
PolkadotConnector,
} from '@paima/providers';
import assertNever from 'assert-never';
Expand Down Expand Up @@ -120,13 +120,13 @@ export const getActiveAddress = (): string => {
switch (postingMode) {
case PostingMode.UNBATCHED:
case PostingMode.BATCHED_ETH:
return EvmConnector.instance().getOrThrowProvider().getAddress();
return EvmInjectedConnector.instance().getOrThrowProvider().getAddress();
case PostingMode.BATCHED_CARDANO:
return CardanoConnector.instance().getOrThrowProvider().getAddress();
case PostingMode.BATCHED_POLKADOT:
return PolkadotConnector.instance().getOrThrowProvider().getAddress();
case PostingMode.AUTOMATIC:
return EvmConnector.instance().getOrThrowProvider().getAddress();
return EvmInjectedConnector.instance().getOrThrowProvider().getAddress();
case PostingMode.BATCHED_ALGORAND:
return AlgorandConnector.instance().getOrThrowProvider().getAddress();
default:
Expand Down
6 changes: 3 additions & 3 deletions packages/paima-sdk/paima-mw-core/src/wallets/algorand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import { PaimaMiddlewareErrorCode, buildEndpointErrorFxn } from '../errors';
import { AlgorandConnector } from '@paima/providers';
import { getGameName } from '../state';
import type { WalletMode } from './wallet-modes';
import { connectWallet } from './wallet-modes';
import { connectInjectedWallet } from './wallet-modes';

export async function algorandLoginWrapper(
loginInfo: LoginInfoMap[WalletMode.ALGORAND]
loginInfo: LoginInfoMap[WalletMode.Algorand]
): Promise<Result<Wallet>> {
const errorFxn = buildEndpointErrorFxn('algorandLoginWrapper');

const gameInfo = {
gameName: getGameName(),
gameChainId: undefined, // Not needed because of batcher
};
const loginResult = await connectWallet(
const loginResult = await connectInjectedWallet(
'algorandLoginWrapper',
errorFxn,
PaimaMiddlewareErrorCode.ALGORAND_LOGIN,
Expand Down
6 changes: 3 additions & 3 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 { connectWallet } from './wallet-modes';
import { connectInjectedWallet } from './wallet-modes';
import { CardanoConnector } from '@paima/providers';
import { getGameName } from '../state';

Expand All @@ -19,15 +19,15 @@ export async function checkCardanoWalletStatus(): Promise<OldResult> {
}

export async function cardanoLoginWrapper(
loginInfo: LoginInfoMap[WalletMode.CARDANO]
loginInfo: LoginInfoMap[WalletMode.Cardano]
): Promise<Result<Wallet>> {
const errorFxn = buildEndpointErrorFxn('cardanoLoginWrapper');

const gameInfo = {
gameName: getGameName(),
gameChainId: undefined, // Not needed because of batcher
};
const loginResult = await connectWallet(
const loginResult = await connectInjectedWallet(
'cardanoLoginWrapper',
errorFxn,
PaimaMiddlewareErrorCode.CARDANO_LOGIN,
Expand Down
60 changes: 60 additions & 0 deletions packages/paima-sdk/paima-mw-core/src/wallets/evm/ethers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { buildEndpointErrorFxn, PaimaMiddlewareErrorCode } from '../../errors';
import { getChainId, getGameName } from '../../state';
import type { LoginInfoMap, Result, Wallet } from '../../types';
import { updateFee } from '../../helpers/posting';

import type { WalletMode } from '../wallet-modes';
import type { EthersApi, IProvider } from '@paima/providers';
import { EthersConnector } from '@paima/providers';

async function connectWallet(
loginInfo: LoginInfoMap[WalletMode.EvmEthers]
): Promise<Result<IProvider<EthersApi>>> {
const errorFxn = buildEndpointErrorFxn('ethersLoginWrapper');

const gameInfo = {
gameName: getGameName(),
gameChainId: '0x' + getChainId().toString(16),
};
const name = loginInfo.connection.metadata.name;
try {
console.log(`ethersLoginWrapper: Attempting to log into ${name}`);
const provider = await EthersConnector.instance().connectExternal(
gameInfo,
loginInfo.connection.api
);
return {
success: true,
result: provider,
};
} catch (err) {
console.log(`ethersLoginWrapper: Error while logging into wallet name}`);

return errorFxn(PaimaMiddlewareErrorCode.EVM_LOGIN, err);
}
}
export async function ethersLoginWrapper(
loginInfo: LoginInfoMap[WalletMode.EvmEthers]
): Promise<Result<Wallet>> {
const errorFxn = buildEndpointErrorFxn('ethersLoginWrapper');

const loginResult = await connectWallet(loginInfo);
if (loginResult.success === false) {
return loginResult;
}

try {
await updateFee();
} catch (err) {
errorFxn(PaimaMiddlewareErrorCode.ERROR_UPDATING_FEE, err);
// The fee has a default value, so this is not fatal and we can continue.
// If the fee has increased beyond the default value, posting won't work.
}

return {
success: true,
result: {
walletAddress: EthersConnector.instance().getOrThrowProvider().getAddress(),
},
};
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { buildEndpointErrorFxn, PaimaMiddlewareErrorCode } from '../errors';
import { buildEndpointErrorFxn, PaimaMiddlewareErrorCode } from '../../errors';
import {
getChainCurrencyDecimals,
getChainCurrencyName,
Expand All @@ -8,13 +8,13 @@ import {
getChainName,
getChainUri,
getGameName,
} from '../state';
import type { LoginInfoMap, OldResult, Result, Wallet } from '../types';
import { updateFee } from '../helpers/posting';
} from '../../state';
import type { LoginInfoMap, OldResult, Result, Wallet } from '../../types';
import { updateFee } from '../../helpers/posting';

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

interface SwitchError {
code: number;
Expand All @@ -27,14 +27,14 @@ async function switchChain(): Promise<boolean> {
const hexChainId = '0x' + getChainId().toString(16);

try {
await EvmConnector.instance().getOrThrowProvider().switchChain(hexChainId);
await EvmInjectedConnector.instance().getOrThrowProvider().switchChain(hexChainId);
return await verifyWalletChain();
} catch (switchError) {
// This error code indicates that the chain has not been added to the wallet.
const se = switchError as SwitchError;
if (se.hasOwnProperty('code') && se.code === CHAIN_NOT_ADDED_ERROR_CODE) {
try {
await EvmConnector.instance()
await EvmInjectedConnector.instance()
.getOrThrowProvider()
.addChain({
chainId: hexChainId,
Expand All @@ -48,7 +48,7 @@ async function switchChain(): Promise<boolean> {
// blockExplorerUrls: Chain not added with empty string.
blockExplorerUrls: getChainExplorerUri() ? [getChainExplorerUri()] : undefined,
});
await EvmConnector.instance().getOrThrowProvider().switchChain(hexChainId);
await EvmInjectedConnector.instance().getOrThrowProvider().switchChain(hexChainId);
return await verifyWalletChain();
} catch (addError) {
errorFxn(PaimaMiddlewareErrorCode.ERROR_ADDING_CHAIN, addError);
Expand All @@ -62,13 +62,13 @@ async function switchChain(): Promise<boolean> {
}

async function verifyWalletChain(): Promise<boolean> {
return await EvmConnector.instance().getOrThrowProvider().verifyWalletChain();
return await EvmInjectedConnector.instance().getOrThrowProvider().verifyWalletChain();
}

export async function checkEthWalletStatus(): Promise<OldResult> {
const errorFxn = buildEndpointErrorFxn('checkEthWalletStatus');

if (EvmConnector.instance().getProvider() === null) {
if (EvmInjectedConnector.instance().getProvider() === null) {
return errorFxn(PaimaMiddlewareErrorCode.NO_ADDRESS_SELECTED);
}

Expand All @@ -84,20 +84,20 @@ export async function checkEthWalletStatus(): Promise<OldResult> {
}

export async function evmLoginWrapper(
loginInfo: LoginInfoMap[WalletMode.EVM]
loginInfo: LoginInfoMap[WalletMode.EvmInjected]
): Promise<Result<Wallet>> {
const errorFxn = buildEndpointErrorFxn('evmLoginWrapper');

const gameInfo = {
gameName: getGameName(),
gameChainId: '0x' + getChainId().toString(16),
};
const loginResult = await connectWallet(
const loginResult = await connectInjectedWallet(
'evmLoginWrapper',
errorFxn,
PaimaMiddlewareErrorCode.EVM_LOGIN,
loginInfo,
EvmConnector.instance(),
EvmInjectedConnector.instance(),
gameInfo
);
if (loginResult.success === false) {
Expand All @@ -124,7 +124,7 @@ export async function evmLoginWrapper(
return {
success: true,
result: {
walletAddress: EvmConnector.instance().getOrThrowProvider().getAddress(),
walletAddress: EvmInjectedConnector.instance().getOrThrowProvider().getAddress(),
},
};
}
6 changes: 3 additions & 3 deletions packages/paima-sdk/paima-mw-core/src/wallets/polkadot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import { getGameName } from '../state';
import type { LoginInfoMap, Result, Wallet } from '../types';
import { PolkadotConnector } from '@paima/providers';
import type { WalletMode } from './wallet-modes';
import { connectWallet } from './wallet-modes';
import { connectInjectedWallet } from './wallet-modes';

export async function polkadotLoginWrapper(
loginInfo: LoginInfoMap[WalletMode.POLKADOT]
loginInfo: LoginInfoMap[WalletMode.Polkadot]
): Promise<Result<Wallet>> {
const errorFxn = buildEndpointErrorFxn('polkadotLoginWrapper');

const gameInfo = {
gameName: getGameName(),
gameChainId: undefined, // Not needed because of batcher
};
const loginResult = await connectWallet(
const loginResult = await connectInjectedWallet(
'polkadotLoginWrapper',
errorFxn,
PaimaMiddlewareErrorCode.POLKADOT_LOGIN,
Expand Down
29 changes: 16 additions & 13 deletions packages/paima-sdk/paima-mw-core/src/wallets/wallet-modes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
IConnector,
IProvider,
GameInfo,
EthersApi,
} from '@paima/providers';
import { WalletNotFound, UnsupportedWallet } from '@paima/providers';
import type { EndpointErrorFxn } from '../errors';
Expand All @@ -16,14 +17,15 @@ import { FE_ERR_SPECIFIC_WALLET_NOT_INSTALLED } from '../errors';
import assertNever from 'assert-never';

export const enum WalletMode {
NO_WALLET,
EVM,
CARDANO,
POLKADOT,
ALGORAND,
NoWallet,
EvmInjected,
EvmEthers,
Cardano,
Polkadot,
Algorand,
}

export type Preference<T> =
export type InjectionPreference<T> =
| {
name: string;
}
Expand All @@ -32,14 +34,15 @@ export type Preference<T> =
};

export type BaseLoginInfo<Api> = {
preference?: Preference<Api>;
preference?: InjectionPreference<Api>;
};
export type LoginInfoMap = {
[WalletMode.EVM]: BaseLoginInfo<EvmApi> & { preferBatchedMode: boolean };
[WalletMode.CARDANO]: BaseLoginInfo<CardanoApi>;
[WalletMode.POLKADOT]: BaseLoginInfo<PolkadotApi>;
[WalletMode.ALGORAND]: BaseLoginInfo<AlgorandApi>;
[WalletMode.NO_WALLET]: void;
[WalletMode.EvmInjected]: BaseLoginInfo<EvmApi> & { preferBatchedMode: boolean };
[WalletMode.EvmEthers]: { connection: ActiveConnection<EthersApi>; preferBatchedMode: boolean };
[WalletMode.Cardano]: BaseLoginInfo<CardanoApi>;
[WalletMode.Polkadot]: BaseLoginInfo<PolkadotApi>;
[WalletMode.Algorand]: BaseLoginInfo<AlgorandApi>;
[WalletMode.NoWallet]: void;
};

type ToUnion<T> = {
Expand All @@ -55,7 +58,7 @@ function getWalletName(info: BaseLoginInfo<unknown>): undefined | string {
}
return info.preference.connection.metadata.name;
}
export async function connectWallet<Api>(
export async function connectInjectedWallet<Api>(
typeName: string,
errorFxn: EndpointErrorFxn,
errorCode: PaimaMiddlewareErrorCode,
Expand Down
Loading

0 comments on commit ff80dd9

Please sign in to comment.