From 634152d885e14d5c41e2caabfd8f45337bbb66c4 Mon Sep 17 00:00:00 2001 From: Stanley Yuen <102275989+stanleyyconsensys@users.noreply.github.com> Date: Wed, 22 May 2024 11:32:01 +0800 Subject: [PATCH] fix: get-starknet execute txn not working in firefox (#242) * fix: firefox undefined issue * fix: lint style --- packages/get-starknet/src/snap.ts | 43 +++++++++++++++++-------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/packages/get-starknet/src/snap.ts b/packages/get-starknet/src/snap.ts index 690b9ffe..2aae13d0 100644 --- a/packages/get-starknet/src/snap.ts +++ b/packages/get-starknet/src/snap.ts @@ -45,7 +45,7 @@ export class MetaMaskSnap { signerAddress: string, transactions: Call[], transactionsDetail: InvocationsSignerDetails, - abis?: Abi[] | undefined, + abis?: Abi[], ): Promise { return (await this.#provider.request({ method: 'wallet_invokeSnap', @@ -53,13 +53,13 @@ export class MetaMaskSnap { snapId: this.#snapId, request: { method: 'starkNet_signTransaction', - params: { + params: this.removeUndefined({ signerAddress, transactions, transactionsDetail, abis: abis, ...(await this.#getSnapParams()), - }, + }), }, }, })) as Signature; @@ -75,11 +75,11 @@ export class MetaMaskSnap { snapId: this.#snapId, request: { method: 'starkNet_signDeployAccountTransaction', - params: { + params: this.removeUndefined({ signerAddress, transaction, ...(await this.#getSnapParams()), - }, + }), }, }, })) as Signature; @@ -92,11 +92,11 @@ export class MetaMaskSnap { snapId: this.#snapId, request: { method: 'starkNet_signDeclareTransaction', - params: { + params: this.removeUndefined({ signerAddress, transaction, ...(await this.#getSnapParams()), - }, + }), }, }, })) as Signature; @@ -105,7 +105,7 @@ export class MetaMaskSnap { async execute( senderAddress: string, txnInvocation: AllowArray, - abis?: Abi[] | undefined, + abis?: Abi[], invocationsDetails?: InvocationsDetails, ): Promise { return (await this.#provider.request({ @@ -114,13 +114,13 @@ export class MetaMaskSnap { snapId: this.#snapId, request: { method: 'starkNet_executeTxn', - params: { + params: this.removeUndefined({ senderAddress, txnInvocation, - abis, invocationsDetails, + abis, ...(await this.#getSnapParams()), - }, + }), }, }, })) as InvokeFunctionResponse; @@ -133,12 +133,12 @@ export class MetaMaskSnap { snapId: this.#snapId, request: { method: 'starkNet_signMessage', - params: { + params: this.removeUndefined({ signerAddress, typedDataMessage, enableAuthorize: enableAuthorize, ...(await this.#getSnapParams()), - }, + }), }, }, })) as Signature; @@ -155,12 +155,12 @@ export class MetaMaskSnap { snapId: this.#snapId, request: { method: 'starkNet_declareContract', - params: { + params: this.removeUndefined({ senderAddress, contractPayload, invocationsDetails, ...(await this.#getSnapParams()), - }, + }), }, }, })) as DeclareContractResponse; @@ -236,12 +236,12 @@ export class MetaMaskSnap { snapId: this.#snapId, request: { method: 'starkNet_addNetwork', - params: { + params: this.removeUndefined({ networkName: chainName, networkChainId: chainId, networkNodeUrl: rpcUrl, networkVoyagerUrl: explorerUrl, - }, + }), }, }, })) as boolean; @@ -254,12 +254,12 @@ export class MetaMaskSnap { snapId: this.#snapId, request: { method: 'starkNet_addErc20Token', - params: { + params: this.removeUndefined({ tokenAddress: address, tokenName: name, tokenSymbol: symbol, tokenDecimals: decimals, - }, + }), }, }, }) as unknown as boolean; @@ -350,4 +350,9 @@ export class MetaMaskSnap { return false; } } + + removeUndefined(obj: Record) { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + return Object.fromEntries(Object.entries(obj).filter(([_, v]) => v !== undefined)); + } }