Lakshya Profile picture
Trying to meet my daily protein needs. Exploring ONDC 👀 I also play chess 👍

Sep 20, 2022, 7 tweets

The curious case of using offset values with data slice property 🤔 #Solana #Day8 #offset

dataSlice is used to literally slice 🔪 the returned data buffer.

Let's say you have a dataBuffer in which the first 8bytes are storing data you don't need. So, we can slice that from all the data and then carry out operations on the remaining data. Optimized much? 🤌

Now, the no. of bytes, to slice from the beginning, is defined as an 'offset' value inside of it.

Or

using offset value in dataSlice defines how much bytes to slice, in the returned buffer.

However, when you specify the offset value within the filters property, you are specifying how much bytes to skip before matching with the query data (mentioned in bytes)

Therefore, it doesn't affect the returned data buffer in terms of byte size.

This is an easy concept, but can be quite confusing for beginners or if someone doesn't know about the underlying concept behind using dataSlice and filters property.

Also, TIL that Borsh uses the the first 4bytes of any dynamic field to store the field's length in bytes.

(That's why started the slice from 4bytes to access the correct data in the snippet 👇)

Incase one doesn't accounts for these bytes, It may result in faulty calculations.

Implemented Paging, Ordering and Searching Functionality in my project. This hands on practice post every tutorial helps me in understanding the concepts better. Calling it a day 🫡.

Can't wait to start the next phase.

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling