Advertisement
dex_investments

Waves RIDE Example: Integer to Float Number as String

Jan 26th, 2020
1,652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 4.41 KB | None | 0 0
  1. {-# STDLIB_VERSION 3 #-}
  2. {-# SCRIPT_TYPE ACCOUNT #-}
  3. {-# CONTENT_TYPE DAPP #-}
  4.  
  5. # Waves RIDE Example: Integer to Float Number as String
  6. #
  7. # https://waves-dapp.com/3P3rCd4SuTSjgxjbd9FgV9eduDi5A74iMT7
  8. # https://wavesexplorer.com/tx/FA5zQtcNHoFZDSeNwbznhBGrS19d6JTzKDfX53XC77Rs
  9. # https://wavesexplorer.com/address/3P3rCd4SuTSjgxjbd9FgV9eduDi5A74iMT7/data
  10.  
  11. # Copyright © 2020 John Silver aka. Джон Ведьмолов, <https://t.me/dex_investments>.
  12. #
  13. # This program is free software: you can redistribute it and/or modify
  14. # it under the terms of the GNU Affero General Public License as
  15. # published by the Free Software Foundation, either version 3 of the
  16. # License, or (at your option) any later version.
  17. #
  18. # This program is distributed in the hope that it will be useful,
  19. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. # GNU Affero General Public License for more details.
  22. #
  23. # You should have received a copy of the GNU Affero General Public License
  24. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  25.  
  26.  
  27. # The function returns a floating point number, separated by thousands.
  28. # rus: Функция возвращает число с плавающей точкой, разделенное тысячами.
  29. func toFloatString(num: Int, mult: Int) = {
  30.     let th = ","    # thousands sign
  31.     let dp = "."    # decimal point sign
  32.  
  33.     # The function separate thousands with commas.
  34.     func joinThousands(acc: String, val: Int) = {
  35.         if (acc == "" && val == 0) then ""
  36.         else if (acc == "") then toString(val)
  37.         else acc + th + toString(val + 1000).drop(1)
  38.     }
  39.    
  40.     # The function remove trailing fractional zeros.
  41.     func dropRightZero(str: String) = {
  42.         if (str.drop(str.size() - 1) != "0")
  43.         then str else str.take(str.size() - 1)
  44.     }
  45.  
  46.     #let abs = if (num < 0) then -num else num
  47.     let ip = num / mult    # integer part
  48.     let fp = num % mult    # fractional part
  49.  
  50.     # abbr: qN - quotient, rN - remainder
  51.     # max integer: 9,223,372,036,854,775,807
  52.     let q6 = ip / (1000 * 1000 * 1000 * 1000 * 1000 * 1000)
  53.     let r6 = ip % (1000 * 1000 * 1000 * 1000 * 1000 * 1000)
  54.     let q5 = r6 / (1000 * 1000 * 1000 * 1000 * 1000)
  55.     let r5 = r6 % (1000 * 1000 * 1000 * 1000 * 1000)
  56.     let q4 = r5 / (1000 * 1000 * 1000 * 1000)
  57.     let r4 = r5 % (1000 * 1000 * 1000 * 1000)
  58.     let q3 = r4 / (1000 * 1000 * 1000)
  59.     let r3 = r4 % (1000 * 1000 * 1000)
  60.     let q2 = r3 / (1000 * 1000)
  61.     let r2 = r3 % (1000 * 1000)
  62.     let q1 = r2 / (1000)
  63.     let r1 = r2 % (1000)    # q0 = r1 / 1
  64.  
  65.     # abbr: is - integer part as string, fs - fractional part as string
  66.     let is = FOLD<7>([q6, q5, q4, q3, q2, q1, r1], "", joinThousands)
  67.     let fs = toString(fp + mult).drop(1).take(8)
  68.              # Uncomment to remove trailing fractional zeros.
  69.              #.dropRightZero().dropRightZero().dropRightZero().dropRightZero()
  70.              #.dropRightZero().dropRightZero().dropRightZero().dropRightZero()
  71.  
  72.     #(if (num < 0) then "-" else "") +
  73.     (if (is != "") then is else "0") + (if (fs != "") then (dp + fs) else "")
  74. }
  75.  
  76.  
  77. @Callable(inv)
  78. func setFloatData(keyName: String, intValue: Int,
  79.                   decPlaces: Int, isDollar: Boolean) = {
  80.     if (inv.caller != this) then
  81.         # rus: Только владелец может изменить данные!
  82.         throw("Only the owner can change the data!")
  83.    
  84.     else if (intValue < 0 || decPlaces < 0) then
  85.         # rus: Отрицательные числа не допускаются.
  86.         throw("Negative numbers are not allowed.")
  87.    
  88.     else if (decPlaces > 8) then
  89.         # rus: Десятичные знаки находятся вне предела 0-8.
  90.         throw("Decimal places are outside the range of 0-8.")
  91.    
  92.     else if (isDollar && decPlaces != 0 && decPlaces != 2) then
  93.         # rus: Допустимые десятичные знаки для доллара: 0 или 2.
  94.         throw("Valid decimal places for the dollar: 0 or 2.")
  95.  
  96.     else {
  97.         let prefix = if isDollar then "$" else ""
  98.         let decMult = parseIntValue("100000000".take(decPlaces + 1))
  99.  
  100.         WriteSet([
  101.             DataEntry(keyName, prefix + intValue.toFloatString(decMult))
  102.         ])
  103.     }
  104. }
  105.  
  106.  
  107. @Verifier(tx)
  108. func verify() = sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement