Skip to content

Commit

Permalink
CR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
shazarre committed Nov 12, 2024
1 parent 9d125ec commit ac4e09a
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 62 deletions.
36 changes: 4 additions & 32 deletions packages/cli/src/commands/governance/approve-l2.test.ts

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions packages/cli/src/test-utils/constants.ts

Large diffs are not rendered by default.

26 changes: 25 additions & 1 deletion packages/cli/src/test-utils/multisigUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
import { multiSigABI, proxyABI } from '@celo/abis'
import { StrongAddress } from '@celo/base'
import { ContractKit } from '@celo/contractkit'
import { multiSigBytecode, proxyBytecode } from './constants'
import { setCode } from '@celo/dev-utils/lib/anvil-test'
import Web3 from 'web3'
import {
multiSigBytecode,
proxyBytecode,
SAFE_FALLBACK_HANDLER_ADDRESS,
SAFE_FALLBACK_HANDLER_CODE,
SAFE_MULTISEND_ADDRESS,
SAFE_MULTISEND_CALL_ONLY_ADDRESS,
SAFE_MULTISEND_CALL_ONLY_CODE,
SAFE_MULTISEND_CODE,
SAFE_PROXY_ADDRESS,
SAFE_PROXY_CODE,
SAFE_PROXY_FACTORY_ADDRESS,
SAFE_PROXY_FACTORY_CODE,
} from './constants'

export async function createMultisig(
kit: ContractKit,
Expand Down Expand Up @@ -54,3 +69,12 @@ export async function createMultisig(

return proxyAddress as StrongAddress
}

export const setupSafeContracts = async (web3: Web3) => {
// Set up safe 1.3.0 in devchain
await setCode(web3, SAFE_MULTISEND_ADDRESS, SAFE_MULTISEND_CODE)
await setCode(web3, SAFE_MULTISEND_CALL_ONLY_ADDRESS, SAFE_MULTISEND_CALL_ONLY_CODE)
await setCode(web3, SAFE_PROXY_FACTORY_ADDRESS, SAFE_PROXY_FACTORY_CODE)
await setCode(web3, SAFE_PROXY_ADDRESS, SAFE_PROXY_CODE)
await setCode(web3, SAFE_FALLBACK_HANDLER_ADDRESS, SAFE_FALLBACK_HANDLER_CODE)
}
29 changes: 15 additions & 14 deletions packages/cli/src/utils/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import {
CeloTx,
Connection,
EventLog,
TransactionResult,
parseDecodedParams,
TransactionResult,
} from '@celo/connect'
import { Errors, ux } from '@oclif/core'
import { TransactionResult as SafeTransactionResult } from '@safe-global/types-kit'
import BigNumber from 'bignumber.js'
import chalk from 'chalk'
import { ethers } from 'ethers'
Expand All @@ -22,34 +23,34 @@ export async function displayWeb3Tx(name: string, txObj: any, tx?: Omit<CeloTx,
ux.action.stop()
}

export async function displaySendSafeTx(name: string, safeTx: Promise<TransactionResult>) {
export async function displaySafeTx(name: string, safeTxResult: SafeTransactionResult) {
ux.action.start(`Sending Transaction: ${name}`)

// TODO resolve errors
try {
const txResult = await safeTx

// @ts-ignore
if (!txResult.transactionResponse) {
if (!safeTxResult.transactionResponse) {
throw new Error('Transaction failed')
}

// @ts-ignore
/**
* wait() method does not exists in the types, but according to the docs
* https://docs.safe.global/sdk/protocol-kit/reference/safe#executetransaction
* this actually exists
*
* It is covered by tests though and working fine
*/
if (
// @ts-ignore
'wait' in (txResult.transactionResponse as any) &&
// @ts-ignore
typeof (txResult.transactionResponse as any).wait === 'function'
'wait' in (safeTxResult.transactionResponse as any) &&
typeof (safeTxResult.transactionResponse as any).wait === 'function'
) {
// @ts-expect-error example taken from the docs and works fine (covered by tests)
const receipt = await txResult.transactionResponse?.wait()
const receipt = await (safeTxResult.transactionResponse as any).wait()

printValueMap({ txHash: receipt.transactionHash })
}

ux.action.stop()
} catch (e) {
ux.action.stop(`failed: ${(e as Error).message}`)

throw e
}
}
Expand Down
26 changes: 11 additions & 15 deletions packages/cli/src/utils/safe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import { CeloProvider } from '@celo/connect/lib/celo-provider'
import Safe from '@safe-global/protocol-kit'
import { MetaTransactionData, TransactionResult } from '@safe-global/types-kit'
import Web3 from 'web3'
import { displaySendSafeTx } from './cli'
import { displaySafeTx } from './cli'

// TODO require CeloProvider directly
export const createSafeFromWeb3 = async (
web3: Web3,
signer: StrongAddress,
Expand Down Expand Up @@ -41,35 +40,32 @@ export const performSafeTransaction = async (
txData: MetaTransactionData
) => {
const safe = await createSafeFromWeb3(web3, safeSigner, safeAddress)
const approveTxPromise = await createApproveSafeTransactionIfNotApproved(safe, txData, safeSigner)

const approveTx = await createApproveSafeTransactionIfNotApproved(safe, txData, safeSigner)

if (approveTx !== null) {
// @ts-ignore
await displaySendSafeTx('approveTx', approveTx)
if (approveTxPromise) {
await displaySafeTx('approveTx', approveTxPromise)
}

const executeTx = await createExecuteSafeTransactionIfThresholdMet(safe, txData)
const executeTxPromise = await createExecuteSafeTransactionIfThresholdMet(safe, txData)

if (executeTx !== null) {
// @ts-expect-error
await displaySendSafeTx('executeTx', executeTx)
if (executeTxPromise) {
await displaySafeTx('executeTx', executeTxPromise)
}
}

const createApproveSafeTransactionIfNotApproved = async (
safe: Safe,
txData: MetaTransactionData,
ownerAddress: StrongAddress
): Promise<Promise<TransactionResult> | null> => {
): Promise<TransactionResult | null> => {
const txHash = await safe.getTransactionHash(
await safe.createTransaction({
transactions: [txData],
})
)

if (!(await safe.getOwnersWhoApprovedTx(txHash)).includes(ownerAddress)) {
return safe.approveTransactionHash(txHash)
return await safe.approveTransactionHash(txHash)
}

return null
Expand All @@ -78,14 +74,14 @@ const createApproveSafeTransactionIfNotApproved = async (
const createExecuteSafeTransactionIfThresholdMet = async (
safe: Safe,
txData: MetaTransactionData
): Promise<Promise<TransactionResult> | null> => {
): Promise<TransactionResult | null> => {
const tx = await safe.createTransaction({
transactions: [txData],
})
const txHash = await safe.getTransactionHash(tx)

if ((await safe.getOwnersWhoApprovedTx(txHash)).length >= (await safe.getThreshold())) {
return safe.executeTransaction(tx)
return await safe.executeTransaction(tx)
}

return null
Expand Down

0 comments on commit ac4e09a

Please sign in to comment.