Skip to content

@lang:bytes.slice

Extract a byte range from a bytes value.

Returns: bytes

@lang:bytes.slice(value start end?)
NameTypeDescription
valuebytesSource bytes or string value
startnumberStart index (inclusive; negative counts from the end)
[end]numberEnd index (exclusive; negative counts from the end; omitted = to the end)

A byte range of the bytes/string return of a call: the @str.slice! recipe with the Bytes category. The off-chain (start, end) pair converts to the on-chain (start, len) at composition time; negative bounds compile to sub(byteLen(s), k) and resolve against the live byte length at assertion time.

load lang
set $oracle 0x44fA8E6f47987339850636F88629646662444217
# Bytes [1, 3) of the blob
assert @bytes.slice!($oracle::{blob()(bytes)} 1 3) == 0xabcd
# The last four bytes, resolved against the live length
assert @bytes.slice!($oracle::{blob()(bytes)} -4) == 0xdeadbeef
  • An empty or inverted range reverts at assertion time — on-chain slicing has no silent clamp (constant inverted ranges are rejected at build time).
  • assert, @bytes.at!, @str.slice!, @slice!