Skip to content

Commit

Permalink
Merge pull request safe-global#123 from gnosis/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
germartinez authored Jan 5, 2022
2 parents 4989ac6 + 73075b0 commit e54280e
Show file tree
Hide file tree
Showing 24 changed files with 753 additions and 267 deletions.
38 changes: 20 additions & 18 deletions packages/safe-core-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gnosis.pm/safe-core-sdk",
"version": "1.1.1",
"version": "1.3.0",
"description": "Safe Core SDK",
"main": "dist/src/index.js",
"typings": "dist/src/index.d.ts",
Expand Down Expand Up @@ -43,40 +43,41 @@
"devDependencies": {
"@gnosis.pm/safe-contracts-v1.2.0": "npm:@gnosis.pm/safe-contracts@1.2.0",
"@gnosis.pm/safe-contracts-v1.3.0": "npm:@gnosis.pm/safe-contracts@1.3.0",
"@nomiclabs/hardhat-ethers": "^2.0.2",
"@nomiclabs/hardhat-ethers": "^2.0.3",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"@nomiclabs/hardhat-web3": "^2.0.0",
"@typechain/ethers-v5": "^8.0.4",
"@typechain/ethers-v5": "^8.0.5",
"@typechain/web3-v1": "^3.0.0",
"@types/chai": "^4.2.22",
"@types/chai": "^4.3.0",
"@types/chai-as-promised": "^7.1.4",
"@types/mocha": "^9.0.0",
"@types/node": "^16.11.9",
"@types/node": "^17.0.8",
"@types/semver": "^7.3.9",
"@types/yargs": "^16.0.1",
"@typescript-eslint/eslint-plugin": "^5.4.0",
"@typescript-eslint/parser": "^5.4.0",
"@typescript-eslint/eslint-plugin": "^5.9.0",
"@typescript-eslint/parser": "^5.9.0",
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"coveralls": "^3.1.1",
"dotenv": "^10.0.0",
"eslint": "^8.3.0",
"eslint": "^8.6.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"ethereum-waffle": "^3.4.0",
"ethers": "^5.5.1",
"hardhat": "^2.3.3",
"hardhat-deploy": "^0.8.6",
"ethers": "^5.5.2",
"hardhat": "^2.8.0",
"hardhat-deploy": "^0.9.24",
"husky": "^7.0.4",
"lint-staged": "^12.1.2",
"lint-staged": "^12.1.5",
"mocha": "^9.1.3",
"nyc": "^15.1.0",
"prettier": "^2.4.1",
"prettier": "^2.5.1",
"ts-generator": "^0.1.1",
"ts-node": "^10.4.0",
"typechain": "^6.0.4",
"typescript": "^4.5.2",
"typechain": "^6.0.5",
"typescript": "^4.5.4",
"web3": "^1.6.1",
"yargs": "^17.0.1"
"yargs": "^17.3.0"
},
"lint-staged": {
"src/**/!(*test).ts": [
Expand All @@ -91,7 +92,8 @@
},
"dependencies": {
"@gnosis.pm/safe-core-sdk-types": "^0.1.1",
"@gnosis.pm/safe-deployments": "^1.4.0",
"ethereumjs-util": "^7.1.3"
"@gnosis.pm/safe-deployments": "^1.7.0",
"ethereumjs-util": "^7.1.3",
"semver": "^7.3.5"
}
}
9 changes: 9 additions & 0 deletions packages/safe-core-sdk/src/Safe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ class Safe {
return this.#contractManager.safeContract.getAddress()
}

/**
* Returns the ContractManager
*
* @returns The current ContractManager
* */
getContractManager(): ContractManager {
return this.#contractManager
}

