How to create date strings using AssemblyScript in The Graph ๐Ÿ“†

A Christmas gift from BlockRocket to the community ๐ŸŽ…

Andy Gray
BlockRocket

--

We have been playing with the amazing blockchain protocol The Graph and plugging in our product KnownOrigin to get lots of lovely stats about crypto-art.

The graph protocol uses AssemblyScript and generates lean and mean WebAssembly modules which is awesome but leads to a few gotchas including not having access (right now) to a fully fledged Date object.

At BlockRocket we are humans and think about our stats in days but we could only create rough day numbers by dividing the timestamp by the number of seconds in day (which leads to inaccuracies due to actual time it takes for our rock ๐ŸŒ to spin around the sun ๐ŸŒž)

So naturally we headed to StackOverflow for answers to copy and paste and we found this:

So Vincent @ BlockRocket had a crack at it in AssemblyScript as it didnโ€™t look too foxing and we managed to get it to work and can now use date string as keys in the graph. Bonus!

Check out that lovely date:

{
"data": {
"days": [
{
"date": "2019-12-19",
"editionsCount": "15",
"highestValueInEth": "0.2",
"salesCount": "79",
"totalValueInEth": "6.0424"
}
]
}
}

Below is the code gist and a reference to the full KnownOrigin subgraph github. Please copy, improve and use in your subgraphs.

Math time magic

Credit to Howard Hinnat ๐Ÿ™ for the real hard work (the clever math bit) and the reference article in C!?

Hope this helps somebody somewhere โ€” Have a great Christmas, BlockRocket โ›„๏ธ

Please let us know if anything can be improved and have a play with the KnownOrigin subgraph

BlockRocket are based in Manchester and we help companies with blockchain and decentralised technology. Get in touch!

Email: hello@blockrocket.tech

Twitter: @blockrockettech

Products we have built include KnownOrigin.io, Nifty Football, BlockCities, and Crypto Kaiju

CryptoArt!

--

--

Andy Gray
BlockRocket
Editor for

Builds stuff with Blockchain. Co-founder of BlockRocket.tech. Part of the KnownOrigin.io team.