Skip to content

Commit

Permalink
Add mainnet fork usage behind feature flag
Browse files Browse the repository at this point in the history
- translate between chainID 1337<>1 on modifying transactions
- use env variables for url and chainID

Using a prodlike environment — running our code agains a mainnet fork
is valuable so we can test contract interaction and dApps integrations
that are deployed on mainnet.

For the deployed dApps to work normaly they need to see chainID 1.
Otherwise it will trigger a "sleect a supported network" response.

Hardhat mainnet fork freezes atm if the chainID is set to 1 but works
well with chainID 1337.

The soution is to translate between them when writing to the fork.
  • Loading branch information
Gergo Nagy committed Mar 5, 2022
1 parent 33ca4ae commit 493a761
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .env.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ HIDE_IMPORT_LEDGER=false
GAS_PRICE_POOLING_FREQUENCY=120
ETHEREUM_NETWORK=mainnet
PERSIST_UI_LOCATION=false
USE_MAINNET_FORK=false
MAINNET_FORK_URL="http://127.0.0.1:8545"
MAINNET_FORK_CHAIN_ID=1337

7 changes: 7 additions & 0 deletions background/constants/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ export const BITCOIN: Network = {
family: "BTC",
}

export const FORK: EVMNetwork = {
name: "Ethereum",
baseAsset: ETH,
chainID: process.env.MAINNET_FORK_CHAIN_ID ?? "1337",
family: "EVM",
}

export const EVM_MAIN_NETWORKS = [ETHEREUM]

export const EVM_TEST_NETWORKS = [ROPSTEN, RINKEBY, GOERLI, KOVAN]
Expand Down
1 change: 1 addition & 0 deletions background/features/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export const HIDE_IMPORT_DERIVATION_PATH =
export const HIDE_CREATE_PHRASE = process.env.HIDE_CREATE_PHRASE === "true"
export const HIDE_IMPORT_LEDGER = process.env.HIDE_IMPORT_LEDGER === "true"
export const PERSIST_UI_LOCATION = process.env.PERSIST_UI_LOCATION === "true"
export const USE_MAINNET_FORK = process.env.USE_MAINNET_FORK === "true"
6 changes: 6 additions & 0 deletions background/redux-slices/transaction-construction.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { createSlice, createSelector } from "@reduxjs/toolkit"
import Emittery from "emittery"
import { FORK } from "../constants"
import {
EXPRESS,
INSTANT,
MAX_FEE_MULTIPLIER,
REGULAR,
} from "../constants/network-fees"
import { USE_MAINNET_FORK } from "../features/features"

import {
BlockEstimate,
Expand Down Expand Up @@ -96,6 +98,10 @@ export const updateTransactionOptions = createBackgroundAsyncThunk(
export const signTransaction = createBackgroundAsyncThunk(
"transaction-construction/sign",
async (request: SignatureRequest) => {
if (USE_MAINNET_FORK) {
request.transaction.chainID = FORK.chainID
}

await emitter.emit("requestSignature", request)
}
)
Expand Down
4 changes: 3 additions & 1 deletion background/services/chain/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
ConfirmedEVMTransaction,
} from "../../networks"
import { FungibleAsset } from "../../assets"
import { USE_MAINNET_FORK } from "../../features/features"
import { FORK } from "../../constants"

/**
* Parse a block as returned by a polling provider.
Expand Down Expand Up @@ -132,7 +134,7 @@ export function ethersTransactionFromSignedTransaction(
from: tx.from,
data: tx.input || "",
type: tx.type,
chainId: parseInt(tx.network.chainID, 10),
chainId: parseInt(USE_MAINNET_FORK ? FORK.chainID : tx.network.chainID, 10),
value: BigNumber.from(tx.value),
gasLimit: BigNumber.from(tx.gasLimit),
}
Expand Down
6 changes: 3 additions & 3 deletions background/services/keyring/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import {
} from "../../types"
import { EIP1559TransactionRequest, SignedEVMTransaction } from "../../networks"
import BaseService from "../base"
import { ETH, MINUTE } from "../../constants"
import { ETH, FORK, MINUTE } from "../../constants"
import { ethersTransactionRequestFromEIP1559TransactionRequest } from "../chain/utils"
import { HIDE_IMPORT_LEDGER } from "../../features/features"
import { HIDE_IMPORT_LEDGER, USE_MAINNET_FORK } from "../../features/features"
import { AddressOnNetwork } from "../../accounts"

export const MAX_KEYRING_IDLE_TIME = 60 * MINUTE
Expand Down Expand Up @@ -444,7 +444,7 @@ export default class KeyringService extends BaseService<Events> {
blockHash: null,
blockHeight: null,
asset: ETH,
network,
network: USE_MAINNET_FORK ? FORK : network,
}
if (HIDE_IMPORT_LEDGER) {
this.emitter.emit("signedTx", signedTx)
Expand Down
6 changes: 5 additions & 1 deletion background/services/signing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import BaseService from "../base"
import { ServiceCreatorFunction, ServiceLifecycleEvents } from "../types"
import ChainService from "../chain"
import { SigningMethod } from "../../redux-slices/signing"
import { USE_MAINNET_FORK } from "../../features/features"
import { FORK } from "../../constants"

type SigningErrorReason = "userRejected" | "genericError"
type ErrorResponse = {
Expand Down Expand Up @@ -138,7 +140,9 @@ export default class SigningService extends BaseService<Events> {
transactionRequest: EIP1559TransactionRequest,
signingMethod: SigningMethod
): Promise<SignedEVMTransaction> {
const network = this.chainService.resolveNetwork(transactionRequest)
const network = USE_MAINNET_FORK
? FORK
: this.chainService.resolveNetwork(transactionRequest)
if (typeof network === "undefined") {
throw new Error(`Unknown chain ID ${transactionRequest.chainID}.`)
}
Expand Down

0 comments on commit 493a761

Please sign in to comment.