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

Add get order method #6

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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;
};