Skip to content

Commit 1f2f93e

Browse files
authored
Added new updateAction to replace the old defender-autotask (#159)
1 parent 4ae52c9 commit 1f2f93e

File tree

6 files changed

+98
-94
lines changed

6 files changed

+98
-94
lines changed

Hardhat.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ unset DEFENDER_API_KEY
1616
unset DEFENDER_API_SECRET
1717
export IMPERSONATE=0x39878253374355DBcc15C86458F084fb6f2d6DE7
1818
export DEBUG=origin*
19-
npx hardhat requestWithdraw --amount 300 --network local
19+
yarn hardhat requestWithdraw --amount 300 --network local
2020
```
2121

2222
3. Create a Relayer API key
@@ -30,15 +30,15 @@ unset IMPERSONATE
3030
export DEFENDER_API_KEY=
3131
export DEFENDER_API_SECRET=
3232
export DEBUG=origin*
33-
npx hardhat requestWithdraw --amount 300 --network mainnet
33+
yarn hardhat requestWithdraw --amount 300 --network mainnet
3434
```
3535

3636
5. Delete the Relayer API key
3737

3838
For more information, see the `requestWithdraw` help
3939

4040
```
41-
npx hardhat requestWithdraw --help
41+
yarn hardhat requestWithdraw --help
4242
```
4343

4444
## Claim all claimable OETH withdraw requests
@@ -56,7 +56,7 @@ unset DEFENDER_API_KEY
5656
unset DEFENDER_API_SECRET
5757
export IMPERSONATE=0x39878253374355DBcc15C86458F084fb6f2d6DE7
5858
export DEBUG=origin*
59-
npx hardhat autoClaimWithdraw --network local
59+
yarn hardhat autoClaimWithdraw --network local
6060
```
6161

6262
3. Create a Relayer API key
@@ -70,13 +70,13 @@ unset IMPERSONATE
7070
export DEFENDER_API_KEY=
7171
export DEFENDER_API_SECRET=
7272
export DEBUG=origin*
73-
npx hardhat autoClaimWithdraw --network mainnet
73+
yarn hardhat autoClaimWithdraw --network mainnet
7474
```
7575

7676
5. Delete the Relayer API key
7777

7878
For more information, see the `autoClaimWithdraw` help
7979

8080
```
81-
npx hardhat autoClaimWithdraw --help
81+
yarn hardhat autoClaimWithdraw --help
8282
```

README.md

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -290,61 +290,55 @@ The following will set the Defender Team API key and bundle the Actions code rea
290290
```bash
291291
cd ./src/js/actions
292292

293-
# Export the Defender Team API key. This is different to the Defender Relayer API key.
294-
export API_KEY=
295-
export API_SECRET=
293+
# Set the DEFENDER_TEAM_KEY and DEFENDER_TEAM_SECRET env vars in the .env file
296294

297-
npx rollup -c
295+
yarn rollup -c
298296
```
299297

300298
The following will upload the different Action bundles to Defender.
301299

302300
```bash
303301

304302
# Set the DEBUG environment variable to oeth* for the Defender Action
305-
npx hardhat setActionVars --id 93c010f9-05b5-460f-bd10-1205dd80a7c9
306-
npx hardhat setActionVars --id 563d8d0c-17dc-46d3-8955-e4824864869f
307-
npx hardhat setActionVars --id c010fb76-ea63-409d-9981-69322d27993a
308-
npx hardhat setActionVars --id 127171fd-7b85-497e-8335-fd7907c08386
309-
npx hardhat setActionVars --id 84b5f134-8351-4402-8f6a-fb4376034bc4
310-
npx hardhat setActionVars --id ffcfc580-7b0a-42ed-a4f2-3f0a3add9779 --name ONEINCH_API_KEY # Don't forget to run `export ONEINCH_API_KEY=...` first!
311-
npx hardhat setActionVars --id 32dbc67b-89f3-4856-8f3d-ad4dc5a09322
312-
npx hardhat setActionVars --id 7a0cb2c9-11c2-41dd-bcd0-d7c2dbda6af6
313-
npx hardhat setActionVars --id a9fc4c86-0506-4809-afbc-93b5e558cb68
314-
npx hardhat setActionVars --id 12977d51-d107-45eb-ac20-45942009ab01
315-
npx hardhat setActionVars --id 6ec46510-0b8e-48b4-a4c8-de759aad0ba4
316-
npx hardhat setActionVars --id 6d148f26-54a6-4377-92f2-3148d572eea3 --name ONEINCH_API_KEY # Don't forget to run `export ONEINCH_API_KEY=...` first!
317-
npx hardhat setActionVars --id acfbb7d6-5ea6-4ffc-a758-fa4b4f584dd1 # allocateLido
318-
npx hardhat setActionVars --id 6e26641e-4132-4824-bb80-7c891fd31455 # collectEtherFiFees
319-
npx hardhat setActionVars --id 002c2b0d-9522-4d5f-a340-9713ee43a1c3 # allocateEtherFi
320-
npx hardhat setActionVars --id 062cfee1-c34e-43ae-beb0-de62bc668bbd # autoRequestEtherFiWithdraw
321-
npx hardhat setActionVars --id 6c52f3a9-85d8-4c7f-8aee-90a95b13965c # autoClaimEtherFiWithdraw
322-
323-
# The Defender autotask client uses generic env var names so we'll set them first from the values in the .env file
324-
export API_KEY=
325-
export API_SECRET=
303+
yarn hardhat setActionVars --id 93c010f9-05b5-460f-bd10-1205dd80a7c9
304+
yarn hardhat setActionVars --id 563d8d0c-17dc-46d3-8955-e4824864869f
305+
yarn hardhat setActionVars --id c010fb76-ea63-409d-9981-69322d27993a
306+
yarn hardhat setActionVars --id 127171fd-7b85-497e-8335-fd7907c08386
307+
yarn hardhat setActionVars --id 84b5f134-8351-4402-8f6a-fb4376034bc4
308+
yarn hardhat setActionVars --id ffcfc580-7b0a-42ed-a4f2-3f0a3add9779 --name ONEINCH_API_KEY # Don't forget to run `export ONEINCH_API_KEY=...` first!
309+
yarn hardhat setActionVars --id 32dbc67b-89f3-4856-8f3d-ad4dc5a09322
310+
yarn hardhat setActionVars --id 7a0cb2c9-11c2-41dd-bcd0-d7c2dbda6af6
311+
yarn hardhat setActionVars --id a9fc4c86-0506-4809-afbc-93b5e558cb68
312+
yarn hardhat setActionVars --id 12977d51-d107-45eb-ac20-45942009ab01
313+
yarn hardhat setActionVars --id 6ec46510-0b8e-48b4-a4c8-de759aad0ba4
314+
yarn hardhat setActionVars --id 6d148f26-54a6-4377-92f2-3148d572eea3 --name ONEINCH_API_KEY # Don't forget to run `export ONEINCH_API_KEY=...` first!
315+
yarn hardhat setActionVars --id acfbb7d6-5ea6-4ffc-a758-fa4b4f584dd1 # allocateLido
316+
yarn hardhat setActionVars --id 6e26641e-4132-4824-bb80-7c891fd31455 # collectEtherFiFees
317+
yarn hardhat setActionVars --id 002c2b0d-9522-4d5f-a340-9713ee43a1c3 # allocateEtherFi
318+
yarn hardhat setActionVars --id 062cfee1-c34e-43ae-beb0-de62bc668bbd # autoRequestEtherFiWithdraw
319+
yarn hardhat setActionVars --id 6c52f3a9-85d8-4c7f-8aee-90a95b13965c # autoClaimEtherFiWithdraw
326320

327321
# Mainnet
328-
npx defender-autotask update-code 93c010f9-05b5-460f-bd10-1205dd80a7c9 ./dist/autoRequestWithdraw
329-
npx defender-autotask update-code 563d8d0c-17dc-46d3-8955-e4824864869f ./dist/autoClaimWithdraw
330-
npx defender-autotask update-code c010fb76-ea63-409d-9981-69322d27993a ./dist/autoRequestLidoWithdraw
331-
npx defender-autotask update-code 127171fd-7b85-497e-8335-fd7907c08386 ./dist/autoClaimLidoWithdraw
332-
npx defender-autotask update-code 84b5f134-8351-4402-8f6a-fb4376034bc4 ./dist/collectLidoFees
333-
npx defender-autotask update-code ffcfc580-7b0a-42ed-a4f2-3f0a3add9779 ./dist/setPrices
334-
npx defender-autotask update-code 32dbc67b-89f3-4856-8f3d-ad4dc5a09322 ./dist/collectFeesSonic
335-
npx defender-autotask update-code 7a0cb2c9-11c2-41dd-bcd0-d7c2dbda6af6 ./dist/allocateSonic
336-
npx defender-autotask update-code a9fc4c86-0506-4809-afbc-93b5e558cb68 ./dist/collectRewardsSonic
337-
npx defender-autotask update-code 12977d51-d107-45eb-ac20-45942009ab01 ./dist/autoRequestWithdrawSonic
338-
npx defender-autotask update-code 6ec46510-0b8e-48b4-a4c8-de759aad0ba4 ./dist/autoClaimWithdrawSonic
339-
npx defender-autotask update-code 6d148f26-54a6-4377-92f2-3148d572eea3 ./dist/setOSSiloPriceAction
340-
npx defender-autotask update-code acfbb7d6-5ea6-4ffc-a758-fa4b4f584dd1 ./dist/allocateLido
341-
npx defender-autotask update-code 6e26641e-4132-4824-bb80-7c891fd31455 ./dist/collectEtherFiFees
342-
npx defender-autotask update-code 002c2b0d-9522-4d5f-a340-9713ee43a1c3 ./dist/allocateEtherFi
343-
npx defender-autotask update-code 062cfee1-c34e-43ae-beb0-de62bc668bbd ./dist/autoRequestEtherFiWithdraw
344-
npx defender-autotask update-code 6c52f3a9-85d8-4c7f-8aee-90a95b13965c ./dist/autoClaimEtherFiWithdraw
322+
yarn hardhat updateAction --id 93c010f9-05b5-460f-bd10-1205dd80a7c9 --file autoRequestWithdraw
323+
yarn hardhat updateAction --id 563d8d0c-17dc-46d3-8955-e4824864869f --file autoClaimWithdraw
324+
yarn hardhat updateAction --id c010fb76-ea63-409d-9981-69322d27993a --file autoRequestLidoWithdraw
325+
yarn hardhat updateAction --id 127171fd-7b85-497e-8335-fd7907c08386 --file autoClaimLidoWithdraw
326+
yarn hardhat updateAction --id 84b5f134-8351-4402-8f6a-fb4376034bc4 --file collectLidoFees
327+
yarn hardhat updateAction --id ffcfc580-7b0a-42ed-a4f2-3f0a3add9779 --file setPrices
328+
yarn hardhat updateAction --id 32dbc67b-89f3-4856-8f3d-ad4dc5a09322 --file collectFeesSonic
329+
yarn hardhat updateAction --id 7a0cb2c9-11c2-41dd-bcd0-d7c2dbda6af6 --file allocateSonic
330+
yarn hardhat updateAction --id a9fc4c86-0506-4809-afbc-93b5e558cb68 --file collectRewardsSonic
331+
yarn hardhat updateAction --id 12977d51-d107-45eb-ac20-45942009ab01 --file autoRequestWithdrawSonic
332+
yarn hardhat updateAction --id 6ec46510-0b8e-48b4-a4c8-de759aad0ba4 --file autoClaimWithdrawSonic
333+
yarn hardhat updateAction --id 6d148f26-54a6-4377-92f2-3148d572eea3 --file setOSSiloPriceAction
334+
yarn hardhat updateAction --id acfbb7d6-5ea6-4ffc-a758-fa4b4f584dd1 --file allocateLido
335+
yarn hardhat updateAction --id 6e26641e-4132-4824-bb80-7c891fd31455 --file collectEtherFiFees
336+
yarn hardhat updateAction --id 002c2b0d-9522-4d5f-a340-9713ee43a1c3 --file allocateEtherFi
337+
yarn hardhat updateAction --id 062cfee1-c34e-43ae-beb0-de62bc668bbd --file autoRequestEtherFiWithdraw
338+
yarn hardhat updateAction --id 6c52f3a9-85d8-4c7f-8aee-90a95b13965c --file autoClaimEtherFiWithdraw
345339
```
346340

347-
`rollup` and `defender-autotask` can be installed globally to avoid the `npx` prefix.
341+
`rollup` can be installed globally to avoid the `yarn` prefix.
348342

349343
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.
350344
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.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
"@nomicfoundation/hardhat-ethers": "3.0.7",
2121
"@nomicfoundation/hardhat-foundry": "^1.1.1",
2222
"@nomicfoundation/hardhat-network-helpers": "^1.0.10",
23-
"@openzeppelin/defender-autotask-client": "^1.54.1",
24-
"@openzeppelin/defender-sdk": "1.14.3",
23+
"@openzeppelin/defender-sdk": "2.7.0",
2524
"@rollup/plugin-commonjs": "^25.0.7",
2625
"@rollup/plugin-json": "^6.1.0",
2726
"@rollup/plugin-node-resolve": "^15.2.3",

src/js/tasks/autotask.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/js/tasks/defender.js

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,68 @@
11
require("dotenv").config();
2-
const { AutotaskClient } = require("@openzeppelin/defender-autotask-client");
2+
const path = require("path");
3+
const fs = require("fs");
4+
const JSZip = require("jszip");
5+
const { Defender } = require("@openzeppelin/defender-sdk");
36

47
const log = require("../utils/logger")("task:defender");
58

6-
const setActionVars = async ({ id, name }) => {
9+
const getClient = () => {
710
log(`Used DEFENDER_TEAM_KEY ${process.env.DEFENDER_TEAM_KEY}`);
811
const creds = {
912
apiKey: process.env.DEFENDER_TEAM_KEY,
1013
apiSecret: process.env.DEFENDER_TEAM_SECRET,
1114
};
12-
const client = new AutotaskClient(creds);
15+
return new Defender(creds);
16+
};
17+
18+
const setActionVars = async ({ id, name }) => {
19+
const client = getClient();
1320

1421
const envVars = {};
1522
if (name) {
1623
envVars[name] = process.env[name];
1724
}
1825

1926
// Update Variables
20-
const variables = await client.updateEnvironmentVariables(id, {
21-
...envVars,
22-
DEBUG: "origin*",
27+
const variables = await client.action.updateEnvironmentVariables(id, {
28+
variables: {
29+
...envVars,
30+
DEBUG: "origin*",
31+
},
2332
});
2433
console.log("Updated Defender Actions environment variables to:", variables);
2534
};
2635

36+
const updateAction = async ({ id, file }) => {
37+
const client = getClient();
38+
39+
// Read and zip the code
40+
const distPath = path.join(process.cwd(), `src/js/actions/dist/${file}`);
41+
const zip = new JSZip();
42+
const files = fs.readdirSync(distPath, { recursive: true });
43+
files.forEach((file) => {
44+
const filePath = path.join(distPath, file);
45+
const relativePath = path.relative(distPath, filePath);
46+
const content = fs.readFileSync(filePath);
47+
zip.file(relativePath, content);
48+
});
49+
const encodedZippedCode =
50+
await client.action.getEncodedZippedCodeFromFolder(distPath); // SDK helper to base64-encode the zip
51+
52+
// Fetch the current autotask/action to preserve existing config
53+
const currentAction = await client.action.get(id);
54+
55+
// Update with new code (preserve name, trigger, etc.)
56+
const updateBody = {
57+
...currentAction,
58+
encodedZippedCode,
59+
};
60+
61+
const updated = await client.action.update(updateBody);
62+
console.log("Autotask updated successfully:", updated);
63+
};
64+
2765
module.exports = {
2866
setActionVars,
67+
updateAction,
2968
};

src/js/tasks/tasks.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ const {
77
parseAddress,
88
parseDeployedAddress,
99
} = require("../utils/addressParser");
10-
const { setAutotaskVars } = require("./autotask");
11-
const { setActionVars } = require("./defender");
10+
const { setActionVars, updateAction } = require("./defender");
1211
const {
1312
submitLido,
1413
snapLido,
@@ -279,22 +278,6 @@ task("withdrawStatus").setAction(async (_, __, runSuper) => {
279278
return runSuper();
280279
});
281280

282-
// Defender
283-
subtask(
284-
"setAutotaskVars",
285-
"Set environment variables on Defender Autotasks. eg DEBUG=origin*",
286-
)
287-
.addOptionalParam(
288-
"id",
289-
"Identifier of the Defender Autotask",
290-
"ffcfc580-7b0a-42ed-a4f2-3f0a3add9779",
291-
types.string,
292-
)
293-
.setAction(setAutotaskVars);
294-
task("setAutotaskVars").setAction(async (_, __, runSuper) => {
295-
return runSuper();
296-
});
297-
298281
// Token tasks.
299282
subtask("allowance", "Get the token allowance an owner has given to a spender")
300283
.addParam(
@@ -1209,6 +1192,14 @@ task("setActionVars").setAction(async (_, __, runSuper) => {
12091192
return runSuper();
12101193
});
12111194

1195+
subtask("updateAction", "Upload a Defender Actions")
1196+
.addParam("id", "Identifier of the Defender Actions", undefined, types.string)
1197+
.addParam("file", "Path to the file to upload", undefined, types.string)
1198+
.setAction(updateAction);
1199+
task("updateAction").setAction(async (_, __, runSuper) => {
1200+
return runSuper();
1201+
});
1202+
12121203
// FlyTrade
12131204
subtask("flyTradeQuote", "Get a Fly quote for a swap")
12141205
.addOptionalParam("from", "Token symbol to swap from.", "SILO", types.string)

0 commit comments

Comments
 (0)