aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdrien Hopkins <[email protected]>2022-06-06 14:51:48 -0500
committerAdrien Hopkins <[email protected]>2022-06-06 14:51:48 -0500
commit8fcd976a04d1cfffdf84306c39a4d6c526aa0a22 (patch)
treede731c67d32fd93d9105a2f0a3ab2776d9768334
parent2549d087fd55191cd711e02522892092ba7e2fda (diff)
downloadcoinos-server-8fcd976a04d1cfffdf84306c39a4d6c526aa0a22.tar.gz
coinos-server-8fcd976a04d1cfffdf84306c39a4d6c526aa0a22.zip
Added withdrawal fee credits, which can pay withdrawal fees
-rw-r--r--db/models/accounts.js9
-rw-r--r--routes/bitcoin/send.js46
-rw-r--r--routes/liquid/send.js48
3 files changed, 67 insertions, 36 deletions
diff --git a/db/models/accounts.js b/db/models/accounts.js
index 12fef548..88ad2d44 100644
--- a/db/models/accounts.js
+++ b/db/models/accounts.js
@@ -124,6 +124,15 @@ const attributes = {
comment: null,
field: "balance"
},
+ fee_credits: {
+ type: DataTypes.DOUBLE,
+ allowNull: true,
+ defaultValue: 0.0,
+ primaryKey: false,
+ autoIncrement: false,
+ comment: "Credits that can be used towards paying withdrawal fees",
+ field: "fee_credits"
+ },
pending: {
type: DataTypes.DOUBLE,
allowNull: true,
diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js
index aa3a7640..efcedb38 100644
--- a/routes/bitcoin/send.js
+++ b/routes/bitcoin/send.js
@@ -34,7 +34,7 @@ module.exports = ah(async (req, res) => {
// get withdrawal fee
// 'total' refers to the total before the withdrawal fee
// (i.e. the total bitcoin that leaves this server)
- let withdrawalFee = amount * withdrawalFeeMultiplier;
+ let withdrawalFee = Math.floor(amount * withdrawalFeeMultiplier);
try {
// withdraw bitcoin
@@ -67,6 +67,14 @@ module.exports = ah(async (req, res) => {
throw new Error("low balance (after withdrawal fee)");
}
+ // use user's credits to reduce fee, if available
+ console.log(account.fee_credits);
+ let withdrawalFeeDeduction = Math.min(account.fee_credits, withdrawalFee);
+ if (withdrawalFeeDeduction) {
+ await account.decrement({ fee_credits: withdrawalFeeDeduction }, { transaction });
+ await account.reload({ transaction });
+ }
+
await account.decrement({ balance: (total + withdrawalFee) }, { transaction });
await account.reload({ transaction });
@@ -84,22 +92,24 @@ module.exports = ah(async (req, res) => {
transaction
});
- await receiverAccount.increment({ balance: withdrawalFee }, { transaction });
- await receiverAccount.reload({ transaction });
- let fee_payment = await db.Payment.create({
- amount: withdrawalFee,
- fee: 0,
- memo: "Bitcoin withdrawal fee",
- account_id: receiverAccount.id,
- user_id: receiverAccount.user_id,
- rate: app.get("rates")[receiverAccount.user.currency],
- currency: receiverAccount.user.currency,
- confirmed: true,
- received: true,
- network: "COINOS"
- },
- { transaction }
- );
+ let fee_payment_id = null;
+ if (withdrawalFee) {
+ await receiverAccount.increment({ balance: withdrawalFee }, { transaction });
+ await receiverAccount.reload({ transaction });
+ let fee_payment = await db.Payment.create({
+ amount: withdrawalFee,
+ fee: 0,
+ memo: "Bitcoin withdrawal fee",
+ account_id: receiverAccount.id,
+ user_id: receiverAccount.user_id,
+ rate: app.get("rates")[receiverAccount.user.currency],
+ currency: receiverAccount.user.currency,
+ confirmed: true,
+ received: true,
+ network: "COINOS"
+ }, { transaction });
+ fee_payment_id = fee_payment.id;
+ }
// record the external bitcoin transaction
let params = {
@@ -114,7 +124,7 @@ module.exports = ah(async (req, res) => {
confirmed: true,
received: false,
network: "bitcoin",
- fee_payment_id: fee_payment.id
+ fee_payment_id: fee_payment_id
};
if (config.bitcoin.walletpass)
diff --git a/routes/liquid/send.js b/routes/liquid/send.js
index 1e4c62d2..8e21e4d4 100644
--- a/routes/liquid/send.js
+++ b/routes/liquid/send.js
@@ -87,7 +87,7 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => {
// get withdrawal fee
// 'total' refers to the total before the withdrawal fee
// (i.e. the total liquid bitcoin that leaves this server)
- var withdrawalFee = amount * withdrawalFeeMultiplier;
+ var withdrawalFee = Math.floor(amount * withdrawalFeeMultiplier);
if (limit && total > limit + fee)
throw new Error("Tx amount exceeds authorized amount");
@@ -131,6 +131,14 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => {
);
}
+ // use user's credits to reduce fee, if available
+ console.log(account.fee_credits);
+ let withdrawalFeeDeduction = Math.min(account.fee_credits, withdrawalFee);
+ if (withdrawalFeeDeduction) {
+ await account.decrement({ fee_credits: withdrawalFeeDeduction }, { transaction });
+ await account.reload({ transaction });
+ }
+
await account.decrement({ balance: (total + withdrawalFee)}, { transaction });
await account.reload({ transaction });
@@ -148,22 +156,26 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => {
transaction
});
- await receiverAccount.increment({ balance: withdrawalFee }, { transaction });
- await receiverAccount.reload({ transaction });
- let fee_payment = {
- amount: withdrawalFee,
- fee: 0,
- memo: "Liquid withdrawal fee",
- account_id: receiverAccount.id,
- user_id: receiverAccount.user_id,
- rate: app.get("rates")[receiverAccount.user.currency],
- currency: receiverAccount.user.currency,
- confirmed: true,
- received: true,
- network: "COINOS"
- };
- fee_payment.account = receiverAccount;
- payments.push(fee_payment);
+ let fee_payment_id = null;
+ if (withdrawalFee) {
+ await receiverAccount.increment({ balance: withdrawalFee }, { transaction });
+ await receiverAccount.reload({ transaction });
+ let fee_payment = {
+ amount: withdrawalFee,
+ fee: 0,
+ memo: "Liquid withdrawal fee",
+ account_id: receiverAccount.id,
+ user_id: receiverAccount.user_id,
+ rate: app.get("rates")[receiverAccount.user.currency],
+ currency: receiverAccount.user.currency,
+ confirmed: true,
+ received: true,
+ network: "COINOS"
+ };
+ fee_payment.account = receiverAccount;
+ payments.push(fee_payment);
+ fee_payment_id = fee_payment.id;
+ }
let payment = {
amount: -amount,
@@ -177,7 +189,7 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => {
confirmed: true,
received: false,
network: "liquid",
- fee_payment_id: fee_payment.id
+ fee_payment_id: fee_payment_id
};
payment.account = account;