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

Make Engine ts #3072

Merged
merged 6 commits into from
Sep 1, 2021
Merged
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
71 changes: 40 additions & 31 deletions app/core/Engine.js → app/core/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
PreferencesController,
TokenBalancesController,
TokenRatesController,
Transaction,
TransactionController,
TypedMessageManager,
WalletDevice,
Expand All @@ -37,7 +38,7 @@ import { LAST_INCOMING_TX_BLOCK_INFO } from '../constants/storage';
const NON_EMPTY = 'NON_EMPTY';

const encryptor = new Encryptor();
let currentChainId;
let currentChainId: any;

/**
* Core controller responsible for composing other metamask controllers together
Expand All @@ -53,7 +54,7 @@ class Engine {
* Object containing the info for the latest incoming tx block
* for each address and network
*/
lastIncomingTxBlockInfo;
lastIncomingTxBlockInfo: any;

/**
* Creates a CoreController instance
Expand All @@ -70,7 +71,7 @@ class Engine {
infuraProjectId: process.env.MM_INFURA_PROJECT_ID || NON_EMPTY,
providerConfig: {
static: {
eth_sendTransaction: async (payload, next, end) => {
eth_sendTransaction: async (payload: { params: any[]; origin: any; }, next: any, end: (arg0: undefined, arg1: undefined) => void) => {
const { TransactionController } = this.context;
try {
const hash = await (await TransactionController.addTransaction(
Expand All @@ -84,7 +85,7 @@ class Engine {
}
}
},
getAccounts: (end, payload) => {
getAccounts: (end: (arg0: null, arg1: any[]) => void, payload: { hostname: string | number; }) => {
const { approvedHosts, privacyMode } = store.getState();
const isEnabled = !privacyMode || approvedHosts[payload.hostname];
const { KeyringController } = this.context;
Expand Down Expand Up @@ -240,7 +241,7 @@ class Engine {
collectibles.setApiKey(process.env.MM_OPENSEA_KEY);
network.refreshNetwork();
transaction.configure({ sign: keyring.signTransaction.bind(keyring) });
network.subscribe(state => {
network.subscribe((state: { network: string; provider: { chainId: any; }; }) => {
if (state.network !== 'loading' && state.provider.chainId !== currentChainId) {
// We should add a state or event emitter saying the provider changed
setTimeout(() => {
Expand Down Expand Up @@ -280,7 +281,7 @@ class Engine {
AccountTrackerController.refresh();
}

refreshTransactionHistory = async forceCheck => {
refreshTransactionHistory = async (forceCheck: any) => {
const { TransactionController, PreferencesController, NetworkController } = this.context;
const { selectedAddress } = PreferencesController.state;
const { type: networkType } = NetworkController.state.provider;
Expand Down Expand Up @@ -358,7 +359,7 @@ class Engine {
if (tokens.length > 0) {
const { contractBalances: tokenBalances } = TokenBalancesController.state;
const { contractExchangeRates: tokenExchangeRates } = TokenRatesController.state;
tokens.forEach(item => {
tokens.forEach((item: { address: string; balance: string | undefined; decimals: number; }) => {
const exchangeRate = item.address in tokenExchangeRates ? tokenExchangeRates[item.address] : undefined;
const tokenBalance =
item.balance ||
Expand Down Expand Up @@ -392,7 +393,7 @@ class Engine {
const tokenBalances = backgroundState.TokenBalancesController.contractBalances;

let tokenFound = false;
tokens.forEach(token => {
tokens.forEach((token: { address: string | number; }) => {
if (tokenBalances[token.address] && !tokenBalances[token.address]?.isZero()) {
tokenFound = true;
}
Expand Down Expand Up @@ -490,7 +491,7 @@ class Engine {
? contractMap[toChecksumAddress(address)].erc20
: true
)
.map(token => ({ ...token, address: toChecksumAddress(token.address) }));
.map((token: { address: string; }) => ({ ...token, address: toChecksumAddress(token.address) }));
});
});
await TokensController.update({ allTokens });
Expand All @@ -512,25 +513,33 @@ class Engine {
PreferencesController.setSelectedAddress(accounts.hd[0]);
}

const mapTx = tx => ({
id: tx.id,
networkID: tx.metamaskNetworkId,
origin: tx.origin,
status: tx.status,
time: tx.time,
transactionHash: tx.hash,
rawTx: tx.rawTx,
transaction: {
from: tx.txParams.from,
to: tx.txParams.to,
nonce: tx.txParams.nonce,
gas: tx.txParams.gas,
gasPrice: tx.txParams.gasPrice,
value: tx.txParams.value,
maxFeePerGas: tx.txParams.maxFeePerGas,
maxPriorityFeePerGas: tx.txParams.maxPriorityFeePerGas,
data: tx.txParams.data
}
const mapTx = ({
id,
metamaskNetworkId,
origin,
status,
time,
hash,
rawTx,
txParams
}: {
id: any;
metamaskNetworkId: string;
origin: string;
status: string;
time: any;
hash: string;
rawTx: string;
txParams: Transaction;
}) => ({
id,
networkID: metamaskNetworkId,
origin,
status,
time,
transactionHash: hash,
rawTx,
transaction: { ...txParams }
});

await TransactionController.update({
Expand All @@ -541,7 +550,7 @@ class Engine {
};
}

let instance;
let instance: Engine;

export default {
get context() {
Expand Down Expand Up @@ -612,13 +621,13 @@ export default {
resetState() {
return instance.resetState();
},
sync(data) {
sync(data: any) {
return instance.sync(data);
},
refreshTransactionHistory(forceCheck = false) {
return instance.refreshTransactionHistory(forceCheck);
},
init(state) {
init(state: {} | undefined) {
instance = new Engine(state);
Object.freeze(instance);
return instance;
Expand Down