Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable the flag exactOptionalPropertyTypes #796

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/ComposableController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ export class ComposableController extends BaseController<never, any> {
);
this.initialize();
this.controllers = controllers;
this.messagingSystem = messenger;
if (messenger) {
this.messagingSystem = messenger;
}

this.controllers.forEach((controller) => {
const { name } = controller;
if ((controller as BaseController<any, any>).subscribe !== undefined) {
Expand Down
4 changes: 2 additions & 2 deletions src/ControllerMessenger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,8 @@ export class ControllerMessenger<
>({
controllerMessenger: this,
name,
allowedActions,
allowedEvents,
...(allowedActions ? { allowedActions } : {}),
...(allowedEvents ? { allowedEvents } : {}),
});
}
}
8 changes: 6 additions & 2 deletions src/assets/CollectiblesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,9 @@ export class CollectiblesController extends BaseController<
}
}

collectible.isCurrentlyOwned = isOwned;
if (isOwned) {
collectible.isCurrentlyOwned = isOwned;
}

if (batch === true) {
return collectible;
Expand All @@ -1084,7 +1086,9 @@ export class CollectiblesController extends BaseController<
item.address.toLowerCase() === address.toLowerCase(),
);
if (collectibleToUpdate) {
collectibleToUpdate.isCurrentlyOwned = isOwned;
if (isOwned) {
collectibleToUpdate.isCurrentlyOwned = isOwned;
}
this.updateNestedCollectibleState(
collectibles,
ALL_COLLECTIBLES_STATE_KEY,
Expand Down
12 changes: 9 additions & 3 deletions src/assets/TokensController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,13 @@ export class TokensController extends BaseController<
address = toChecksumHexAddress(address);
const { tokens, ignoredTokens } = this.state;
const isERC721 = await this._detectIsERC721(address);
const newEntry: Token = { address, symbol, decimals, image, isERC721 };
const newEntry: Token = {
address,
symbol,
decimals,
...(image ? { image } : {}),
isERC721,
};
const previousEntry = tokens.find(
(token) => token.address.toLowerCase() === address.toLowerCase(),
);
Expand Down Expand Up @@ -276,8 +282,8 @@ export class TokensController extends BaseController<
address: checksumAddress,
symbol,
decimals,
image,
isERC721,
...(image ? { image } : {}),
...(isERC721 ? { isERC721 } : {}),
};
const previousEntry = tokens.find(
(token) =>
Expand Down
2 changes: 1 addition & 1 deletion src/gas/GasFeeController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ describe('GasFeeController', () => {
getCurrentNetworkEIP1559Compatibility: getIsEIP1559Compatible, // change this for networkController.state.properties.isEIP1559Compatible ???
legacyAPIEndpoint,
EIP1559APIEndpoint,
clientId,
...(clientId ? { clientId } : {}),
});
}

Expand Down
5 changes: 4 additions & 1 deletion src/gas/GasFeeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,10 @@ export class GasFeeController extends BaseController<
this.currentChainId = this.getChainId();
const provider = getProvider();
this.ethQuery = new EthQuery(provider);
this.clientId = clientId;
if (clientId) {
this.clientId = clientId;
}

onNetworkStateChange(async () => {
const newProvider = getProvider();
const newChainId = this.getChainId();
Expand Down
3 changes: 0 additions & 3 deletions src/keyring/KeyringController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -756,10 +756,7 @@ describe('KeyringController', () => {
maxFeePerGas: '0x2540be400',
maxPriorityFeePerGas: '0x3b9aca00',
nonce: '0x68',
r: undefined,
s: undefined,
to: '0x0c54fccd2e384b4bb6f2e405bf5cbc15a017aafb',
v: undefined,
value: '0x0',
type: 2,
},
Expand Down
4 changes: 3 additions & 1 deletion src/keyring/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ export class KeyringController extends BaseController<
this.syncIdentities = syncIdentities;
this.updateIdentities = updateIdentities;
this.setSelectedAddress = setSelectedAddress;
this.setAccountLabel = setAccountLabel;
if (setAccountLabel) {
this.setAccountLabel = setAccountLabel;
}
this.initialize();
this.fullUpdate();
}
Expand Down
2 changes: 1 addition & 1 deletion src/message-manager/MessageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class MessageManager extends AbstractMessageManager<
* @returns The id of the newly created message.
*/
addUnapprovedMessage(messageParams: MessageParams, req?: OriginalRequest) {
if (req) {
if (req?.origin) {
messageParams.origin = req.origin;
}
messageParams.data = normalizeMessageData(messageParams.data);
Expand Down
2 changes: 1 addition & 1 deletion src/message-manager/PersonalMessageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class PersonalMessageManager extends AbstractMessageManager<
messageParams: PersonalMessageParams,
req?: OriginalRequest,
) {
if (req) {
if (req?.origin) {
messageParams.origin = req.origin;
}
messageParams.data = normalizeMessageData(messageParams.data);
Expand Down
2 changes: 1 addition & 1 deletion src/message-manager/TypedMessageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export class TypedMessageManager extends AbstractMessageManager<
metamaskId: messageId,
version,
};
if (req) {
if (req?.origin) {
messageParams.origin = req.origin;
}
const messageData: TypedMessage = {
Expand Down
6 changes: 5 additions & 1 deletion src/network/NetworkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,11 @@ export class NetworkController extends BaseController<
this.update({
provider: {
...this.state.provider,
...{ type: RPC, ticker, rpcTarget, chainId, nickname },
type: RPC,
rpcTarget,
chainId,
...(ticker ? { ticker } : {}),
...(nickname ? { nickname } : {}),
},
});
this.refreshNetwork();
Expand Down
2 changes: 1 addition & 1 deletion src/permissions/PermissionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1582,7 +1582,7 @@ export class PermissionController<
);

const permissionOptions = {
caveats,
...(caveats ? { caveats } : {}),
invoker: origin,
target: targetName,
};
Expand Down
12 changes: 6 additions & 6 deletions src/permissions/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function unauthorized(opts: UnauthorizedArg) {
return ethErrors.provider.unauthorized({
message:
'Unauthorized to perform action. Try requesting the required permission(s) first. For more information, see: https://docs.metamask.io/guide/rpc-api.html#permissions',
data: opts.data,
...(opts.data ? { data: opts.data } : {}),
});
}

Expand Down Expand Up @@ -47,10 +47,7 @@ type InvalidParamsArg = {
* @returns The built error
*/
export function invalidParams(opts: InvalidParamsArg) {
return ethErrors.rpc.invalidParams({
data: opts.data,
message: opts.message,
});
return ethErrors.rpc.invalidParams(opts);
}

/**
Expand All @@ -76,7 +73,10 @@ export function internalError<Data extends Record<string, unknown>>(
message: string,
data?: Data,
): EthereumRpcError<Data> {
return ethErrors.rpc.internal({ message, data });
return ethErrors.rpc.internal({
...(message ? { message } : {}),
...(data ? { data } : {}),
});
}

export class InvalidSubjectIdentifierError extends Error {
Expand Down
13 changes: 6 additions & 7 deletions src/transaction/TransactionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,11 +508,11 @@ export class TransactionController extends BaseController<
id: random(),
networkID: network,
chainId: provider.chainId,
origin,
...(origin ? { origin } : {}),
status: TransactionStatus.unapproved as TransactionStatus.unapproved,
time: Date.now(),
transaction,
deviceConfirmedOn,
...(deviceConfirmedOn ? { deviceConfirmedOn } : {}),
verifiedOnBlockchain: false,
};

Expand Down Expand Up @@ -591,7 +591,7 @@ export class TransactionController extends BaseController<
}

const customChainParams = {
name,
...(name ? { name } : {}),
chainId: parseInt(chainId, undefined),
networkId: parseInt(networkId, undefined),
};
Expand Down Expand Up @@ -978,10 +978,9 @@ export class TransactionController extends BaseController<
return { gas: '0x5208', gasPrice };
}

// if data, should be hex string format
estimatedTransaction.data = !data
? data
: /* istanbul ignore next */ addHexPrefix(data);
if (data) {
estimatedTransaction.data = addHexPrefix(data);
}

// 3. If this is a contract address, safely estimate gas using RPC
estimatedTransaction.value =
Expand Down
4 changes: 0 additions & 4 deletions src/transaction/mocks/txsMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,6 @@ export const txsInStateWithOutdatedGasDataMock = (
data: '0x',
gas: '624874',
gasPrice: '20000000000',
gasUsed: undefined,
nonce: '0x12',
to: '0x881d40237659c251811cec9c364ef91dc08d300c',
value: '0x0',
Expand All @@ -483,7 +482,6 @@ export const txsInStateWithOutdatedGasDataMock = (
data: '0x',
gas: '0x51d68',
gasPrice: '0x2540be400',
gasUsed: undefined,
nonce: '0x12',
to: '0x6bf137f335ea1b8f193b8f6ea92561a60d23a207',
value: '100000000000000000',
Expand All @@ -507,7 +505,6 @@ export const txsInStateWithOutdatedStatusAndGasDataMock = (
data: '0x',
gas: '624874',
gasPrice: '20000000000',
gasUsed: undefined,
nonce: '0x12',
to: '0x881d40237659c251811cec9c364ef91dc08d300c',
value: '0x0',
Expand All @@ -525,7 +522,6 @@ export const txsInStateWithOutdatedStatusAndGasDataMock = (
data: '0x',
gas: '0x51d68',
gasPrice: '0x2540be400',
gasUsed: undefined,
nonce: '0x12',
to: '0x6bf137f335ea1b8f193b8f6ea92561a60d23a207',
value: '100000000000000000',
Expand Down
19 changes: 15 additions & 4 deletions src/user/PreferencesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,22 @@ export class PreferencesController extends BaseController<
}
const newFrequestRpc: FrequentRpc = {
rpcUrl: url,
chainId,
ticker,
nickname,
rpcPrefs,
};
if (chainId) {
newFrequestRpc.chainId = chainId;
}

if (ticker) {
newFrequestRpc.ticker = ticker;
}

if (nickname) {
newFrequestRpc.nickname = nickname;
}

if (rpcPrefs) {
newFrequestRpc.rpcPrefs = rpcPrefs;
}
frequentRpcList.push(newFrequestRpc);
this.update({ frequentRpcList: [...frequentRpcList] });
}
Expand Down
2 changes: 1 addition & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ export function getIpfsCIDv1AndPath(ipfsUrl: string): {
// because most cid v0s appear to be incompatible with IPFS subdomains
return {
cid: CID.parse(cid).toV1().toString(),
path,
...(path ? { path } : {}),
};
}

Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"baseUrl": ".",
"esModuleInterop": true,
"exactOptionalPropertyTypes": true,
"inlineSources": true,
"module": "commonjs",
"moduleResolution": "node",
Expand Down