From f0734e18a584ee6bdf83166696f6a50e6302b7f2 Mon Sep 17 00:00:00 2001 From: rabi-siddique Date: Wed, 18 Dec 2024 09:41:58 +0500 Subject: [PATCH] refactor: use zx for creating vaults --- .github/actions/createVault/action.yml | 19 +-------- .github/actions/createVault/createVault.mjs | 42 +++++++++++++++++++ package.json | 3 +- yarn.lock | 46 +++++++++++++++++++++ 4 files changed, 92 insertions(+), 18 deletions(-) create mode 100644 .github/actions/createVault/createVault.mjs diff --git a/.github/actions/createVault/action.yml b/.github/actions/createVault/action.yml index 3460914a..1ad3c0ac 100644 --- a/.github/actions/createVault/action.yml +++ b/.github/actions/createVault/action.yml @@ -21,20 +21,5 @@ inputs: required: true runs: - using: 'composite' - steps: - - name: Create Vault - shell: bash - run: | - createVaultCommand="/usr/src/agoric-sdk/packages/agoric-cli/bin/agops vaults open --wantMinted \"${{ inputs.wantMinted }}\" --giveCollateral \"${{ inputs.giveCollateral }}\" > /tmp/want-ist.json" - echo "Executing create vault command in the container..." - - docker exec ${{ inputs.containerName }} /bin/bash -c "env AGORIC_NET=${{ inputs.agoricNet }} timeout ${{ inputs.commandTimeout }} $createVaultCommand" - - - name: Broadcast Offer - shell: bash - run: | - broadcastCommand="/usr/src/agoric-sdk/packages/agoric-cli/bin/agops perf satisfaction --executeOffer /tmp/want-ist.json --from \"${{ inputs.userKey }}\" --keyring-backend=test" - echo "Executing broadcast command in the container..." - - docker exec ${{ inputs.containerName }} /bin/bash -c "env AGORIC_NET=${{ inputs.agoricNet }} timeout ${{ inputs.commandTimeout }} $broadcastCommand" + using: 'node20' + main: 'createVault.mjs' diff --git a/.github/actions/createVault/createVault.mjs b/.github/actions/createVault/createVault.mjs new file mode 100644 index 00000000..e9d92eba --- /dev/null +++ b/.github/actions/createVault/createVault.mjs @@ -0,0 +1,42 @@ +import { $ } from 'zx'; + +/** + * Creates a vault by executing a command in a Docker container. + * + * @param {string} containerName - The name of the Docker container where the command will be executed. + * @param {string} agoricNet - The network configuration for the Agoric CLI. + * @param {string} commandTimeout - The maximum time in seconds for the command to complete. + * @param {string} wantMinted - The amount of ISTs to be minted by the vault. + * @param {string} giveCollateral - The amount of ATOMs to secure the minted currency. + */ +const createVault = async (containerName, agoricNet, commandTimeout, userKey, wantMinted, giveCollateral) => { + console.log('Starting the vault creation process...'); + const agops = '/usr/src/agoric-sdk/packages/agoric-cli/bin/agops'; + + const createVaultCommand = `timeout ${commandTimeout} ${agops} vaults open --wantMinted ${wantMinted} --giveCollateral ${giveCollateral} > /tmp/want-ist.json`; + const executeCreateVaultCommand = `docker exec ${containerName} /bin/bash -c "env AGORIC_NET=${agoricNet} ${createVaultCommand}"`; + + try { + await $`${executeCreateVaultCommand}`; + console.log('Vault created successfully. Executing broadcast command...'); + + const broadcastCommand = `timeout ${commandTimeout} ${agops} perf satisfaction --executeOffer /tmp/want-ist.json --from ${userKey} --keyring-backend=test`; + const executeBroadcastCommand = `docker exec ${containerName} /bin/bash -c "env AGORIC_NET=${agoricNet} ${broadcastCommand}"`; + + await $`${executeBroadcastCommand}`; + console.log('Offer broadcast successfully.'); + } catch (error) { + console.error('Error during vault creation:', error); + process.exit(1); + } +}; + +const containerName = process.env.INPUT_CONTAINERNAME; +const agoricNet = process.env.INPUT_AGORICNET; +const commandTimeout = process.env.INPUT_COMMANDTIMEOUT; +const userKey = process.env.INPUT_USERKEY; +const wantMinted = process.env.INPUT_WANTMINTED; +const giveCollateral = process.env.INPUT_GIVECOLLATERAL; + +console.log('ARGS:', { containerName, agoricNet, commandTimeout, userKey, wantMinted, giveCollateral }); +createVault(containerName, agoricNet, commandTimeout, userKey, wantMinted, giveCollateral); diff --git a/package.json b/package.json index fc659b91..64c0a235 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,8 @@ "@subql/testing": "latest", "prettier": "^3.4.2", "starknet": "6.11.0", - "typescript": "^5.7.2" + "typescript": "^5.7.2", + "zx": "^8.2.4" }, "dependencies": { "@subql/types-cosmos": "^4.0.0", diff --git a/yarn.lock b/yarn.lock index 4c0e2d2d..b2836ce5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3617,6 +3617,16 @@ __metadata: languageName: node linkType: hard +"@types/fs-extra@npm:>=11": + version: 11.0.4 + resolution: "@types/fs-extra@npm:11.0.4" + dependencies: + "@types/jsonfile": "npm:*" + "@types/node": "npm:*" + checksum: 10c0/9e34f9b24ea464f3c0b18c3f8a82aefc36dc524cc720fc2b886e5465abc66486ff4e439ea3fb2c0acebf91f6d3f74e514f9983b1f02d4243706bdbb7511796ad + languageName: node + linkType: hard + "@types/glob@npm:*": version: 8.1.0 resolution: "@types/glob@npm:8.1.0" @@ -3678,6 +3688,15 @@ __metadata: languageName: node linkType: hard +"@types/jsonfile@npm:*": + version: 6.1.4 + resolution: "@types/jsonfile@npm:6.1.4" + dependencies: + "@types/node": "npm:*" + checksum: 10c0/b12d068b021e4078f6ac4441353965769be87acf15326173e2aea9f3bf8ead41bd0ad29421df5bbeb0123ec3fc02eb0a734481d52903704a1454a1845896b9eb + languageName: node + linkType: hard + "@types/keyv@npm:^3.1.1": version: 3.1.4 resolution: "@types/keyv@npm:3.1.4" @@ -3747,6 +3766,15 @@ __metadata: languageName: node linkType: hard +"@types/node@npm:>=20": + version: 22.10.2 + resolution: "@types/node@npm:22.10.2" + dependencies: + undici-types: "npm:~6.20.0" + checksum: 10c0/2c7b71a040f1ef5320938eca8ebc946e6905caa9bbf3d5665d9b3774a8d15ea9fab1582b849a6d28c7fc80756a62c5666bc66b69f42f4d5dafd1ccb193cdb4ac + languageName: node + linkType: hard + "@types/node@npm:^17.0.21": version: 17.0.45 resolution: "@types/node@npm:17.0.45" @@ -4118,6 +4146,7 @@ __metadata: prettier: "npm:^3.4.2" starknet: "npm:6.11.0" typescript: "npm:^5.7.2" + zx: "npm:^8.2.4" languageName: unknown linkType: soft @@ -10679,3 +10708,20 @@ __metadata: checksum: 10c0/71cc2f2bbb537300c3f569e25693d37b3bc91f225cefce251a71c30bc6bb3e7f8e9420ca0eb57f2ac9e492b085b8dfa075fd1e8195c40b83c951dd59c6e4fbf8 languageName: node linkType: hard + +"zx@npm:^8.2.4": + version: 8.2.4 + resolution: "zx@npm:8.2.4" + dependencies: + "@types/fs-extra": "npm:>=11" + "@types/node": "npm:>=20" + dependenciesMeta: + "@types/fs-extra": + optional: true + "@types/node": + optional: true + bin: + zx: build/cli.js + checksum: 10c0/ae60ceef4eaf62695de0a24edab302efc770da10f2f4e79b7b8d50c2e7bee8c4b100e8722c6963a6f4badafe3f00d8d0fd50080c90745bf7bca90203c4aee59b + languageName: node + linkType: hard