Skip to content

Commit

Permalink
Merge branch 'main' into release-0.13.11
Browse files Browse the repository at this point in the history
  • Loading branch information
Gergő Nagy authored Jun 21, 2022
2 parents f816f71 + 764428d commit b04f9eb
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 99 deletions.
20 changes: 3 additions & 17 deletions background/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ import {
migrateReduxState,
REDUX_STATE_VERSION,
} from "./redux-slices/migrations"
import { selectCurrentAccountAssetBalance } from "./redux-slices/selectors"
import getMinMainAssetAmountForTransaction from "./utils/transaction"
import { TALLY_INTERNAL_ORIGIN } from "./services/internal-ethereum-provider/constants"

// This sanitizer runs on store and action data before serializing for remote
// redux devtools. The goal is to end up with an object that is directly
Expand Down Expand Up @@ -567,10 +566,6 @@ export default class Main extends BaseService<never> {
const {
values: { maxFeePerGas, maxPriorityFeePerGas },
} = selectDefaultNetworkFeeSettings(this.store.getState())
const mainAssetBalance = selectCurrentAccountAssetBalance(
this.store.getState(),
network.baseAsset.symbol
)

const { transactionRequest: populatedRequest, gasEstimationError } =
await this.chainService.populatePartialEVMTransactionRequest(
Expand All @@ -595,16 +590,6 @@ export default class Main extends BaseService<never> {
annotation,
}

const mainAssetNeeded = getMinMainAssetAmountForTransaction(
enrichedPopulatedRequest
)

if (mainAssetBalance && mainAssetBalance?.amount < mainAssetNeeded) {
this.store.dispatch(
setSnackbarMessage("Probably not enough funds to pay gas fee")
)
}

if (typeof gasEstimationError === "undefined") {
this.store.dispatch(
transactionRequest({
Expand Down Expand Up @@ -1035,7 +1020,8 @@ export default class Main extends BaseService<never> {
uiSliceEmitter.on("newSelectedNetwork", (network) => {
this.internalEthereumProviderService.routeSafeRPCRequest(
"wallet_switchEthereumChain",
[{ chainId: network.chainID }]
[{ chainId: network.chainID }],
TALLY_INTERNAL_ORIGIN
)
this.store.dispatch(clearCustomGas())
})
Expand Down
27 changes: 23 additions & 4 deletions background/services/enrichment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,25 @@ export default class EnrichmentService extends BaseService<Events> {
const resolvedTime = Date.now()
let block: AnyEVMBlock | undefined

if (transaction?.blockHash) {
block = await this.chainService.getBlockData(
let hasInsufficientFunds = false

const { gasLimit, maxFeePerGas, maxPriorityFeePerGas, blockHash } =
transaction

if (gasLimit && maxFeePerGas && maxPriorityFeePerGas) {
const gasFee = gasLimit * (maxFeePerGas + maxPriorityFeePerGas)
const {
assetAmount: { amount: baseAssetBalance },
} = await this.chainService.getLatestBaseAccountBalance({
address: transaction.from,
network,
transaction.blockHash
)
})
hasInsufficientFunds =
gasFee + (transaction.value ?? 0n) > baseAssetBalance
}

if (blockHash) {
block = await this.chainService.getBlockData(network, blockHash)
}

if (typeof transaction.to === "undefined") {
Expand Down Expand Up @@ -285,6 +299,11 @@ export default class EnrichmentService extends BaseService<Events> {
}
}

if (hasInsufficientFunds) {
txAnnotation.warnings ??= []
txAnnotation.warnings.push("insufficient-funds")
}

return txAnnotation
}

Expand Down
6 changes: 5 additions & 1 deletion background/services/enrichment/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ export type BaseTransactionAnnotation = {
warnings?: Warning[]
}

export type Warning = "send-to-token" | "send-to-contract" | "approve-eoa"
export type Warning =
| "send-to-token"
| "send-to-contract"
| "approve-eoa"
| "insufficient-funds"

export type ContractDeployment = BaseTransactionAnnotation & {
type: "contract-deployment"
Expand Down
2 changes: 2 additions & 0 deletions background/services/internal-ethereum-provider/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line import/prefer-default-export
export const TALLY_INTERNAL_ORIGIN = "@tally-internal"
46 changes: 46 additions & 0 deletions background/services/internal-ethereum-provider/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import Dexie from "dexie"
import { ETHEREUM } from "../../constants"
import { EVMNetwork } from "../../networks"
import { TALLY_INTERNAL_ORIGIN } from "./constants"

export type ActiveNetwork = {
origin: string
network: EVMNetwork
}

export class InternalEthereumProviderDatabase extends Dexie {
private activeNetwork!: Dexie.Table<ActiveNetwork, string>

constructor() {
super("tally/internal-ethereum-provider")

this.version(1)
.stores({
activeNetwork: "&origin,chainId,network, address",
})
.upgrade((tx) => {
return tx.table("activeNetwork").put({
origin: TALLY_INTERNAL_ORIGIN,
// New installs will default to having `Ethereum` as their active chain.
network: ETHEREUM,
})
})
}

async setActiveChainIdForOrigin(
origin: string,
network: EVMNetwork
): Promise<string | undefined> {
return this.activeNetwork.put({ origin, network })
}

async getActiveNetworkForOrigin(
origin: string
): Promise<ActiveNetwork | undefined> {
return this.activeNetwork.get({ origin })
}
}

export async function getOrCreateDB(): Promise<InternalEthereumProviderDatabase> {
return new InternalEthereumProviderDatabase()
}
Loading

0 comments on commit b04f9eb

Please sign in to comment.