WA-238 Creating Safe Master Copy once

This commit is contained in:
apanizo 2018-05-02 15:31:26 +02:00
parent 2cd55379ee
commit 068b08eb82
2 changed files with 56 additions and 32 deletions

View File

@ -5,14 +5,9 @@ import { connect } from 'react-redux'
import Page from '~/components/layout/Page'
import { getAccountsFrom, getThresholdFrom, getNamesFrom, getSafeNameFrom, getDailyLimitFrom } from '~/routes/open/utils/safeDataExtractor'
import { getWeb3 } from '~/wallets/getWeb3'
import {
getCreateAddExtensionContract,
getCreateDailyLimitExtensionContract,
getCreateProxyFactoryContract,
getGnosisSafeContract,
} from '~/wallets/safeContracts'
import { getGnosisSafeContract, deploySafeContract, initContracts } from '~/wallets/safeContracts'
import selector from './selector'
import actions, { type Actions } from './actions'
import actions, { type Actions, type AddSafe } from './actions'
import Layout from '../components/Layout'
type Props = Actions & {
@ -25,7 +20,7 @@ type State = {
safeTx: string,
}
const createSafe = async (values, userAccount, addSafe): State => {
const createSafe = async (values: Object, userAccount: string, addSafe: AddSafe): Promise<State> => {
const accounts = getAccountsFrom(values)
const numConfirmations = getThresholdFrom(values)
const name = getSafeNameFrom(values)
@ -34,28 +29,9 @@ const createSafe = async (values, userAccount, addSafe): State => {
const web3 = getWeb3()
const GnosisSafe = getGnosisSafeContract(web3)
const ProxyFactory = getCreateProxyFactoryContract(web3)
const CreateAndAddExtension = getCreateAddExtensionContract(web3)
const DailyLimitExtension = getCreateDailyLimitExtensionContract(web3)
// Create Master Copies
const proxyFactory = await ProxyFactory.new({ from: userAccount, gas: '5000000' })
const createAndAddExtension = await CreateAndAddExtension.new({ from: userAccount, gas: '5000000' })
// Initialize safe master copy
const gnosisSafeMasterCopy = await GnosisSafe.new({ from: userAccount, gas: '5000000' })
gnosisSafeMasterCopy.setup([userAccount], 1, 0, 0, { from: userAccount, gas: '5000000' })
// Initialize extension master copy
const dailyLimitExtensionMasterCopy = await DailyLimitExtension.new({ from: userAccount, gas: '5000000' })
dailyLimitExtensionMasterCopy.setup([], [], { from: userAccount, gas: '5000000' })
// Create Gnosis Safe and Daily Limit Extension in one transactions
const extensionData = await dailyLimitExtensionMasterCopy.contract.setup.getData([0], [100], { from: userAccount, gas: '5000000' })
const proxyFactoryData = await proxyFactory.contract.createProxy.getData(dailyLimitExtensionMasterCopy.address, extensionData, { from: userAccount, gas: '5000000' })
const createAndAddExtensionData = createAndAddExtension.contract.createAndAddExtension.getData(proxyFactory.address, proxyFactoryData, { from: userAccount, gas: '5000000' })
const gnosisSafeData = await gnosisSafeMasterCopy.contract.setup.getData(accounts, numConfirmations, createAndAddExtension.address, createAndAddExtensionData, { from: userAccount, gas: '5000000' })
const safe = await proxyFactory.createProxy(gnosisSafeMasterCopy.address, gnosisSafeData, { from: userAccount, gas: '5000000' })
await initContracts()
const safe = await deploySafeContract(accounts, numConfirmations, userAccount)
const param = safe.logs[1].args.proxy
const safeContract = GnosisSafe.at(param)

View File

@ -1,11 +1,18 @@
// @flow
import contract from 'truffle-contract'
import { promisify } from '~/utils/promisify'
import { ensureOnce } from '~/utils/singleton'
import { getWeb3 } from '~/wallets/getWeb3'
import GnosisSafeSol from '#/GnosisSafe.json'
import ProxyFactorySol from '#/ProxyFactory.json'
import CreateAndAddExtensionSol from '#/CreateAndAddExtension.json'
import DailyLimitExtensionSol from '#/DailyLimitExtension.json'
let proxyFactoryMaster
let createAndAddExtensionMaster
let safeMaster
let dailyLimitMaster
const createGnosisSafeContract = (web3: any) => {
const gnosisSafe = contract(GnosisSafeSol)
gnosisSafe.setProvider(web3.currentProvider)
@ -35,6 +42,47 @@ const createDailyLimitExtensionContract = (web3: any) => {
}
export const getGnosisSafeContract = ensureOnce(createGnosisSafeContract)
export const getCreateProxyFactoryContract = ensureOnce(createProxyFactoryContract)
export const getCreateAddExtensionContract = ensureOnce(createAddExtensionContract)
export const getCreateDailyLimitExtensionContract = ensureOnce(createDailyLimitExtensionContract)
const getCreateProxyFactoryContract = ensureOnce(createProxyFactoryContract)
const getCreateAddExtensionContract = ensureOnce(createAddExtensionContract)
const getCreateDailyLimitExtensionContract = ensureOnce(createDailyLimitExtensionContract)
const createMasterCopies = async () => {
const web3 = getWeb3()
const accounts = await promisify(cb => web3.eth.getAccounts(cb))
const userAccount = accounts[0]
const ProxyFactory = getCreateProxyFactoryContract(web3)
const CreateAndAddExtension = getCreateAddExtensionContract(web3)
const DailyLimitExtension = getCreateDailyLimitExtensionContract(web3)
// Create Master Copies
proxyFactoryMaster = await ProxyFactory.new({ from: userAccount, gas: '5000000' })
createAndAddExtensionMaster = await CreateAndAddExtension.new({ from: userAccount, gas: '5000000' })
// Initialize safe master copy
const GnosisSafe = getGnosisSafeContract(web3)
safeMaster = await GnosisSafe.new({ from: userAccount, gas: '5000000' })
safeMaster.setup([userAccount], 1, 0, 0, { from: userAccount, gas: '5000000' })
// Initialize extension master copy
dailyLimitMaster = await DailyLimitExtension.new({ from: userAccount, gas: '5000000' })
dailyLimitMaster.setup([], [], { from: userAccount, gas: '5000000' })
}
export const initContracts = ensureOnce(createMasterCopies)
const getSafeDataBasedOn = async (accounts, numConfirmations) => {
const extensionData = await dailyLimitMaster.contract.setup
.getData([0], [100])
const proxyFactoryData = await proxyFactoryMaster.contract.createProxy
.getData(dailyLimitMaster.address, extensionData)
const createAndAddExtensionData = createAndAddExtensionMaster.contract.createAndAddExtension
.getData(proxyFactoryMaster.address, proxyFactoryData)
return safeMaster.contract.setup
.getData(accounts, numConfirmations, createAndAddExtensionMaster.address, createAndAddExtensionData)
}
export const deploySafeContract = async (safeAccounts: string[], numConfirmations: number, userAccount: string) => {
const gnosisSafeData = await getSafeDataBasedOn(safeAccounts, numConfirmations)
return proxyFactoryMaster.createProxy(safeMaster.address, gnosisSafeData, { from: userAccount, gas: '5000000' })
}