|
| 1 | +const core = require('@actions/core'); |
| 2 | +const SafeApiKit = require('@safe-global/api-kit').default; |
| 3 | +const Safe = require('@safe-global/protocol-kit').default; |
| 4 | +const { OperationType } = require('@safe-global/types-kit'); |
| 5 | +const { Wallet } = require('ethers'); |
| 6 | + |
| 7 | +async function run() { |
| 8 | + try { |
| 9 | + // Get inputs from environment variables |
| 10 | + const proposerPrivateKey = process.env.INPUT_PROPOSER_PRIVATE_KEY; |
| 11 | + const rpcUrl = process.env.INPUT_RPC_URL; |
| 12 | + const safeAddress = process.env.INPUT_SAFE_ADDRESS; |
| 13 | + const targetAddress = process.env.INPUT_TARGET_ADDRESS; |
| 14 | + const safeApiKey = process.env.INPUT_SAFE_API_KEY; |
| 15 | + const chainId = BigInt(process.env.INPUT_CHAIN_ID || '42161'); |
| 16 | + const transactionValue = process.env.INPUT_TRANSACTION_VALUE || '0'; |
| 17 | + const transactionData = process.env.INPUT_TRANSACTION_DATA || '0x'; |
| 18 | + const operation = process.env.INPUT_OPERATION || '0'; |
| 19 | + |
| 20 | + core.info(`🚀 Starting Safe transaction creation...`); |
| 21 | + core.info(`📍 Safe Address: ${safeAddress}`); |
| 22 | + core.info(`🎯 Target Address: ${targetAddress}`); |
| 23 | + |
| 24 | + // Initialize wallet |
| 25 | + const wallet = new Wallet(proposerPrivateKey); |
| 26 | + core.info(`👤 Proposer Address: ${wallet.address}`); |
| 27 | + |
| 28 | + // Initialize API Kit |
| 29 | + const apiKit = new SafeApiKit({ |
| 30 | + chainId: chainId, |
| 31 | + apiKey: safeApiKey |
| 32 | + }); |
| 33 | + |
| 34 | + // Initialize Protocol Kit |
| 35 | + const protocolKit = await Safe.init({ |
| 36 | + provider: rpcUrl, |
| 37 | + signer: proposerPrivateKey, |
| 38 | + safeAddress: safeAddress |
| 39 | + }); |
| 40 | + |
| 41 | + // Create transaction |
| 42 | + const safeTransactionData = { |
| 43 | + to: targetAddress, |
| 44 | + value: transactionValue, |
| 45 | + data: transactionData, |
| 46 | + operation: parseInt(operation) |
| 47 | + }; |
| 48 | + |
| 49 | + core.info('📝 Creating Safe transaction...'); |
| 50 | + const safeTransaction = await protocolKit.createTransaction({ |
| 51 | + transactions: [safeTransactionData] |
| 52 | + }); |
| 53 | + |
| 54 | + const safeTxHash = await protocolKit.getTransactionHash(safeTransaction); |
| 55 | + const signature = await protocolKit.signHash(safeTxHash); |
| 56 | + |
| 57 | + core.info(`🔐 Transaction signed with hash: ${safeTxHash}`); |
| 58 | + |
| 59 | + // Propose transaction to the service |
| 60 | + await apiKit.proposeTransaction({ |
| 61 | + safeAddress: safeAddress, |
| 62 | + safeTransactionData: safeTransaction.data, |
| 63 | + safeTxHash: safeTxHash, |
| 64 | + senderAddress: wallet.address, |
| 65 | + senderSignature: signature.data |
| 66 | + }); |
| 67 | + |
| 68 | + core.info('📤 Transaction proposed to Safe service'); |
| 69 | + |
| 70 | + // Get transaction details |
| 71 | + const transaction = await apiKit.getTransaction(safeTxHash); |
| 72 | + |
| 73 | + // Set outputs |
| 74 | + core.setOutput('safe-tx-hash', safeTxHash); |
| 75 | + core.setOutput('transaction', JSON.stringify(transaction)); |
| 76 | + |
| 77 | + core.info(`✅ Transaction created successfully!`); |
| 78 | + core.info(`🔗 Transaction Hash: ${safeTxHash}`); |
| 79 | + core.info(`📋 Transaction Details: ${JSON.stringify(transaction, null, 2)}`); |
| 80 | + |
| 81 | + } catch (error) { |
| 82 | + core.setFailed(`❌ Error creating Safe transaction: ${error.message}`); |
| 83 | + core.error(error.stack); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +run(); |
0 commit comments