Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/olive-pots-brush.md
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion src/decodeResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions src/simulateScript/deno-sandbox/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,6 @@ try {
}) as () => Promise<unknown>
const result = await userScript()

__2___.close()

if (!(result instanceof ArrayBuffer) && !(result instanceof Uint8Array)) {
throw Error('returned value not an ArrayBuffer or Uint8Array')
}
Expand Down Expand Up @@ -495,4 +493,6 @@ try {
},
}),
)
} finally {
__2___.close()
}
18 changes: 18 additions & 0 deletions test/unit/decode_result.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down