Skip to content

Commit

Permalink
Refactor: Snap State and Account Static Classes (#111)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivy Astrix <ivy.astrix@gmail.com>
  • Loading branch information
poi-son-ivy authored Feb 27, 2024
1 parent 9934044 commit 667275b
Show file tree
Hide file tree
Showing 23 changed files with 918 additions and 910 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "git+https://github.com/hashgraph/hedera-metamask-snaps.git"
},
"source": {
"shasum": "bg0dUT+K57KjvAI8AB1sQAdW/r5QSG/XamegqcfZxhc=",
"shasum": "aJWBuxTQqeVNei+Y/kY9Wp0+HbXJ27a6I5jMpAL7ItI=",
"location": {
"npm": {
"filePath": "dist/snap.js",
Expand Down
13 changes: 6 additions & 7 deletions packages/hedera-wallet-snap/packages/snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ import _ from 'lodash';
import { getAccountBalance } from './rpc/account/getAccountBalance';
import { getAccountInfo } from './rpc/account/getAccountInfo';
import { transferCrypto } from './rpc/transactions/transferCrypto';
import { setCurrentAccount } from './snap/account';
import { getSnapStateUnchecked, initSnapState } from './snap/state';
import { SnapAccounts } from './snap/SnapAccounts';
import { SnapState } from './snap/SnapState';
import { WalletSnapParams } from './types/state';

import { approveAllowance } from './rpc/account/approveAllowance';
import { deleteAccount } from './rpc/account/deleteAccount';
import { deleteAllowance } from './rpc/account/deleteAllowance';
Expand All @@ -38,8 +37,8 @@ import { associateTokens } from './rpc/hts/associateTokens';
import { createToken } from './rpc/hts/createToken';
import { signMessage } from './rpc/misc/signMessage';
import { getTransactions } from './rpc/transactions/getTransactions';
import { StakeHbarRequestParams } from './types/params';
import { HederaUtils } from './utils/HederaUtils';
import { StakeHbarRequestParams } from './types/params';

/**
* Handle incoming JSON-RPC requests, sent through `wallet_invokeSnap`.
Expand All @@ -63,9 +62,9 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
JSON.stringify(request.params, null, 4),
);

let state = await getSnapStateUnchecked();
let state = await SnapState.getStateUnchecked();
if (_.isEmpty(state)) {
state = await initSnapState();
state = await SnapState.initState();
}

let isExternalAccount = false;
Expand All @@ -79,7 +78,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
);

// Set current account
await setCurrentAccount(
await SnapAccounts.setCurrentAccount(
origin,
state,
request.params,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
import { providerErrors } from '@metamask/rpc-errors';
import { divider, heading, text } from '@metamask/snaps-ui';
import _ from 'lodash';
import { HederaClientFactory } from '../../snap/HederaClientFactory';
import { HederaServiceImpl } from '../../services/impl/hedera';
import { createHederaClient } from '../../snap/account';
import { SnapUtils } from '../../utils/SnapUtils';
import { MirrorTokenInfo, TxReceipt } from '../../types/hedera';
import {
ApproveAllowanceAssetDetail,
ApproveAllowanceRequestParams,
} from '../../types/params';
import { SnapDialogParams, WalletSnapParams } from '../../types/state';
import { SnapUtils } from '../../utils/SnapUtils';

/**
* Approve an allowance for a given asset.
Expand Down Expand Up @@ -158,7 +158,7 @@ export async function approveAllowance(
throw providerErrors.userRejectedRequest();
}

const hederaClient = await createHederaClient(
const hederaClient = await HederaClientFactory.create(
curve,
privateKey,
hederaAccountId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@

import { providerErrors } from '@metamask/rpc-errors';
import { heading, text } from '@metamask/snaps-ui';
import { createHederaClient } from '../../snap/account';
import { updateSnapState } from '../../snap/state';
import { HederaClientFactory } from '../../snap/HederaClientFactory';
import { SnapUtils } from '../../utils/SnapUtils';
import { SnapState } from '../../snap/SnapState';
import { Account, AccountInfo } from '../../types/account';
import { AccountBalance, TxReceipt } from '../../types/hedera';
import { DeleteAccountRequestParams } from '../../types/params';
import { SnapDialogParams, WalletSnapParams } from '../../types/state';
import { SnapUtils } from '../../utils/SnapUtils';

/**
* Delete an account.
Expand Down Expand Up @@ -76,7 +76,7 @@ export async function deleteAccount(
throw providerErrors.userRejectedRequest();
}

const hederaClient = await createHederaClient(
const hederaClient = await HederaClientFactory.create(
curve,
privateKey,
hederaAccountId,
Expand All @@ -99,7 +99,7 @@ export async function deleteAccount(
// eslint-disable-next-line require-atomic-updates
state.accountState[hederaEvmAddress][network].accountInfo =
{} as AccountInfo;
await updateSnapState(state);
await SnapState.updateState(state);
} catch (error: any) {
const errMessage = `Error while trying to delete account: ${String(error)}`;
console.error(errMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import { providerErrors } from '@metamask/rpc-errors';
import { divider, heading, text } from '@metamask/snaps-ui';
import { HederaServiceImpl } from '../../services/impl/hedera';
import { createHederaClient } from '../../snap/account';
import { HederaClientFactory } from '../../snap/HederaClientFactory';
import { SnapUtils } from '../../utils/SnapUtils';
import { MirrorTokenInfo, TxReceipt } from '../../types/hedera';
import { DeleteAllowanceRequestParams } from '../../types/params';
import { SnapDialogParams, WalletSnapParams } from '../../types/state';
import { SnapUtils } from '../../utils/SnapUtils';

/**
* Delete an allowance for a given asset.
Expand Down Expand Up @@ -108,7 +108,7 @@ export async function deleteAllowance(
throw providerErrors.userRejectedRequest();
}

const hederaClient = await createHederaClient(
const hederaClient = await HederaClientFactory.create(
curve,
privateKey,
hederaAccountId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
*/

import { providerErrors } from '@metamask/rpc-errors';
import { createHederaClient } from '../../snap/account';
import { updateSnapState } from '../../snap/state';
import { HederaClientFactory } from '../../snap/HederaClientFactory';
import { SnapState } from '../../snap/SnapState';
import { WalletSnapParams } from '../../types/state';

/**
Expand All @@ -41,7 +41,7 @@ export async function getAccountBalance(
state.currentAccount;

try {
const hederaClient = await createHederaClient(
const hederaClient = await HederaClientFactory.create(
state.accountState[hederaEvmAddress][network].keyStore.curve,
state.accountState[hederaEvmAddress][network].keyStore.privateKey,
hederaAccountId,
Expand All @@ -62,7 +62,7 @@ export async function getAccountBalance(
].accountInfo.balance.timestamp = currentTimestamp;
state.currentAccount.balance.timestamp = currentTimestamp;

await updateSnapState(state);
await SnapState.updateState(state);
} catch (error: any) {
console.error(
`Error while trying to get account balance: ${String(error)}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import { providerErrors } from '@metamask/rpc-errors';
import { divider, heading, text } from '@metamask/snaps-ui';
import _ from 'lodash';
import { HederaServiceImpl } from '../../services/impl/hedera';
import { createHederaClient } from '../../snap/account';
import { updateSnapState } from '../../snap/state';
import { HederaClientFactory } from '../../snap/HederaClientFactory';
import { SnapUtils } from '../../utils/SnapUtils';
import { SnapState } from '../../snap/SnapState';
import { AccountInfo } from '../../types/account';
import { GetAccountInfoRequestParams, ServiceFee } from '../../types/params';
import { SnapDialogParams, WalletSnapParams } from '../../types/state';
import { SnapUtils } from '../../utils/SnapUtils';
import { TuumUtils } from '../../utils/TuumUtils';

/**
Expand Down Expand Up @@ -88,7 +88,7 @@ export async function getAccountInfo(
const hederaService = new HederaServiceImpl(network, mirrorNodeUrl);
accountInfo = await hederaService.getMirrorAccountInfo(accountIdToQuery);
} else {
const hederaClient = await createHederaClient(
const hederaClient = await HederaClientFactory.create(
curve,
privateKey,
hederaAccountId,
Expand Down Expand Up @@ -183,7 +183,7 @@ export async function getAccountInfo(
state.currentAccount.balance = accountInfo.balance;
state.accountState[hederaEvmAddress][network].mirrorNodeUrl =
mirrorNodeUrl;
await updateSnapState(state);
await SnapState.updateState(state);
}
} catch (error: any) {
console.error(`Error while trying to get account info: ${String(error)}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import { providerErrors } from '@metamask/rpc-errors';
import { divider, heading, text } from '@metamask/snaps-ui';
import _ from 'lodash';
import { HederaServiceImpl } from '../../services/impl/hedera';
import { createHederaClient } from '../../snap/account';
import { updateSnapState } from '../../snap/state';
import { HederaClientFactory } from '../../snap/HederaClientFactory';
import { SnapUtils } from '../../utils/SnapUtils';
import { SnapState } from '../../snap/SnapState';
import { TxReceipt } from '../../types/hedera';
import { StakeHbarRequestParams } from '../../types/params';
import { SnapDialogParams, WalletSnapParams } from '../../types/state';
import { SnapUtils } from '../../utils/SnapUtils';
import { Utils } from '../../utils/Utils';

/**
Expand Down Expand Up @@ -136,7 +136,7 @@ export async function stakeHbar(
throw providerErrors.userRejectedRequest();
}

const hederaClient = await createHederaClient(
const hederaClient = await HederaClientFactory.create(
curve,
privateKey,
hederaAccountId,
Expand All @@ -158,7 +158,7 @@ export async function stakeHbar(
state.accountState[hederaEvmAddress][
network
].accountInfo.stakingInfo.declineStakingReward = declineStakingReward;
await updateSnapState(state);
await SnapState.updateState(state);
} catch (error: any) {
const errMessage = `Error while trying to stake Hbar: ${String(error)}`;
console.error(errMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import { providerErrors } from '@metamask/rpc-errors';
import { divider, heading, text } from '@metamask/snaps-ui';
import _ from 'lodash';
import { HederaServiceImpl } from '../../services/impl/hedera';
import { createHederaClient } from '../../snap/account';
import { HederaClientFactory } from '../../snap/HederaClientFactory';
import { SnapUtils } from '../../utils/SnapUtils';
import { TxReceipt } from '../../types/hedera';
import { AssociateTokensRequestParams } from '../../types/params';
import { SnapDialogParams, WalletSnapParams } from '../../types/state';
import { SnapUtils } from '../../utils/SnapUtils';

/**
* Associates the provided Hedera account with the provided Hedera token(s).
Expand Down Expand Up @@ -115,7 +115,7 @@ export async function associateTokens(
throw providerErrors.userRejectedRequest();
}

const hederaClient = await createHederaClient(
const hederaClient = await HederaClientFactory.create(
curve,
privateKey,
hederaAccountId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import { providerErrors } from '@metamask/rpc-errors';
import { divider, heading, text } from '@metamask/snaps-ui';
import _ from 'lodash';
import { createHederaClient } from '../../snap/account';
import { HederaClientFactory } from '../../snap/HederaClientFactory';
import { SnapUtils } from '../../utils/SnapUtils';
import { TxReceipt } from '../../types/hedera';
import { CreateTokenRequestParams } from '../../types/params';
import { SnapDialogParams, WalletSnapParams } from '../../types/state';
import { SnapUtils } from '../../utils/SnapUtils';

/**
* Associates the provided Hedera account with the provided Hedera token(s).
Expand Down Expand Up @@ -187,7 +187,7 @@ export async function createToken(
throw providerErrors.userRejectedRequest();
}

const hederaClient = await createHederaClient(
const hederaClient = await HederaClientFactory.create(
curve,
privateKey,
hederaAccountId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import { providerErrors } from '@metamask/rpc-errors';
import { divider, heading, text } from '@metamask/snaps-ui';
import _ from 'lodash';
import { HederaServiceImpl } from '../../services/impl/hedera';
import { createHederaClient } from '../../snap/account';
import { HederaClientFactory } from '../../snap/HederaClientFactory';
import { SnapUtils } from '../../utils/SnapUtils';
import { AccountInfo } from '../../types/account';
import { SimpleTransfer, TxReceipt } from '../../types/hedera';
import {
Expand All @@ -31,7 +32,6 @@ import {
TransferCryptoRequestParams,
} from '../../types/params';
import { SnapDialogParams, WalletSnapParams } from '../../types/state';
import { SnapUtils } from '../../utils/SnapUtils';
import { getAccountInfo } from '../account/getAccountInfo';

/**
Expand Down Expand Up @@ -247,7 +247,7 @@ export async function transferCrypto(
throw providerErrors.userRejectedRequest();
}

const hederaClient = await createHederaClient(
const hederaClient = await HederaClientFactory.create(
state.accountState[hederaEvmAddress][network].keyStore.curve,
state.accountState[hederaEvmAddress][network].keyStore.privateKey,
hederaAccountId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { providerErrors } from '@metamask/rpc-errors';
import { getHederaClient } from '../services/impl/hedera';
import { SimpleHederaClient } from '../types/hedera';

export class HederaClientFactory {
/**
* Create Hedera Client to use for transactions.
*
* @param curve - Curve that was used to derive the keys('ECDSA_SECP256K1' | 'ED25519').
* @param privateKey - Private key of the account.
* @param hederaAccountId - Hedera Account ID.
* @param network - Hedera network.
* @param mirrorNodeUrl - Hedera mirror node URL.
* @returns SimpleHederaClient.
*/
public static async create(
curve: string,
privateKey: string,
hederaAccountId: string,
network: string,
mirrorNodeUrl: string,
): Promise<SimpleHederaClient> {
const hederaClient = await getHederaClient(
curve,
privateKey,
hederaAccountId,
network,
mirrorNodeUrl,
);
if (!hederaClient) {
console.error(
`Could not setup a Hedera client with '${hederaAccountId}' at this time. Please try again later.`,
);
throw providerErrors.custom({
code: 4200,
message: `Could not setup a Hedera client with ${hederaAccountId} on ${network} at this time. Please try again later.`,
data: hederaAccountId,
});
}

return hederaClient;
}
}
Loading

0 comments on commit 667275b

Please sign in to comment.