Skip to content

Commit

Permalink
Finish removing GnosisSafeL2 Typechain from project
Browse files Browse the repository at this point in the history
  • Loading branch information
adamgall committed May 7, 2024
1 parent e8dc274 commit 9a4a5df
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 59 deletions.
69 changes: 37 additions & 32 deletions src/helpers/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { TypedDataSigner } from '@ethersproject/abstract-signer';
import { Contract, Signer } from 'ethers';
import { Contract } from 'ethers';
import {
hashTypedData,
Hash,
Expand All @@ -14,6 +13,7 @@ import {
isHex,
encodeFunctionData,
Abi,
WalletClient,
} from 'viem';
import { MetaTransaction, SafePostTransaction, SafeTransaction } from '../types/transaction';

Expand Down Expand Up @@ -43,12 +43,12 @@ export function getRandomBytes() {
}

export const calculateSafeTransactionHash = (
safe: Contract,
safeAddress: Address,
safeTx: SafeTransaction,
chainId: number,
): string => {
return hashTypedData({
domain: { verifyingContract: getAddress(safe.address), chainId },
domain: { verifyingContract: safeAddress, chainId },
types: EIP712_SAFE_TX_TYPE,
primaryType: 'SafeTx',
message: { ...safeTx },
Expand Down Expand Up @@ -93,31 +93,42 @@ export const buildSafeTransaction = (template: {
};

export const safeSignTypedData = async (
signer: Signer & TypedDataSigner,
safe: Contract,
walletClient: WalletClient,
contractAddress: Address,
safeTx: SafeTransaction,
chainId?: number,
chainId: number,
): Promise<SafeSignature> => {
if (!chainId && !signer.provider) throw Error('Provider required to retrieve chainId');
const cid = chainId || (await signer.provider!.getNetwork()).chainId;
const signerAddress = await signer.getAddress();
const signedData = await signer._signTypedData(
{ verifyingContract: safe.address, chainId: cid },
EIP712_SAFE_TX_TYPE,
safeTx,
);
if (!isHex(signedData)) {
throw new Error('Error signing message');
}
if (!walletClient.account) throw new Error("Signer doesn't have account");

const signerAddress = walletClient.account.address;
const signedData = await walletClient.signTypedData({
account: signerAddress,
domain: { verifyingContract: contractAddress, chainId },
types: EIP712_SAFE_TX_TYPE,
primaryType: 'SafeTx',
message: {
to: safeTx.to,
value: safeTx.value,
data: safeTx.data,
operation: safeTx.operation,
safeTxGas: safeTx.safeTxGas,
baseGas: safeTx.baseGas,
gasPrice: safeTx.gasPrice,
gasToken: safeTx.gasToken,
refundReceiver: safeTx.refundReceiver,
nonce: safeTx.nonce,
},
});

return {
signer: signerAddress,
data: signedData,
};
};

export const buildSafeAPIPost = async (
safeContract: Contract,
signerOrProvider: Signer & TypedDataSigner,
safeAddress: Address,
walletClient: WalletClient,
chainId: number,
template: {
to: Address;
Expand All @@ -132,20 +143,14 @@ export const buildSafeAPIPost = async (
nonce: number;
},
): Promise<SafePostTransaction> => {
const safeTx = buildSafeTransaction(template);
if (!walletClient.account) throw new Error("Signer doesn't have account");

const txHash = calculateSafeTransactionHash(safeContract, safeTx, chainId);
const sig = [
await safeSignTypedData(
signerOrProvider as Signer & TypedDataSigner,
safeContract,
safeTx,
chainId,
),
];
const safeTx = buildSafeTransaction(template);
const txHash = calculateSafeTransactionHash(safeAddress, safeTx, chainId);
const sig = [await safeSignTypedData(walletClient, safeAddress, safeTx, chainId)];
const signatureBytes = buildSignatureBytes(sig);
return {
safe: safeContract.address,
safe: safeAddress,
to: safeTx.to,
value: safeTx.value ? safeTx.value.toString() : '0',
data: safeTx.data,
Expand All @@ -157,7 +162,7 @@ export const buildSafeAPIPost = async (
refundReceiver: safeTx.refundReceiver,
nonce: safeTx.nonce,
contractTransactionHash: txHash,
sender: await signerOrProvider.getAddress(),
sender: walletClient.account.address,
signature: signatureBytes,
};
};
Expand Down
46 changes: 19 additions & 27 deletions src/hooks/DAO/proposal/useSubmitProposal.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { TypedDataSigner } from '@ethersproject/abstract-signer';
import { Azorius } from '@fractal-framework/fractal-contracts';
import axios from 'axios';
import { Signer } from 'ethers';
import { useCallback, useMemo, useState } from 'react';
import { toast } from 'react-toastify';
import { isAddress, getAddress, encodeAbiParameters, parseAbiParameters, isHex } from 'viem';
import { GnosisSafeL2__factory } from '../../../assets/typechain-types/usul/factories/@gnosis.pm/safe-contracts/contracts';
import { useWalletClient } from 'wagmi';
import { ADDRESS_MULTISIG_METADATA, SENTINEL_ADDRESS } from '../../../constants/common';
import { buildSafeAPIPost, encodeMultiSend } from '../../../helpers';
import { logError } from '../../../helpers/errorLogging';
import { useFractal } from '../../../providers/App/AppProvider';
import useIPFSClient from '../../../providers/App/hooks/useIPFSClient';
import { useSafeAPI } from '../../../providers/App/hooks/useSafeAPI';
import { useEthersProvider } from '../../../providers/Ethers/hooks/useEthersProvider';
import { useEthersSigner } from '../../../providers/Ethers/hooks/useEthersSigner';
import { useNetworkConfig } from '../../../providers/NetworkConfig/NetworkConfigProvider';
import { MetaTransaction, ProposalExecuteData, CreateProposalMetadata } from '../../../types';
Expand Down Expand Up @@ -54,7 +51,7 @@ export default function useSubmitProposal() {
const [pendingCreateTx, setPendingCreateTx] = useState(false);
const loadDAOProposals = useDAOProposals();
const signer = useEthersSigner();
const provider = useEthersProvider();
const { data: walletClient } = useWalletClient();

const {
node: { safe, fractalModules },
Expand Down Expand Up @@ -91,7 +88,7 @@ export default function useSubmitProposal() {
successCallback,
safeAddress,
}: ISubmitProposal) => {
if (!proposalData || !baseContracts) {
if (!proposalData || !baseContracts || !walletClient) {
return;
}
const { multiSendContract } = baseContracts;
Expand Down Expand Up @@ -162,21 +159,15 @@ export default function useSubmitProposal() {
operation = 0;
}

const safeContract = GnosisSafeL2__factory.connect(safeAddress, signerOrProvider);
await axios.post(
buildSafeApiUrl(safeBaseURL, `/safes/${safeAddress}/multisig-transactions/`),
await buildSafeAPIPost(
safeContract,
signerOrProvider as Signer & TypedDataSigner,
chain.id,
{
to,
value,
data,
operation,
nonce,
},
),
await buildSafeAPIPost(getAddress(safeAddress), walletClient, chain.id, {
to,
value,
data,
operation,
nonce,
}),
);
await new Promise(resolve => setTimeout(resolve, 1000));
await loadDAOProposals();
Expand All @@ -194,13 +185,14 @@ export default function useSubmitProposal() {
}
},
[
signerOrProvider,
safeBaseURL,
chain,
loadDAOProposals,
ipfsClient,
baseContracts,
addressPrefix,
baseContracts,
chain.id,
ipfsClient,
loadDAOProposals,
safeBaseURL,
signerOrProvider,
walletClient,
],
);

Expand All @@ -215,7 +207,7 @@ export default function useSubmitProposal() {
failedToastMessage,
safeAddress,
}: ISubmitAzoriusProposal) => {
if (!proposalData || !provider) {
if (!proposalData) {
return;
}
const toastId = toast(pendingToastMessage, {
Expand Down Expand Up @@ -261,7 +253,7 @@ export default function useSubmitProposal() {
setPendingCreateTx(false);
}
},
[provider, addressPrefix],
[addressPrefix],
);

const submitProposal = useCallback(
Expand Down

0 comments on commit 9a4a5df

Please sign in to comment.