Skip to content

Commit d0872b1

Browse files
committed
api: update types in the Loop and Pool API classes
1 parent 7092d1e commit d0872b1

File tree

2 files changed

+51
-48
lines changed

2 files changed

+51
-48
lines changed

app/src/api/loop.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class LoopApi extends BaseApi<LoopEvents> {
5959
confTarget?: number,
6060
): Promise<LOOP.InQuoteResponse.AsObject> {
6161
const req = new LOOP.QuoteRequest();
62-
req.setAmt(+amount);
62+
req.setAmt(amount.toString());
6363
if (confTarget) req.setConfTarget(confTarget);
6464
const res = await this._grpc.request(SwapClient.GetLoopInQuote, req, this._meta);
6565
return res.toObject();
@@ -73,7 +73,7 @@ class LoopApi extends BaseApi<LoopEvents> {
7373
confTarget?: number,
7474
): Promise<LOOP.OutQuoteResponse.AsObject> {
7575
const req = new LOOP.QuoteRequest();
76-
req.setAmt(+amount);
76+
req.setAmt(amount.toString());
7777
if (confTarget) req.setConfTarget(confTarget);
7878
const res = await this._grpc.request(SwapClient.LoopOutQuote, req, this._meta);
7979
return res.toObject();
@@ -89,9 +89,9 @@ class LoopApi extends BaseApi<LoopEvents> {
8989
confTarget?: number,
9090
): Promise<LOOP.SwapResponse.AsObject> {
9191
const req = new LOOP.LoopInRequest();
92-
req.setAmt(+amount);
93-
req.setMaxSwapFee(+quote.swapFee);
94-
req.setMaxMinerFee(+quote.minerFee);
92+
req.setAmt(amount.toString());
93+
req.setMaxSwapFee(quote.swapFee.toString());
94+
req.setMaxMinerFee(quote.minerFee.toString());
9595
req.setInitiator(LOOP_INITIATOR);
9696
if (lastHop) req.setLastHop(b64(lastHop));
9797
if (confTarget) req.setHtlcConfTarget(confTarget);
@@ -105,20 +105,20 @@ class LoopApi extends BaseApi<LoopEvents> {
105105
async loopOut(
106106
amount: Big,
107107
quote: Quote,
108-
chanIds: number[],
108+
chanIds: string[],
109109
deadline: number,
110110
confTarget?: number,
111111
destAddress?: string,
112112
): Promise<LOOP.SwapResponse.AsObject> {
113113
const req = new LOOP.LoopOutRequest();
114-
req.setAmt(+amount);
115-
req.setMaxSwapFee(+quote.swapFee);
116-
req.setMaxMinerFee(+quote.minerFee);
117-
req.setMaxPrepayAmt(+quote.prepayAmount);
118-
req.setMaxSwapRoutingFee(this._calcRoutingFee(+amount));
119-
req.setMaxPrepayRoutingFee(this._calcRoutingFee(+quote.prepayAmount));
120-
req.setOutgoingChanSetList(chanIds);
121-
req.setSwapPublicationDeadline(deadline);
114+
req.setAmt(amount.toString());
115+
req.setMaxSwapFee(quote.swapFee.toString());
116+
req.setMaxMinerFee(quote.minerFee.toString());
117+
req.setMaxPrepayAmt(quote.prepayAmount.toString());
118+
req.setMaxSwapRoutingFee(this._calcRoutingFee(amount).toString());
119+
req.setMaxPrepayRoutingFee(this._calcRoutingFee(quote.prepayAmount).toString());
120+
req.setOutgoingChanSetList(chanIds.map(id => id.toString()));
121+
req.setSwapPublicationDeadline(deadline.toString());
122122
req.setInitiator(LOOP_INITIATOR);
123123
if (confTarget) req.setSweepConfTarget(confTarget);
124124
if (destAddress) req.setDest(destAddress);
@@ -138,15 +138,16 @@ class LoopApi extends BaseApi<LoopEvents> {
138138
this._meta,
139139
);
140140
}
141+
141142
/**
142143
* Calculates the max routing fee params for loop out. this mimics the loop cli
143144
* behavior of using 2% of the amount
144145
* @param amount the amount of the payment
145146
*/
146-
private _calcRoutingFee(amount: number) {
147-
const routingFeePct = 2;
147+
private _calcRoutingFee(amount: Big): Big {
148+
const routingFeeFactor = 0.02;
148149
// round up to avoid decimals
149-
return Math.ceil(amount * (routingFeePct / 100));
150+
return amount.mul(routingFeeFactor).round(0, Big.roundUp);
150151
}
151152
}
152153

app/src/api/pool.ts

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as AUCT from 'types/generated/auctioneerrpc/auctioneer_pb';
22
import * as POOL from 'types/generated/trader_pb';
33
import { Trader } from 'types/generated/trader_pb_service';
4+
import Big from 'big.js';
45
import { b64 } from 'util/strings';
56
import { OrderType, Tier } from 'store/models/order';
67
import BaseApi from './base';
@@ -43,11 +44,11 @@ class PoolApi extends BaseApi<PoolEvents> {
4344
* call the pool `QuoteAccount` RPC and return the response
4445
*/
4546
async quoteAccount(
46-
amount: number,
47+
amount: Big,
4748
confTarget: number,
4849
): Promise<POOL.QuoteAccountResponse.AsObject> {
4950
const req = new POOL.QuoteAccountRequest();
50-
req.setAccountValue(amount);
51+
req.setAccountValue(amount.toString());
5152
req.setConfTarget(confTarget);
5253
const res = await this._grpc.request(Trader.QuoteAccount, req, this._meta);
5354
return res.toObject();
@@ -57,12 +58,12 @@ class PoolApi extends BaseApi<PoolEvents> {
5758
* call the pool `InitAccount` RPC and return the response
5859
*/
5960
async initAccount(
60-
amount: number,
61+
amount: Big,
6162
expiryBlocks: number,
6263
confTarget = 6,
6364
): Promise<POOL.Account.AsObject> {
6465
const req = new POOL.InitAccountRequest();
65-
req.setAccountValue(amount);
66+
req.setAccountValue(amount.toString());
6667
req.setRelativeHeight(expiryBlocks);
6768
req.setConfTarget(confTarget);
6869
req.setInitiator(POOL_INITIATOR);
@@ -76,12 +77,12 @@ class PoolApi extends BaseApi<PoolEvents> {
7677
async renewAccount(
7778
traderKey: string,
7879
expiryBlocks: number,
79-
feeRateSatPerKw: number,
80+
feeRateSatPerKw: Big,
8081
): Promise<POOL.RenewAccountResponse.AsObject> {
8182
const req = new POOL.RenewAccountRequest();
8283
req.setAccountKey(b64(traderKey));
8384
req.setRelativeExpiry(expiryBlocks);
84-
req.setFeeRateSatPerKw(feeRateSatPerKw);
85+
req.setFeeRateSatPerKw(feeRateSatPerKw.toString());
8586

8687
const res = await this._grpc.request(Trader.RenewAccount, req, this._meta);
8788
return res.toObject();
@@ -99,7 +100,7 @@ class PoolApi extends BaseApi<PoolEvents> {
99100
req.setTraderKey(b64(traderKey));
100101

101102
const output = new POOL.OutputWithFee();
102-
output.setFeeRateSatPerKw(feeRateSatPerKw);
103+
output.setFeeRateSatPerKw(feeRateSatPerKw.toString());
103104
if (destinationAddr) {
104105
output.setAddress(destinationAddr);
105106
}
@@ -123,13 +124,13 @@ class PoolApi extends BaseApi<PoolEvents> {
123124
*/
124125
async deposit(
125126
traderKey: string,
126-
amount: number,
127+
amount: Big,
127128
feeRateSatPerKw = 253,
128129
): Promise<POOL.DepositAccountResponse.AsObject> {
129130
const req = new POOL.DepositAccountRequest();
130131
req.setTraderKey(Buffer.from(traderKey, 'hex').toString('base64'));
131-
req.setAmountSat(amount);
132-
req.setFeeRateSatPerKw(feeRateSatPerKw);
132+
req.setAmountSat(amount.toString());
133+
req.setFeeRateSatPerKw(feeRateSatPerKw.toString());
133134
const res = await this._grpc.request(Trader.DepositAccount, req, this._meta);
134135
return res.toObject();
135136
}
@@ -139,14 +140,14 @@ class PoolApi extends BaseApi<PoolEvents> {
139140
*/
140141
async withdraw(
141142
traderKey: string,
142-
amount: number,
143+
amount: Big,
143144
feeRateSatPerKw = 253,
144145
): Promise<POOL.WithdrawAccountResponse.AsObject> {
145146
const req = new POOL.WithdrawAccountRequest();
146147
req.setTraderKey(Buffer.from(traderKey, 'hex').toString('base64'));
147-
req.setFeeRateSatPerKw(feeRateSatPerKw);
148+
req.setFeeRateSatPerKw(feeRateSatPerKw.toString());
148149
const output = new POOL.Output();
149-
output.setValueSat(amount);
150+
output.setValueSat(amount.toString());
150151
req.setOutputsList([output]);
151152
const res = await this._grpc.request(Trader.WithdrawAccount, req, this._meta);
152153
return res.toObject();
@@ -165,18 +166,18 @@ class PoolApi extends BaseApi<PoolEvents> {
165166
* call the pool `QuoteOrder` RPC and return the response
166167
*/
167168
async quoteOrder(
168-
amount: number,
169+
amount: Big,
169170
rateFixed: number,
170171
duration: number,
171172
minUnitsMatch: number,
172-
feeRateSatPerKw: number,
173+
feeRateSatPerKw = 253,
173174
): Promise<POOL.QuoteOrderResponse.AsObject> {
174175
const req = new POOL.QuoteOrderRequest();
175-
req.setAmt(amount);
176+
req.setAmt(amount.toString());
176177
req.setRateFixed(rateFixed);
177178
req.setLeaseDurationBlocks(duration);
178179
req.setMinUnitsMatch(minUnitsMatch);
179-
req.setMaxBatchFeeRateSatPerKw(feeRateSatPerKw);
180+
req.setMaxBatchFeeRateSatPerKw(feeRateSatPerKw.toString());
180181

181182
const res = await this._grpc.request(Trader.QuoteOrder, req, this._meta);
182183
return res.toObject();
@@ -188,11 +189,11 @@ class PoolApi extends BaseApi<PoolEvents> {
188189
async submitOrder(
189190
traderKey: string,
190191
type: OrderType,
191-
amount: number,
192+
amount: Big,
192193
rateFixed: number,
193194
duration: number,
194195
minUnitsMatch: number,
195-
feeRateSatPerKw: number,
196+
feeRateSatPerKw = 253,
196197
minNodeTier?: Tier,
197198
): Promise<POOL.SubmitOrderResponse.AsObject> {
198199
if (rateFixed < 1) {
@@ -204,10 +205,10 @@ class PoolApi extends BaseApi<PoolEvents> {
204205

205206
const order = new POOL.Order();
206207
order.setTraderKey(b64(traderKey));
207-
order.setAmt(amount);
208+
order.setAmt(amount.toString());
208209
order.setRateFixed(rateFixed);
209210
order.setMinUnitsMatch(minUnitsMatch);
210-
order.setMaxBatchFeeRateSatPerKw(feeRateSatPerKw);
211+
order.setMaxBatchFeeRateSatPerKw(feeRateSatPerKw.toString());
211212

212213
switch (type) {
213214
case OrderType.Bid:
@@ -324,36 +325,37 @@ class PoolApi extends BaseApi<PoolEvents> {
324325
* Converts from sats per kilo-weight to sats per vByte
325326
* @param satsPerVByte the number of sats per kilo-weight
326327
*/
327-
satsPerKWeightToVByte(satsPerKWeight: number) {
328+
satsPerKWeightToVByte(satsPerKWeight: Big) {
328329
// convert to kilo-vbyte
329-
const satsPerKVByte = satsPerKWeight * 4;
330+
const satsPerKVByte = satsPerKWeight.mul(4);
330331
// convert to vbyte
331-
return satsPerKVByte / 1000;
332+
return satsPerKVByte.div(1000);
332333
}
333334

334335
/**
335336
* Calculates the per block fixed rate given an amount and premium
336337
* @param amount the amount of the order
337338
* @param premium the premium being paid
339+
* @param duration the lease duration in blocks
338340
*/
339-
calcFixedRate(amount: number, premium: number, duration: number) {
340-
const ratePct = (premium * 100) / amount;
341+
calcFixedRate(amount: Big, premium: Big, duration: number) {
342+
const ratePct = premium.mul(100).div(amount);
341343
// rate = % / 100
342344
// rate = rateFixed / totalParts
343345
// rateFixed = rate * totalParts
344-
const interestRate = ratePct / 100;
345-
const rateFixedFloat = interestRate * FEE_RATE_TOTAL_PARTS;
346+
const interestRate = ratePct.div(100);
347+
const rateFixedFloat = interestRate.mul(FEE_RATE_TOTAL_PARTS);
346348
// We then take this rate fixed, and divide it by the number of blocks
347349
// as the user wants this rate to be the final lump sum they pay.
348-
return Math.floor(rateFixedFloat / duration);
350+
return +rateFixedFloat.div(duration).round(0, Big.roundDown);
349351
}
350352

351353
/**
352354
* Calculates the percentage interest rate for a given fixed rate
353355
* @param fixedRate the per block fixed rate
354356
*/
355-
calcPctRate(fixedRate: number, duration: number) {
356-
return (fixedRate * duration) / FEE_RATE_TOTAL_PARTS;
357+
calcPctRate(fixedRate: Big, duration: Big) {
358+
return +fixedRate.mul(duration).div(FEE_RATE_TOTAL_PARTS);
357359
}
358360
}
359361

0 commit comments

Comments
 (0)