Skip to content

Commit

Permalink
chore: update get-starknet to match new rpcs endpoints (#349)
Browse files Browse the repository at this point in the history
* chore: rollback keep legacy send txn

* fix: get-starknet to match new rpcs parameters

* docs: mark abis as deprecated in get-starknet signTransaction

* fix: address comments

* chore: lint + prettier

* fix: return resource bound in estimateFee and use it in executeTxn

* fix: return resource bound in estimateFee and use it in executeTxn

* fix: return resource bound in estimateFee and use it in executeTxn

* fix: apply suggestions from code review

Co-authored-by: Stanley Yuen <[email protected]>

* fix: address comment

---------

Co-authored-by: stanleyyuen <[email protected]>
  • Loading branch information
khanti42 and stanleyyconsensys authored Sep 6, 2024
1 parent 6f2b099 commit ac41722
Show file tree
Hide file tree
Showing 15 changed files with 770 additions and 544 deletions.
11 changes: 9 additions & 2 deletions packages/get-starknet/src/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,23 @@ export class MetaMaskSigner implements SignerInterface {
return new ec.starkCurve.Signature(numUtils.toBigInt(result[0]), numUtils.toBigInt(result[1]));
}

/**
* Signs a transaction calling the Snap.
*
* @param transactions - The array of transactions to be signed.
* @param transactionsDetail - The details required for signing the transactions.
* @param _abis - [Deprecated] The ABI definitions for the contracts involved in the transactions. This parameter is optional and may be undefined.
* @returns A promise that resolves to the transaction signature.
*/
async signTransaction(
transactions: Call[],
transactionsDetail: InvocationsSignerDetails,
abis?: Abi[] | undefined,
_abis?: Abi[] | undefined,
): Promise<Signature> {
const result = (await this.#snap.signTransaction(
this.#address,
transactions,
transactionsDetail,
abis,
)) as ArraySignatureType;
return new ec.starkCurve.Signature(numUtils.toBigInt(result[0]), numUtils.toBigInt(result[1]));
}
Expand Down
22 changes: 10 additions & 12 deletions packages/get-starknet/src/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ export class MetaMaskSnap {
}

