From e3df20917715b8e78886d1988aeadd835b6bbf2a Mon Sep 17 00:00:00 2001 From: Kyle Fang Date: Fri, 13 Dec 2024 05:17:02 +0000 Subject: [PATCH] feat: add isSponsoredTxServiceAvailable and fix PC --- package.json | 2 +- src/alexSDK.ts | 19 +++++++++++++++-- src/config.ts | 1 + src/helpers/SponsorTxHelper.ts | 39 +++++++++++++++++++++++++++------- 4 files changed, 50 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 26c56da..fbb2eb9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "packageManager": "pnpm@9.4.0", - "version": "3.0.0", + "version": "3.0.1", "license": "MIT", "main": "dist/index.js", "typings": "dist/index.d.ts", diff --git a/src/alexSDK.ts b/src/alexSDK.ts index 99c8e1c..3d1d4ac 100644 --- a/src/alexSDK.ts +++ b/src/alexSDK.ts @@ -13,6 +13,7 @@ import { fromEntries } from './utils/utils'; import type { AMMRoute } from './utils/ammRouteResolver'; import { broadcastSponsoredTx, + getSponsorData, requiredStxAmountForSponsorTx, runSponsoredSpotTx, SponsoredTxError, @@ -174,6 +175,16 @@ export class AlexSDK { ); } + /** + * Check if the sponsor service is available. + * + * @returns {Promise} - A promise that resolves to true if the sponsor service is available, false otherwise. + */ + async isSponsoredTxServiceAvailable(): Promise { + const { status } = await getSponsorData(); + return status === 'ok'; + } + /** * Get the amount of destination currency that will be received when swapping from one currency to another * in the context of sponsor tx. @@ -195,7 +206,9 @@ export class AlexSDK { const sponsorFeeAmount = from === Currency.STX ? stxAmount - : await this.getAmountTo(Currency.STX, stxAmount, from); + : await this.getAmountTo(Currency.STX, BigInt(1e8), from).then( + (x) => (x * stxAmount) / BigInt(1e8) + ); if (sponsorFeeAmount > fromAmount) { return BigInt(0); } @@ -269,7 +282,9 @@ export class AlexSDK { const sponsorFeeAmount = currencyX === Currency.STX ? stxAmount - : await this.getAmountTo(Currency.STX, stxAmount, currencyX); + : await this.getAmountTo(Currency.STX, BigInt(1e8), currencyX).then( + (x) => (x * stxAmount) / BigInt(1e8) + ); if (sponsorFeeAmount > fromAmount) { throw new SponsoredTxError( SponsoredTxErrorCode.insufficient_funds, diff --git a/src/config.ts b/src/config.ts index 2e85ba2..c43ac54 100644 --- a/src/config.ts +++ b/src/config.ts @@ -6,4 +6,5 @@ export const configs = { STACKS_API_HOST: 'https://api.hiro.so', READONLY_CALL_API_HOST: 'https://stacks-node.alexlab.co', SPONSORED_TX_EXECUTOR: 'https://api.stxer.xyz/sponsor/execute', + SPONSORED_TX_STATUS: 'https://api.stxer.xyz/sponsor/status', }; diff --git a/src/helpers/SponsorTxHelper.ts b/src/helpers/SponsorTxHelper.ts index 4e1d0e7..ce2980e 100644 --- a/src/helpers/SponsorTxHelper.ts +++ b/src/helpers/SponsorTxHelper.ts @@ -11,13 +11,26 @@ import { hasLength } from '../utils/arrayHelper'; import { transferFactory } from '../utils/postConditions'; import { composeTx, type TxToBroadCast } from './SwapHelper'; +let sponsorData: Promise<{ status: 'ok' | string, perRouteFee: bigint }> | undefined; + +export function getSponsorData(): Promise<{ status: 'ok' | string, perRouteFee: bigint }> { + if (sponsorData == null) { + sponsorData = fetch(configs.SPONSORED_TX_STATUS, { + method: 'GET', + mode: 'cors', + }).then(res => res.json()).then(data => ({ status: data.status, perRouteFee: BigInt(data.per_route_fee) })); + } + return sponsorData; +} + export const requiredStxAmountForSponsorTx = async ( _from: Currency, _to: Currency, customRoute: AMMRoute ): Promise => { - const feePerSegment = 0.05 * 1e8; - return BigInt(Math.floor(customRoute.length * feePerSegment)); + // we need to convert the fee to the same unit as the amount + const feePerSegment = await getSponsorData().then(data => data.perRouteFee * BigInt(1e8) / BigInt(1e6)); + return BigInt(customRoute.length) * feePerSegment; }; export function runSponsoredSpotTx( @@ -62,11 +75,16 @@ export function runSponsoredSpotTx( fee: feeAmount, }, [ - transfer(stxAddress, currencyX, totalAmount), + transfer( + stxAddress, + currencyX, + totalAmount, + FungibleConditionCode.LessEqual + ), transfer( AlexVault, currencyY, - minDy, + BigInt(0), FungibleConditionCode.GreaterEqual ), ] @@ -89,7 +107,12 @@ export function runSponsoredSpotTx( fee: feeAmount, }, [ - transfer(stxAddress, currencyX, totalAmount), + transfer( + stxAddress, + currencyX, + totalAmount, + FungibleConditionCode.LessEqual + ), transfer( AlexVault, segment1.neighbour, @@ -105,7 +128,7 @@ export function runSponsoredSpotTx( transfer( AlexVault, currencyY, - minDy, + BigInt(0), FungibleConditionCode.GreaterEqual ), ] @@ -158,7 +181,7 @@ export function runSponsoredSpotTx( transfer( AlexVault, currencyY, - minDy, + BigInt(0), FungibleConditionCode.GreaterEqual ), ] @@ -225,7 +248,7 @@ export function runSponsoredSpotTx( transfer( AlexVault, currencyY, - minDy, + BigInt(0), FungibleConditionCode.GreaterEqual ), ]