|
| 1 | +import { |
| 2 | + logInfo, |
| 3 | + retryCall, |
| 4 | + stringTo2dArray, |
| 5 | + stringToAddress, |
| 6 | + stringToArray, |
| 7 | +} from 'utils'; |
| 8 | +import { validators } from './main.js'; |
| 9 | +import Safe from '@safe-global/protocol-kit'; |
| 10 | +import { getSafeTxHash } from '../../../features/consolidation.js'; |
| 11 | +import { Address } from 'viem'; |
| 12 | + |
| 13 | +const validatorsWrite = validators |
| 14 | + .command('write') |
| 15 | + .aliases(['w']) |
| 16 | + .description('validators write commands'); |
| 17 | + |
| 18 | +validatorsWrite |
| 19 | + .command('consolidate') |
| 20 | + .description('consolidate validators') |
| 21 | + .argument('<safe_address>', 'safe address', stringToAddress) |
| 22 | + .argument('<consolidation_address>', 'consolidation address', stringToAddress) |
| 23 | + .argument('<refund_recipient>', 'refund recipient address', stringToAddress) |
| 24 | + .argument( |
| 25 | + '<source_pubkeys>', |
| 26 | + 'source pubkeys of the validators to consolidate from. Comma separated list of lists pubkeys', |
| 27 | + stringTo2dArray, |
| 28 | + ) |
| 29 | + .argument( |
| 30 | + '<target_pubkeys>', |
| 31 | + 'pubkeys of the validators to withdraw. Comma separated list of pubkeys', |
| 32 | + stringToArray, |
| 33 | + ) |
| 34 | + .action( |
| 35 | + async ( |
| 36 | + safeAddress: Address, |
| 37 | + consolidationAddress: Address, |
| 38 | + refundRecipient: Address, |
| 39 | + sourcePubkeys: string[][], |
| 40 | + targetPubkeys: string[], |
| 41 | + ) => { |
| 42 | + logInfo('create and approve consolidation hash'); |
| 43 | + logInfo('safeAddress:', safeAddress); |
| 44 | + logInfo('consolidationAddress:', consolidationAddress); |
| 45 | + logInfo('refundRecipient:', refundRecipient); |
| 46 | + logInfo('sourcePubkeys:', sourcePubkeys); |
| 47 | + logInfo('targetPubkeys:', targetPubkeys); |
| 48 | + |
| 49 | + const provider = process.env.RPC_URL; |
| 50 | + if (!provider) { |
| 51 | + throw new Error('Missing RPC_URL environment variable'); |
| 52 | + } |
| 53 | + |
| 54 | + const signer = process.env.OWNER_PRIVATE_KEY; |
| 55 | + if (!signer) { |
| 56 | + throw new Error('Missing OWNER_PRIVATE_KEY environment variable'); |
| 57 | + } |
| 58 | + |
| 59 | + const safe = await retryCall(async () => { |
| 60 | + return await Safe.init({ |
| 61 | + provider: provider, |
| 62 | + signer: signer, |
| 63 | + safeAddress: safeAddress, |
| 64 | + }); |
| 65 | + }); |
| 66 | + logInfo('Safe instance initialized'); |
| 67 | + |
| 68 | + const safeTxHash = await getSafeTxHash( |
| 69 | + safe, |
| 70 | + sourcePubkeys, |
| 71 | + targetPubkeys, |
| 72 | + refundRecipient, |
| 73 | + ); |
| 74 | + logInfo('Safe tx hash:'); |
| 75 | + logInfo(safeTxHash); |
| 76 | + |
| 77 | + const signature = await retryCall(async () => { |
| 78 | + return await safe.signHash(safeTxHash); |
| 79 | + }); |
| 80 | + |
| 81 | + logInfo('Signature:'); |
| 82 | + logInfo(signature); |
| 83 | + |
| 84 | + logInfo('Proposing transaction to the service'); |
| 85 | + |
| 86 | + await retryCall(async () => { |
| 87 | + await safe.approveTransactionHash(safeTxHash); |
| 88 | + }); |
| 89 | + }, |
| 90 | + ); |
0 commit comments