UNPKG

@robertprp/intents-sdk

Version:

Shogun Network Intent-based cross-chain swaps SDK

112 lines 4.65 kB
import { AUCTIONEER_URL } from '../constants.js'; import { NetworkError } from '../errors/index.js'; import { Parsers } from '../utils/parsers.js'; import { CrossChainOrder } from './orders/cross-chain.js'; import { SingleChainOrder } from './orders/single-chain.js'; /** * Base SDK providing common functionality for all blockchain implementations * * This abstract class serves as the foundation for chain-specific SDK implementations * (EVM, Solana, Sui), providing shared functionality for creating and sending orders * across different blockchains. */ export class BaseSDK { async createSingleChainOrder(params) { const userAddress = await this.getUserAddress(); const order = await SingleChainOrder.create({ ...params, user: userAddress, }); return this.prepareSingleChainOrder(order); } async createCrossChainOrder(params) { const userAddress = await this.getUserAddress(); const order = await CrossChainOrder.create({ ...params, user: userAddress, }); return this.prepareCrossChainOrder(order); } async createAndSendSingleChainOrder(params) { const preparedOrder = await this.createSingleChainOrder(params); return BaseSDK.sendSingleChainOrder(preparedOrder); } static async sendSingleChainOrder(preparedOrder) { const intentRequest = preparedOrder.order.toIntentRequest(preparedOrder.preparedData); const body = JSON.stringify(intentRequest, Parsers.bigIntReplacer); const url = `${AUCTIONEER_URL}/user_intent/single_chain/limit_order`; const response = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body, }); if (!response.ok) { const errorJson = await response.json(); const extraErrorData = errorJson.extra_error_data; throw new NetworkError(`Server error ${extraErrorData}`); } return response.json(); } static async validateCrossChainOrder(intentRequest) { const body = JSON.stringify(intentRequest, Parsers.bigIntReplacer); const url = `${AUCTIONEER_URL}/validate_intent/cross_chain/limit_order`; const response = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body, }); if (!response.ok) { const errorJson = await response.json(); const mainError = errorJson.error; const extraErrorData = errorJson.extra_error_data; throw new NetworkError(`Validation error: ${mainError}. Extra error data: ${extraErrorData}`); } return; } static async validateSingleChainOrder(intentRequest) { const body = JSON.stringify(intentRequest, Parsers.bigIntReplacer); const url = `${AUCTIONEER_URL}/validate_intent/single_chain/limit_order`; const response = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body, }); if (!response.ok) { const errorJson = await response.json(); const mainError = errorJson.error; const extraErrorData = errorJson.extra_error_data; throw new NetworkError(`Validation error: ${mainError}. Extra error data: ${extraErrorData}`); } return; } async createAndSendCrossChainOrder(params) { const preparedOrder = await this.createCrossChainOrder(params); return BaseSDK.sendCrossChainOrder(preparedOrder); } static async sendCrossChainOrder(preparedOrder) { const intentRequest = preparedOrder.order.toIntentRequest(preparedOrder.preparedData); const body = JSON.stringify(intentRequest, Parsers.bigIntReplacer); const url = `${AUCTIONEER_URL}/user_intent/cross_chain/limit_order`; const response = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body, }); if (!response.ok) { const errorJson = await response.json(); const mainError = errorJson.error; const extraErrorData = errorJson.extra_error_data; throw new NetworkError(`Failed to send cross-chain order. Main error: ${mainError}. Extra error data: ${extraErrorData}`); } return response.json(); } } //# sourceMappingURL=sdk.js.map