gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-wallet-core] 02/04: remove from float


From: gnunet
Subject: [taler-wallet-core] 02/04: remove from float
Date: Tue, 20 Jun 2023 19:29:09 +0200

This is an automated email from the git hooks/post-receive script.

sebasjm pushed a commit to branch master
in repository wallet-core.

commit 9f07ab8d4a13d52d7fd61a3e9b8ddc423fee164e
Author: Sebastian <sebasjm@gmail.com>
AuthorDate: Tue Jun 20 11:50:04 2023 -0300

    remove from float
---
 packages/taler-util/src/amounts.ts      | 26 +++++++-----------------
 packages/taler-util/src/wallet-types.ts | 36 +++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 19 deletions(-)

diff --git a/packages/taler-util/src/amounts.ts 
b/packages/taler-util/src/amounts.ts
index a998c8f63..a8df3679f 100644
--- a/packages/taler-util/src/amounts.ts
+++ b/packages/taler-util/src/amounts.ts
@@ -147,8 +147,10 @@ export class Amounts {
       throw Error(`incompatible currency (${am1.currency} vs${am2.currency})`);
     }
 
-    const x1 = BigInt(am1.value) * BigInt(amountFractionalBase) + 
BigInt(am1.fraction);
-    const x2 = BigInt(am2.value) * BigInt(amountFractionalBase) + 
BigInt(am2.fraction);
+    const x1 =
+      BigInt(am1.value) * BigInt(amountFractionalBase) + BigInt(am1.fraction);
+    const x2 =
+      BigInt(am2.value) * BigInt(amountFractionalBase) + BigInt(am2.fraction);
 
     const quotient = x1 / x2;
     const remainderScaled = x1 % x2;
@@ -158,9 +160,9 @@ export class Amounts {
       remainder: {
         currency: am1.currency,
         value: Number(remainderScaled / BigInt(amountFractionalBase)),
-        fraction: Number(remainderScaled % BigInt(amountFractionalBase))
-      }
-    }
+        fraction: Number(remainderScaled % BigInt(amountFractionalBase)),
+      },
+    };
   }
 
   static sum(amounts: AmountLike[]): Result {
@@ -391,20 +393,6 @@ export class Amounts {
     }
   }
 
-  /**
-   * Convert a float to a Taler amount.
-   * Loss of precision possible.
-   */
-  static fromFloat(floatVal: number, currency: string): AmountJson {
-    return {
-      currency,
-      fraction: Math.floor(
-        (floatVal - Math.floor(floatVal)) * amountFractionalBase,
-      ),
-      value: Math.floor(floatVal),
-    };
-  }
-
   static min(a: AmountLike, b: AmountLike): AmountJson {
     const cr = Amounts.cmp(a, b);
     if (cr >= 0) {
diff --git a/packages/taler-util/src/wallet-types.ts 
b/packages/taler-util/src/wallet-types.ts
index b17860fc8..5a87864fc 100644
--- a/packages/taler-util/src/wallet-types.ts
+++ b/packages/taler-util/src/wallet-types.ts
@@ -187,6 +187,31 @@ interface GetPlanForWalletInitiatedOperation {
   mode: TransactionAmountMode;
 }
 
+export interface ConvertAmountRequest {
+  amount: AmountString;
+  type: TransactionAmountMode;
+}
+
+export const codecForConvertAmountRequest =
+  buildCodecForObject<ConvertAmountRequest>()
+    .property("amount", codecForAmountString())
+    .property(
+      "type",
+      codecForEither(
+        codecForConstString(TransactionAmountMode.Raw),
+        codecForConstString(TransactionAmountMode.Effective),
+      ),
+    )
+    .build("ConvertAmountRequest");
+
+export interface GetAmountRequest {
+  currency: string;
+}
+
+export const codecForGetAmountRequest = buildCodecForObject<GetAmountRequest>()
+  .property("currency", codecForString())
+  .build("GetAmountRequest");
+
 interface GetPlanToCompleteOperation {
   instructedAmount: AmountString;
 }
@@ -328,6 +353,17 @@ export const codecForGetPlanForOperationResponse =
       .property("counterPartyAmount", codecOptional(codecForAmountString()))
       .build("GetPlanForOperationResponse");
 
+export interface AmountResponse {
+  effectiveAmount: AmountString;
+  rawAmount: AmountString;
+}
+
+export const codecForAmountResponse = (): Codec<AmountResponse> =>
+  buildCodecForObject<AmountResponse>()
+    .property("effectiveAmount", codecForAmountString())
+    .property("rawAmount", codecForAmountString())
+    .build("AmountResponse");
+
 export interface Balance {
   scopeInfo: ScopeInfo;
   available: AmountString;

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]