|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const ethers = require('ethers'); |
| 4 | +const { EthersAdapter } = require('@gnosis.pm/safe-core-sdk'); |
| 5 | +const GnosisSafe = require('@gnosis.pm/safe-core-sdk').default; |
| 6 | +const SafeServiceClient = require('@gnosis.pm/safe-service-client').default; |
| 7 | + |
| 8 | +class SafeBatchSubmitter { |
| 9 | + constructor({ network, signer, safeAddress }) { |
| 10 | + this.network = network; |
| 11 | + this.signer = signer; |
| 12 | + this.safeAddress = safeAddress; |
| 13 | + |
| 14 | + this.ethAdapter = new EthersAdapter({ |
| 15 | + ethers, |
| 16 | + signer, |
| 17 | + }); |
| 18 | + |
| 19 | + this.service = new SafeServiceClient( |
| 20 | + `https://safe-transaction${network === 'rinkeby' ? '.rinkeby' : ''}.gnosis.io` |
| 21 | + ); |
| 22 | + } |
| 23 | + |
| 24 | + async init() { |
| 25 | + const { ethAdapter, service, safeAddress, signer } = this; |
| 26 | + this.transactions = []; |
| 27 | + this.safe = await GnosisSafe.create({ |
| 28 | + ethAdapter, |
| 29 | + safeAddress, |
| 30 | + }); |
| 31 | + // check if signer is on the list of owners |
| 32 | + if (!(await this.safe.isOwner(signer.address))) { |
| 33 | + throw Error(`Account ${signer.address} is not a signer on this safe`); |
| 34 | + } |
| 35 | + const currentNonce = await this.safe.getNonce(); |
| 36 | + const pendingTxns = await service.getPendingTransactions(safeAddress, currentNonce); |
| 37 | + return { currentNonce, pendingTxns }; |
| 38 | + } |
| 39 | + |
| 40 | + async appendTransaction({ to, value = '0', data, force }) { |
| 41 | + const { safe, service, safeAddress, transactions } = this; |
| 42 | + if (!force) { |
| 43 | + // check it does not exist in the pending list |
| 44 | + // Note: this means that a duplicate transaction - like an acceptOwnership on |
| 45 | + // the same contract cannot be added in one batch. This could be useful in situations |
| 46 | + // where you want to accept, nominate another owner, migrate, then accept again. |
| 47 | + // In these cases, use "force: true" |
| 48 | + const currentNonce = await safe.getNonce(); |
| 49 | + const pendingTxns = await service.getPendingTransactions(safeAddress, currentNonce); |
| 50 | + |
| 51 | + this.currentNonce = currentNonce; |
| 52 | + this.pendingTxns = pendingTxns; |
| 53 | + |
| 54 | + this.unusedNoncePosition = currentNonce; |
| 55 | + |
| 56 | + let matchedTxnIsPending = false; |
| 57 | + |
| 58 | + for (const { |
| 59 | + nonce, |
| 60 | + dataDecoded: { |
| 61 | + parameters: [{ valueDecoded }], |
| 62 | + }, |
| 63 | + } of pendingTxns.results) { |
| 64 | + // figure out what the next unused nonce position is (including everything else in the queue) |
| 65 | + this.unusedNoncePosition = Math.max(this.unusedNoncePosition, nonce + 1); |
| 66 | + matchedTxnIsPending = |
| 67 | + matchedTxnIsPending || |
| 68 | + valueDecoded.find( |
| 69 | + entry => entry.to === to && entry.data === data && entry.value === value |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + if (matchedTxnIsPending) { |
| 74 | + return {}; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + transactions.push({ to, value, data, nonce: this.unusedNoncePosition }); |
| 79 | + return { appended: true }; |
| 80 | + } |
| 81 | + |
| 82 | + async submit() { |
| 83 | + const { safe, transactions, safeAddress, service, unusedNoncePosition: nonce } = this; |
| 84 | + if (!safe) { |
| 85 | + throw Error('Safe must first be initialized'); |
| 86 | + } |
| 87 | + if (!transactions.length) { |
| 88 | + return { transactions }; |
| 89 | + } |
| 90 | + const batchTxn = await safe.createTransaction(...transactions); |
| 91 | + const txHash = await safe.getTransactionHash(batchTxn); |
| 92 | + const signature = await safe.signTransactionHash(txHash); |
| 93 | + |
| 94 | + try { |
| 95 | + await service.proposeTransaction(safeAddress, batchTxn.data, txHash, signature); |
| 96 | + |
| 97 | + return { transactions, nonce }; |
| 98 | + } catch (err) { |
| 99 | + throw Error(`Error trying to submit batch to safe.\n${err}`); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +module.exports = SafeBatchSubmitter; |
0 commit comments