aboutsummaryrefslogtreecommitdiffstats
path: root/routes/liquid
diff options
context:
space:
mode:
authorAdrien Hopkins <[email protected]>2022-06-13 16:16:37 -0500
committerAdrien Hopkins <[email protected]>2022-06-13 16:16:37 -0500
commitf37c8ba68418a2a325f45a7a36c139cb5bc696aa (patch)
tree98759089e8015931030877fb064507150ade7c36 /routes/liquid
parent9527f8a68f6a0a329c82dd8350703d10070437d5 (diff)
parent1c02aa63933a234faa45eecbfdc82dae7f6e174c (diff)
downloadcoinos-server-f37c8ba68418a2a325f45a7a36c139cb5bc696aa.tar.gz
coinos-server-f37c8ba68418a2a325f45a7a36c139cb5bc696aa.zip
Merge branch 'feature-withdrawal-fee'
Diffstat (limited to 'routes/liquid')
-rw-r--r--routes/liquid/receive.js4
-rw-r--r--routes/liquid/send.js66
2 files changed, 68 insertions, 2 deletions
diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js
index d2864872..0eed35fd 100644
--- a/routes/liquid/receive.js
+++ b/routes/liquid/receive.js
@@ -13,6 +13,9 @@ const network =
config.liquid.network === "mainnet" ? "liquid" : config.liquid.network
];
+// used to calculate how many credits to give
+const withdrawalFeeMultiplier = 0.01;
+
const getAccount = async (params, transaction) => {
let account = await db.Account.findOne({
where: params,
@@ -341,6 +344,7 @@ setInterval(async () => {
{ pending: Math.min(p.account.pending, total) },
{ transaction }
);
+ await account.increment({ fee_credits: Math.floor(total * withdrawalFeeMultiplier) }, { transaction });
await p.account.reload({ transaction });
await p.save({ transaction });
diff --git a/routes/liquid/send.js b/routes/liquid/send.js
index 6f3616e8..fcb05942 100644
--- a/routes/liquid/send.js
+++ b/routes/liquid/send.js
@@ -84,6 +84,11 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => {
total += fee - covered;
}
+ // get withdrawal fee
+ // 'total' refers to the total before the withdrawal fee
+ // (i.e. the total liquid bitcoin that leaves this server)
+ var withdrawalFee = Math.floor(amount * withdrawalFeeMultiplier);
+
if (limit && total > limit + fee)
throw new Error("Tx amount exceeds authorized amount");
@@ -112,11 +117,67 @@ 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", {
+ total,
+ fee,
+ withdrawalFee,
+ balance: account.balance
+ });
+ throw new Error(
+ `Insufficient funds, need ${total + withdrawalFee} ${
+ 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 }, { transaction });
+ await account.decrement({ balance: (total + withdrawalFee)}, { transaction });
await account.reload({ transaction });
+ let receiverAccount = await db.Account.findOne({
+ where: {
+ "$user.username$": withdrawalFeeReceiver
+ },
+ include: [
+ {
+ model: db.User,
+ as: "user"
+ },
+ ],
+ lock: transaction.LOCK.UPDATE,
+ transaction
+ });
+
+ 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,
account_id: account.id,
@@ -128,7 +189,8 @@ sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => {
address,
confirmed: true,
received: false,
- network: "liquid"
+ network: "liquid",
+ fee_payment_id: fee_payment_id
};
payment.account = account;