aboutsummaryrefslogtreecommitdiffstats
path: root/node_modules/jade/lib/nodes/each.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/jade/lib/nodes/each.js')
0 files changed, 0 insertions, 0 deletions
Soltys <[email protected]> 2022-09-05 20:44:26 -0700 drop js extension' href='/coinos-server.git/commit/routes/send.js?h=tickets&id=3ae34e6bf84d988c25254374768f2898865df857'>3ae34e6b ^
412df5f8 ^
2f0f9e64 ^
3161dd7e ^
4c73bfdd ^






75bd9237 ^
4c73bfdd ^
2f0f9e64 ^
936cea87 ^
2f0f9e64 ^
92e913ba ^

957b1598 ^
8a82c694 ^
2f0f9e64 ^

75bd9237 ^

1330871b ^
75bd9237 ^


1330871b ^
75bd9237 ^



a86552b7 ^
75bd9237 ^








1330871b ^
2f0f9e64 ^






8afa3af7 ^

2f0f9e64 ^
55a37c3a ^
99d71f03 ^



13352f9b ^
99d71f03 ^

13352f9b ^
a86552b7 ^

75bd9237 ^
99d71f03 ^


d1cc66d1 ^
13352f9b ^
071a97a7 ^
99d71f03 ^


2f0f9e64 ^



d1cc66d1 ^
2f0f9e64 ^
7a3a07bc ^
35c62a7b ^
2f0f9e64 ^
99d71f03 ^
eb860d78 ^




75bd9237 ^

eb860d78 ^


2f0f9e64 ^
4c73bfdd ^

75bd9237 ^
4c73bfdd ^
98eceb8d ^

a86552b7 ^
6510e2e7 ^
49883ffc ^
98d8dedf ^


75bd9237 ^
98d8dedf ^

49883ffc ^
cea3e506 ^
b5607df4 ^
d01159ce ^


75bd9237 ^
d01159ce ^
b5607df4 ^
99d71f03 ^
49883ffc ^
8afa3af7 ^

af2d18b6 ^
99d71f03 ^




13352f9b ^

99d71f03 ^
d01159ce ^

75bd9237 ^
99d71f03 ^
d01159ce ^


75bd9237 ^
d01159ce ^
99d71f03 ^
99d71f03 ^


af2d18b6 ^
99d71f03 ^
98d8dedf ^


eb860d78 ^
98d8dedf ^
2f0f9e64 ^

3ce9c73b ^
6510e2e7 ^





13352f9b ^
6510e2e7 ^

13352f9b ^
6510e2e7 ^

75bd9237 ^
6510e2e7 ^

29a31a25 ^




d1cc66d1 ^
29a31a25 ^











75bd9237 ^
29a31a25 ^


6510e2e7 ^

99d71f03 ^
49883ffc ^

49883ffc ^
bf1c162f ^
99d71f03 ^
d1cc66d1 ^
49883ffc ^
6510e2e7 ^
957b1598 ^
071a97a7 ^
2f0f9e64 ^

2cc2510c ^
d1cc66d1 ^
8a82c694 ^
2f0f9e64 ^
3161dd7e ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
                                    

                                            
                          
                          
                                            
                                                 
 
                                          






                   
            
               
                     
                            
 

                                             
                            
                                                                  

       

                                               
                                         


                                            
                        



                                        
              








                                            
       






                                              

                                                                    
 
                    



                               
                                         

                                
                                       

                                                                   
                         


                      
                                         
                                 
                                                                         


                                                                     



                                                     
                                                         
 
                                              
                                              
 
                     




                                              

                           


                         
 

                                           
                                 
          

                                                    
                                                                                
 
               


                                
                      

          
                                                                           
                                 
              


                                          
                       
             
         
 
                 

                                                                   
                




                                                        

                                                                        
                  

                                                       
                     
                


                                            
                         
               
 


                                                               
           
 


                                                                   
                                 
                                                             

         
                                                           





                                             
                                                         

                                       
                                                                      

                            
                        

          




                                         
              











                                                         
                           


                                  

                                                                  
 

                                             
                                                
                                                        
 
                                                           
                                                                  
                                 
       
                        

               
                   
                                                                      
                                         
   
  
import { emit } from "$lib/sockets";
import store from "$lib/store";
import { callWebhook } from "$lib/webhooks";
import axios from "axios";
import { v4 } from "uuid";
import { notify } from "$lib/notifications";
import { sendLiquid } from "$routes/liquid/send";

