From ea14456884f0ee127aba6968a5936528db23856f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduard=20Bardaj=C3=AD=20Puig?= Date: Wed, 10 Apr 2024 13:59:23 +0200 Subject: [PATCH] Add get order method --- src/adapters/satsConnectAdapter.ts | 22 +++++++++++++++++- src/request/types/index.ts | 9 +++++++- src/request/types/runesMethods.ts | 8 +++++++ src/runes/index.ts | 37 +++++++++++++++++++++--------- src/runes/types.ts | 12 ++++++++++ 5 files changed, 75 insertions(+), 13 deletions(-) diff --git a/src/adapters/satsConnectAdapter.ts b/src/adapters/satsConnectAdapter.ts index 8ed0a68..41ff717 100644 --- a/src/adapters/satsConnectAdapter.ts +++ b/src/adapters/satsConnectAdapter.ts @@ -201,6 +201,24 @@ abstract class SatsConnectAdapter { }; } + private async getOrder(params: Params<'runes_getOrder'>): Promise> { + 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: Method, params: Params @@ -218,7 +236,9 @@ abstract class SatsConnectAdapter { return this.estimateEtch(params as Params<'runes_estimateEtch'>) as Promise< RpcResult >; - + case 'runes_getOrder': { + return this.getOrder(params as Params<'runes_getOrder'>) as Promise>; + } default: return this.requestInternal(method, params); } diff --git a/src/request/types/index.ts b/src/request/types/index.ts index fa04a55..746b343 100644 --- a/src/request/types/index.ts +++ b/src/request/types/index.ts @@ -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, @@ -47,6 +53,7 @@ export interface RunesRequests { runes_mint: MintRunes; runes_estimateEtch: EstimateRunesEtch; runes_etch: EtchRunes; + runes_getOrder: GetOrder; } export type RunesRequestMethod = keyof RunesRequests; diff --git a/src/request/types/runesMethods.ts b/src/request/types/runesMethods.ts index 508da73..565e460 100644 --- a/src/request/types/runesMethods.ts +++ b/src/request/types/runesMethods.ts @@ -4,6 +4,8 @@ import { EstimateEtchOrderRequest, EstimateMintOrderRequest, EstimateOrderResponse, + GetOrderRequest, + GetOrderResponse, } from '../../runes/types'; import { BitcoinNetworkType, MethodParamsAndResult } from '../../types'; @@ -50,3 +52,9 @@ export type EtchRunesResult = { }; export type EtchRunes = MethodParamsAndResult; + +interface GetOrderParams extends GetOrderRequest { + network?: BitcoinNetworkType; +} + +export type GetOrder = MethodParamsAndResult; diff --git a/src/runes/index.ts b/src/runes/index.ts index 1a79385..6349ded 100644 --- a/src/runes/index.ts +++ b/src/runes/index.ts @@ -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), }); } @@ -31,7 +32,7 @@ export class RunesApi { estimateMintCost = async (mintParams: EstimateMintOrderRequest) => { try { - const response = await this.client.post('/mint/estimate', { + const response = await this.client.post('/runes/mint/estimate', { ...mintParams, }); return { @@ -47,7 +48,7 @@ export class RunesApi { estimateEtchCost = async (etchParams: EstimateEtchOrderRequest) => { try { - const response = await this.client.post('/etch/estimate', { + const response = await this.client.post('/runes/etch/estimate', { ...etchParams, }); return { @@ -63,7 +64,7 @@ export class RunesApi { createMintOrder = async (mintOrderParams: CreateMintOrderRequest) => { try { - const response = await this.client.post('/mint/orders', { + const response = await this.client.post('/runes/mint/orders', { ...mintOrderParams, }); return { @@ -79,7 +80,7 @@ export class RunesApi { createEtchOrder = async (etchOrderParams: CreateEtchOrderRequest) => { try { - const response = await this.client.post('/etch/orders', { + const response = await this.client.post('/runes/etch/orders', { ...etchOrderParams, }); return { @@ -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 { @@ -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 { @@ -124,6 +125,20 @@ export class RunesApi { }; } }; + + getOrder = async (orderId: string) => { + try { + const response = await this.client.get(`/orders/${orderId}`); + return { + data: response.data, + }; + } catch (error) { + const err = error as AxiosError; + return { + error: this.parseError(err), + }; + } + }; } const testnetClient = new RunesApi(BitcoinNetworkType.Testnet); diff --git a/src/runes/types.ts b/src/runes/types.ts index 2e9ece3..ca88fd2 100644 --- a/src/runes/types.ts +++ b/src/runes/types.ts @@ -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; +};