Skip to content

Commit

Permalink
feat: add isSponsoredTxServiceAvailable and fix PC
Browse files Browse the repository at this point in the history
  • Loading branch information
zhigang1992 committed Dec 13, 2024
1 parent 292c1c3 commit e3df209
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"packageManager": "[email protected]",
"version": "3.0.0",
"version": "3.0.1",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
19 changes: 17 additions & 2 deletions src/alexSDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { fromEntries } from './utils/utils';
import type { AMMRoute } from './utils/ammRouteResolver';
import {
broadcastSponsoredTx,
getSponsorData,
requiredStxAmountForSponsorTx,
runSponsoredSpotTx,
SponsoredTxError,
Expand Down Expand Up @@ -174,6 +175,16 @@ export class AlexSDK {
);
}

/**
* Check if the sponsor service is available.
*

Check failure on line 180 in src/alexSDK.ts

View workflow job for this annotation

GitHub Actions / build

Delete `·`
* @returns {Promise<boolean>} - A promise that resolves to true if the sponsor service is available, false otherwise.
*/
async isSponsoredTxServiceAvailable(): Promise<boolean> {
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.
Expand All @@ -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)

Check failure on line 210 in src/alexSDK.ts

View workflow job for this annotation

GitHub Actions / build

Insert `··`
);

Check failure on line 211 in src/alexSDK.ts

View workflow job for this annotation

GitHub Actions / build

Insert `··`
if (sponsorFeeAmount > fromAmount) {
return BigInt(0);
}
Expand Down Expand Up @@ -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)

Check failure on line 286 in src/alexSDK.ts

View workflow job for this annotation

GitHub Actions / build

Insert `··`
);

Check failure on line 287 in src/alexSDK.ts

View workflow job for this annotation

GitHub Actions / build

Insert `··`
if (sponsorFeeAmount > fromAmount) {
throw new SponsoredTxError(
SponsoredTxErrorCode.insufficient_funds,
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
39 changes: 31 additions & 8 deletions src/helpers/SponsorTxHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Check failure on line 14 in src/helpers/SponsorTxHelper.ts

View workflow job for this annotation

GitHub Actions / build

Replace `·Promise<{·status:·'ok'·|·string,·perRouteFee:·bigint·}>` with `⏎··|·Promise<{·status:·'ok'·|·string;·perRouteFee:·bigint·}>⏎·`

export function getSponsorData(): Promise<{ status: 'ok' | string, perRouteFee: bigint }> {

Check failure on line 16 in src/helpers/SponsorTxHelper.ts

View workflow job for this annotation

GitHub Actions / build

Replace `·status:·'ok'·|·string,·perRouteFee:·bigint·` with `⏎··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) }));

Check failure on line 21 in src/helpers/SponsorTxHelper.ts

View workflow job for this annotation

GitHub Actions / build

Replace `.then(res·=>·res.json()).then(data·=>·({·status:·data.status,·perRouteFee:·BigInt(data.per_route_fee)` with `⏎······.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<bigint> => {
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));

Check failure on line 32 in src/helpers/SponsorTxHelper.ts

View workflow job for this annotation

GitHub Actions / build

Replace `data·=>·data.perRouteFee·*·BigInt(1e8)·/·BigInt(1e6)` with `⏎····(data)·=>·(data.perRouteFee·*·BigInt(1e8))·/·BigInt(1e6)⏎··`
return BigInt(customRoute.length) * feePerSegment;
};

export function runSponsoredSpotTx(
Expand Down Expand Up @@ -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
),
]
Expand All @@ -89,7 +107,12 @@ export function runSponsoredSpotTx(
fee: feeAmount,
},
[
transfer(stxAddress, currencyX, totalAmount),
transfer(
stxAddress,
currencyX,
totalAmount,
FungibleConditionCode.LessEqual
),
transfer(
AlexVault,
segment1.neighbour,
Expand All @@ -105,7 +128,7 @@ export function runSponsoredSpotTx(
transfer(
AlexVault,
currencyY,
minDy,
BigInt(0),
FungibleConditionCode.GreaterEqual
),
]
Expand Down Expand Up @@ -158,7 +181,7 @@ export function runSponsoredSpotTx(
transfer(
AlexVault,
currencyY,
minDy,
BigInt(0),
FungibleConditionCode.GreaterEqual
),
]
Expand Down Expand Up @@ -225,7 +248,7 @@ export function runSponsoredSpotTx(
transfer(
AlexVault,
currencyY,
minDy,
BigInt(0),
FungibleConditionCode.GreaterEqual
),
]
Expand Down

0 comments on commit e3df209

Please sign in to comment.