Skip to content

Commit

Permalink
Merge pull request safe-global#331 from safe-global/features
Browse files Browse the repository at this point in the history
Fix Safe version feature helper
  • Loading branch information
germartinez authored Jan 25, 2023
2 parents b1e69b9 + 9584607 commit 0090944
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 48 deletions.
2 changes: 1 addition & 1 deletion packages/safe-core-sdk-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@safe-global/safe-core-sdk-utils",
"version": "1.7.0",
"version": "1.7.1",
"description": "Safe Core SDK Utilities",
"main": "dist/src/index.js",
"typings": "dist/src/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions packages/safe-core-sdk-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './eip-3770'
export * from './eip-712'
export * from './safeVersions'
23 changes: 23 additions & 0 deletions packages/safe-core-sdk-utils/src/safeVersions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import semverSatisfies from 'semver/functions/satisfies'

export enum SAFE_FEATURES {
SAFE_TX_GAS_OPTIONAL = 'SAFE_TX_GAS_OPTIONAL',
SAFE_TX_GUARDS = 'SAFE_TX_GUARDS',
SAFE_FALLBACK_HANDLER = 'SAFE_FALLBACK_HANDLER',
ETH_SIGN = 'ETH_SIGN'
}

const SAFE_FEATURES_BY_VERSION: Record<SAFE_FEATURES, string> = {
[SAFE_FEATURES.SAFE_TX_GAS_OPTIONAL]: '>=1.3.0',
[SAFE_FEATURES.SAFE_TX_GUARDS]: '>=1.3.0',
[SAFE_FEATURES.SAFE_FALLBACK_HANDLER]: '>=1.1.1',
[SAFE_FEATURES.ETH_SIGN]: '>=1.1.0'
}

export const hasSafeFeature = (feature: SAFE_FEATURES, version: string): boolean => {
if (!(feature in SAFE_FEATURES_BY_VERSION)) {
return false
}

return semverSatisfies(version, SAFE_FEATURES_BY_VERSION[feature])
}
3 changes: 2 additions & 1 deletion packages/safe-core-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@safe-global/safe-core-sdk",
"version": "3.3.0",
"version": "3.3.1",
"description": "Safe Core SDK",
"main": "dist/src/index.js",
"typings": "dist/src/index.d.ts",
Expand Down Expand Up @@ -85,6 +85,7 @@
"dependencies": {
"@ethersproject/solidity": "^5.7.0",
"@safe-global/safe-core-sdk-types": "^1.9.0",
"@safe-global/safe-core-sdk-utils": "^1.7.1",
"@gnosis.pm/safe-deployments": "1.19.0",
"ethereumjs-util": "^7.1.5",
"semver": "^7.3.8",
Expand Down
4 changes: 2 additions & 2 deletions packages/safe-core-sdk/src/Safe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
TransactionOptions,
TransactionResult
} from '@safe-global/safe-core-sdk-types'
import { SAFE_FEATURES, hasSafeFeature } from '@safe-global/safe-core-sdk-utils'
import ContractManager from './managers/contractManager'
import FallbackHandlerManager from './managers/fallbackHandlerManager'
import GuardManager from './managers/guardManager'
Expand All @@ -31,7 +32,6 @@ import {
standardizeMetaTransactionData,
standardizeSafeTransactionData
} from './utils/transactions/utils'
import { FEATURES, hasFeature } from './utils/safeVersions'

