Skip to content

Commit 3fec266

Browse files
authored
feat(express): migrate calculateminerfeeinfo to typed routes
2 parents 8ef80b3 + 5f2771d commit 3fec266

File tree

4 files changed

+381
-6
lines changed

4 files changed

+381
-6
lines changed

modules/express/src/clientRoutes.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ function handleFanOutUnspents(req: express.Request) {
297297
* @deprecated
298298
* @param req
299299
*/
300-
function handleCalculateMinerFeeInfo(req: express.Request) {
300+
function handleCalculateMinerFeeInfo(req: ExpressApiRouteRequest<'express.calculateminerfeeinfo', 'post'>) {
301301
return req.bitgo.calculateMinerFeeInfo({
302302
bitgo: req.bitgo,
303303
feeRate: req.body.feeRate,
@@ -1560,12 +1560,10 @@ export function setupAPIRoutes(app: express.Application, config: Config): void {
15601560
router.post('express.encrypt', [prepareBitGo(config), typedPromiseWrapper(handleEncrypt)]);
15611561
router.post('express.verifyaddress', [prepareBitGo(config), typedPromiseWrapper(handleVerifyAddress)]);
15621562
router.post('express.lightning.initWallet', [prepareBitGo(config), typedPromiseWrapper(handleInitLightningWallet)]);
1563-
app.post(
1564-
'/api/v[12]/calculateminerfeeinfo',
1565-
parseBody,
1563+
router.post('express.calculateminerfeeinfo', [
15661564
prepareBitGo(config),
1567-
promiseWrapper(handleCalculateMinerFeeInfo)
1568-
);
1565+
typedPromiseWrapper(handleCalculateMinerFeeInfo),
1566+
]);
15691567

15701568
app.post('/api/v1/keychain/local', parseBody, prepareBitGo(config), promiseWrapper(handleCreateLocalKeyChain));
15711569
app.post('/api/v1/keychain/derive', parseBody, prepareBitGo(config), promiseWrapper(handleDeriveLocalKeyChain));
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import * as t from 'io-ts';
2+
import { httpRoute, httpRequest, optional } from '@api-ts/io-ts-http';
3+
import { BitgoExpressError } from '../../schemas/error';
4+
5+
export const CalculateMinerFeeInfoRequestBody = {
6+
/** fee rate in satoshis per kilobyte, if not provided a fallback fee rate will be used */
7+
feeRate: optional(t.number),
8+
/** number of P2SH (multisig) inputs in the transaction */
9+
nP2shInputs: t.number,
10+
/** number of P2PKH (single sig) inputs in the transaction */
11+
nP2pkhInputs: t.number,
12+
/** number of P2SH-P2WSH (segwit) inputs in the transaction */
13+
nP2shP2wshInputs: t.number,
14+
/** number of outputs in the transaction */
15+
nOutputs: t.number,
16+
/** whether the transaction contains uncompressed public keys (affects size calculation) */
17+
containsUncompressedPublicKeys: optional(t.boolean),
18+
};
19+
20+
export const CalculateMinerFeeInfoResponse = t.type({
21+
/** estimated size of the transaction in bytes */
22+
size: t.number,
23+
/** estimated fee in satoshis for the transaction */
24+
fee: t.number,
25+
/** fee rate that was used to estimate the fee (in satoshis per kilobyte) */
26+
feeRate: t.number,
27+
});
28+
29+
/**
30+
* Calculate miner fee info
31+
*
32+
* Calculates the estimated size and fee for a transaction based on the number and types of inputs and outputs.
33+
* This is useful for estimating the fee before creating a transaction.
34+
*
35+
* The calculation takes into account:
36+
* 1. The number and types of inputs (P2SH, P2PKH, P2SH-P2WSH)
37+
* 2. The number of outputs
38+
* 3. Whether the transaction contains uncompressed public keys
39+
* 4. The fee rate (in satoshis per kilobyte)
40+
*
41+
* @operationId express.calculateminerfeeinfo
42+
*/
43+
export const PostCalculateMinerFeeInfo = httpRoute({
44+
path: '/api/v[12]/calculateminerfeeinfo',
45+
method: 'POST',
46+
request: httpRequest({
47+
body: CalculateMinerFeeInfoRequestBody,
48+
}),
49+
response: {
50+
200: CalculateMinerFeeInfoResponse,
51+
400: BitgoExpressError,
52+
404: BitgoExpressError,
53+
},
54+
});

modules/express/src/typedRoutes/api/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { PostLogin } from './common/login';
88
import { PostDecrypt } from './common/decrypt';
99
import { PostEncrypt } from './common/encrypt';
1010
import { PostVerifyAddress } from './common/verifyAddress';
11+
import { PostCalculateMinerFeeInfo } from './common/calculateMinerFeeInfo';
1112
import { PostAcceptShare } from './v1/acceptShare';
1213
import { PostSimpleCreate } from './v1/simpleCreate';
1314
import { PutPendingApproval } from './v1/pendingApproval';
@@ -52,6 +53,9 @@ export const ExpressApi = apiSpec({
5253
'express.verifycoinaddress': {
5354
post: PostVerifyCoinAddress,
5455
},
56+
'express.calculateminerfeeinfo': {
57+
post: PostCalculateMinerFeeInfo,
58+
},
5559
});
5660

5761
export type ExpressApi = typeof ExpressApi;

0 commit comments

Comments
 (0)