We are proud to announce the release of our new Scala Forex library, developed by Snowplow wintern Jiawen Zhou. Jiawen joined us in the Snowplow offices in London this winter and was tasked with taking Scala Forex from a README file to an enterprise-strength Scala library for foreign exchange operations. One month later and we are hugely excited to be sharing her work with the community!
Scala Forex is a high-performance Scala library for performing exchange rate lookups and currency conversions, leveraging the excellent Open Exchange Rates web service. We are excited to be working with Open Exchange Rates, Snowplow’s second external data provider after MaxMind.
In Jiawen’s own words:
In the rest of this post we will cover:
Late last year a Snowplow customer asked us if we could add currency conversions into their custom Snowplow implementation. This seemed like a great idea to explore, and we started to sketch out an approach:
- In our Enrichment process, convert all transactions to the Snowplow user’s “base currency”
- In our Storage targets, store both the original transaction value and the value converted to the user’s base currency, for easy revenue analysis
The big missing piece for the above was a Scala library to handle currency conversions. We had some very specific requirements for this library:
- It had to support close to all of the world’s national currencies
- It had to support historical (“end of day”) currency conversions. This is because a Snowplow Enrichment process can be run over many days or months of historical event data
- It had to minimize the number of external web service calls. Running in an environment like Hadoop or Kinesis, we cannot afford to call a web service each time for millions of individual events
Custom building a Scala library calling out to the Open Exchange Rates web service seemed the right way to meet these requirements, and so Scala Forex was born!
Scala Forex makes heavy use of the Joda-Money and Joda-Time libraries in its public API. These are enterprise-grade Java libraries for working with currency, money and time, and we were keen not to re-invent the wheel!
Under the covers, Jiawen’s library makes heavy use of two “least recently used” (LRU) caches, one to hold historic exchange rates and one to hold recent rates. It is these LRU caches which minimize calls out to the Open Exchange Rates web service, which is critical for performance (and cost) reasons.
The library is thoroughly tested using Specs2 (including Specs2 tables), and Mockito to verify cache behaviour. The library is integrated into Travis CI for continuous testing.
Using the library is straightforward: you initialize Scala Forex with general and OER-specific configuration, and then use a simple Scala DSL to both look up exchange rates and perform currency conversions.
For detailed guidance on configuring the library, please see the Configuration section of the Scala Forex README.
Exchange rate lookups
Once initialized, an exchange rate lookup is as simple as:
Here’s a slightly more complex example, of looking up a historic rate:
For detailed help on currency lookups, please see the Rate Lookup section of the Scala Forex README.
A currency conversion can be as simple as:
Here’s a slightly more complex example, of converting a trade using the end of day rate:
For detailed help on currency conversions, please see the Currency Conversion section of the Scala Forex README.
And that’s it; huge thanks to Jiawen Zhou for delivering such a sophisticated library in such a short time - and in a language she had never used before!
We hope that you find the Scala Forex library helpful - and stay tuned for when we can integrate it into Snowplow as part of our planned new currency enrichment capabilities!