-
Notifications
You must be signed in to change notification settings - Fork 2
/
sendTransaction.js
78 lines (76 loc) · 3.11 KB
/
sendTransaction.js
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
const sendSMS = require('./sms/index')
const getAccount = require('./db/getAccount.js')
const updateAddressBalance = require('./db/updateAddressBalance.js')
const pay = require('./web3/pay.js')
const Bluebird = require('bluebird')
const sendTransaction = ({ socket }) => ({
req, res, telephone, message
}) => {
const [ unusedVar, unFormattedTelephone, unformattedAmount ] = message.split(' ')
const amount = Number(unformattedAmount) * 100
const toTelephone = `44${unFormattedTelephone.slice(unFormattedTelephone.indexOf(7))}`.replace(/ /g, '')
const fromTelephone = telephone.replace('+', '')
return Bluebird.props({
fromAccount: getAccount({ telephone: fromTelephone }),
toAccount: getAccount({ telephone: toTelephone })
})
.then(({ fromAccount, toAccount }) => {
console.log({ fromAccount, toAccount })
// 1. Check balance of ETH wallet (check if balance sufficient)
if (!fromAccount || !fromAccount.address) {
return sendSMS(telephone, `You do not have an account yet. Create one by sending 'create' to this number`)
}
if (!fromAccount.balance || fromAccount.balance < Number(amount)) {
return sendSMS(telephone, `Not enough balance. Your balance is ${(fromAccount.balance / 100).toFixed(2)}`)
}
if (!toAccount || !toAccount.address) {
return sendSMS(telephone, 'You tried to send to a number that is not registered with PGKT yet!')
}
// 2. Send ETH from one wallet to the other
return pay({
fromAddress: fromAccount.address,
toAddress: toAccount.address,
amount: amount,
privateKey: fromAccount.private_key
})
.then((hash) => {
const newFromAccountBalance = Number(fromAccount.balance) - amount
const newToAccountBalance = Number(toAccount.balance) + amount
// 3. Update both DB entries to reflect new balances
return Bluebird.props({
updatedFromAccount: updateAddressBalance({ privateKey: fromAccount.private_key, balance: newFromAccountBalance }),
updatedToAccount: updateAddressBalance({ privateKey: toAccount.private_key, balance: newToAccountBalance }),
hash
})
})
.then(({ updatedFromAccount, updatedToAccount, hash }) => {
// 4. Send success SMS to both numbers stating new balance.
socket.emit('transactionMade',
`+${updatedToAccount.telephone}`,
'Payment made',
(amount / 100).toFixed(2),
new Date()
)
return Bluebird.props({
sentFromAccountSMS: sendSMS(
telephone,
`You paid ${(amount / 100).toFixed(2)}KT to +${toTelephone} (tx hash: ${hash}). New balance is ${(updatedFromAccount.balance / 100).toFixed(2)}`
),
sentToAccountSMS: sendSMS(
`+${toTelephone}`,
`You received ${(amount / 100).toFixed(2)}KT from ${telephone} (tx hash: ${hash}). New balance is ${(updatedToAccount.balance / 100).toFixed(2)}`
)
})
})
.then(() => {
res.set('Content-Type', 'text/xml')
res.send(`
<?xml version="1.0" encoding="UTF-8"?>
<Response>
</Response>
`)
})
})
.catch(console.error)
}
module.exports = sendTransaction