/
submitTx.js
138 lines (119 loc) · 3.54 KB
/
submitTx.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
const { ethers } = require('hardhat')
const {
getSafeAddress,
getSafeVersion,
submitTxOldMultisig,
confirmMultisigTx,
getSafeService,
} = require('../../helpers/multisig')
const { ADDRESS_ZERO, getNetwork } = require('@unlock-protocol/hardhat-helpers')
const { EthersAdapter } = require('@safe-global/protocol-kit')
const Safe = require('@safe-global/protocol-kit').default
async function main({ safeAddress, tx, signer }) {
const { id: chainId } = await getNetwork()
if (!safeAddress) {
safeAddress = getSafeAddress(chainId)
}
if (!signer) {
;[signer] = await ethers.getSigners()
}
if (process.env.RUN_FORK) {
throw Error(`Can not send multisig tx on a forked network`)
}
// check safe version
const version = await getSafeVersion(safeAddress)
// mainnet still use older versions of the safe
if (version === 'old') {
const nonce = await submitTxOldMultisig({ safeAddress, tx, signer })
return nonce
}
// Use Safe v1+ with SDK
const ethAdapter = new EthersAdapter({
ethers,
signerOrProvider: signer,
})
// get Safe service URL if not default
const safeService = await getSafeService(chainId)
// create tx
const safeSdk = await Safe.create({ ethAdapter, safeAddress })
const txs = !Array.isArray(tx) ? [tx] : tx
let explainer = ''
try {
explainer = txs
.map(({ functionName, functionArgs, explainer }) =>
explainer
? explainer
: `'${functionName}(${Object.values(functionArgs).toString()})'`
)
.join(', ')
console.log(`Submitting txs: ${explainer}`)
} catch (error) {
console.log(`Missing explainers...`)
}
// parse transactions
const transactions = await Promise.all(
txs.map(async (tx) => {
// encode contract call
const {
contractNameOrAbi,
contractAddress,
functionName,
functionArgs,
calldata,
value, // in ETH
} = tx
let encodedFunctionCall
if (!calldata) {
const { interface } = await ethers.getContractAt(
contractNameOrAbi,
ADDRESS_ZERO
)
encodedFunctionCall = interface.encodeFunctionData(
functionName,
functionArgs
)
} else {
encodedFunctionCall = calldata
}
return {
to: contractAddress,
data: encodedFunctionCall,
value: value || 0,
// operation, // Optional
}
})
)
console.log(transactions)
// get correct nonce
const nonce = await safeService.getNextNonce(safeAddress)
const txOptions = {
origin: explainer,
nonce, // make sure we get the correct nonce, so we dont override
// txs that havent been executed yet
}
// create a MultiSend tx
const safeTransaction = await safeSdk.createTransaction({
transactions,
options: txOptions,
})
// now send tx via Safe Global web service
const safeTxHash = await safeSdk.getTransactionHash(safeTransaction)
const senderSignature = await safeSdk.signTransactionHash(safeTxHash)
await safeService.proposeTransaction({
safeAddress,
safeTransactionData: safeTransaction.data,
safeTxHash,
senderAddress: signer.address,
senderSignature: senderSignature.data,
})
const { nonce: actualNonce } = await safeService.getTransaction(safeTxHash)
console.log(`Tx submitted to multisig with id: '${actualNonce}'`)
if (process.env.RUN_MAINNET_FORK) {
console.log(`Signing multisigs: ${nonce}`)
await confirmMultisigTx({
transactionId: nonce,
multisigAddress: safeAddress,
})
}
}
module.exports = main