Unify and refactor gnosis safe instance functions

This commit is contained in:
Germán Martínez 2019-04-30 14:18:03 +02:00
parent e4d2e3b35d
commit 2e2a7d906c
13 changed files with 34 additions and 47 deletions

View File

@ -75,7 +75,7 @@ export const deploySafeContract = async (safeAccounts: string[], numConfirmation
export const getGnosisSafeInstanceAt = async (safeAddress: string) => {
const web3 = getWeb3()
const GnosisSafe = await getGnosisSafeContract(web3)
const GnosisSafe = getGnosisSafeContract(web3)
const gnosisSafe = await GnosisSafe.at(safeAddress)
return gnosisSafe

View File

@ -2,7 +2,7 @@
import { List } from 'immutable'
import { calculateGasOf, checkReceiptStatus, calculateGasPrice } from '~/logic/wallets/ethTransactions'
import { type Operation, submitOperation } from '~/logic/safe/safeTxHistory'
import { getSafeEthereumInstance } from '~/logic/safe/safeFrontendOperations'
import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts'
import { buildSignaturesFrom } from '~/logic/safe/safeTxSigner'
import { generateMetamaskSignature, generateTxGasEstimateFrom, estimateDataGas } from '~/logic/safe/safeTxSignerEIP712'
import { storeSignature, getSignaturesFrom } from '~/utils/storage/signatures'
@ -21,7 +21,7 @@ export const approveTransaction = async (
if (signaturesViaMetamask()) {
// return executeTransaction(safeAddress, to, valueInWei, data, operation, nonce, sender)
const safe = await getSafeEthereumInstance(safeAddress)
const safe = await getGnosisSafeInstanceAt(safeAddress)
const txGasEstimate = await generateTxGasEstimateFrom(safe, safeAddress, data, to, valueInWei, operation)
const signature = await generateMetamaskSignature(
safe,
@ -39,7 +39,7 @@ export const approveTransaction = async (
return undefined
}
const gnosisSafe = await getSafeEthereumInstance(safeAddress)
const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress)
const contractTxHash = await gnosisSafe.getTransactionHash(to, valueInWei, data, operation, 0, 0, 0, 0, 0, nonce)
const approveData = gnosisSafe.contract.methods.approveHash(contractTxHash).encodeABI()
@ -67,7 +67,7 @@ export const executeTransaction = async (
const gasPrice = await calculateGasPrice()
if (signaturesViaMetamask()) {
const safe = await getSafeEthereumInstance(safeAddress)
const safe = await getGnosisSafeInstanceAt(safeAddress)
const txGasEstimate = await generateTxGasEstimateFrom(safe, safeAddress, data, to, valueInWei, operation)
const signature = await generateMetamaskSignature(
safe,
@ -120,7 +120,7 @@ export const executeTransaction = async (
return txHash
}
const gnosisSafe = await getSafeEthereumInstance(safeAddress)
const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress)
const signatures = buildSignaturesFrom(ownersWhoHasSigned, sender)
const txExecutionData = gnosisSafe.contract.methods
.execTransaction(to, valueInWei, data, operation, 0, 0, 0, 0, 0, signatures)

View File

@ -5,7 +5,6 @@ import { executeTransaction, approveTransaction } from '~/logic/safe/safeBlockch
import { EMPTY_DATA } from '~/logic/wallets/ethTransactions'
import { getWeb3 } from '~/logic/wallets/getWeb3'
import { type Safe } from '~/routes/safe/store/models/safe'
import { getGnosisSafeContract } from '~/logic/contracts/safeContracts'
import { storeSubject } from '~/utils/storage/transactions'
export const TX_NAME_PARAM = 'txName'
@ -23,13 +22,6 @@ const hasOneOwner = (safe: Safe) => {
return owners.count() === 1
}
export const getSafeEthereumInstance = async (safeAddress: string) => {
const web3 = getWeb3()
const GnosisSafe = await getGnosisSafeContract(web3)
const safeInstance = await GnosisSafe.at(safeAddress)
return safeInstance
}
export const createTransaction = async (
safe: Safe,
name: string,

View File

@ -1,7 +1,7 @@
// @flow
import { getWeb3 } from '~/logic/wallets/getWeb3'
import { getTxServiceUriFrom, getTxServiceHost } from '~/config'
import { getSafeEthereumInstance } from '~/logic/safe/safeFrontendOperations'
import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts'
export type TxServiceType = 'confirmation' | 'execution' | 'initialised'
export type Operation = 0 | 1 | 2
@ -17,7 +17,7 @@ const calculateBodyFrom = async (
sender: string,
type: TxServiceType,
) => {
const gnosisSafe = await getSafeEthereumInstance(safeAddress)
const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress)
const contractTransactionHash = await gnosisSafe.getTransactionHash(
to,
valueInWei,

View File

@ -5,8 +5,7 @@ import Page from '~/components/layout/Page'
import {
getAccountsFrom, getThresholdFrom, getNamesFrom, getSafeNameFrom,
} from '~/routes/open/utils/safeDataExtractor'
import { getWeb3 } from '~/logic/wallets/getWeb3'
import { getGnosisSafeContract, deploySafeContract, initContracts } from '~/logic/contracts/safeContracts'
import { getGnosisSafeInstanceAt, deploySafeContract, initContracts } from '~/logic/contracts/safeContracts'
import { checkReceiptStatus } from '~/logic/wallets/ethTransactions'
import { history } from '~/store'
import { OPENING_ADDRESS, stillInOpeningView, SAFELIST_ADDRESS } from '~/routes/routes'
@ -30,15 +29,12 @@ export const createSafe = async (values: Object, userAccount: string, addSafe: A
const name = getSafeNameFrom(values)
const owners = getNamesFrom(values)
const web3 = getWeb3()
const GnosisSafe = getGnosisSafeContract(web3)
await initContracts()
const safe = await deploySafeContract(accounts, numConfirmations, userAccount)
checkReceiptStatus(safe.tx)
await checkReceiptStatus(safe.tx)
const param = safe.logs[0].args.proxy
const safeContract = await GnosisSafe.at(param)
const safeAddress = safe.logs[0].args.proxy
const safeContract = await getGnosisSafeInstanceAt(safeAddress)
addSafe(name, safeContract.address, numConfirmations, owners, accounts)

View File

@ -6,7 +6,8 @@ import { connect } from 'react-redux'
import { type Safe } from '~/routes/safe/store/models/safe'
import { type Owner, makeOwner } from '~/routes/safe/store/models/owner'
import { setOwners } from '~/logic/safe/utils'
import { getSafeEthereumInstance, createTransaction } from '~/logic/safe/safeFrontendOperations'
import { createTransaction } from '~/logic/safe/safeFrontendOperations'
import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts'
import AddOwnerForm, { NAME_PARAM, OWNER_ADDRESS_PARAM, INCREASE_PARAM } from './AddOwnerForm'
import Review from './Review'
import selector, { type SelectorProps } from './selector'
@ -36,7 +37,7 @@ const getOwnerAddressesFrom = (owners: List<Owner>) => {
export const addOwner = async (values: Object, safe: Safe, threshold: number, executor: string) => {
const safeAddress = safe.get('address')
const gnosisSafe = await getSafeEthereumInstance(safeAddress)
const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress)
const nonce = await gnosisSafe.nonce()
const newThreshold = values[INCREASE_PARAM] ? threshold + 1 : threshold

View File

@ -3,7 +3,8 @@ import * as React from 'react'
import Stepper from '~/components/Stepper'
import { connect } from 'react-redux'
import { type Safe } from '~/routes/safe/store/models/safe'
import { getSafeEthereumInstance, createTransaction } from '~/logic/safe/safeFrontendOperations'
import { createTransaction } from '~/logic/safe/safeFrontendOperations'
import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts'
import RemoveOwnerForm, { DECREASE_PARAM } from './RemoveOwnerForm'
import Review from './Review'
import selector, { type SelectorProps } from './selector'
@ -42,7 +43,7 @@ export const removeOwner = async (
executor: string,
) => {
const safeAddress = safe.get('address')
const gnosisSafe = await getSafeEthereumInstance(safeAddress)
const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress)
const nonce = await gnosisSafe.nonce()
const newThreshold = values[DECREASE_PARAM] ? threshold - 1 : threshold
const storedOwners = await gnosisSafe.getOwners()

View File

@ -10,7 +10,8 @@ import { type Token } from '~/logic/tokens/store/model/token'
import { isEther } from '~/logic/tokens/utils/tokenHelpers'
import { EMPTY_DATA } from '~/logic/wallets/ethTransactions'
import { toNative } from '~/logic/wallets/tokens'
import { createTransaction, getSafeEthereumInstance } from '~/logic/safe/safeFrontendOperations'
import { createTransaction } from '~/logic/safe/safeFrontendOperations'
import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts'
import actions, { type Actions } from './actions'
import selector, { type SelectorProps } from './selector'
import SendTokenForm, { TKN_DESTINATION_PARAM, TKN_VALUE_PARAM } from './SendTokenForm'
@ -40,7 +41,7 @@ const getTransferData = async (tokenAddress: string, to: string, amount: BigNumb
const processTokenTransfer = async (safe: Safe, token: Token, to: string, amount: string, userAddress: string) => {
const safeAddress = safe.get('address')
const gnosisSafe = await getSafeEthereumInstance(safeAddress)
const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress)
const nonce = await gnosisSafe.nonce()
const symbol = token.get('symbol')
const name = `Send ${amount} ${symbol} to ${to}`

View File

@ -2,7 +2,8 @@
import * as React from 'react'
import Stepper from '~/components/Stepper'
import { connect } from 'react-redux'
import { getSafeEthereumInstance, createTransaction } from '~/logic/safe/safeFrontendOperations'
import { createTransaction } from '~/logic/safe/safeFrontendOperations'
import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts'
import { type Safe } from '~/routes/safe/store/models/safe'
import ThresholdForm, { THRESHOLD_PARAM } from './ThresholdForm'
import selector, { type SelectorProps } from './selector'
@ -35,7 +36,7 @@ class Threshold extends React.PureComponent<Props, State> {
const { safe, userAddress, fetchTransactions } = this.props // , fetchThreshold } = this.props
const newThreshold = values[THRESHOLD_PARAM]
const safeAddress = safe.get('address')
const gnosisSafe = await getSafeEthereumInstance(safeAddress)
const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress)
const nonce = await gnosisSafe.nonce()
const data = gnosisSafe.contract.changeThreshold(newThreshold).encodeABI()
await createTransaction(safe, `Change Safe's threshold [${nonce}]`, safeAddress, '0', nonce, userAddress, data)

View File

@ -6,8 +6,8 @@ import { makeOwner } from '~/routes/safe/store/models/owner'
import type { SafeProps } from '~/routes/safe/store/models/safe'
import { addSafe } from '~/routes/safe/store/actions/addSafe'
import { getOwners, getSafeName } from '~/logic/safe/utils'
import { getGnosisSafeContract } from '~/logic/contracts/safeContracts'
import { getWeb3, getBalanceInEtherOf } from '~/logic/wallets/getWeb3'
import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts'
import { getBalanceInEtherOf } from '~/logic/wallets/getWeb3'
import updateSafe from '~/routes/safe/store/actions/updateSafe'
const buildOwnersFrom = (
@ -19,9 +19,7 @@ const buildOwnersFrom = (
})
export const buildSafe = async (safeAddress: string, safeName: string) => {
const web3 = getWeb3()
const SafeContract = await getGnosisSafeContract(web3)
const gnosisSafe = await SafeContract.at(safeAddress)
const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress)
const ethBalance = await getBalanceInEtherOf(safeAddress)
const threshold = Number(await gnosisSafe.getThreshold())

View File

@ -15,7 +15,7 @@ import {
import { getTransactionFromReduxStore } from '~/routes/safe/test/testMultisig'
import { buildMathPropsFrom } from '~/test/utils/buildReactRouterProps'
import { createTransaction } from '~/wallets/createTransactions'
import { getGnosisSafeContract } from '~/wallets/safeContracts'
import { getGnosisSafeInstanceAt } from '~/wallets/safeContracts'
import fetchTransactions from '~/routes/safe/store/actions/fetchTransactions'
*/
describe('React DOM TESTS > Change threshold', () => {
@ -32,9 +32,7 @@ describe('React DOM TESTS > Change threshold', () => {
const match: Match = buildMathPropsFrom(address)
const safe = safeSelector(store.getState(), { match })
if (!safe) throw new Error()
const web3 = getWeb3()
const GnosisSafe = await getGnosisSafeContract(web3)
const gnosisSafe = GnosisSafe.at(address)
const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress)
// WHEN
const nonce = Date.now()
@ -79,9 +77,7 @@ describe('React DOM TESTS > Change threshold', () => {
const match: Match = buildMathPropsFrom(address)
const safe = safeSelector(store.getState(), { match })
if (!safe) throw new Error()
const web3 = getWeb3()
const GnosisSafe = await getGnosisSafeContract(web3)
const gnosisSafe = GnosisSafe.at(address)
const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress)
// WHEN
const nonce = Date.now()

View File

@ -1,6 +1,7 @@
// @flow
import { List } from 'immutable'
import { getSafeEthereumInstance, createTransaction } from '~/logic/safe/safeFrontendOperations'
import { createTransaction } from '~/logic/safe/safeFrontendOperations'
import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts'
import { type Safe } from '~/routes/safe/store/models/safe'
import { makeOwner } from '~/routes/safe/store/models/owner'
import fetchTransactions from '~/routes/safe/store/actions/fetchTransactions'
@ -29,7 +30,7 @@ describe('Transactions Suite', () => {
it('retrieves tx info from service having subject available', async () => {
let safe: Safe = getSafeFrom(store.getState(), safeAddress)
const gnosisSafe = await getSafeEthereumInstance(safeAddress)
const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress)
const firstTxData = gnosisSafe.contract.methods.addOwnerWithThreshold(accounts[1], 2).encodeABI()
const executor = accounts[0]
const nonce = await gnosisSafe.nonce()

View File

@ -6,7 +6,7 @@ import abi from 'ethereumjs-abi'
console.log(`to[${to}] \n\n valieInWei[${valueInWei}] \n\n
data[${data}] \n\n operation[${operation}] \n\n sigs[${sigs}]`)
const gnosisSafe = await getSafeEthereumInstance(address)
const gnosisSafe = await getGnosisSafeInstanceAt(address)
await printOutApprove("Remove owner 3", address, await gnosisSafe.getOwners(), tx.get('data'), tx.get('nonce'))
const txData =
await gnosisSafe.contract.execTransactionIfApproved.getData(address, 0, tx.get('data'), 0, tx.get('nonce'))