|
| 1 | +import Safe from "@safe-global/protocol-kit"; |
| 2 | +import { |
| 3 | + Address, |
| 4 | + ConsoleKit, |
| 5 | + UpdateAutomationParams, |
| 6 | + VendorCancelAutomationParams |
| 7 | +} from "brahma-console-kit"; |
| 8 | +import { ethers, Wallet } from "ethers"; |
| 9 | +import { encodeMulti } from "ethers-multisend"; |
| 10 | +import { encodePacked } from "viem"; |
| 11 | +import { SafeABI } from "./entity/abi"; |
| 12 | + |
| 13 | +const OwnerEoaPK = process.env.USER_EOA_PRIVATE_KEY!; /// This user must have enough tokens to execute automation update |
| 14 | +const JsonRpcUrl = process.env.JSON_RPC_URL!; |
| 15 | +const ConsoleApiKey = process.env.CONSOLE_API_KEY!; |
| 16 | +const ConsoleBaseUrl = process.env.CONSOLE_BASE_URL!; |
| 17 | + |
| 18 | +const SUBACCOUNT_ADDRESS: Address = "0x"; // your automation subaccount address |
| 19 | +const CONSOLE_ADDRESS: Address = "0x"; // your owner console address |
| 20 | + |
| 21 | +const AutomationCancelData = { |
| 22 | + execViaSubAcc: [] /** |
| 23 | + array of arbitrary txns to execute via subAccount of type- |
| 24 | + { |
| 25 | + "to": "target Address", |
| 26 | + "value": "0", |
| 27 | + "data": "0xcallData" |
| 28 | + }[] |
| 29 | + */, |
| 30 | + sweepTokens: [] // array of tokens to transfer out of subAccount |
| 31 | +}; |
| 32 | + |
| 33 | +const cancelAutomation = async ( |
| 34 | + _consoleKit: ConsoleKit, |
| 35 | + _rpcUrl: string, |
| 36 | + _automationCancelParams: VendorCancelAutomationParams, |
| 37 | + _ownerEoa: Wallet, |
| 38 | + _consoleAddress: Address |
| 39 | +) => { |
| 40 | + try { |
| 41 | + const { |
| 42 | + data: { transactions } |
| 43 | + } = await _consoleKit.automationContext.cancelAutomation( |
| 44 | + _automationCancelParams |
| 45 | + ); |
| 46 | + console.log("[cancel-txns]", { transactions }); |
| 47 | + |
| 48 | + const safe = await Safe.init({ |
| 49 | + provider: _rpcUrl, |
| 50 | + safeAddress: _consoleAddress |
| 51 | + }); |
| 52 | + const cancelTx = await safe.createTransaction({ |
| 53 | + transactions: transactions, |
| 54 | + onlyCalls: false |
| 55 | + }); // safe sdk can also alternatively be used to create & propose tx: https://docs.safe.global/reference-sdk-protocol-kit/transactions/createtransaction |
| 56 | + console.log("[safe-cancel-tx]", { cancelTx }); |
| 57 | + |
| 58 | + const { |
| 59 | + baseGas, |
| 60 | + data, |
| 61 | + gasPrice, |
| 62 | + gasToken, |
| 63 | + operation, |
| 64 | + refundReceiver, |
| 65 | + safeTxGas, |
| 66 | + to, |
| 67 | + value |
| 68 | + } = cancelTx.data; |
| 69 | + const signature = encodePacked( |
| 70 | + ["bytes12", "address", "bytes32", "bytes1"], |
| 71 | + [ |
| 72 | + "0x000000000000000000000000", |
| 73 | + _ownerEoa.address as Address, |
| 74 | + "0x0000000000000000000000000000000000000000000000000000000000000000", |
| 75 | + "0x01" |
| 76 | + ] |
| 77 | + ); |
| 78 | + |
| 79 | + const safeContract = new ethers.Contract( |
| 80 | + _consoleAddress, |
| 81 | + SafeABI, |
| 82 | + _ownerEoa |
| 83 | + ); |
| 84 | + const tx = await _ownerEoa.sendTransaction({ |
| 85 | + to: await safeContract.getAddress(), |
| 86 | + value: 0, |
| 87 | + data: safeContract.interface.encodeFunctionData("execTransaction", [ |
| 88 | + to, |
| 89 | + value, |
| 90 | + data, |
| 91 | + operation, |
| 92 | + safeTxGas, |
| 93 | + baseGas, |
| 94 | + gasPrice, |
| 95 | + gasToken, |
| 96 | + refundReceiver, |
| 97 | + signature |
| 98 | + ]) |
| 99 | + }); |
| 100 | + await tx.wait(2); |
| 101 | + |
| 102 | + console.log("[automation-cancelled]", tx.hash); |
| 103 | + } catch (e) { |
| 104 | + console.log("an error occured", e); |
| 105 | + } |
| 106 | +}; |
| 107 | + |
| 108 | +(async () => { |
| 109 | + const consoleKit = new ConsoleKit(ConsoleApiKey, ConsoleBaseUrl); |
| 110 | + |
| 111 | + const provider = new ethers.JsonRpcProvider(JsonRpcUrl); |
| 112 | + const ownerWallet = new ethers.Wallet(OwnerEoaPK, provider); |
| 113 | + |
| 114 | + const { chainId: chainIdBig } = await provider.getNetwork(); |
| 115 | + const chainId = parseInt(chainIdBig.toString(), 10); |
| 116 | + |
| 117 | + const automationCancelData: VendorCancelAutomationParams = { |
| 118 | + chainId, |
| 119 | + data: { |
| 120 | + execViaSubAcc: AutomationCancelData.execViaSubAcc, |
| 121 | + sweepTokens: AutomationCancelData.sweepTokens, |
| 122 | + ownerConsole: CONSOLE_ADDRESS |
| 123 | + }, |
| 124 | + subAccountAddress: SUBACCOUNT_ADDRESS |
| 125 | + }; |
| 126 | + |
| 127 | + await cancelAutomation( |
| 128 | + consoleKit, |
| 129 | + JsonRpcUrl, |
| 130 | + automationCancelData, |
| 131 | + ownerWallet, |
| 132 | + CONSOLE_ADDRESS |
| 133 | + ); |
| 134 | +})(); |
0 commit comments