diff --git a/Hardhat.md b/Hardhat.md index c463aae8..ddce79e7 100644 --- a/Hardhat.md +++ b/Hardhat.md @@ -16,7 +16,7 @@ unset DEFENDER_API_KEY unset DEFENDER_API_SECRET export IMPERSONATE=0x39878253374355DBcc15C86458F084fb6f2d6DE7 export DEBUG=origin* -npx hardhat requestWithdraw --amount 300 --network local +yarn hardhat requestWithdraw --amount 300 --network local ``` 3. Create a Relayer API key @@ -30,7 +30,7 @@ unset IMPERSONATE export DEFENDER_API_KEY= export DEFENDER_API_SECRET= export DEBUG=origin* -npx hardhat requestWithdraw --amount 300 --network mainnet +yarn hardhat requestWithdraw --amount 300 --network mainnet ``` 5. Delete the Relayer API key @@ -38,7 +38,7 @@ npx hardhat requestWithdraw --amount 300 --network mainnet For more information, see the `requestWithdraw` help ``` -npx hardhat requestWithdraw --help +yarn hardhat requestWithdraw --help ``` ## Claim all claimable OETH withdraw requests @@ -56,7 +56,7 @@ unset DEFENDER_API_KEY unset DEFENDER_API_SECRET export IMPERSONATE=0x39878253374355DBcc15C86458F084fb6f2d6DE7 export DEBUG=origin* -npx hardhat autoClaimWithdraw --network local +yarn hardhat autoClaimWithdraw --network local ``` 3. Create a Relayer API key @@ -70,7 +70,7 @@ unset IMPERSONATE export DEFENDER_API_KEY= export DEFENDER_API_SECRET= export DEBUG=origin* -npx hardhat autoClaimWithdraw --network mainnet +yarn hardhat autoClaimWithdraw --network mainnet ``` 5. Delete the Relayer API key @@ -78,5 +78,5 @@ npx hardhat autoClaimWithdraw --network mainnet For more information, see the `autoClaimWithdraw` help ``` -npx hardhat autoClaimWithdraw --help +yarn hardhat autoClaimWithdraw --help ``` diff --git a/README.md b/README.md index bbdf1427..f16c51c0 100644 --- a/README.md +++ b/README.md @@ -290,11 +290,9 @@ The following will set the Defender Team API key and bundle the Actions code rea ```bash cd ./src/js/actions -# Export the Defender Team API key. This is different to the Defender Relayer API key. -export API_KEY= -export API_SECRET= +# Set the DEFENDER_TEAM_KEY and DEFENDER_TEAM_SECRET env vars in the .env file -npx rollup -c +yarn rollup -c ``` The following will upload the different Action bundles to Defender. @@ -302,49 +300,45 @@ The following will upload the different Action bundles to Defender. ```bash # Set the DEBUG environment variable to oeth* for the Defender Action -npx hardhat setActionVars --id 93c010f9-05b5-460f-bd10-1205dd80a7c9 -npx hardhat setActionVars --id 563d8d0c-17dc-46d3-8955-e4824864869f -npx hardhat setActionVars --id c010fb76-ea63-409d-9981-69322d27993a -npx hardhat setActionVars --id 127171fd-7b85-497e-8335-fd7907c08386 -npx hardhat setActionVars --id 84b5f134-8351-4402-8f6a-fb4376034bc4 -npx hardhat setActionVars --id ffcfc580-7b0a-42ed-a4f2-3f0a3add9779 --name ONEINCH_API_KEY # Don't forget to run `export ONEINCH_API_KEY=...` first! -npx hardhat setActionVars --id 32dbc67b-89f3-4856-8f3d-ad4dc5a09322 -npx hardhat setActionVars --id 7a0cb2c9-11c2-41dd-bcd0-d7c2dbda6af6 -npx hardhat setActionVars --id a9fc4c86-0506-4809-afbc-93b5e558cb68 -npx hardhat setActionVars --id 12977d51-d107-45eb-ac20-45942009ab01 -npx hardhat setActionVars --id 6ec46510-0b8e-48b4-a4c8-de759aad0ba4 -npx hardhat setActionVars --id 6d148f26-54a6-4377-92f2-3148d572eea3 --name ONEINCH_API_KEY # Don't forget to run `export ONEINCH_API_KEY=...` first! -npx hardhat setActionVars --id acfbb7d6-5ea6-4ffc-a758-fa4b4f584dd1 # allocateLido -npx hardhat setActionVars --id 6e26641e-4132-4824-bb80-7c891fd31455 # collectEtherFiFees -npx hardhat setActionVars --id 002c2b0d-9522-4d5f-a340-9713ee43a1c3 # allocateEtherFi -npx hardhat setActionVars --id 062cfee1-c34e-43ae-beb0-de62bc668bbd # autoRequestEtherFiWithdraw -npx hardhat setActionVars --id 6c52f3a9-85d8-4c7f-8aee-90a95b13965c # autoClaimEtherFiWithdraw - -# The Defender autotask client uses generic env var names so we'll set them first from the values in the .env file -export API_KEY= -export API_SECRET= +yarn hardhat setActionVars --id 93c010f9-05b5-460f-bd10-1205dd80a7c9 +yarn hardhat setActionVars --id 563d8d0c-17dc-46d3-8955-e4824864869f +yarn hardhat setActionVars --id c010fb76-ea63-409d-9981-69322d27993a +yarn hardhat setActionVars --id 127171fd-7b85-497e-8335-fd7907c08386 +yarn hardhat setActionVars --id 84b5f134-8351-4402-8f6a-fb4376034bc4 +yarn hardhat setActionVars --id ffcfc580-7b0a-42ed-a4f2-3f0a3add9779 --name ONEINCH_API_KEY # Don't forget to run `export ONEINCH_API_KEY=...` first! +yarn hardhat setActionVars --id 32dbc67b-89f3-4856-8f3d-ad4dc5a09322 +yarn hardhat setActionVars --id 7a0cb2c9-11c2-41dd-bcd0-d7c2dbda6af6 +yarn hardhat setActionVars --id a9fc4c86-0506-4809-afbc-93b5e558cb68 +yarn hardhat setActionVars --id 12977d51-d107-45eb-ac20-45942009ab01 +yarn hardhat setActionVars --id 6ec46510-0b8e-48b4-a4c8-de759aad0ba4 +yarn hardhat setActionVars --id 6d148f26-54a6-4377-92f2-3148d572eea3 --name ONEINCH_API_KEY # Don't forget to run `export ONEINCH_API_KEY=...` first! +yarn hardhat setActionVars --id acfbb7d6-5ea6-4ffc-a758-fa4b4f584dd1 # allocateLido +yarn hardhat setActionVars --id 6e26641e-4132-4824-bb80-7c891fd31455 # collectEtherFiFees +yarn hardhat setActionVars --id 002c2b0d-9522-4d5f-a340-9713ee43a1c3 # allocateEtherFi +yarn hardhat setActionVars --id 062cfee1-c34e-43ae-beb0-de62bc668bbd # autoRequestEtherFiWithdraw +yarn hardhat setActionVars --id 6c52f3a9-85d8-4c7f-8aee-90a95b13965c # autoClaimEtherFiWithdraw # Mainnet -npx defender-autotask update-code 93c010f9-05b5-460f-bd10-1205dd80a7c9 ./dist/autoRequestWithdraw -npx defender-autotask update-code 563d8d0c-17dc-46d3-8955-e4824864869f ./dist/autoClaimWithdraw -npx defender-autotask update-code c010fb76-ea63-409d-9981-69322d27993a ./dist/autoRequestLidoWithdraw -npx defender-autotask update-code 127171fd-7b85-497e-8335-fd7907c08386 ./dist/autoClaimLidoWithdraw -npx defender-autotask update-code 84b5f134-8351-4402-8f6a-fb4376034bc4 ./dist/collectLidoFees -npx defender-autotask update-code ffcfc580-7b0a-42ed-a4f2-3f0a3add9779 ./dist/setPrices -npx defender-autotask update-code 32dbc67b-89f3-4856-8f3d-ad4dc5a09322 ./dist/collectFeesSonic -npx defender-autotask update-code 7a0cb2c9-11c2-41dd-bcd0-d7c2dbda6af6 ./dist/allocateSonic -npx defender-autotask update-code a9fc4c86-0506-4809-afbc-93b5e558cb68 ./dist/collectRewardsSonic -npx defender-autotask update-code 12977d51-d107-45eb-ac20-45942009ab01 ./dist/autoRequestWithdrawSonic -npx defender-autotask update-code 6ec46510-0b8e-48b4-a4c8-de759aad0ba4 ./dist/autoClaimWithdrawSonic -npx defender-autotask update-code 6d148f26-54a6-4377-92f2-3148d572eea3 ./dist/setOSSiloPriceAction -npx defender-autotask update-code acfbb7d6-5ea6-4ffc-a758-fa4b4f584dd1 ./dist/allocateLido -npx defender-autotask update-code 6e26641e-4132-4824-bb80-7c891fd31455 ./dist/collectEtherFiFees -npx defender-autotask update-code 002c2b0d-9522-4d5f-a340-9713ee43a1c3 ./dist/allocateEtherFi -npx defender-autotask update-code 062cfee1-c34e-43ae-beb0-de62bc668bbd ./dist/autoRequestEtherFiWithdraw -npx defender-autotask update-code 6c52f3a9-85d8-4c7f-8aee-90a95b13965c ./dist/autoClaimEtherFiWithdraw +yarn hardhat updateAction --id 93c010f9-05b5-460f-bd10-1205dd80a7c9 --file autoRequestWithdraw +yarn hardhat updateAction --id 563d8d0c-17dc-46d3-8955-e4824864869f --file autoClaimWithdraw +yarn hardhat updateAction --id c010fb76-ea63-409d-9981-69322d27993a --file autoRequestLidoWithdraw +yarn hardhat updateAction --id 127171fd-7b85-497e-8335-fd7907c08386 --file autoClaimLidoWithdraw +yarn hardhat updateAction --id 84b5f134-8351-4402-8f6a-fb4376034bc4 --file collectLidoFees +yarn hardhat updateAction --id ffcfc580-7b0a-42ed-a4f2-3f0a3add9779 --file setPrices +yarn hardhat updateAction --id 32dbc67b-89f3-4856-8f3d-ad4dc5a09322 --file collectFeesSonic +yarn hardhat updateAction --id 7a0cb2c9-11c2-41dd-bcd0-d7c2dbda6af6 --file allocateSonic +yarn hardhat updateAction --id a9fc4c86-0506-4809-afbc-93b5e558cb68 --file collectRewardsSonic +yarn hardhat updateAction --id 12977d51-d107-45eb-ac20-45942009ab01 --file autoRequestWithdrawSonic +yarn hardhat updateAction --id 6ec46510-0b8e-48b4-a4c8-de759aad0ba4 --file autoClaimWithdrawSonic +yarn hardhat updateAction --id 6d148f26-54a6-4377-92f2-3148d572eea3 --file setOSSiloPriceAction +yarn hardhat updateAction --id acfbb7d6-5ea6-4ffc-a758-fa4b4f584dd1 --file allocateLido +yarn hardhat updateAction --id 6e26641e-4132-4824-bb80-7c891fd31455 --file collectEtherFiFees +yarn hardhat updateAction --id 002c2b0d-9522-4d5f-a340-9713ee43a1c3 --file allocateEtherFi +yarn hardhat updateAction --id 062cfee1-c34e-43ae-beb0-de62bc668bbd --file autoRequestEtherFiWithdraw +yarn hardhat updateAction --id 6c52f3a9-85d8-4c7f-8aee-90a95b13965c --file autoClaimEtherFiWithdraw ``` -`rollup` and `defender-autotask` can be installed globally to avoid the `npx` prefix. +`rollup` can be installed globally to avoid the `yarn` prefix. The Defender Actions need to be under 5MB in size. The [rollup-plugin-visualizer](https://www.npmjs.com/package/rollup-plugin-visualizer) can be used to visualize the size of an Action's dependencies. A `stats.html` file is generated in the`src/js/actions` folder that can be opened in a browser to see the size of the Action's dependencies. diff --git a/package.json b/package.json index 8264af88..f5e009b7 100644 --- a/package.json +++ b/package.json @@ -20,8 +20,7 @@ "@nomicfoundation/hardhat-ethers": "3.0.7", "@nomicfoundation/hardhat-foundry": "^1.1.1", "@nomicfoundation/hardhat-network-helpers": "^1.0.10", - "@openzeppelin/defender-autotask-client": "^1.54.1", - "@openzeppelin/defender-sdk": "1.14.3", + "@openzeppelin/defender-sdk": "2.7.0", "@rollup/plugin-commonjs": "^25.0.7", "@rollup/plugin-json": "^6.1.0", "@rollup/plugin-node-resolve": "^15.2.3", diff --git a/src/js/tasks/autotask.js b/src/js/tasks/autotask.js deleted file mode 100644 index 71df4111..00000000 --- a/src/js/tasks/autotask.js +++ /dev/null @@ -1,19 +0,0 @@ -const { AutotaskClient } = require("@openzeppelin/defender-autotask-client"); - -const setAutotaskVars = async (options) => { - const creds = { - apiKey: process.env.API_KEY, - apiSecret: process.env.API_SECRET, - }; - const client = new AutotaskClient(creds); - - // Update Variables - const variables = await client.updateEnvironmentVariables(options.id, { - DEBUG: "origin*", - }); - console.log("updated Autotask environment variables", variables); -}; - -module.exports = { - setAutotaskVars, -}; diff --git a/src/js/tasks/defender.js b/src/js/tasks/defender.js index 333555f7..be9caebd 100644 --- a/src/js/tasks/defender.js +++ b/src/js/tasks/defender.js @@ -1,15 +1,22 @@ require("dotenv").config(); -const { AutotaskClient } = require("@openzeppelin/defender-autotask-client"); +const path = require("path"); +const fs = require("fs"); +const JSZip = require("jszip"); +const { Defender } = require("@openzeppelin/defender-sdk"); const log = require("../utils/logger")("task:defender"); -const setActionVars = async ({ id, name }) => { +const getClient = () => { log(`Used DEFENDER_TEAM_KEY ${process.env.DEFENDER_TEAM_KEY}`); const creds = { apiKey: process.env.DEFENDER_TEAM_KEY, apiSecret: process.env.DEFENDER_TEAM_SECRET, }; - const client = new AutotaskClient(creds); + return new Defender(creds); +}; + +const setActionVars = async ({ id, name }) => { + const client = getClient(); const envVars = {}; if (name) { @@ -17,13 +24,45 @@ const setActionVars = async ({ id, name }) => { } // Update Variables - const variables = await client.updateEnvironmentVariables(id, { - ...envVars, - DEBUG: "origin*", + const variables = await client.action.updateEnvironmentVariables(id, { + variables: { + ...envVars, + DEBUG: "origin*", + }, }); console.log("Updated Defender Actions environment variables to:", variables); }; +const updateAction = async ({ id, file }) => { + const client = getClient(); + + // Read and zip the code + const distPath = path.join(process.cwd(), `src/js/actions/dist/${file}`); + const zip = new JSZip(); + const files = fs.readdirSync(distPath, { recursive: true }); + files.forEach((file) => { + const filePath = path.join(distPath, file); + const relativePath = path.relative(distPath, filePath); + const content = fs.readFileSync(filePath); + zip.file(relativePath, content); + }); + const encodedZippedCode = + await client.action.getEncodedZippedCodeFromFolder(distPath); // SDK helper to base64-encode the zip + + // Fetch the current autotask/action to preserve existing config + const currentAction = await client.action.get(id); + + // Update with new code (preserve name, trigger, etc.) + const updateBody = { + ...currentAction, + encodedZippedCode, + }; + + const updated = await client.action.update(updateBody); + console.log("Autotask updated successfully:", updated); +}; + module.exports = { setActionVars, + updateAction, }; diff --git a/src/js/tasks/tasks.js b/src/js/tasks/tasks.js index fda77683..a5d0c85f 100644 --- a/src/js/tasks/tasks.js +++ b/src/js/tasks/tasks.js @@ -7,8 +7,7 @@ const { parseAddress, parseDeployedAddress, } = require("../utils/addressParser"); -const { setAutotaskVars } = require("./autotask"); -const { setActionVars } = require("./defender"); +const { setActionVars, updateAction } = require("./defender"); const { submitLido, snapLido, @@ -279,22 +278,6 @@ task("withdrawStatus").setAction(async (_, __, runSuper) => { return runSuper(); }); -// Defender -subtask( - "setAutotaskVars", - "Set environment variables on Defender Autotasks. eg DEBUG=origin*", -) - .addOptionalParam( - "id", - "Identifier of the Defender Autotask", - "ffcfc580-7b0a-42ed-a4f2-3f0a3add9779", - types.string, - ) - .setAction(setAutotaskVars); -task("setAutotaskVars").setAction(async (_, __, runSuper) => { - return runSuper(); -}); - // Token tasks. subtask("allowance", "Get the token allowance an owner has given to a spender") .addParam( @@ -1209,6 +1192,14 @@ task("setActionVars").setAction(async (_, __, runSuper) => { return runSuper(); }); +subtask("updateAction", "Upload a Defender Actions") + .addParam("id", "Identifier of the Defender Actions", undefined, types.string) + .addParam("file", "Path to the file to upload", undefined, types.string) + .setAction(updateAction); +task("updateAction").setAction(async (_, __, runSuper) => { + return runSuper(); +}); + // FlyTrade subtask("flyTradeQuote", "Get a Fly quote for a swap") .addOptionalParam("from", "Token symbol to swap from.", "SILO", types.string)