/**
* Returns the current EthAdapter.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class GnosisSafeContract_V1_1_1_Ethers extends GnosisSafeContractEthers {
}

async getModules(): Promise<string[]> {
return (super.contract as GnosisSafe).getModules()
return this.contract.getModules()
}

async isModuleEnabled(moduleAddress: string): Promise<boolean> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class GnosisSafeContract_V1_1_1_Web3 extends GnosisSafeContractWeb3 {
}

async getModules(): Promise<string[]> {
return (super.contract as GnosisSafe).methods.getModules().call()
return this.contract.methods.getModules().call()
}

async isModuleEnabled(moduleAddress: string): Promise<boolean> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TransactionOptions } from '../../utils/transactions/types'
export interface CreateProxyProps {
safeMasterCopyAddress: string
initializer: string
saltNonce?: number
saltNonce: number
options?: TransactionOptions
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@ class GnosisSafeProxyFactoryEthersContract implements GnosisSafeProxyFactoryCont
saltNonce,
options
}: CreateProxyProps): Promise<string> {
let txResponse: ContractTransaction
if (saltNonce) {
txResponse = await this.contract.createProxyWithNonce(
safeMasterCopyAddress,
initializer,
saltNonce,
options
)
} else {
txResponse = await this.contract.createProxy(safeMasterCopyAddress, initializer, options)
}
const txResponse = await this.contract.createProxyWithNonce(
safeMasterCopyAddress,
initializer,
saltNonce,
options
)
const txReceipt = await txResponse.wait()
const proxyCreationEvent = txReceipt.events?.find(
({ event }: Event) => event === 'ProxyCreation'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,10 @@ class GnosisSafeProxyFactoryWeb3Contract implements GnosisSafeProxyFactoryContra
saltNonce,
options
}: CreateProxyProps): Promise<string> {
let txResponse: PromiEvent<TransactionReceipt>
if (saltNonce) {
txResponse = this.contract.methods
.createProxyWithNonce(safeMasterCopyAddress, initializer, saltNonce)
.send(options)
} else {
txResponse = this.contract.methods
.createProxy(safeMasterCopyAddress, initializer)
.send(options)
}
const txResponse = this.contract.methods
.createProxyWithNonce(safeMasterCopyAddress, initializer, saltNonce)
.send(options)

const txResult: TransactionReceipt = await new Promise((resolve, reject) =>
txResponse.once('receipt', (receipt: TransactionReceipt) => resolve(receipt)).catch(reject)
)
Expand Down
6 changes: 5 additions & 1 deletion packages/safe-core-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { SafeVersion } from './contracts/config'
import EthAdapter, { EthAdapterTransaction } from './ethereumLibs/EthAdapter'
import EthersAdapter, { EthersAdapterConfig } from './ethereumLibs/EthersAdapter'
import Web3Adapter, { Web3AdapterConfig } from './ethereumLibs/Web3Adapter'
import ContractManager from './managers/contractManager'
import Safe, {
AddOwnerTxParams,
ConnectSafeConfig,
Expand All @@ -21,9 +22,11 @@ import {
TransactionOptions,
TransactionResult
} from './utils/transactions/types'
import { standardizeSafeTransactionData } from './utils/transactions/utils'

export default Safe
export {
ContractManager,
SafeVersion,
SafeFactory,
SafeFactoryConfig,
Expand All @@ -44,5 +47,6 @@ export {
AddOwnerTxParams,
RemoveOwnerTxParams,
SwapOwnerTxParams,
EthSignSignature
EthSignSignature,
standardizeSafeTransactionData
}
4 changes: 3 additions & 1 deletion packages/safe-core-sdk/src/safeFactory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,12 @@ class SafeFactory {
const chainId = await this.#ethAdapter.getChainId()
const signerAddress = await this.#ethAdapter.getSignerAddress()
const initializer = await this.encodeSetupCallData(safeAccountConfig)
const saltNonce =
safeDeploymentConfig?.saltNonce ?? Date.now() * 1000 + Math.floor(Math.random() * 1000)
const safeAddress = await this.#safeProxyFactoryContract.createProxy({
safeMasterCopyAddress: this.#gnosisSafeContract.getAddress(),
initializer,
saltNonce: safeDeploymentConfig?.saltNonce,
saltNonce,
options: { from: signerAddress }
})
const safeContract = await this.#ethAdapter.getSafeContract({
Expand Down
25 changes: 25 additions & 0 deletions packages/safe-core-sdk/src/utils/safeVersions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import semverSatisfies from 'semver/functions/satisfies'

export enum FEATURES {
SAFE_TX_GAS_OPTIONAL
}

const FEATURES_BY_VERSION: Record<string, string> = {
[FEATURES.SAFE_TX_GAS_OPTIONAL]: '>=1.3.0'
}

const isEnabledByVersion = (feature: FEATURES, version: string): boolean => {
if (!(feature in FEATURES_BY_VERSION)) {
return true
}
return semverSatisfies(version, FEATURES_BY_VERSION[feature])
}

export const enabledFeatures = (version: string): FEATURES[] => {
const features = Object.values(FEATURES) as FEATURES[]
return features.filter((feature) => isEnabledByVersion(feature, version))
}

export const hasFeature = (name: FEATURES, version: string): boolean => {
return enabledFeatures(version).includes(name)
}
27 changes: 17 additions & 10 deletions packages/safe-core-sdk/src/utils/transactions/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import GnosisSafeContract from '../../contracts/GnosisSafe/GnosisSafeContract'
import EthAdapter from '../../ethereumLibs/EthAdapter'
import { ZERO_ADDRESS } from '../constants'
import { FEATURES, hasFeature } from '../safeVersions'
import { estimateTxGas } from './gas'

export function standardizeMetaTransactionData(
Expand Down Expand Up @@ -37,16 +38,22 @@ export async function standardizeSafeTransactionData(
refundReceiver: tx.refundReceiver || ZERO_ADDRESS,
nonce: tx.nonce ?? (await safeContract.getNonce())
}
const safeTxGas =
tx.safeTxGas ??
(await estimateTxGas(
safeContract,
ethAdapter,
standardizedTxs.to,
standardizedTxs.value,
standardizedTxs.data,
standardizedTxs.operation
))
let safeTxGas: number
const safeVersion = await safeContract.getVersion()
if (hasFeature(FEATURES.SAFE_TX_GAS_OPTIONAL, safeVersion)) {
safeTxGas = 0
} else {
safeTxGas =
tx.safeTxGas ??
(await estimateTxGas(
safeContract,
ethAdapter,
standardizedTxs.to,
standardizedTxs.value,
standardizedTxs.data,
standardizedTxs.operation
))
}
return {
...standardizedTxs,
safeTxGas
Expand Down
Loading

0 comments on commit e54280e

Please sign in to comment.