damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

amount.h (7898B)


      1 #ifndef LIGHTNING_COMMON_AMOUNT_H
      2 #define LIGHTNING_COMMON_AMOUNT_H
      3 #include "config.h"
      4 #include "short_types.h"
      5 #include "tal.h"
      6 
      7 #define MSAT_PER_SAT ((u64)1000)
      8 #define SAT_PER_BTC ((u64)100000000)
      9 #define MSAT_PER_BTC (MSAT_PER_SAT * SAT_PER_BTC)
     10 
     11 /* Use these to wrap amounts, for typesafety.  Please use ops where possible,
     12  * rather than accessing the members directly. */
     13 struct amount_sat {
     14     /* Amount in satoshis. */
     15     u64 satoshis;
     16 };
     17 
     18 struct amount_msat {
     19     /* Amount in millisatoshis. */
     20     u64 millisatoshis;
     21 };
     22 
     23 struct amount_asset {
     24     u64 value;
     25     u8 asset[33]; /* 1 version byte + 32 byte asset_tag */
     26 };
     27 
     28 /* For constants only: others must be built from primitives! */
     29 #if HAVE_BUILTIN_CONSTANT_P
     30 #define AMOUNT_MUST_BE_CONST(c) BUILD_ASSERT_OR_ZERO(IS_COMPILE_CONSTANT(c))
     31 #else
     32 #define AMOUNT_MUST_BE_CONST(c) 0
     33 #endif
     34 
     35 /* GCC 4.8.5 (Centos 7.6!) thinks struct casts are not constants, so we
     36  * need to not use a cast for static initializations. */
     37 #define AMOUNT_MSAT_INIT(msat)        \
     38     { .millisatoshis = (msat) }
     39 #define AMOUNT_SAT_INIT(sat)        \
     40     { .satoshis = (sat) }
     41 
     42 #define AMOUNT_MSAT(constant)                        \
     43     ((struct amount_msat){(constant) + AMOUNT_MUST_BE_CONST(constant)})
     44 
     45 #define AMOUNT_SAT(constant)                        \
     46     ((struct amount_sat){(constant) + AMOUNT_MUST_BE_CONST(constant)})
     47 
     48 /* We do sometimes need to import from raw types, eg. wally or wire fmt */
     49 struct amount_msat amount_msat(u64 millisatoshis);
     50 struct amount_sat amount_sat(u64 satoshis);
     51 
     52 /* You may not always be able to convert satoshis->millisatoshis. */
     53 WARN_UNUSED_RESULT bool amount_sat_to_msat(struct amount_msat *msat,
     54                        struct amount_sat sat);
     55 
     56 /* You may not always be able to convert millisatoshis->satoshis without rounding. */
     57 WARN_UNUSED_RESULT bool amount_msat_to_sat(struct amount_sat *sat,
     58                        struct amount_msat msat);
     59 
     60 /* You can always truncate millisatoshis->satoshis. */
     61 struct amount_sat amount_msat_to_sat_round_down(struct amount_msat msat);
     62 
     63 /* Simple operations: val = a + b, val = a - b. */
     64 WARN_UNUSED_RESULT bool amount_msat_add(struct amount_msat *val,
     65                     struct amount_msat a,
     66                     struct amount_msat b);
     67 WARN_UNUSED_RESULT bool amount_msat_sub(struct amount_msat *val,
     68                     struct amount_msat a,
     69                     struct amount_msat b);
     70 WARN_UNUSED_RESULT bool amount_sat_add(struct amount_sat *val,
     71                        struct amount_sat a,
     72                        struct amount_sat b);
     73 WARN_UNUSED_RESULT bool amount_sat_sub(struct amount_sat *val,
     74                        struct amount_sat a,
     75                        struct amount_sat b);
     76 WARN_UNUSED_RESULT bool amount_msat_sub_sat(struct amount_msat *val,
     77                         struct amount_msat a,
     78                         struct amount_sat b);
     79 WARN_UNUSED_RESULT bool amount_msat_add_sat(struct amount_msat *val,
     80                         struct amount_msat a,
     81                         struct amount_sat b);
     82 WARN_UNUSED_RESULT bool amount_sat_sub_msat(struct amount_msat *val,
     83                         struct amount_sat a,
     84                         struct amount_msat b);
     85 WARN_UNUSED_RESULT bool amount_msat_scale(struct amount_msat *val,
     86                       struct amount_msat msat,
     87                       double scale);
     88 WARN_UNUSED_RESULT bool amount_sat_scale(struct amount_sat *val,
     89                      struct amount_sat sat,
     90                      double scale);
     91 
     92 struct amount_msat amount_msat_div(struct amount_msat msat, u64 div);
     93 struct amount_sat amount_sat_div(struct amount_sat sat, u64 div);
     94 
     95 /* Is a == b? */
     96 bool amount_sat_eq(struct amount_sat a, struct amount_sat b);
     97 bool amount_msat_eq(struct amount_msat a, struct amount_msat b);
     98 
     99 /* Is a zero? */
    100 bool amount_sat_zero(struct amount_sat a);
    101 bool amount_msat_zero(struct amount_msat a);
    102 
    103 /* Is a > b? */
    104 bool amount_sat_greater(struct amount_sat a, struct amount_sat b);
    105 bool amount_msat_greater(struct amount_msat a, struct amount_msat b);
    106 
    107 /* Is a >= b */
    108 bool amount_sat_greater_eq(struct amount_sat a, struct amount_sat b);
    109 bool amount_msat_greater_eq(struct amount_msat a, struct amount_msat b);
    110 
    111 /* Is a < b? */
    112 bool amount_sat_less(struct amount_sat a, struct amount_sat b);
    113 bool amount_msat_less(struct amount_msat a, struct amount_msat b);
    114 
    115 /* Is a <= b? */
    116 bool amount_sat_less_eq(struct amount_sat a, struct amount_sat b);
    117 bool amount_msat_less_eq(struct amount_msat a, struct amount_msat b);
    118 
    119 /* Is msat > sat? */
    120 bool amount_msat_greater_sat(struct amount_msat msat, struct amount_sat sat);
    121 /* Is msat >= sat? */
    122 bool amount_msat_greater_eq_sat(struct amount_msat msat, struct amount_sat sat);
    123 /* Is msat < sat? */
    124 bool amount_msat_less_sat(struct amount_msat msat, struct amount_sat sat);
    125 /* Is msat <= sat? */
    126 bool amount_msat_less_eq_sat(struct amount_msat msat, struct amount_sat sat);
    127 /* Is msat == sat? */
    128 bool amount_msat_eq_sat(struct amount_msat msat, struct amount_sat sat);
    129 
    130 /* a / b */
    131 double amount_msat_ratio(struct amount_msat a, struct amount_msat b);
    132 
    133 /* Check whether this asset is actually the main / fee-paying asset of the
    134  * current chain. */
    135 bool amount_asset_is_main(struct amount_asset *asset);
    136 
    137 /* Convert an amount_sat to an amount_asset */
    138 struct amount_asset amount_sat_to_asset(struct amount_sat *sat, const u8 *asset);
    139 
    140 /* amount_asset_extract_value -Prefix the amount_asset's value
    141  * to have the 'explicit' marker. Returns NULL if the
    142  * asset was originally blinded.
    143  * FIXME: pass through blinded amounts */
    144 u8 *amount_asset_extract_value(const tal_t *ctx, struct amount_asset *asset);
    145 
    146 /* Convert from a generic asset to the fee-paying asset if possible. */
    147 struct amount_sat amount_asset_to_sat(struct amount_asset *asset);
    148 
    149 /* Returns true if msat fits in a u32 value. */
    150 WARN_UNUSED_RESULT bool amount_msat_to_u32(struct amount_msat msat,
    151                        u32 *millisatoshis);
    152 
    153 /* Common operation: what is the HTLC fee for given feerate?  Can overflow! */
    154 WARN_UNUSED_RESULT bool amount_msat_fee(struct amount_msat *fee,
    155                     struct amount_msat amt,
    156                     u32 fee_base_msat,
    157                     u32 fee_proportional_millionths);
    158 
    159 /* Same, but add into amt. */
    160 WARN_UNUSED_RESULT bool amount_msat_add_fee(struct amount_msat *amt,
    161                         u32 fee_base_msat,
    162                         u32 fee_proportional_millionths);
    163 
    164 /* What is the fee for this tx weight? */
    165 struct amount_sat amount_tx_fee(u32 fee_per_kw, size_t weight);
    166 
    167 /* Different formatting by amounts: btc, sat and msat */
    168 /* => 1.23456789012btc (11 decimals!) */
    169 const char *fmt_amount_msat_btc(const tal_t *ctx,
    170                 struct amount_msat msat,
    171                 bool append_unit);
    172 /* => 1234msat */
    173 const char *fmt_amount_msat(const tal_t *ctx, struct amount_msat msat);
    174 
    175 /* => 1.23456789btc (8 decimals!) */
    176 const char *fmt_amount_sat_btc(const tal_t *ctx,
    177                    struct amount_sat sat,
    178                    bool append_unit);
    179 /* => 1234sat */
    180 const char *fmt_amount_sat(const tal_t *ctx, struct amount_sat sat);
    181 
    182 /* Valid strings:
    183  *  [0-9]+ => millisatoshi.
    184  *  [0-9]+msat => millisatoshi.
    185  *  [0-9]+sat => *1000 -> millisatopshi.
    186  *  [0-9]+.[0-9]{1,11}btc => millisatoshi.
    187  */
    188 bool parse_amount_msat(struct amount_msat *msat, const char *s, size_t slen);
    189 
    190 /* Valid strings:
    191  *  [0-9]+ => satoshi.
    192  *  [0-9]+sat => satoshi.
    193  *  [0-9]+000msat => satoshi.
    194  *  [0-9]+.[0-9]{1,8}btc => satoshi.
    195  */
    196 bool parse_amount_sat(struct amount_sat *sat, const char *s, size_t slen);
    197 
    198 /* Marshal/unmarshal functions */
    199 struct amount_msat fromwire_amount_msat(const u8 **cursor, size_t *max);
    200 struct amount_sat fromwire_amount_sat(const u8 **cursor, size_t *max);
    201 void towire_amount_msat(u8 **pptr, const struct amount_msat msat);
    202 void towire_amount_sat(u8 **pptr, const struct amount_sat sat);
    203 #endif /* LIGHTNING_COMMON_AMOUNT_H */