Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/runes-rpf #10

Merged
merged 4 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions src/adapters/satsConnectAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ abstract class SatsConnectAdapter {
result: {
orderId: orderResponse.data.orderId,
fundTransactionId: paymentResponse.result.txid,
fundingAddress: orderResponse.data.fundAddress,
},
};
} catch (error) {
Expand Down Expand Up @@ -124,6 +125,7 @@ abstract class SatsConnectAdapter {
result: {
orderId: orderResponse.data.orderId,
fundTransactionId: paymentResponse.result.txid,
fundingAddress: orderResponse.data.fundAddress,
},
};
} catch (error) {
Expand Down Expand Up @@ -219,6 +221,88 @@ abstract class SatsConnectAdapter {
};
}

private async estimateRbfOrder(
params: Params<'runes_estimateRbfOrder'>
): Promise<RpcResult<'runes_estimateRbfOrder'>> {
const rbfOrderRequest: Omit<Params<'runes_estimateRbfOrder'>, 'network'> = {
newFeeRate: params.newFeeRate,
orderId: params.orderId,
};
const response = await getRunesApiClient(params.network).rbfOrder(rbfOrderRequest);
if (response.data) {
return {
status: 'success',
result: {
fundingAddress: response.data.fundingAddress,
rbfCost: response.data.rbfCost,
},
};
}
return {
status: 'error',
error: {
code:
response.error.code === 400 ? RpcErrorCode.INVALID_REQUEST : RpcErrorCode.INTERNAL_ERROR,
m-aboelenein marked this conversation as resolved.
Show resolved Hide resolved
message: response.error.message,
},
};
}

private async rbfOrder(params: Params<'runes_rbfOrder'>): Promise<RpcResult<'runes_rbfOrder'>> {
try {
const rbfOrderRequest: Omit<Params<'runes_rbfOrder'>, 'network'> = {
newFeeRate: params.newFeeRate,
orderId: params.orderId,
};
const orderResponse = await getRunesApiClient(params.network).rbfOrder(rbfOrderRequest);
if (!orderResponse.data) {
return {
status: 'error',
error: {
code:
orderResponse.error.code === 400
m-aboelenein marked this conversation as resolved.
Show resolved Hide resolved
? RpcErrorCode.INVALID_REQUEST
: RpcErrorCode.INTERNAL_ERROR,
message: orderResponse.error.message,
},
};
}
const paymentResponse = await this.requestInternal('sendTransfer', {
recipients: [
{
address: orderResponse.data.fundingAddress,
amount: orderResponse.data.rbfCost,
},
],
});
if (paymentResponse?.status !== 'success') {
return {
status: 'error',
error: {
code: RpcErrorCode.USER_REJECTION,
message: 'User rejected the payment request',
},
};
}
return {
status: 'success',
result: {
fundingAddress: orderResponse.data.fundingAddress,
orderId: rbfOrderRequest.orderId,
fundRBFTransactionId: paymentResponse.result.txid,
},
};
} catch (error) {
return {
status: 'error',
error: {
code: RpcErrorCode.INTERNAL_ERROR,
message: error.message,
},
};
}
}

async request<Method extends keyof Requests>(
method: Method,
params: Params<Method>
Expand All @@ -239,6 +323,14 @@ abstract class SatsConnectAdapter {
case 'runes_getOrder': {
return this.getOrder(params as Params<'runes_getOrder'>) as Promise<RpcResult<Method>>;
}
case 'runes_estimateRbfOrder': {
return this.estimateRbfOrder(params as Params<'runes_estimateRbfOrder'>) as Promise<
RpcResult<Method>
>;
}
case 'runes_rbfOrder': {
return this.rbfOrder(params as Params<'runes_rbfOrder'>) as Promise<RpcResult<Method>>;
}
default:
return this.requestInternal(method, params);
}
Expand Down
4 changes: 4 additions & 0 deletions src/request/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import {
SignPsbt,
} from './btcMethods';
import {
EstimateRbfOrder,
EstimateRunesEtch,
EstimateRunesMint,
EtchRunes,
GetOrder,
MintRunes,
RbfOrder,
} from './runesMethods';
import {
StxCallContract,
Expand Down Expand Up @@ -54,6 +56,8 @@ export interface RunesRequests {
runes_estimateEtch: EstimateRunesEtch;
runes_etch: EtchRunes;
runes_getOrder: GetOrder;
runes_estimateRbfOrder: EstimateRbfOrder;
runes_rbfOrder: RbfOrder;
}

export type RunesRequestMethod = keyof RunesRequests;
Expand Down
22 changes: 22 additions & 0 deletions src/request/types/runesMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
EstimateOrderResponse,
GetOrderRequest,
GetOrderResponse,
RBFOrderRequest,
RBFOrderResponse,
} from '../../runes/types';
import { BitcoinNetworkType, MethodParamsAndResult } from '../../types';

Expand All @@ -27,6 +29,7 @@ export interface MintRunesParams extends CreateMintOrderRequest {
export type MintRunesResult = {
orderId: string;
fundTransactionId: string;
fundingAddress: string;
};

export type MintRunes = MethodParamsAndResult<MintRunesParams, MintRunesResult>;
Expand All @@ -49,6 +52,7 @@ export interface EtchRunesParams extends CreateEtchOrderRequest {
export type EtchRunesResult = {
orderId: string;
fundTransactionId: string;
fundingAddress: string;
};

export type EtchRunes = MethodParamsAndResult<EtchRunesParams, EtchRunesResult>;
Expand All @@ -58,3 +62,21 @@ interface GetOrderParams extends GetOrderRequest {
}

export type GetOrder = MethodParamsAndResult<GetOrderParams, GetOrderResponse>;

interface EstimateRbfOrderParams extends RBFOrderRequest {
network?: BitcoinNetworkType;
}

export type EstimateRbfOrder = MethodParamsAndResult<EstimateRbfOrderParams, RBFOrderResponse>;

interface RbfOrderParams extends RBFOrderRequest {
network?: BitcoinNetworkType;
}

interface RbfOrderResult {
orderId: string;
fundRBFTransactionId: string;
fundingAddress: string;
}

export type RbfOrder = MethodParamsAndResult<RbfOrderParams, RbfOrderResult>;
19 changes: 19 additions & 0 deletions src/runes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
EstimateMintOrderRequest,
EstimateOrderResponse,
GetOrderResponse,
RBFOrderRequest,
RBFOrderResponse,
} from './types';
import { BitcoinNetworkType } from '../types';

Expand Down Expand Up @@ -139,6 +141,23 @@ export class RunesApi {
};
}
};

rbfOrder = async (rbfRequest: RBFOrderRequest) => {
const { orderId, newFeeRate } = rbfRequest;
try {
const response = await this.client.post<RBFOrderResponse>(`/orders/${orderId}/rbf-estimate`, {
newFeeRate,
});
return {
data: response.data,
};
} catch (error) {
const err = error as AxiosError;
return {
error: this.parseError(err),
};
}
};
}

const testnetClient = new RunesApi(BitcoinNetworkType.Testnet);
Expand Down
11 changes: 11 additions & 0 deletions src/runes/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ export type GetOrderResponse = {
id: string;
orderType: 'rune_mint' | 'rune_etch';
state: 'new' | 'pending' | 'executing' | 'complete' | 'failed' | 'refunded' | 'stale';
fundingAddress: string;
reason?: string;
createdAt: string;
};

export type RBFOrderRequest = {
orderId: string;
newFeeRate: number;
};

export type RBFOrderResponse = {
rbfCost: number;
fundingAddress: string;
};