export interface SafeConfig {
/** ethAdapter - Ethereum adapter */
Expand Down Expand Up @@ -470,7 +470,7 @@ class Safe {
signature = await this.signTypedData(transaction)
} else {
const safeVersion = await this.getContractVersion()
if (!hasFeature(FEATURES.ETH_SIGN, safeVersion)) {
if (!hasSafeFeature(SAFE_FEATURES.ETH_SIGN, safeVersion)) {
throw new Error('eth_sign is only supported by Safes >= v1.1.0')
}
const txHash = await this.getTransactionHash(transaction)
Expand Down
4 changes: 2 additions & 2 deletions packages/safe-core-sdk/src/managers/fallbackHandlerManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EthAdapter, GnosisSafeContract } from '@safe-global/safe-core-sdk-types'
import { SAFE_FEATURES, hasSafeFeature } from '@safe-global/safe-core-sdk-utils'
import { isZeroAddress, sameString } from '../utils'
import { ZERO_ADDRESS } from '../utils/constants'
import { FEATURES, hasFeature } from '../utils/safeVersions'

class FallbackHandlerManager {
#ethAdapter: EthAdapter
Expand Down Expand Up @@ -38,7 +38,7 @@ class FallbackHandlerManager {

async getFallbackHandler(): Promise<string> {
const safeVersion = await this.#safeContract.getVersion()
if (hasFeature(FEATURES.SAFE_FALLBACK_HANDLER, safeVersion)) {
if (hasSafeFeature(SAFE_FEATURES.SAFE_FALLBACK_HANDLER, safeVersion)) {
return this.#ethAdapter.getStorageAt(this.#safeContract.getAddress(), this.#slot)
} else {
throw new Error(
Expand Down
4 changes: 2 additions & 2 deletions packages/safe-core-sdk/src/managers/guardManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EthAdapter, GnosisSafeContract } from '@safe-global/safe-core-sdk-types'
import { SAFE_FEATURES, hasSafeFeature } from '@safe-global/safe-core-sdk-utils'
import { isZeroAddress, sameString } from '../utils'
import { ZERO_ADDRESS } from '../utils/constants'
import { FEATURES, hasFeature } from '../utils/safeVersions'

class GuardManager {
#ethAdapter: EthAdapter
Expand Down Expand Up @@ -35,7 +35,7 @@ class GuardManager {

async getGuard(): Promise<string> {
const safeVersion = await this.#safeContract.getVersion()
if (hasFeature(FEATURES.SAFE_TX_GUARDS, safeVersion)) {
if (hasSafeFeature(SAFE_FEATURES.SAFE_TX_GUARDS, safeVersion)) {
return this.#ethAdapter.getStorageAt(this.#safeContract.getAddress(), this.#slot)
} else {
throw new Error(
Expand Down
31 changes: 0 additions & 31 deletions packages/safe-core-sdk/src/utils/safeVersions.ts

This file was deleted.

7 changes: 5 additions & 2 deletions packages/safe-core-sdk/src/utils/transactions/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
SafeTransactionData,
SafeTransactionDataPartial
} from '@safe-global/safe-core-sdk-types'
import { SAFE_FEATURES, hasSafeFeature } from '@safe-global/safe-core-sdk-utils'
import { ZERO_ADDRESS } from '../constants'
import { FEATURES, hasFeature } from '../safeVersions'
import { estimateTxGas } from './gas'

export function standardizeMetaTransactionData(
Expand Down Expand Up @@ -47,7 +47,10 @@ export async function standardizeSafeTransactionData(
}
}
const safeVersion = await safeContract.getVersion()
if (hasFeature(FEATURES.SAFE_TX_GAS_OPTIONAL, safeVersion) && standardizedTxs.gasPrice === 0) {
if (
hasSafeFeature(SAFE_FEATURES.SAFE_TX_GAS_OPTIONAL, safeVersion) &&
standardizedTxs.gasPrice === 0
) {
safeTxGas = 0
} else {
safeTxGas = await estimateTxGas(
Expand Down
4 changes: 2 additions & 2 deletions packages/safe-ethers-lib/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@safe-global/safe-ethers-lib",
"version": "1.9.0",
"version": "1.9.1",
"description": "Ethers library adapter to be used by Safe Core SDK",
"main": "dist/src/index.js",
"typings": "dist/src/index.d.ts",
Expand Down Expand Up @@ -47,7 +47,7 @@
},
"dependencies": {
"@safe-global/safe-core-sdk-types": "^1.9.0",
"@safe-global/safe-core-sdk-utils": "^1.7.0",
"@safe-global/safe-core-sdk-utils": "^1.7.1",
"ethers": "5.7.2"
}
}
4 changes: 2 additions & 2 deletions packages/safe-web3-lib/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@safe-global/safe-web3-lib",
"version": "1.9.0",
"version": "1.9.1",
"description": "Web3 library adapter to be used by Safe Core SDK",
"main": "dist/src/index.js",
"typings": "dist/src/index.d.ts",
Expand Down Expand Up @@ -48,7 +48,7 @@
"dependencies": {
"@ethersproject/bignumber": "^5.7.0",
"@safe-global/safe-core-sdk-types": "^1.9.0",
"@safe-global/safe-core-sdk-utils": "^1.7.0",
"@safe-global/safe-core-sdk-utils": "^1.7.1",
"web3": "^1.8.1",
"web3-core": "^1.8.1",
"web3-utils": "^1.8.1"
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5646,9 +5646,9 @@ cookie@^0.4.1:
integrity sha512-aSWTXFzaKWkvHO1Ny/s+ePFpvKsPnjc551iI41v3ny/ow6tBG5Vd+FuqGNhh1LxOmVzOlGUriIlOaokOvhaStA==

cookiejar@^2.1.1:
version "2.1.3"
resolved "https://registry.yarnpkg.com/cookiejar/-/cookiejar-2.1.3.tgz#fc7a6216e408e74414b90230050842dacda75acc"
integrity sha512-JxbCBUdrfr6AQjOXrxoTvAMJO4HBTUIlBzslcJPAz+/KT8yk53fXun51u+RenNYvad/+Vc2DIz5o9UxlCDymFQ==
version "2.1.4"
resolved "https://registry.yarnpkg.com/cookiejar/-/cookiejar-2.1.4.tgz#ee669c1fea2cf42dc31585469d193fef0d65771b"
integrity sha512-LDx6oHrK+PhzLKJU9j5S7/Y3jM/mUHvD/DeI1WQmJn652iPC5Y4TBzC9l+5OMOXlyTTA+SmVUPm0HQUwpD5Jqw==

copy-descriptor@^0.1.0:
version "0.1.1"
Expand Down

0 comments on commit 0090944

Please sign in to comment.