From f6a54843743b9e8904cbb297f650d576cf51a27a Mon Sep 17 00:00:00 2001 From: Alejandro Date: Mon, 16 Sep 2024 13:51:17 -0300 Subject: [PATCH] Add hex util --- packages/ethernaut-util/README.md | 1 + packages/ethernaut-util/src/tasks/hex.js | 13 +++++++++++++ packages/ethernaut-util/test/tasks/hex.test.js | 10 ++++++++++ 3 files changed, 24 insertions(+) create mode 100644 packages/ethernaut-util/src/tasks/hex.js create mode 100644 packages/ethernaut-util/test/tasks/hex.test.js diff --git a/packages/ethernaut-util/README.md b/packages/ethernaut-util/README.md index 38b43a40..42cc35bd 100644 --- a/packages/ethernaut-util/README.md +++ b/packages/ethernaut-util/README.md @@ -41,6 +41,7 @@ This plugin adds the tasks listed below. - unit Converts between different units of Ether - gas Fetch gas info on the current network - chain Finds a network name from a chain ID, or vice versa +- hex Converts integers to hex ## Environment extensions diff --git a/packages/ethernaut-util/src/tasks/hex.js b/packages/ethernaut-util/src/tasks/hex.js new file mode 100644 index 00000000..b6828539 --- /dev/null +++ b/packages/ethernaut-util/src/tasks/hex.js @@ -0,0 +1,13 @@ +const types = require('ethernaut-common/src/validation/types') +const output = require('ethernaut-common/src/ui/output') + +require('../scopes/util') + .task('hex', 'Converts integers to hex') + .addPositionalParam('value', 'The value to convert.', undefined, types.int) + .setAction(async ({ value }, hre) => { + try { + return output.resultBox(hre.ethers.toQuantity(value)) + } catch (err) { + return output.errorBox(err) + } + }) diff --git a/packages/ethernaut-util/test/tasks/hex.test.js b/packages/ethernaut-util/test/tasks/hex.test.js new file mode 100644 index 00000000..0bf37357 --- /dev/null +++ b/packages/ethernaut-util/test/tasks/hex.test.js @@ -0,0 +1,10 @@ +const assert = require('assert') + +describe('hex', function () { + it('converts "42" to hex', async function () { + assert.equal( + await hre.run({ scope: 'util', task: 'hex' }, { value: '42' }), + '0x2a', + ) + }) +})