xpring-eng / XpringKit

XpringKit provides a Swift SDK for interacting with Xpring Protocols (XRP/PayID/ILP). This library is deprecated.
https://xrpl.org/
MIT License
26 stars 14 forks source link

Adds `xrpToDrops` and `dropsToXrp` to XRP/Utils #203

Closed amiecorso closed 4 years ago

amiecorso commented 4 years ago

High Level Overview of Change

This PR adds the utility functions xrpToDrops and dropsToXrp to XrpingKit/XRP/Utils. These utility methods originally existed in ripple-lib, which is a robust and well-tested implementation that can handle either string or numeric values. Their implementation here is a translation into Swift. Note, however, that the Swift (and Java) methods currently only handle String arguments (whereas the JS implementation also handles BigNumber and Number types). Test cases are also based on the cases included in ripple-lib and added to Tests/UtilsTest.

The logic in the conversions begins with a regex to determine whether the argument is a well-formed number, and includes additional checks such as (in dropsToXrp) that any drops amount submitted as a decimal is actually a whole number (i.e. 5.00, 0.0 etc.) and (in xrpToDrops) that any xrp amount submitted as a decimal doesn't contain more than 6 non-zero numbers after the decimal, as the 7th and beyond would represent a fractional amount in drops - not allowed.

Analogue in JS: https://github.com/xpring-eng/Xpring-JS/pull/399 Analogue in Java: https://github.com/xpring-eng/xpring4j/pull/217

Context of Change

Since this is such a common conversion, a robust implementation should be part of the SDKs.

Type of Change

Before / After

xrpToDrops and dropsToXrp can now be imported from XpringKit

Test Plan

New test cases pulled from ripple-lib to explore all conversion logic.

Future Tasks

codecov-io commented 4 years ago

Codecov Report

Merging #203 into master will increase coverage by 1.15%. The diff coverage is 95.23%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #203      +/-   ##
==========================================
+ Coverage   42.23%   43.38%   +1.15%     
==========================================
  Files          61       61              
  Lines        1892     1934      +42     
==========================================
+ Hits          799      839      +40     
- Misses       1093     1095       +2     
Impacted Files Coverage Δ
XpringKit/XRP/Utils.swift 96.29% <95.23%> (-3.71%) :arrow_down:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 405ce23...0132e26. Read the comment docs.