/
multisig.js
205 lines (182 loc) · 5.85 KB
/
multisig.js
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const { ethers } = require('hardhat')
const { networks } = require('@unlock-protocol/networks')
const { getNetwork } = require('@unlock-protocol/hardhat-helpers')
const multisigABI = require('@unlock-protocol/hardhat-helpers/dist/ABIs/multisig2.json')
const multisigOldABI = require('@unlock-protocol/hardhat-helpers/dist/ABIs/multisig.json')
const SafeApiKit = require('@safe-global/api-kit').default
// custom services URL for network not supported by Safe
const safeServiceURLs = {
324: 'https://safe-transaction-zksync.safe.global/api',
1101: 'https://safe-transaction-zkevm.safe.global/api',
534352: 'https://transaction.safe.scroll.xyz/api',
59144: 'https://transaction.safe.linea.build/api',
}
const prodSigners = [
'0x9d3ea9e9adde71141f4534dB3b9B80dF3D03Ee5f', // cc
'0x4Ce2DD8373ECe0d7baAA16E559A5817CC875b16a', // jg
'0x4011d09a86D0acA8377a4A8baD691F1ACeeCd672', // nf
'0xcFd35259E3A468E7bDF84a95bCddAc0B614A9212', // aa
'0xccb5D94FbfBFDc4953Ca8a114f88773C2fF98e80', // sm
'0x246A13358Fb27523642D86367a51C2aEB137Ac6C', // cr
'0x2785f2a3DDaCfDE5947F1A9D6c878CCD7F885400', // cn
'0x7A23608a8eBe71868013BDA0d900351A83bb4Dc2', // nm
'0x8de33D8204929ceb2F7AA6299d0643a7f6664c9b', // bw
].sort()
const devSigners = [
'0x4Ce2DD8373ECe0d7baAA16E559A5817CC875b16a', // jg
'0x246A13358Fb27523642D86367a51C2aEB137Ac6C', // cr
'0x9d3ea9e9adde71141f4534dB3b9B80dF3D03Ee5f', // cc
].sort()
const getExpectedSigners = async (chainId) => {
const { isTestNetwork } = await getNetwork(chainId)
const expectedSigners = isTestNetwork ? devSigners : prodSigners
return expectedSigners
}
const logError = (name, chainId, multisig, msg) =>
console.log(`[${name} (${chainId})]: ${multisig} ${msg}`)
const getSafeService = async (chainId) => {
const txServiceUrl = safeServiceURLs[chainId] || null
console.log(`Using Safe Global service at ${txServiceUrl} - chain ${chainId}`)
const safeService = new SafeApiKit({
chainId,
txServiceUrl,
})
return safeService
}
const getMultiSigInfo = async (chainId, multisig) => {
const errors = []
const { isTestNetwork } = networks[chainId]
const expectedSigners = isTestNetwork ? devSigners : prodSigners
const provider = await getProvider(chainId)
const safeService = await getSafeService(chainId)
const { count } = await safeService.getPendingTransactions(multisig)
if (count) {
errors.push(`${count} pending txs are waiting to be signed`)
}
// the flags to get only un-executed transactions does not work
// filed here https://github.com/safe-global/safe-core-sdk/issues/690
// const allTxs = await safeService.getAllTransactions(multisig, {
// executed: false,
// trusted: false,
// queued: false,
// })
if (!multisig) {
errors.push('Missing multisig')
} else {
const safe = new ethers.Contract(multisig, multisigABI, provider)
const owners = await safe.getOwners()
const policy = await safe.getThreshold()
if (isTestNetwork && policy < 2) {
errors.push('❌ Policy below 2!')
}
if (!isTestNetwork && policy < 4) {
errors.push(
`❌ Unexpected policy: ${policy}/${owners.length} for 4/${expectedSigners.length} expected`
)
}
let extraSigners = owners.filter((x) => !expectedSigners.includes(x))
if (extraSigners.length > 0) {
errors.push(`❌ Extra signers: ${[...extraSigners].sort()}`)
}
let missingSigners = expectedSigners.filter((x) => !owners.includes(x))
if (missingSigners.length > 0) {
errors.push(`❌ Missing signers: ${missingSigners}`)
}
}
return errors
}
// get the correct provider if chainId is specified
const getProvider = async (chainId) => {
let provider
if (chainId) {
const { publicProvider } = networks[chainId]
provider = new ethers.JsonRpcProvider(publicProvider)
} else {
;({ provider } = ethers)
;({ chainId } = await getNetwork())
}
return { provider, chainId }
}
// get safeAddress directly from unlock if needed
const getSafeAddress = async (chainId) => {
const { multisig } = networks[chainId]
return multisig
}
const getSafeVersion = async (safeAddress) => {
const abi = [
{
inputs: [],
name: 'VERSION',
outputs: [
{
internalType: 'string',
name: '',
type: 'string',
},
],
stateMutability: 'view',
type: 'function',
},
]
const safe = await ethers.getContractAt(abi, safeAddress)
try {
const version = await safe.VERSION()
return version
} catch (error) {
return 'old'
}
}
// mainnet still uses older versions of the safe
const submitTxOldMultisig = async ({ safeAddress, tx, signer }) => {
// encode contract call
const {
contractNameOrAbi,
contractAddress,
functionName,
functionArgs,
calldata,
value, // in ETH
} = tx
let encodedFunctionCall
if (!calldata) {
const { interface } = await ethers.getContractFactory(contractNameOrAbi)
encodedFunctionCall = interface.encodeFunctionData(
functionName,
functionArgs
)
} else {
encodedFunctionCall = calldata
}
console.log(
`Submitting ${functionName} to multisig ${safeAddress} (v: old)...`
)
const safe = new ethers.Contract(safeAddress, multisigOldABI, signer)
const txSubmitted = await safe.submitTransaction(
contractAddress,
value || 0, // ETH value
encodedFunctionCall
)
// submit to multisig
const receipt = await txSubmitted.wait()
const { transactionHash, events } = receipt
const nonce = events
.find(({ event }) => event === 'Submission')
.args.transactionId.toNumber()
console.log(
`Tx submitted to multisig with id '${nonce}' (txid: ${transactionHash})`
)
return nonce
}
module.exports = {
getProvider,
getSafeAddress,
getSafeVersion,
submitTxOldMultisig,
safeServiceURLs,
prodSigners,
devSigners,
getMultiSigInfo,
getExpectedSigners,
logError,
getSafeService,
}