Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/npm_and_yarn/fast-xml-parser-4.4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
khanti42 authored Aug 13, 2024
2 parents 4d41000 + 458fa8a commit 51c0f41
Show file tree
Hide file tree
Showing 43 changed files with 1,220 additions and 967 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches: [main]
pull_request:
merge_group:

jobs:
check-workflows:
Expand Down
4 changes: 2 additions & 2 deletions packages/starknet-snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
"serve": "mm-snap serve",
"start": "mm-snap watch",
"test": "yarn run test:unit && yarn run cover:report && yarn run jest",
"test:unit": "nyc --check-coverage --statements 70 --branches 70 --functions 70 --lines 70 mocha --colors -r ts-node/register \"test/**/*.test.ts\"",
"test:unit:one": "nyc --check-coverage --statements 70 --branches 70 --functions 70 --lines 70 mocha --colors -r ts-node/register"
"test:unit": "nyc --check-coverage --statements 50 --branches 50 --functions 50 --lines 50 mocha --colors -r ts-node/register \"test/**/*.test.ts\"",
"test:unit:one": "nyc --check-coverage --statements 50 --branches 50 --functions 50 --lines 50 mocha --colors -r ts-node/register"
},
"nyc": {
"exclude": [
Expand Down
35 changes: 35 additions & 0 deletions packages/starknet-snap/src/__tests__/fixture/typedDataExample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"types": {
"StarkNetDomain": [
{ "name": "name", "type": "felt" },
{ "name": "version", "type": "felt" },
{ "name": "chainId", "type": "felt" }
],
"Person": [
{ "name": "name", "type": "felt" },
{ "name": "wallet", "type": "felt" }
],
"Mail": [
{ "name": "from", "type": "Person" },
{ "name": "to", "type": "Person" },
{ "name": "contents", "type": "felt" }
]
},
"primaryType": "Mail",
"domain": {
"name": "Starknet Mail",
"version": "1",
"chainId": 1
},
"message": {
"from": {
"name": "Cow",
"wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
},
"to": {
"name": "Bob",
"wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
},
"contents": "Hello, Bob!"
}
}
14 changes: 2 additions & 12 deletions packages/starknet-snap/src/declareContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import { toJson } from './utils/serializer';
import {
getNetworkFromChainId,
getDeclareSnapTxt,
showAccountRequireUpgradeOrDeployModal,
verifyIfAccountNeedUpgradeOrDeploy,
} from './utils/snapUtils';
import {
getKeysFromAddress,
declareContract as declareContractUtil,
validateAccountRequireUpgradeOrDeploy,
} from './utils/starknetUtils';

/**
Expand All @@ -38,16 +37,7 @@ export async function declareContract(params: ApiParamsWithKeyDeriver) {
senderAddress,
);

try {
await validateAccountRequireUpgradeOrDeploy(
network,
senderAddress,
publicKey,
);
} catch (validateError) {
await showAccountRequireUpgradeOrDeployModal(wallet, validateError);
throw validateError;
}
await verifyIfAccountNeedUpgradeOrDeploy(network, senderAddress, publicKey);

const snapComponents = getDeclareSnapTxt(
senderAddress,
Expand Down
58 changes: 21 additions & 37 deletions packages/starknet-snap/src/estimateFee.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Invocations } from 'starknet';
import type { EstimateFee, Invocations } from 'starknet';
import { TransactionType } from 'starknet';

import type {
Expand All @@ -8,13 +8,14 @@ import type {
import { ACCOUNT_CLASS_HASH } from './utils/constants';
import { logger } from './utils/logger';
import { toJson } from './utils/serializer';
import { getNetworkFromChainId } from './utils/snapUtils';
import {
validateAccountRequireUpgradeOrDeploy,
getNetworkFromChainId,
verifyIfAccountNeedUpgradeOrDeploy,
} from './utils/snapUtils';
import {
validateAndParseAddress,
getKeysFromAddress,
getCallDataArray,
estimateFee as estimateFeeUtil,
getAccContractAddressAndCallData,
estimateFeeBulk,
addFeesFromAllTransactions,
Expand Down Expand Up @@ -65,10 +66,11 @@ export async function estimateFee(params: ApiParamsWithKeyDeriver) {
const { privateKey: senderPrivateKey, publicKey } =
await getKeysFromAddress(keyDeriver, network, state, senderAddress);

await validateAccountRequireUpgradeOrDeploy(
await verifyIfAccountNeedUpgradeOrDeploy(
network,
senderAddress,
publicKey,
false,
);

const txnInvocation = {
Expand Down Expand Up @@ -108,44 +110,26 @@ export async function estimateFee(params: ApiParamsWithKeyDeriver) {
];
}

let estimateFeeResp;

if (accountDeployed) {
// This condition branch will be removed later when starknet.js
// supports estimateFeeBulk in rpc mode
estimateFeeResp = await estimateFeeUtil(
network,
senderAddress,
senderPrivateKey,
txnInvocation,
);
logger.log(
`estimateFee:\nestimateFeeUtil estimateFeeResp: ${toJson(
estimateFeeResp,
)}`,
);
} else {
const estimateBulkFeeResp = await estimateFeeBulk(
network,
senderAddress,
senderPrivateKey,
bulkTransactions,
);
logger.log(
`estimateFee:\nestimateFeeBulk estimateBulkFeeResp: ${toJson(
estimateBulkFeeResp,
)}`,
);
estimateFeeResp = addFeesFromAllTransactions(estimateBulkFeeResp);
}
const estimateBulkFeeResp = await estimateFeeBulk(
network,
senderAddress,
senderPrivateKey,
bulkTransactions,
);
logger.log(
`estimateFee:\nestimateFeeBulk estimateBulkFeeResp: ${toJson(
estimateBulkFeeResp,
)}`,
);
const estimateFeeResp = addFeesFromAllTransactions(
estimateBulkFeeResp,
) as EstimateFee;

logger.log(`estimateFee:\nestimateFeeResp: ${toJson(estimateFeeResp)}`);

const resp = {
suggestedMaxFee: estimateFeeResp.suggestedMaxFee.toString(10),
overallFee: estimateFeeResp.overall_fee.toString(10),
gasConsumed: estimateFeeResp.gas_consumed?.toString(10) ?? '0',
gasPrice: estimateFeeResp.gas_price?.toString(10) ?? '0',
unit: 'wei',
includeDeploy: !accountDeployed,
};
Expand Down
14 changes: 2 additions & 12 deletions packages/starknet-snap/src/executeTxn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
getNetworkFromChainId,
getTxnSnapTxt,
addDialogTxt,
showAccountRequireUpgradeOrDeployModal,
verifyIfAccountNeedUpgradeOrDeploy,
} from './utils/snapUtils';
import {
getKeysFromAddress,
Expand All @@ -23,7 +23,6 @@ import {
estimateFeeBulk,
getAccContractAddressAndCallData,
addFeesFromAllTransactions,
validateAccountRequireUpgradeOrDeploy,
} from './utils/starknetUtils';

/**
Expand All @@ -42,16 +41,7 @@ export async function executeTxn(params: ApiParamsWithKeyDeriver) {
addressIndex,
} = await getKeysFromAddress(keyDeriver, network, state, senderAddress);

try {
await validateAccountRequireUpgradeOrDeploy(
network,
senderAddress,
publicKey,
);
} catch (validateError) {
await showAccountRequireUpgradeOrDeployModal(wallet, validateError);
throw validateError;
}
await verifyIfAccountNeedUpgradeOrDeploy(network, senderAddress, publicKey);

const txnInvocationArray = Array.isArray(requestParamsObj.txnInvocation)
? requestParamsObj.txnInvocation
Expand Down
11 changes: 7 additions & 4 deletions packages/starknet-snap/src/extractPrivateKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import type {
} from './types/snapApi';
import { logger } from './utils/logger';
import { toJson } from './utils/serializer';
import { getNetworkFromChainId } from './utils/snapUtils';
import {
getNetworkFromChainId,
verifyIfAccountNeedUpgradeOrDeploy,
} from './utils/snapUtils';
import {
validateAndParseAddress,
getKeysFromAddress,
validateAccountRequireUpgradeOrDeploy,
} from './utils/starknetUtils';

/**
*
* @param params
Expand Down Expand Up @@ -43,10 +44,12 @@ export async function extractPrivateKey(params: ApiParamsWithKeyDeriver) {
state,
userAddress,
);
await validateAccountRequireUpgradeOrDeploy(

await verifyIfAccountNeedUpgradeOrDeploy(
network,
userAddress,
publicKey,
false,
);

const response = await wallet.request({
Expand Down
11 changes: 8 additions & 3 deletions packages/starknet-snap/src/extractPublicKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import type {
} from './types/snapApi';
import { logger } from './utils/logger';
import { toJson } from './utils/serializer';
import { getAccount, getNetworkFromChainId } from './utils/snapUtils';
import {
getAccount,
getNetworkFromChainId,
verifyIfAccountNeedUpgradeOrDeploy,
} from './utils/snapUtils';
import {
validateAndParseAddress,
validateAccountRequireUpgradeOrDeploy,
getKeysFromAddress,
} from './utils/starknetUtils';

Expand Down Expand Up @@ -48,10 +51,12 @@ export async function extractPublicKey(params: ApiParamsWithKeyDeriver) {
state,
userAddress,
);
await validateAccountRequireUpgradeOrDeploy(

await verifyIfAccountNeedUpgradeOrDeploy(
network,
userAddress,
publicKey,
false,
);

let userPublicKey;
Expand Down
31 changes: 15 additions & 16 deletions packages/starknet-snap/src/getErc20TokenBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import { BlockIdentifierEnum } from './utils/constants';
import { logger } from './utils/logger';
import { toJson } from './utils/serializer';
import { getNetworkFromChainId } from './utils/snapUtils';
import {
getBalance,
isAccountDeployed,
validateAndParseAddress,
} from './utils/starknetUtils';
import { getBalance, validateAndParseAddress } from './utils/starknetUtils';

/**
*
Expand Down Expand Up @@ -53,23 +49,26 @@ export async function getErc20TokenBalance(params: ApiParams) {
`getErc20Balance:\nerc20Address: ${erc20Address}\nuserAddress: ${userAddress}`,
);

// For deployed accounts, use the PENDING block to show balance updates as soon as possible,
// facilitating concurrent transactions without delays.
// For non-deployed accounts, use the LATEST block to avoid displaying non-zero balances
// from pending transactions, because confirmed non-zero balance is required for deployment.
const blockIdentifier = (await isAccountDeployed(network, userAddress))
? BlockIdentifierEnum.Pending
: BlockIdentifierEnum.Latest;
const balance = await getBalance(
const balanceLatest = await getBalance(
userAddress,
erc20Address,
network,
blockIdentifier,
BlockIdentifierEnum.Latest,
);
const balancePending = await getBalance(
userAddress,
erc20Address,
network,
BlockIdentifierEnum.Pending,
);

logger.log(`getErc20Balance:\nresp: ${toJson(balance)}`);
const resp = {
balancePending,
balanceLatest,
};
logger.log(`getErc20Balance:\nresp: ${toJson(resp)}`);

return balance;
return resp;
} catch (error) {
logger.error(`Problem found:`, error);
throw error;
Expand Down
5 changes: 3 additions & 2 deletions packages/starknet-snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { recoverAccounts } from './recoverAccounts';
import { sendTransaction } from './sendTransaction';
import { signDeclareTransaction } from './signDeclareTransaction';
import { signDeployAccountTransaction } from './signDeployAccountTransaction';
import type { SignMessageParams } from './signMessage';
import { signMessage } from './signMessage';
import { signTransaction } from './signTransaction';
import { switchNetwork } from './switchNetwork';
Expand Down Expand Up @@ -170,9 +171,9 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
);

case 'starkNet_signMessage':
apiParams.keyDeriver = await getAddressKeyDeriver(snap);
return await signMessage(
apiParams as unknown as ApiParamsWithKeyDeriver,
apiParams.requestParams as unknown as SignMessageParams,
state,
);

case 'starkNet_signTransaction':
Expand Down
14 changes: 2 additions & 12 deletions packages/starknet-snap/src/signDeclareTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import { toJson } from './utils/serializer';
import {
getNetworkFromChainId,
getSignTxnTxt,
showAccountRequireUpgradeOrDeployModal,
verifyIfAccountNeedUpgradeOrDeploy,
} from './utils/snapUtils';
import {
getKeysFromAddress,
signDeclareTransaction as signDeclareTransactionUtil,
validateAccountRequireUpgradeOrDeploy,
} from './utils/starknetUtils';

/**
Expand All @@ -38,16 +37,7 @@ export async function signDeclareTransaction(
signerAddress,
);

try {
await validateAccountRequireUpgradeOrDeploy(
network,
signerAddress,
publicKey,
);
} catch (validateError) {
await showAccountRequireUpgradeOrDeployModal(wallet, validateError);
throw validateError;
}
await verifyIfAccountNeedUpgradeOrDeploy(network, signerAddress, publicKey);

logger.log(
`signDeclareTransaction params: ${toJson(
Expand Down
Loading

0 comments on commit 51c0f41

Please sign in to comment.