Skip to content

Commit

Permalink
Merge branch 'dev' into chore/bump-typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
0xKheops committed Oct 31, 2023
2 parents ba79078 + c9732fc commit 35c14c3
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 32 deletions.
11 changes: 7 additions & 4 deletions apps/extension/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-env es6 */
/* eslint-env es2022 */
/** @type {import('ts-jest').InitialOptionsTsJest} */
const { defaults } = require("jest-config")
const { pathsToModuleNameMapper } = require("ts-jest")
Expand All @@ -11,9 +11,12 @@ module.exports = {
transform: {
"^.+\\.(t|j)sx?$": ["@swc/jest"],
},
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {
prefix: "<rootDir>/src",
}),
moduleNameMapper: {
"^rxjs/internal/(.*)$": "rxjs/dist/cjs/internal/$1",
...pathsToModuleNameMapper(compilerOptions.paths, {
prefix: "<rootDir>/src",
}),
},
extraGlobals: ["Math"],
moduleFileExtensions: [...defaults.moduleFileExtensions, "ts", "tsx"],
setupFiles: [
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "extension",
"version": "1.19.3",
"version": "1.19.4",
"private": true,
"license": "GPL-3.0-or-later",
"dependencies": {
Expand Down
49 changes: 26 additions & 23 deletions apps/extension/src/ui/domains/SendFunds/useSendFunds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,44 +269,47 @@ const useSendFundsProvider = () => {
tokenRates,
])

const estimatedFee = useMemo(() => {
const [estimatedFee, maxFee] = useMemo(() => {
if (evmTransaction?.txDetails?.estimatedFee) {
return new BalanceFormatter(
BigNumber.from(evmTransaction.txDetails.estimatedFee).toBigInt(),
feeToken?.decimals,
feeTokenRates
)
return [
new BalanceFormatter(
BigNumber.from(evmTransaction.txDetails.estimatedFee).toBigInt(),
feeToken?.decimals,
feeTokenRates
),
new BalanceFormatter(
BigNumber.from(evmTransaction.txDetails.maxFee).toBigInt(),
feeToken?.decimals,
feeTokenRates
),
]
}
if (subTransaction?.partialFee) {
return new BalanceFormatter(
const fee = new BalanceFormatter(
BigInt(subTransaction.partialFee),
feeToken?.decimals,
feeTokenRates
)
return [fee, fee]
}
return null
return [null, null]
}, [
evmTransaction?.txDetails?.estimatedFee,
evmTransaction?.txDetails?.maxFee,
feeToken?.decimals,
feeTokenRates,
subTransaction?.partialFee,
])

const costBreakdown = useMemo(() => {
const maxCostBreakdown = useMemo(() => {
try {
const transferAmount = sendMax ? maxAmount : transfer
if (
!token ||
!feeToken ||
!transferAmount ||
!estimatedFee ||
(requiresTip && (!tip || !tipToken))
)
if (!token || !feeToken || !transferAmount || !maxFee || (requiresTip && (!tip || !tipToken)))
return null

const spend: Record<TokenId, bigint> = {}
spend[token.id] = transferAmount.planck
spend[feeToken.id] = (spend[feeToken.id] ?? 0n) + estimatedFee.planck
spend[feeToken.id] = (spend[feeToken.id] ?? 0n) + maxFee.planck
if (tip && tipToken && tip.planck > 0n)
spend[tipToken.id] = (spend[tipToken.id] ?? 0n) + tip.planck

Expand All @@ -327,7 +330,7 @@ const useSendFundsProvider = () => {
}
}, [
balances,
estimatedFee,
maxFee,
feeToken,
maxAmount,
requiresTip,
Expand All @@ -342,7 +345,7 @@ const useSendFundsProvider = () => {
])

const tokensToBeReaped = useMemo(() => {
return costBreakdown
return maxCostBreakdown
?.map(({ token, cost, balance }) => {
const remaining = balance.planck - cost.planck

Expand All @@ -369,7 +372,7 @@ const useSendFundsProvider = () => {
amount: BalanceFormatter
}[]
| undefined
}, [costBreakdown, sendMax, tokenRatesMap])
}, [maxCostBreakdown, sendMax, tokenRatesMap])

const { data: recipientBalance } = useRecipientBalance(token, to)

Expand Down Expand Up @@ -406,7 +409,7 @@ const useSendFundsProvider = () => {
!to ||
!(transfer || (sendMax && maxAmount)) ||
!tokenId ||
!costBreakdown ||
!maxCostBreakdown ||
!tokensToBeReaped ||
!feeToken ||
!feeTokenBalance ||
Expand All @@ -427,7 +430,7 @@ const useSendFundsProvider = () => {
error: t("Insufficient {{symbol}} to pay for fees", { symbol: feeToken.symbol }),
}

for (const cost of costBreakdown)
for (const cost of maxCostBreakdown)
if (cost.balance.planck < cost.cost.planck)
return {
isValid: false,
Expand Down Expand Up @@ -460,7 +463,7 @@ const useSendFundsProvider = () => {
}
}, [
balance,
costBreakdown,
maxCostBreakdown,
evmInvalidTxError,
evmTransaction?.error,
from,
Expand Down
4 changes: 4 additions & 0 deletions apps/extension/webpack/webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const config = (env) => ({
"@substrate/txwrapper-core",
"@talismn/chaindata-provider-extension",
"@metamask/eth-sig-util",
"@acala-network/types",
"@acala-network/eth-providers",
"@acala-network/eth-transactions",
"@acala-network/api-derive",
],

// Wallet injected scripts
Expand Down
2 changes: 2 additions & 0 deletions packages/chain-connector-evm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"clean": "rm -rf dist && rm -rf .turbo rm -rf node_modules"
},
"dependencies": {
"@acala-network/api": "^6.0.0",
"@acala-network/eth-providers": "^2.7.8",
"@talismn/chaindata-provider": "workspace:*",
"@talismn/util": "workspace:*",
"anylogger": "^1.0.11",
Expand Down
11 changes: 10 additions & 1 deletion packages/chain-connector-evm/src/ChainConnectorEvm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import { ethers } from "ethers"

import { RPC_CALL_TIMEOUT } from "./constants"
import log from "./log"
import { BatchRpcProvider, StandardRpcProvider, addOnfinalityApiKey, getHealthyRpc } from "./util"
import {
AcalaRpcProvider,
BatchRpcProvider,
StandardRpcProvider,
addOnfinalityApiKey,
getHealthyRpc,
isAcalaNetwork,
} from "./util"

export type GetProviderOptions = {
/** If true, returns a provider which will batch requests */
Expand Down Expand Up @@ -160,6 +167,8 @@ export class ChainConnectorEvm {
const provider =
batch === true
? new BatchRpcProvider(connection, network)
: isAcalaNetwork(network.chainId)
? new AcalaRpcProvider(connection, network)
: new StandardRpcProvider(connection, network)

// in case an error is thrown, rotate rpc urls cache
Expand Down
7 changes: 7 additions & 0 deletions packages/chain-connector-evm/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
export const RPC_HEALTHCHECK_TIMEOUT = 10_000
export const RPC_CALL_TIMEOUT = 20_000
export const ACALA_NETWORK_IDS = [
595, // Mandala
596, // Karura Testnet
597, // Acala Testnet
686, // Karura
787, // Acala
]
17 changes: 16 additions & 1 deletion packages/chain-connector-evm/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { AcalaJsonRpcProvider } from "@acala-network/eth-providers"
import { throwAfter } from "@talismn/util"
import { ethers } from "ethers"

import { RPC_HEALTHCHECK_TIMEOUT } from "./constants"
import { ACALA_NETWORK_IDS, RPC_HEALTHCHECK_TIMEOUT } from "./constants"
import { EvmJsonRpcBatchProvider } from "./EvmJsonRpcBatchProvider"
import log from "./log"

Expand Down Expand Up @@ -45,6 +46,8 @@ export const isHealthyRpc = async (url: string, chainId: number) => {
}
}

export const isAcalaNetwork = (chainId: number) => ACALA_NETWORK_IDS.includes(chainId)

export const getHealthyRpc = async (rpcUrls: string[], network: ethers.providers.Network) => {
for (const rpcUrl of rpcUrls) if (await isHealthyRpc(rpcUrl, network.chainId)) return rpcUrl

Expand Down Expand Up @@ -75,6 +78,18 @@ export class StandardRpcProvider extends ethers.providers.JsonRpcProvider {
}
}

export class AcalaRpcProvider extends AcalaJsonRpcProvider {
async send(method: string, params: Array<unknown>): Promise<unknown> {
try {
return await super.send(method, params)
} catch (err) {
// emit error so rpc manager considers this rpc unhealthy
if (isUnhealthyRpcError(err)) this.emit("error", err)
throw err
}
}
}

export class BatchRpcProvider extends EvmJsonRpcBatchProvider {
async send(method: string, params: Array<unknown>): Promise<unknown> {
try {
Expand Down
Loading

0 comments on commit 35c14c3

Please sign in to comment.