aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--db/index.js5
-rw-r--r--db/models/payments.js9
-rw-r--r--routes/bitcoin/send.js60
3 files changed, 44 insertions, 30 deletions
diff --git a/db/index.js b/db/index.js
index 57586954..5a10a01a 100644
--- a/db/index.js
+++ b/db/index.js
@@ -162,4 +162,9 @@ Referral.belongsTo(User, {
foreignKey: "user_id",
});
+Payment.belongsTo(Payment, {
+ as: "fee",
+ foreignKey: "fee_payment_id"
+});
+
db.Order = Order;
diff --git a/db/models/payments.js b/db/models/payments.js
index c315a5a0..bbf7c436 100644
--- a/db/models/payments.js
+++ b/db/models/payments.js
@@ -189,6 +189,15 @@ const attributes = {
autoIncrement: false,
comment: null,
field: "confirmed"
+ },
+ fee_payment_id: {
+ type: DataTypes.INTEGER(11),
+ allowNull: true,
+ defaultValue: null,
+ primaryKey: false,
+ autoIncrement: false,
+ comment: null,
+ field: "fee_payment_id"
}
};
diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js
index 67857121..70cca161 100644
--- a/routes/bitcoin/send.js
+++ b/routes/bitcoin/send.js
@@ -36,11 +36,10 @@ module.exports = ah(async (req, res) => {
// (i.e. the total bitcoin that leaves this server)
let withdrawalFee = amount * withdrawalFeeMultiplier;
- let account, params;
try {
// withdraw bitcoin
await db.transaction(async transaction => {
- account = await db.Account.findOne({
+ let account = await db.Account.findOne({
where: {
id: user.account_id
},
@@ -87,7 +86,7 @@ module.exports = ah(async (req, res) => {
await receiverAccount.increment({ balance: withdrawalFee }, { transaction });
await receiverAccount.reload({ transaction });
- await db.Payment.create({
+ let fee_payment = await db.Payment.create({
amount: withdrawalFee,
fee: 0,
memo: "Bitcoin withdrawal fee",
@@ -101,40 +100,41 @@ module.exports = ah(async (req, res) => {
},
{ transaction }
);
- });
- // record the external bitcoin transaction
- // no need to record the withdrawal fee - since that is internal only
- params = {
- amount: -amount,
- fee,
- memo,
- account_id: account.id,
- user_id: user.id,
- rate: app.get("rates")[user.currency],
- currency: user.currency,
- address,
- confirmed: true,
- received: false,
- network: "bitcoin"
- };
+ // record the external bitcoin transaction
+ // no need to record the withdrawal fee - since that is internal only
+ let params = {
+ amount: -amount,
+ fee,
+ memo,
+ account_id: account.id,
+ user_id: user.id,
+ rate: app.get("rates")[user.currency],
+ currency: user.currency,
+ address,
+ confirmed: true,
+ received: false,
+ network: "bitcoin",
+ fee_payment_id: fee_payment.id
+ };
- if (config.bitcoin.walletpass)
- await bc.walletPassphrase(config.bitcoin.walletpass, 300);
+ if (config.bitcoin.walletpass)
+ await bc.walletPassphrase(config.bitcoin.walletpass, 300);
- hex = (await bc.signRawTransactionWithWallet(hex)).hex;
- params.hash = await bc.sendRawTransaction(hex);
+ hex = (await bc.signRawTransactionWithWallet(hex)).hex;
+ params.hash = await bc.sendRawTransaction(hex);
- let payment = await db.Payment.create(params);
+ let payment = await db.Payment.create(params);
- payment = payment.get({ plain: true });
- payment.account = account.get({ plain: true });
+ payment = payment.get({ plain: true });
+ payment.account = account.get({ plain: true });
- emit(user.username, "payment", payment);
- res.send(payment);
+ emit(user.username, "payment", payment);
+ res.send(payment);
- payments.push(params.hash);
- l.info("sent bitcoin", user.username, total);
+ payments.push(params.hash);
+ l.info("sent bitcoin", user.username, total);
+ });
} catch (e) {
if (e.message.includes("Insufficient")) e.message = "The coinos server hot wallet has insufficient funds to complete the payment, try again later";
l.error("error sending bitcoin", e.message);