smartcontractkit / smartcontractkit/chainlink
[FEAT] Length task to return the length of a JSON Array
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 8.2k
- Forks
- 2k
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 202
Description
Description
Currently the Length Task only returns the length of strings or byte arrays. It would be very valuable if the task also supported JSON Arrays.
length_myvar [type="length" input="$(myvar)"] // Being 'myvar' a JSON Array, e.g. [1, 2, 3]
Motivation
I have a TOML spec that requires first to read from a mapping(uint256 => uint256[]) (via ethcall) and ideally make the job fail fast if the uint256[] is empty.
Testing Consumer on ETH Goerli:
https://goerli.etherscan.io/address/0xB056566a48F29cDEFbAF60Ac31C75370e5a39623#readContract#F1
Testing TOML spec:
NB: _sportId: 1 will output [] at the end of the job run, whilst _sportId: 18 will output [3, 11].
type = "webhook"
schemaVersion = 1
name = "Test Get Bookmaker IDs"
observationSource = """
encode_bookmakerids_var_call [type="ethabiencode" abi="getBookmakerIdsBySportId(uint256 _sportId)" data=<{ "_sportId": 1}>]
call_bookmakerids_var [
type="ethcall"
contract="0xB056566a48F29cDEFbAF60Ac31C75370e5a39623"
data="$(encode_bookmakerids_var_call)"
extractRevertReason=true
]
decode_bookmakerids [type="ethabidecode" abi="uint256[] bookmakerIds" data="$(call_bookmakerids_var)"]
encode_bookmakerids_var_call -> call_bookmakerids_var -> decode_bookmakerids
"""
For sportId = 1 the decode_bookmakerids output is []. If length accepted JSON Arrays I could use the lessthan and conditional tasks to make it fail fast, e.g.
type = "webhook"
schemaVersion = 1
name = "Test Get Bookmaker IDs - If length supported JSON Arrays"
observationSource = """
encode_bookmakerids_var_call [type="ethabiencode" abi="getBookmakerIdsBySportId(uint256 _sportId)" data=<{ "_sportId": 1}>]
call_bookmakerids_var [
type="ethcall"
contract="0xB056566a48F29cDEFbAF60Ac31C75370e5a39623"
data="$(encode_bookmakerids_var_call)"
extractRevertReason=true
]
decode_bookmakerids [type="ethabidecode" abi="uint256[] bookmakerIds" data="$(call_bookmakerids_var)"]
length_bookmakerids [type="length" input=$(decode_bookmakerids.bookmakerIds)]
is_bookmakerids_empty [type="lessthan" left="$(length_bookmakerids)" right=1]
// NB: revert before going further executing tasks, e.g. requesting the bridge
revert_if_empty [type="conditional" data="$(is_bookmakerids_empty)"]
encode_bookmakerids_var_call -> call_bookmakerids_var -> decode_bookmakerids -> length_bookmakerids -> is_bookmakerids_empty -> revert_if_empty
"""
Justification
Increase the offchain computation capabilities without having recurr to any of the following strategies:
- EA-based: either there is an endpoint that calculates the length or the length of N JSON result properties are part of the EA result.
- Contract-based: there is a callable method (either
pureorview) that gets/calculates lengths on-demand.
Mroeover, I've been recently using mode and length could be handy to calculate the length of the results.
On a similar topic I'd like to request support JSON Arrays as $(my_array_var) on any built-in task with the parameter values, e.g. mean, median, mode, sum, etc. It would then be possible to use them with on-chain arrays (of unknown size/items) via ethcall.
Thanks as always
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the Length Task implementation and its existing handling for strings and byte arrays. Add support for JSON Arrays, including empty and non-empty arrays, and verify that the provided examples produce the expected lengths so downstream lessthan and conditional tasks can use them.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, json
- Domain
- backend, blockchain
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100