export default async (req, res, next) => {
  let {
    amount,
    address,
    payreq,
    unconfidential,
    asset,
    memo,
    username
  } = req.body;
  let { user } = req;
  amount = parseInt(amount);

  if (!asset) asset = config.liquid.btcasset;

  if (!amount || amount < 0)
    return res.code(500).send("Amount must be greater than zero");

  try {
    await db.transaction(async transaction => {
      let account;
      if (user.account.asset === asset) {
        account = await db.Account.findOne({
          where: {
            id: user.account.id,
            pubkey: null
          },
          lock: transaction.LOCK.UPDATE,
          transaction
        });
      } else {
        account = await db.Account.findOne({
          where: {
            user_id: user.id,
            asset,
            pubkey: null
          },
          lock: transaction.LOCK.UPDATE,
          transaction
        });
      }

      if (account.balance < amount) {
        throw new Error("Insufficient funds");
      }

      let fee = 0;

      await account.decrement({ balance: amount }, { transaction });
      await account.reload({ transaction });

      let params = {
        amount: -amount,
        account_id: account.id,
        memo,
        user_id: user.id,
        rate: store.rates[user.currency],
        currency: user.currency,
        confirmed: true,
        hash: `#${v4().substr(0, 6)} ${
          username ? `Payment to ${username}` : "Internal Transfer"
        }`,
        network: "COINOS"
      };

      if (!username) {
        l("creating redeemable payment");
        params.redeemcode = v4();
        params.hash = `${req.get("origin")}/redeem/${params.redeemcode}`;
      }

      let payment = await db.Payment.create(params, { transaction });

      payment = payment.get({ plain: true });
      payment.account = account.get({ plain: true });

      l("sent internal", user.username, -payment.amount);

      emit(user.username, "payment", payment);
      emit(user.username, "account", account);

      if (username) {
        let recipient = await db.User.findOne(
          {
            where: { username },
            include: {
              model: db.Account,
              as: "account"
            }
          },
          { transaction }
        );

        params = {
          where: { user_id: recipient.id },
          order: [["id", "DESC"]]
        };
        if (address) params.where.address = address;
        else if (payreq) params.where.text = payreq;
        let invoice = (address || payreq) && (await db.Invoice.findOne(params));

        let a2;
        let acc = {
          user_id: recipient.id,
          asset,
          pubkey: null
        };

        if (recipient.account.asset === asset && !recipient.account.pubkey)
          a2 = recipient.account;
        else {
          a2 = await db.Account.findOne({
            where: acc,
            lock: transaction.LOCK.UPDATE,
            transaction
          });
        }

        if (a2) {
          await a2.increment({ balance: amount }, { transaction });
          await a2.reload({ transaction });
        } else {
          let name = asset.substr(0, 6);
          let domain;
          let ticker = asset.substr(0, 3).toUpperCase();
          let precision = 8;

          if (store.assets[asset]) {
            ({ domain, ticker, precision, name } = store.assets[asset]);
          } else {
            const existing = await db.Account.findOne({
              where: {
                asset
              },
              order: [["id", "ASC"]],
              limit: 1,
              lock: transaction.LOCK.UPDATE,
              transaction
            });

            if (existing) {
              ({ domain, ticker, precision, name } = existing);
            }
          }

          acc = { ...acc, ...{ domain, ticker, precision, name } };
          acc.balance = amount;
          acc.pending = 0;
          acc.network = "liquid";
          a2 = await db.Account.create(acc, { transaction });
        }

        let spread = user.username === "coinos" ? 1.02 : 1;
        amount = Math.round(amount / spread);

        params = {
          amount,
          account_id: a2.id,
          user_id: recipient.id,
          rate: store.rates[recipient.currency] * spread,
          currency: recipient.currency,
          confirmed: true,
          hash: `#${v4().substr(0, 6)} Payment from ${user.username}`,
          memo,
          network: "COINOS",
          received: true
        };

        if (invoice) {
          params.invoice_id = invoice.id;

          let c = convert[invoice.text];
          if (c) {
            l(
              "internal payment detected for conversion",
              invoice.text,
              c.address,
              recipient.username
            );

            recipient.account = a2;

            sendLiquid({
              address: c.address,
              amount: amount - 100,
              user: recipient,
              limit: amount
            }).catch(console.log);
          }
        }

        let p2 = await db.Payment.create(params, { transaction });

        p2 = p2.get({ plain: true });
        p2.account = a2.get({ plain: true });
        emit(recipient.username, "payment", p2);
        emit(recipient.username, "account", p2.account);

        l("received internal", recipient.username, amount);
        notify(recipient, `Received ${amount} ${a2.ticker} sats`);
        callWebhook(invoice, p2);
      }
      res.send(payment);
    });
  } catch (e) {
    console.log(e);
    err("problem sending internal payment", user.username, e.message);
    return res.code(500).send(e.message);
  }
};