async signTransaction(
signerAddress: string,
address: string,
transactions: Call[],
transactionsDetail: InvocationsSignerDetails,
abis?: Abi[],
): Promise<Signature> {
return (await this.#provider.request({
method: 'wallet_invokeSnap',
Expand All @@ -57,10 +56,9 @@ export class MetaMaskSnap {
request: {
method: 'starkNet_signTransaction',
params: this.removeUndefined({
signerAddress,
address,
transactions,
transactionsDetail,
abis,
...(await this.#getSnapParams()),
}),
},
Expand Down Expand Up @@ -106,10 +104,10 @@ export class MetaMaskSnap {
}

async execute(
senderAddress: string,
txnInvocation: AllowArray<Call>,
address: string,
calls: AllowArray<Call>,
abis?: Abi[],
invocationsDetails?: InvocationsDetails,
details?: InvocationsDetails,
): Promise<InvokeFunctionResponse> {
return (await this.#provider.request({
method: 'wallet_invokeSnap',
Expand All @@ -118,9 +116,9 @@ export class MetaMaskSnap {
request: {
method: 'starkNet_executeTxn',
params: this.removeUndefined({
senderAddress,
txnInvocation,
invocationsDetails,
address,
calls,
details,
abis,
...(await this.#getSnapParams()),
}),
Expand All @@ -129,15 +127,15 @@ export class MetaMaskSnap {
})) as InvokeFunctionResponse;
}

async signMessage(typedDataMessage: TypedData, enableAuthorize: boolean, signerAddress: string): Promise<Signature> {
async signMessage(typedDataMessage: TypedData, enableAuthorize: boolean, address: string): Promise<Signature> {
return (await this.#provider.request({
method: 'wallet_invokeSnap',
params: {
snapId: this.#snapId,
request: {
method: 'starkNet_signMessage',
params: this.removeUndefined({
signerAddress,
address,
typedDataMessage,
enableAuthorize,
...(await this.#getSnapParams()),
Expand Down
37 changes: 36 additions & 1 deletion packages/starknet-snap/src/__tests__/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from '@metamask/key-tree';
import { generateMnemonic } from 'bip39';
import { getRandomValues } from 'crypto';
import type { constants } from 'starknet';
import type { constants, EstimateFee } from 'starknet';
import {
ec,
CallData,
Expand Down Expand Up @@ -284,3 +284,38 @@ export function generateTransactions({

return transactions.sort((a, b) => b.timestamp - a.timestamp);
}

/**
* Method to generate a mock estimate fee response.
*
* @returns An array containing a mock EstimateFee object.
*/
export function getEstimateFees() {
return [
{
// eslint-disable-next-line @typescript-eslint/naming-convention
overall_fee: BigInt(1500000000000000).toString(10),
// eslint-disable-next-line @typescript-eslint/naming-convention
gas_consumed: BigInt('0x0'),
suggestedMaxFee: BigInt(1500000000000000).toString(10),
// eslint-disable-next-line @typescript-eslint/naming-convention
gas_price: BigInt('0x0'),
resourceBounds: {
// eslint-disable-next-line @typescript-eslint/naming-convention
l1_gas: {
// eslint-disable-next-line @typescript-eslint/naming-convention
max_amount: '0',
// eslint-disable-next-line @typescript-eslint/naming-convention
max_price_per_unit: '0',
},
// eslint-disable-next-line @typescript-eslint/naming-convention
l2_gas: {
// eslint-disable-next-line @typescript-eslint/naming-convention
max_amount: '0',
// eslint-disable-next-line @typescript-eslint/naming-convention
max_price_per_unit: '0',
},
},
} as unknown as EstimateFee,
];
}
156 changes: 0 additions & 156 deletions packages/starknet-snap/src/executeTxn.ts

This file was deleted.

11 changes: 5 additions & 6 deletions packages/starknet-snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { createAccount } from './createAccount';
import { declareContract } from './declareContract';
import { estimateAccDeployFee } from './estimateAccountDeployFee';
import { estimateFees } from './estimateFees';
import { executeTxn as executeTxnLegacy } from './executeTxn';
import { extractPublicKey } from './extractPublicKey';
import { getCurrentNetwork } from './getCurrentNetwork';
import { getErc20TokenBalance } from './getErc20TokenBalance';
Expand Down Expand Up @@ -54,6 +53,7 @@ import {
signDeclareTransaction,
verifySignature,
} from './rpcs';
import { sendTransaction } from './sendTransaction';
import { signDeployAccountTransaction } from './signDeployAccountTransaction';
import { switchNetwork } from './switchNetwork';
import type {
Expand Down Expand Up @@ -218,8 +218,8 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {

case 'starkNet_sendTransaction':
apiParams.keyDeriver = await getAddressKeyDeriver(snap);
return await executeTxn.execute(
apiParams.requestParams as unknown as ExecuteTxnParams,
return await sendTransaction(
apiParams as unknown as ApiParamsWithKeyDeriver,
);

case 'starkNet_getValue':
Expand Down Expand Up @@ -267,9 +267,8 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
);

case 'starkNet_executeTxn':
apiParams.keyDeriver = await getAddressKeyDeriver(snap);
return await executeTxnLegacy(
apiParams as unknown as ApiParamsWithKeyDeriver,
return await executeTxn.execute(
apiParams.requestParams as unknown as ExecuteTxnParams,
);

case 'starkNet_estimateFees':
Expand Down
11 changes: 10 additions & 1 deletion packages/starknet-snap/src/rpcs/estimateFee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Invocations } from 'starknet';
import { constants, TransactionType } from 'starknet';
import type { Infer } from 'superstruct';

import { getEstimateFees } from '../__tests__/helper';
import { FeeTokenUnit } from '../types/snapApi';
import { STARKNET_SEPOLIA_TESTNET_NETWORK } from '../utils/constants';
import * as starknetUtils from '../utils/starknetUtils';
Expand Down Expand Up @@ -44,11 +45,14 @@ const prepareMockEstimateFee = ({
details: { version },
} as unknown as EstimateFeeParams;

const estimateResults = getEstimateFees();

const estimateBulkFeeRespMock = {
suggestedMaxFee: BigInt(1000000000000000).toString(10),
overallFee: BigInt(1500000000000000).toString(10),
unit: FeeTokenUnit.ETH,
includeDeploy,
estimateResults,
};

const getEstimatedFeesSpy = jest.spyOn(starknetUtils, 'getEstimatedFees');
Expand Down Expand Up @@ -89,7 +93,12 @@ describe('estimateFee', () => {
version: constants.TRANSACTION_VERSION.V1,
},
);
expect(result).toStrictEqual(estimateBulkFeeRespMock);
expect(result).toStrictEqual({
includeDeploy: estimateBulkFeeRespMock.includeDeploy,
overallFee: estimateBulkFeeRespMock.overallFee,
suggestedMaxFee: estimateBulkFeeRespMock.suggestedMaxFee,
unit: estimateBulkFeeRespMock.unit,
});
});

it('throws `InvalidParamsError` when request parameter is not correct', async () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/starknet-snap/src/rpcs/estimateFee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ export class EstimateFeeRpc extends AccountRpcController<
details,
);

return estimateFeeResp;
return {
suggestedMaxFee: estimateFeeResp.suggestedMaxFee,
overallFee: estimateFeeResp.overallFee,
unit: estimateFeeResp.unit,
includeDeploy: estimateFeeResp.includeDeploy,
};
}
}

Expand Down
Loading

0 comments on commit ac41722

Please sign in to comment.