Skip to content

Commit

Permalink
code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
m-aboelenein committed Apr 5, 2024
1 parent 759aae1 commit 18ecd6e
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 102 deletions.
193 changes: 120 additions & 73 deletions src/adapters/satsConnectAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RunesApi } from '../runes';
import { getRunesApiClient, RunesApi } from '../runes';
import { Params, Requests } from '../request';
import { RpcErrorCode, RpcResult } from '../types';

Expand All @@ -8,45 +8,46 @@ abstract class SatsConnectAdapter {
private async mintRunes(params: Params<'runes_mint'>): Promise<RpcResult<'runes_mint'>> {
try {
const orderResponse = await new RunesApi(params.network).createMintOrder(params);
if (orderResponse.data) {
const paymentResponse = await this.requestInternal('sendTransfer', {
recipients: [
{
address: orderResponse.data.fundAddress,
amount: orderResponse.data.fundAmount,
},
],
});
if (paymentResponse?.status === 'success') {
await new RunesApi(params.network).executeMint(
orderResponse.data.orderId,
paymentResponse.result.txid
);
return {
status: 'success',
result: {
orderId: orderResponse.data.orderId,
fundTransactionId: paymentResponse.result.txid,
},
};
} else {
return {
status: 'error',
error: {
code: RpcErrorCode.USER_REJECTION,
message: 'User rejected the payment request',
},
};
}
} else {
if (!orderResponse.data) {
return {
status: 'error',
error: {
code: RpcErrorCode.INTERNAL_ERROR,
message: orderResponse.error,
code:
orderResponse.error.code === 400
? RpcErrorCode.INVALID_REQUEST
: RpcErrorCode.INTERNAL_ERROR,
message: orderResponse.error.message,
},
};
}
const paymentResponse = await this.requestInternal('sendTransfer', {
recipients: [
{
address: orderResponse.data.fundAddress,
amount: orderResponse.data.fundAmount,
},
],
});
if (paymentResponse?.status !== 'success') {
return {
status: 'error',
error: {
code: RpcErrorCode.USER_REJECTION,
message: 'User rejected the payment request',
},
};
}
await new RunesApi(params.network).executeMint(
orderResponse.data.orderId,
paymentResponse.result.txid
);
return {
status: 'success',
result: {
orderId: orderResponse.data.orderId,
fundTransactionId: paymentResponse.result.txid,
},
};
} catch (error) {
return {
status: 'error',
Expand All @@ -61,45 +62,46 @@ abstract class SatsConnectAdapter {
private async etchRunes(params: Params<'runes_etch'>): Promise<RpcResult<'runes_etch'>> {
try {
const orderResponse = await new RunesApi(params.network).createEtchOrder(params);
if (orderResponse.data) {
const paymentResponse = await this.requestInternal('sendTransfer', {
recipients: [
{
address: orderResponse.data.fundAddress,
amount: orderResponse.data.fundAmount,
},
],
});
if (paymentResponse?.status === 'success') {
await new RunesApi(params.network).executeEtch(
orderResponse.data.orderId,
paymentResponse.result.txid
);
return {
status: 'success',
result: {
orderId: orderResponse.data.orderId,
fundTransactionId: paymentResponse.result.txid,
},
};
} else {
return {
status: 'error',
error: {
code: RpcErrorCode.USER_REJECTION,
message: 'User rejected the payment request',
},
};
}
} else {
if (!orderResponse.data) {
return {
status: 'error',
error: {
code: RpcErrorCode.INTERNAL_ERROR,
message: orderResponse.error,
code:
orderResponse.error.code === 400
? RpcErrorCode.INVALID_REQUEST
: RpcErrorCode.INTERNAL_ERROR,
message: orderResponse.error.message,
},
};
}
const paymentResponse = await this.requestInternal('sendTransfer', {
recipients: [
{
address: orderResponse.data.fundAddress,
amount: orderResponse.data.fundAmount,
},
],
});
if (paymentResponse?.status !== 'success') {
return {
status: 'error',
error: {
code: RpcErrorCode.USER_REJECTION,
message: 'User rejected the payment request',
},
};
}
await new RunesApi(params.network).executeEtch(
orderResponse.data.orderId,
paymentResponse.result.txid
);
return {
status: 'success',
result: {
orderId: orderResponse.data.orderId,
fundTransactionId: paymentResponse.result.txid,
},
};
} catch (error) {
return {
status: 'error',
Expand All @@ -111,6 +113,50 @@ abstract class SatsConnectAdapter {
}
}

private async estimateMint(
params: Params<'runes_estimateMint'>
): Promise<RpcResult<'runes_estimateMint'>> {
const response = await getRunesApiClient(
(params as Params<'runes_estimateMint'>).network
).estimateMintCost(params as Params<'runes_estimateMint'>);
if (response.data) {
return {
status: 'success',
result: response.data,
};
}
return {
status: 'error',
error: {
code:
response.error.code === 400 ? RpcErrorCode.INVALID_REQUEST : RpcErrorCode.INTERNAL_ERROR,
message: response.error.message,
},
};
}

private async estimateEtch(
params: Params<'runes_estimateEtch'>
): Promise<RpcResult<'runes_estimateMint'>> {
const response = await getRunesApiClient(
(params as Params<'runes_estimateEtch'>).network
).estimateEtchCost(params as Params<'runes_estimateEtch'>);
if (response.data) {
return {
status: 'success',
result: response.data,
};
}
return {
status: 'error',
error: {
code:
response.error.code === 400 ? RpcErrorCode.INVALID_REQUEST : RpcErrorCode.INTERNAL_ERROR,
message: response.error.message,
},
};
}

async request<Method extends keyof Requests>(
method: Method,
params: Params<Method>
Expand All @@ -121,13 +167,14 @@ abstract class SatsConnectAdapter {
case 'runes_etch':
return this.etchRunes(params as Params<'runes_etch'>) as Promise<RpcResult<Method>>;
case 'runes_estimateMint':
return new RunesApi((params as Params<'runes_estimateMint'>).network).estimateMintCost(
params as Params<'runes_estimateMint'>
) as Promise<RpcResult<Method>>;
return this.estimateMint(params as Params<'runes_estimateMint'>) as Promise<
RpcResult<Method>
>;
case 'runes_estimateEtch':
return new RunesApi((params as Params<'runes_estimateEtch'>).network).estimateEtchCost(
params as Params<'runes_estimateEtch'>
) as Promise<RpcResult<Method>>;
return this.estimateEtch(params as Params<'runes_estimateEtch'>) as Promise<
RpcResult<Method>
>;

default:
return this.requestInternal(method, params);
}
Expand Down
64 changes: 35 additions & 29 deletions src/runes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from './types';
import { BitcoinNetworkType, RpcErrorCode, RpcResult } from '../types';

import axios, { AxiosInstance } from 'axios';
import axios, { Axios, AxiosError, AxiosInstance } from 'axios';

export const RUNES_API_BASE_URL = (network: BitcoinNetworkType = BitcoinNetworkType.Mainnet) =>
`https://ordinals${network === BitcoinNetworkType.Testnet ? '-testnet' : ''}.xverse.app/v1/runes`;
Expand All @@ -22,52 +22,38 @@ export class RunesApi {
});
}

estimateMintCost = async (
mintParams: EstimateMintOrderRequest
): Promise<RpcResult<'runes_estimateMint'>> => {
estimateMintCost = async (mintParams: EstimateMintOrderRequest) => {
try {
const response = await this.client.post<EstimateOrderResponse>('/mint/estimate', {
...mintParams,
});
return {
status: 'success',
result: {
costBreakdown: response.data.costBreakdown,
totalCost: response.data.totalCost,
totalSize: response.data.totalSize,
},
data: response.data,
};
} catch (error) {
const err = error as AxiosError;
return {
status: 'error',
error: {
code: RpcErrorCode.INTERNAL_ERROR,
message: error.message,
code: err.response?.status,
message: error.data.message,
},
};
}
};

estimateEtchCost = async (
etchParams: EstimateEtchOrderRequest
): Promise<RpcResult<'runes_estimateEtch'>> => {
estimateEtchCost = async (etchParams: EstimateEtchOrderRequest) => {
try {
const response = await this.client.post<EstimateOrderResponse>('/etch/estimate', {
...etchParams,
});
return {
status: 'success',
result: {
costBreakdown: response.data.costBreakdown,
totalCost: response.data.totalCost,
totalSize: response.data.totalSize,
},
data: response.data,
};
} catch (error) {
const err = error as AxiosError;
return {
status: 'error',
error: {
code: RpcErrorCode.INTERNAL_ERROR,
code: err.response?.status,
message: error.data.message,
},
};
Expand All @@ -83,8 +69,12 @@ export class RunesApi {
data: response.data,
};
} catch (error) {
const err = error as AxiosError;
return {
error: error.data.message,
error: {
code: err.response?.status,
message: error.data.message,
},
};
}
};
Expand All @@ -98,8 +88,12 @@ export class RunesApi {
data: response.data,
};
} catch (error) {
const err = error as AxiosError;
return {
error: error.data.message,
error: {
code: err.response?.status,
message: error.data.message,
},
};
}
};
Expand All @@ -113,8 +107,12 @@ export class RunesApi {
data: response.data,
};
} catch (error) {
const err = error as AxiosError;
return {
error: error.data.message,
error: {
code: err.response?.status,
message: error.data.message,
},
};
}
};
Expand All @@ -128,11 +126,19 @@ export class RunesApi {
data: response.data,
};
} catch (error) {
const err = error as AxiosError;
return {
error: error.data.message,
error: {
code: err.response?.status,
message: error.data.message,
},
};
}
};
}

export default RunesApi;
const testnetClient = new RunesApi(BitcoinNetworkType.Testnet);
const mainnetClient = new RunesApi(BitcoinNetworkType.Mainnet);

export const getRunesApiClient = (network: BitcoinNetworkType = BitcoinNetworkType.Mainnet) =>
network === BitcoinNetworkType.Mainnet ? mainnetClient : testnetClient;

0 comments on commit 18ecd6e

Please sign in to comment.