From 86b822c16f3f93ea4916f36e6067ce060500ae6c Mon Sep 17 00:00:00 2001 From: Morgan Kuphal Date: Wed, 20 Sep 2023 13:47:26 -0500 Subject: [PATCH 1/2] Minor fixes described in changeset --- .changeset/olive-pots-brush.md | 5 +++++ README.md | 5 +++++ src/decodeResult.ts | 11 ++++++++++- src/simulateScript/deno-sandbox/sandbox.ts | 4 ++-- test/unit/decode_result.test.ts | 18 ++++++++++++++++++ 5 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 .changeset/olive-pots-brush.md diff --git a/.changeset/olive-pots-brush.md b/.changeset/olive-pots-brush.md new file mode 100644 index 0000000..3df6ad7 --- /dev/null +++ b/.changeset/olive-pots-brush.md @@ -0,0 +1,5 @@ +--- +'@chainlink/functions-toolkit': patch +--- + +Support `0x` in decodeResult, fixed simulator shutdown when error is encountered, added explicitly supported versions to README diff --git a/README.md b/README.md index 87ca7c2..5d2b35c 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,10 @@ Up-to-date documentation on Chainlink Functions can be found [here](https://docs > :warning: Chainlink Functions requires signing a terms of service agreement before creating a billing subscription. See this [getting started](https://docs.chain.link/chainlink-functions/getting-started) section in the docs. +# Prerequisites + +Install Node.js version `18.18.0` or higher *and* Deno version `1.36.0` or higher. + # How to use this package The following classes expose functionality one would expect from their name. @@ -23,6 +27,7 @@ The `SubscriptionManager` class is used to manage the Chainlink billing [subscri The typical subscriptions-related operations are +- [Prerequisites](#prerequisites) - [How to use this package](#how-to-use-this-package) - [Functions Billing Subscription Management](#functions-billing-subscription-management) - [Subscription Initialization](#subscription-initialization) diff --git a/src/decodeResult.ts b/src/decodeResult.ts index 9abd9fd..8f82a0f 100644 --- a/src/decodeResult.ts +++ b/src/decodeResult.ts @@ -6,7 +6,7 @@ export const decodeResult = ( resultHexstring: string, expectedReturnType: ReturnType, ): DecodedResult => { - if (!isValidHexadecimal(resultHexstring)) { + if (!isValidHexadecimal(resultHexstring) && resultHexstring.slice(0, 2) !== '0x') { throw Error(`'${resultHexstring}' is not a valid hexadecimal string`) } expectedReturnType = expectedReturnType.toLowerCase() as ReturnType @@ -29,6 +29,9 @@ export const decodeResult = ( `'${resultHexstring}' has '${resultHexBits}' bits which is too large for uint256`, ) } + if (resultHexstring === '0x') { + return BigInt(0) + } decodedOutput = BigInt('0x' + resultHexstring.slice(2).slice(-64)) break case ReturnType.int256: @@ -37,9 +40,15 @@ export const decodeResult = ( `'${resultHexstring}' has '${resultHexBits}' bits which is too large for int256`, ) } + if (resultHexstring === '0x') { + return BigInt(0) + } decodedOutput = signedInt256toBigInt('0x' + resultHexstring.slice(2).slice(-64)) break case ReturnType.string: + if (resultHexstring === '0x') { + return '' + } decodedOutput = Buffer.from(resultHexstring.slice(2), 'hex').toString() break case ReturnType.bytes: diff --git a/src/simulateScript/deno-sandbox/sandbox.ts b/src/simulateScript/deno-sandbox/sandbox.ts index 2984313..80eb5ff 100644 --- a/src/simulateScript/deno-sandbox/sandbox.ts +++ b/src/simulateScript/deno-sandbox/sandbox.ts @@ -452,8 +452,6 @@ try { }) as () => Promise const result = await userScript() - __2___.close() - if (!(result instanceof ArrayBuffer) && !(result instanceof Uint8Array)) { throw Error('returned value not an ArrayBuffer or Uint8Array') } @@ -495,4 +493,6 @@ try { }, }), ) +} finally { + __2___.close() } diff --git a/test/unit/decode_result.test.ts b/test/unit/decode_result.test.ts index c0c6b19..40bc314 100644 --- a/test/unit/decode_result.test.ts +++ b/test/unit/decode_result.test.ts @@ -2,6 +2,24 @@ import { decodeResult, ReturnType } from '../../src/index' describe('decodeResult', () => { it.each([ + { + result: '0x', + expectedDataType: ReturnType.string, + decodedResult: '', + label: 'decodes empty string', + }, + { + result: '0x', + expectedDataType: ReturnType.uint256, + decodedResult: BigInt(0), + label: 'decodes empty uint256', + }, + { + result: '0x', + expectedDataType: ReturnType.int256, + decodedResult: BigInt(0), + label: 'decodes empty int256', + }, { result: '0x48656c6c6f2c20576f726c6421', expectedDataType: ReturnType.string, From 87fcec1f4b8568d72826ca997e6714ac2ed4fc98 Mon Sep 17 00:00:00 2001 From: Morgan Kuphal Date: Wed, 20 Sep 2023 13:49:44 -0500 Subject: [PATCH 2/2] lint --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5d2b35c..f28e8fb 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Up-to-date documentation on Chainlink Functions can be found [here](https://docs # Prerequisites -Install Node.js version `18.18.0` or higher *and* Deno version `1.36.0` or higher. +Install Node.js version `18.18.0` or higher _and_ Deno version `1.36.0` or higher. # How to use this package