-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathcreateSafePrepareTransactionRequest.ts
More file actions
164 lines (152 loc) Β· 4.63 KB
/
createSafePrepareTransactionRequest.ts
File metadata and controls
164 lines (152 loc) Β· 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import {
Address,
Chain,
PrivateKeyAccount,
PublicClient,
Transport,
encodeFunctionData,
TransactionRequest,
PrepareTransactionRequestParameters,
} from 'viem';
import {
DEFAULT_SAFE_VERSION,
SafeAccountConfig,
SafeFactory,
encodeSetupCallData,
getSafeContract,
} from '@safe-global/protocol-kit';
import {
getChainSpecificDefaultSaltNonce,
validateSafeAccountConfig,
} from '@safe-global/protocol-kit/dist/src/contracts/utils';
import { validateChain } from './utils/validateChain';
export const SafeProxyFactoryAbi = [
{
anonymous: false,
inputs: [
{
indexed: false,
internalType: 'contract GnosisSafeProxy',
name: 'proxy',
type: 'address',
},
{
indexed: false,
internalType: 'address',
name: 'singleton',
type: 'address',
},
],
name: 'ProxyCreation',
type: 'event',
},
{
inputs: [
{
internalType: 'address',
name: '_singleton',
type: 'address',
},
{
internalType: 'bytes',
name: 'initializer',
type: 'bytes',
},
{
internalType: 'uint256',
name: 'saltNonce',
type: 'uint256',
},
],
name: 'createProxyWithNonce',
outputs: [
{
internalType: 'contract GnosisSafeProxy',
name: 'proxy',
type: 'address',
},
],
stateMutability: 'nonpayable',
type: 'function',
},
] as const;
/**
* This type is for the parameters of the createSafePrepareTransactionRequest function
*/
export type CreateSafePrepareTransactionRequestParams<TChain extends Chain | undefined> = {
publicClient: PublicClient<Transport, TChain>;
account: PrivateKeyAccount;
owners: Address[];
threshold: number;
saltNonce?: bigint;
};
/**
* Prepares the transaction to create a new Safe using the default SafeFactory
*
* It leverages the [Protocol Kit](https://docs.safe.global/sdk/protocol-kit) from the Safe{Core} SDK.
*
* Returns the transaction to sign and send to the blockchain.
*
* @param {CreateSafePrepareTransactionRequestParams} createSafePrepareTransactionRequestParams {@link CreateSafePrepareTransactionRequestParams}
* @param {PublicClient} createSafePrepareTransactionRequestParams.publicClient - A Viem Public Client
* @param {PrivateKeyAccount} createSafePrepareTransactionRequestParams.account - The private key of the deployer of the new Safe
* @param {Address[]} createSafePrepareTransactionRequestParams.owners - Array of addresses of the signers of the Safe
* @param {number} createSafePrepareTransactionRequestParams.threshold - Number of signatures needed to validate a transaction in the Safe
* @param {bigint} createSafePrepareTransactionRequestParams.saltNonce - Optional salt nonce for the call to Create2
*
* @returns Promise<{@link TransactionRequest}> - the transaction to sign and send to the blockchain.
*/
export async function createSafePrepareTransactionRequest<TChain extends Chain | undefined>({
publicClient,
account,
owners,
threshold,
saltNonce,
}: CreateSafePrepareTransactionRequestParams<TChain>) {
const chainId = validateChain(publicClient);
// set and validate Safe configuration
const safeAccountConfig: SafeAccountConfig = {
owners,
threshold,
};
validateSafeAccountConfig(safeAccountConfig);
// instantiate Safe Factory
const safeFactory = await SafeFactory.init({
provider: publicClient.chain!.rpcUrls.default.http[0],
});
// instantiate Safe Contract
const safeContract = await getSafeContract({
safeProvider: safeFactory.getSafeProvider(),
safeVersion: DEFAULT_SAFE_VERSION,
});
// get Safe Proxy Factory address
const safeProxyFactoryAddress = (await safeFactory.getAddress()) as Address;
// get Safe Contract address
const safeContractAddress = (await safeContract.getAddress()) as Address;
// initializer calldata
const initializer = (await encodeSetupCallData({
safeProvider: safeFactory.getSafeProvider(),
safeAccountConfig,
safeContract,
})) as `0x${string}`;
// salt nonce
if (!saltNonce) {
saltNonce = BigInt(getChainSpecificDefaultSaltNonce(BigInt(chainId)));
}
// prepare the transaction request
const request = await publicClient.prepareTransactionRequest({
chain: publicClient.chain,
to: safeProxyFactoryAddress,
data: encodeFunctionData({
abi: SafeProxyFactoryAbi,
functionName: 'createProxyWithNonce',
args: [
safeContractAddress, // _singleton
initializer, // initializer
saltNonce, // saltNonce
],
}),
account,
} satisfies PrepareTransactionRequestParameters);
return { ...request, chainId };
}