Skip to content

Commit

Permalink
Add get order method
Browse files Browse the repository at this point in the history
  • Loading branch information
aryzing committed Apr 10, 2024
1 parent ade293f commit ea14456
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 13 deletions.
22 changes: 21 additions & 1 deletion src/adapters/satsConnectAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,24 @@ abstract class SatsConnectAdapter {
};
}

private async getOrder(params: Params<'runes_getOrder'>): Promise<RpcResult<'runes_getOrder'>> {
const response = await getRunesApiClient(params.network).getOrder(params.id);
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 @@ -218,7 +236,9 @@ abstract class SatsConnectAdapter {
return this.estimateEtch(params as Params<'runes_estimateEtch'>) as Promise<
RpcResult<Method>
>;

case 'runes_getOrder': {
return this.getOrder(params as Params<'runes_getOrder'>) as Promise<RpcResult<Method>>;
}
default:
return this.requestInternal(method, params);
}
Expand Down
9 changes: 8 additions & 1 deletion src/request/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import {
SignMessage,
SignPsbt,
} from './btcMethods';
import { EstimateRunesEtch, EstimateRunesMint, EtchRunes, MintRunes } from './runesMethods';
import {
EstimateRunesEtch,
EstimateRunesMint,
EtchRunes,
GetOrder,
MintRunes,
} from './runesMethods';
import {
StxCallContract,
StxDeployContract,
Expand Down Expand Up @@ -47,6 +53,7 @@ export interface RunesRequests {
runes_mint: MintRunes;
runes_estimateEtch: EstimateRunesEtch;
runes_etch: EtchRunes;
runes_getOrder: GetOrder;
}

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

Expand Down Expand Up @@ -50,3 +52,9 @@ export type EtchRunesResult = {
};

export type EtchRunes = MethodParamsAndResult<EtchRunesParams, EtchRunesResult>;

interface GetOrderParams extends GetOrderRequest {
network?: BitcoinNetworkType;
}

export type GetOrder = MethodParamsAndResult<GetOrderParams, GetOrderResponse>;
37 changes: 26 additions & 11 deletions src/runes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ import {
EstimateEtchOrderRequest,
EstimateMintOrderRequest,
EstimateOrderResponse,
GetOrderResponse,
} from './types';
import { BitcoinNetworkType, RpcErrorCode, RpcResult } from '../types';
import { BitcoinNetworkType } from '../types';

import axios, { Axios, AxiosError, AxiosInstance } from 'axios';
import 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`;
export const ORDINALS_API_BASE_URL = (network: BitcoinNetworkType = BitcoinNetworkType.Mainnet) =>
`https://ordinals${network === BitcoinNetworkType.Testnet ? '-testnet' : ''}.xverse.app/v1`;

export class RunesApi {
client: AxiosInstance;

constructor(network?: BitcoinNetworkType) {
this.client = axios.create({
baseURL: `${RUNES_API_BASE_URL(network)}`,
baseURL: ORDINALS_API_BASE_URL(network),
});
}

Expand All @@ -31,7 +32,7 @@ export class RunesApi {

estimateMintCost = async (mintParams: EstimateMintOrderRequest) => {
try {
const response = await this.client.post<EstimateOrderResponse>('/mint/estimate', {
const response = await this.client.post<EstimateOrderResponse>('/runes/mint/estimate', {
...mintParams,
});
return {
Expand All @@ -47,7 +48,7 @@ export class RunesApi {

estimateEtchCost = async (etchParams: EstimateEtchOrderRequest) => {
try {
const response = await this.client.post<EstimateOrderResponse>('/etch/estimate', {
const response = await this.client.post<EstimateOrderResponse>('/runes/etch/estimate', {
...etchParams,
});
return {
Expand All @@ -63,7 +64,7 @@ export class RunesApi {

createMintOrder = async (mintOrderParams: CreateMintOrderRequest) => {
try {
const response = await this.client.post<CreateOrderResponse>('/mint/orders', {
const response = await this.client.post<CreateOrderResponse>('/runes/mint/orders', {
...mintOrderParams,
});
return {
Expand All @@ -79,7 +80,7 @@ export class RunesApi {

createEtchOrder = async (etchOrderParams: CreateEtchOrderRequest) => {
try {
const response = await this.client.post<CreateOrderResponse>('/etch/orders', {
const response = await this.client.post<CreateOrderResponse>('/runes/etch/orders', {
...etchOrderParams,
});
return {
Expand All @@ -95,7 +96,7 @@ export class RunesApi {

executeMint = async (orderId: string, fundTransactionId: string) => {
try {
const response = await this.client.post(`/mint/orders/${orderId}/execute`, {
const response = await this.client.post(`/runes/mint/orders/${orderId}/execute`, {
fundTransactionId,
});
return {
Expand All @@ -111,7 +112,7 @@ export class RunesApi {

executeEtch = async (orderId: string, fundTransactionId: string) => {
try {
const response = await this.client.post(`/etch/orders/${orderId}/execute`, {
const response = await this.client.post(`/runes/etch/orders/${orderId}/execute`, {
fundTransactionId,
});
return {
Expand All @@ -124,6 +125,20 @@ export class RunesApi {
};
}
};

getOrder = async (orderId: string) => {
try {
const response = await this.client.get<GetOrderResponse>(`/orders/${orderId}`);
return {
data: response.data,
};
} catch (error) {
const err = error as AxiosError;
return {
error: this.parseError(err),
};
}
};
}

const testnetClient = new RunesApi(BitcoinNetworkType.Testnet);
Expand Down
12 changes: 12 additions & 0 deletions src/runes/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,15 @@ export type GetMintOrderResponse = {
reason: string;
createdAt: string;
};

export type GetOrderRequest = {
id: string;
};

export type GetOrderResponse = {
id: string;
orderType: 'rune_mint' | 'rune_etch';
state: 'new' | 'pending' | 'executing' | 'complete' | 'failed' | 'refunded' | 'stale';
reason?: string;
createdAt: string;
};

0 comments on commit ea14456

Please sign in to comment.