aboutsummaryrefslogtreecommitdiffstats
path: root/routes/liquid
diff options
context:
space:
mode:
authorAdrien Hopkins <[email protected]>2022-06-16 14:48:27 -0500
committerAdrien Hopkins <[email protected]>2022-06-16 14:48:27 -0500
commitc66b9c6b5be4b1b524ee2675dd1f65f425d72ee9 (patch)
treeeba3a920bd31da13e76afea2835c1f6e515d1d11 /routes/liquid
parentffbd27b2e9ddcb52a37febc0855e16b4fe09dbdc (diff)
parent67e2ec07cf90cfbd1b5c1cbed18c4dffe38ea4aa (diff)
downloadcoinos-server-c66b9c6b5be4b1b524ee2675dd1f65f425d72ee9.tar.gz
coinos-server-c66b9c6b5be4b1b524ee2675dd1f65f425d72ee9.zip
Merge branch 'feature-withdrawal-fee'
Diffstat (limited to 'routes/liquid')
-rw-r--r--routes/liquid/conversionFee.js12
-rw-r--r--routes/liquid/receive.js5
-rw-r--r--routes/liquid/send.js46
3 files changed, 36 insertions, 27 deletions
diff --git a/routes/liquid/conversionFee.js b/routes/liquid/conversionFee.js
new file mode 100644
index 00000000..d365185b
--- /dev/null
+++ b/routes/liquid/conversionFee.js
@@ -0,0 +1,12 @@
+// Code for the conversion fee
+// This is in a separate file so that the same function can be used by ./send and ./receive
+
+const conversionFeeMultiplier = 0.01;
+const conversionFeeReceiver = "coinosfees"; // account that receives the fees
+
+/**
+ * Returns the fee you have to pay for withdrawing 'amount' L-BTC.
+ */
+function computeConversionFee(amount) {
+ return Math.floor(amount * conversionFeeMultiplier);
+}
diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js
index 9650e5b7..2bd86805 100644
--- a/routes/liquid/receive.js
+++ b/routes/liquid/receive.js
@@ -13,8 +13,7 @@ const network =
config.liquid.network === "mainnet" ? "liquid" : config.liquid.network
];
-// used to calculate how many credits to give
-const withdrawalFeeMultiplier = 0.01;
+const { computeConversionFee } = require('./conversionFee.js');
const getAccount = async (params, transaction) => {
let account = await db.Account.findOne({
@@ -344,7 +343,7 @@ setInterval(async () => {
{ pending: Math.min(p.account.pending, total) },
{ transaction }
);
- await p.account.increment({ fee_credits: Math.floor(total * withdrawalFeeMultiplier) }, { transaction });
+ await p.account.increment({ liquid_credits: computeConversionFee(total) }, { transaction });
await p.account.reload({ transaction });
await p.save({ transaction });
diff --git a/routes/liquid/send.js b/routes/liquid/send.js
index fcb05942..254972ad 100644
--- a/routes/liquid/send.js
+++ b/routes/liquid/send.js
@@ -1,8 +1,7 @@
const btc = config.liquid.btcasset;
const lcad = config.liquid.cadasset;
const { Transaction } = require("liquidjs-lib");
-const withdrawalFeeMultiplier = 0.01; // 1% withdrawal fee
-const withdrawalFeeReceiver = "coinosfees"; // 1% withdrawal fee
+const { computeConversionFee, conversionFeeReceiver } = require('./conversionFee.js');
sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => {
try {
@@ -84,10 +83,10 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => {
total += fee - covered;
}
- // get withdrawal fee
- // 'total' refers to the total before the withdrawal fee
+ // get conversion fee
+ // 'total' refers to the total before the conversion fee
// (i.e. the total liquid bitcoin that leaves this server)
- var withdrawalFee = Math.floor(amount * withdrawalFeeMultiplier);
+ var conversionFee = computeConversionFee(amount);
if (limit && total > limit + fee)
throw new Error("Tx amount exceeds authorized amount");
@@ -106,6 +105,14 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => {
transaction
});
+ // use user's credits to reduce fee, if available
+ let conversionFeeDeduction = Math.min(account.liquid_credits, conversionFee);
+ if (conversionFeeDeduction) {
+ await account.decrement({ liquid_credits: conversionFeeDeduction }, { transaction });
+ await account.reload({ transaction });
+ conversionFee -= conversionFeeDeduction;
+ }
+
if (total > account.balance) {
l.warn("amount exceeds balance", {
total,
@@ -117,35 +124,26 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => {
account.ticker === "BTC" ? "SAT" : account.ticker
}, have ${account.balance}`
);
- } else if (total + withdrawalFee > account.balance) {
- l.warn("amount plus withdrawal fee exceeds balance", {
+ } else if (total + conversionFee > account.balance) {
+ l.warn("amount plus conversion fee exceeds balance", {
total,
fee,
- withdrawalFee,
+ conversionFee,
balance: account.balance
});
throw new Error(
- `Insufficient funds, need ${total + withdrawalFee} ${
+ `Insufficient funds, need ${total + conversionFee} ${
account.ticker === "BTC" ? "SAT" : account.ticker
}, have ${account.balance}`
);
}
- // 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 });
- withdrawalFee -= withdrawalFeeDeduction;
- }
-
- await account.decrement({ balance: (total + withdrawalFee)}, { transaction });
+ await account.decrement({ balance: (total + conversionFee)}, { transaction });
await account.reload({ transaction });
let receiverAccount = await db.Account.findOne({
where: {
- "$user.username$": withdrawalFeeReceiver
+ "$user.username$": conversionFeeReceiver
},
include: [
{
@@ -158,13 +156,13 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => {
});
let fee_payment_id = null;
- if (withdrawalFee) {
- await receiverAccount.increment({ balance: withdrawalFee }, { transaction });
+ if (conversionFee) {
+ await receiverAccount.increment({ balance: conversionFee }, { transaction });
await receiverAccount.reload({ transaction });
let fee_payment = {
- amount: withdrawalFee,
+ amount: conversionFee,
fee: 0,
- memo: "Liquid withdrawal fee",
+ memo: "Liquid conversion fee",
account_id: receiverAccount.id,
user_id: receiverAccount.user_id,
rate: app.get("rates")[receiverAccount.user.currency],