Skip to content

Commit

Permalink
Merge branch 'main' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
alfetopito committed Jan 31, 2025
2 parents ce156d8 + 7965224 commit 964dc79
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 28 deletions.
6 changes: 3 additions & 3 deletions .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"apps/cowswap-frontend": "1.97.0",
"apps/cowswap-frontend": "1.97.1",
"apps/explorer": "2.42.0",
"libs/permit-utils": "0.6.0",
"libs/widget-lib": "0.18.0",
Expand All @@ -9,15 +9,15 @@
"libs/assets": "1.12.0",
"libs/common-const": "1.13.0",
"libs/common-hooks": "1.6.1",
"libs/common-utils": "1.9.0",
"libs/common-utils": "1.9.1",
"libs/core": "1.6.0",
"libs/ens": "1.3.0",
"libs/events": "1.5.0",
"libs/snackbars": "1.1.1",
"libs/tokens": "1.14.0",
"libs/types": "1.5.0",
"libs/ui": "1.18.0",
"libs/wallet": "1.9.1",
"libs/wallet": "1.9.2",
"apps/cow-fi": "1.19.3",
"libs/wallet-provider": "1.0.0",
"libs/ui-utils": "1.1.0",
Expand Down
7 changes: 7 additions & 0 deletions apps/cowswap-frontend/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [1.97.1](https://github.com/cowprotocol/cowswap/compare/cowswap-v1.97.0...cowswap-v1.97.1) (2025-01-30)


### Bug Fixes

* type guard to make sure provider is a JsonRpcProvider ([#5357](https://github.com/cowprotocol/cowswap/issues/5357)) ([6a2e030](https://github.com/cowprotocol/cowswap/commit/6a2e030aa7f88ef207fad8910750b18dcf719503))

## [1.97.0](https://github.com/cowprotocol/cowswap/compare/cowswap-v1.96.2...cowswap-v1.97.0) (2025-01-29)


Expand Down
2 changes: 1 addition & 1 deletion apps/cowswap-frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cowprotocol/cowswap",
"version": "1.97.0",
"version": "1.97.1",
"description": "CoW Swap",
"main": "index.js",
"author": "",
Expand Down
23 changes: 23 additions & 0 deletions apps/cowswap-frontend/src/common/utils/assertProviderNetwork.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { getChainIdImmediately } from '@cowprotocol/common-utils'
import { SupportedChainId } from '@cowprotocol/cow-sdk'
import { JsonRpcProvider, Provider } from '@ethersproject/providers'

export async function assertProviderNetwork(
chainId: SupportedChainId,
provider: JsonRpcProvider | Provider,
description: string,
): Promise<SupportedChainId> {
const ethereumProvider = (provider as unknown as { provider: typeof window.ethereum }).provider

// Do assert only for Metamask
if (!ethereumProvider || !ethereumProvider.isMetaMask || ethereumProvider.isRabby) return chainId

const network = await getChainIdImmediately(provider)
if (network !== chainId) {
throw new Error(
`Wallet chainId differs from app chainId. Wallet: ${network}, App: ${chainId}. Action: ${description}`,
)
}

return network
}
15 changes: 5 additions & 10 deletions apps/cowswap-frontend/src/legacy/hooks/useWrapCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ import { getChainCurrencySymbols, RADIX_HEX } from '@cowprotocol/common-const'
import {
calculateGasMargin,
formatTokenAmount,
getChainIdImmediately,
getIsNativeToken,
isRejectRequestProviderError,
} from '@cowprotocol/common-utils'
import { SupportedChainId } from '@cowprotocol/cow-sdk'
import { Command } from '@cowprotocol/types'
import { BigNumber } from '@ethersproject/bignumber'
import { Contract } from '@ethersproject/contracts'
import { JsonRpcProvider, TransactionResponse } from '@ethersproject/providers'
import { TransactionResponse } from '@ethersproject/providers'
import { Currency, CurrencyAmount } from '@uniswap/sdk-core'

import { useTransactionAdder } from 'legacy/state/enhancedTransactions/hooks'

import { wrapAnalytics } from 'modules/analytics'

import { assertProviderNetwork } from 'common/utils/assertProviderNetwork'

// Use a 180K gas as a fallback if there's issue calculating the gas estimation (fixes some issues with some nodes failing to calculate gas costs for SC wallets)
const WRAP_UNWRAP_GAS_LIMIT_DEFAULT = BigNumber.from('180000')

Expand Down Expand Up @@ -114,10 +115,7 @@ async function wrapContractCall(
const estimatedGas = await wethContract.estimateGas.deposit({ value: amountHex }).catch(_handleGasEstimateError)
const gasLimit = calculateGasMargin(estimatedGas)

const network = await getChainIdImmediately(wethContract.provider as JsonRpcProvider)
if (network !== chainId) {
throw new Error(`Wallet chainId differs from app chainId. Wallet: ${network}, App: ${chainId}`)
}
const network = await assertProviderNetwork(chainId, wethContract.provider, 'wrap')

const tx = await wethContract.populateTransaction.deposit({ value: amountHex, gasLimit })

Expand All @@ -134,10 +132,7 @@ async function unwrapContractCall(

const tx = await wethContract.populateTransaction.withdraw(amountHex, { gasLimit })

const network = await getChainIdImmediately(wethContract.provider as JsonRpcProvider)
if (network !== chainId) {
throw new Error(`Wallet chainId differs from app chainId. Wallet: ${network}, App: ${chainId}`)
}
const network = await assertProviderNetwork(chainId, wethContract.provider, 'unwrap')

return wethContract.signer.sendTransaction({ ...tx, chainId: network })
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { CoWSwapEthFlow } from '@cowprotocol/abis'
import { calculateGasMargin, getChainIdImmediately } from '@cowprotocol/common-utils'
import { calculateGasMargin } from '@cowprotocol/common-utils'
import { OrderClass, SigningScheme, UnsignedOrder } from '@cowprotocol/cow-sdk'
import { ContractTransaction } from '@ethersproject/contracts'
import { JsonRpcProvider } from '@ethersproject/providers'
import { NativeCurrency } from '@uniswap/sdk-core'

import { Order } from 'legacy/state/orders/actions'
Expand All @@ -11,6 +10,7 @@ import { getSignOrderParams, mapUnsignedOrderToOrder, PostOrderParams } from 'le
import { logTradeFlow, logTradeFlowError } from 'modules/trade/utils/logger'

import { GAS_LIMIT_DEFAULT } from 'common/constants/common'
import { assertProviderNetwork } from 'common/utils/assertProviderNetwork'

type EthFlowOrderParams = Omit<PostOrderParams, 'sellToken'> & {
sellToken: NativeCurrency
Expand Down Expand Up @@ -45,12 +45,7 @@ export async function signEthFlowOrderStep(
throw new Error('[EthFlow::SignEthFlowOrderStep] No quoteId passed')
}

const network = await getChainIdImmediately(ethFlowContract.provider as JsonRpcProvider)
if (network !== orderParams.chainId) {
throw new Error(
`Wallet chainId differs from order params chainId. Wallet: ${network}, Order: ${orderParams.chainId}`,
)
}
const network = await assertProviderNetwork(orderParams.chainId, ethFlowContract.provider, 'eth-flow')

const ethOrderParams: EthFlowCreateOrderParams = {
...order,
Expand Down
1 change: 1 addition & 0 deletions apps/cowswap-frontend/src/react-app-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface Window {
// value that is populated and returns true by the Coinbase Wallet mobile dapp browser
isCoinbaseWallet?: true
isMetaMask?: true
isRabby?: true
autoRefreshOnNetworkChange?: boolean
autoConnect?: boolean
setSelectedProvider: (any) => void
Expand Down
7 changes: 7 additions & 0 deletions libs/common-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [1.9.1](https://github.com/cowprotocol/cowswap/compare/common-utils-v1.9.0...common-utils-v1.9.1) (2025-01-30)


### Bug Fixes

* type guard to make sure provider is a JsonRpcProvider ([#5357](https://github.com/cowprotocol/cowswap/issues/5357)) ([6a2e030](https://github.com/cowprotocol/cowswap/commit/6a2e030aa7f88ef207fad8910750b18dcf719503))

## [1.9.0](https://github.com/cowprotocol/cowswap/compare/common-utils-v1.8.0...common-utils-v1.9.0) (2025-01-29)


Expand Down
2 changes: 1 addition & 1 deletion libs/common-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cowprotocol/common-utils",
"version": "1.9.0",
"version": "1.9.1",
"main": "./index.js",
"types": "./index.d.ts",
"exports": {
Expand Down
13 changes: 11 additions & 2 deletions libs/common-utils/src/getChainIdImmediately.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { JsonRpcProvider } from '@ethersproject/providers'
import { JsonRpcProvider, Provider } from '@ethersproject/providers'

export async function getChainIdImmediately(provider: JsonRpcProvider | Provider): Promise<number | undefined> {
if (!isJsonRpcProvider(provider)) {
console.error('Provider is not a JsonRpcProvider')
return undefined
}

export async function getChainIdImmediately(provider: JsonRpcProvider): Promise<number | undefined> {
try {
const chainId = await provider.send('eth_chainId', [])

Expand All @@ -10,3 +15,7 @@ export async function getChainIdImmediately(provider: JsonRpcProvider): Promise<
return undefined
}
}

function isJsonRpcProvider(provider: JsonRpcProvider | Provider): provider is JsonRpcProvider {
return (provider as JsonRpcProvider).send !== undefined
}
7 changes: 7 additions & 0 deletions libs/wallet/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [1.9.2](https://github.com/cowprotocol/cowswap/compare/wallet-v1.9.1...wallet-v1.9.2) (2025-01-30)


### Bug Fixes

* type guard to make sure provider is a JsonRpcProvider ([#5357](https://github.com/cowprotocol/cowswap/issues/5357)) ([6a2e030](https://github.com/cowprotocol/cowswap/commit/6a2e030aa7f88ef207fad8910750b18dcf719503))

## [1.9.1](https://github.com/cowprotocol/cowswap/compare/wallet-v1.9.0...wallet-v1.9.1) (2025-01-29)


Expand Down
2 changes: 1 addition & 1 deletion libs/wallet/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cowprotocol/wallet",
"version": "1.9.1",
"version": "1.9.2",
"main": "./index.js",
"types": "./index.d.ts",
"exports": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export class TrezorConnector extends Connector {

this.customProvider = customProvider

const chainId = await customProvider.send('eth_chainId', [])
const chainId = +(await customProvider.send('eth_chainId', []))

trezorConnect.on('DEVICE_EVENT', (event) => {
if (event.type === 'device-disconnect') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export async function sendTransactionHandler(
provider: JsonRpcProvider,
trezorConnect: TrezorConnect,
) {
const chainId = await provider.send('eth_chainId', [])
const chainId = +(await provider.send('eth_chainId', []))
const nonce = await provider.send('eth_getTransactionCount', [account, 'latest'])

const originalTx = params[0]
Expand Down

0 comments on commit 964dc79

Please sign in to comment.