|
|
|
|
@@ -23,7 +23,7 @@ import {ARBITRUM} from '../../generated/gmxsdk/configs/chains.js'
|
|
|
|
|
import {TokenData, TokensData} from '../../generated/gmxsdk/types/tokens.js';
|
|
|
|
|
import {getByKey} from '../../generated/gmxsdk/utils/objects.js';
|
|
|
|
|
import {GmxSdkConfig} from '../../generated/gmxsdk/types/sdk.js';
|
|
|
|
|
import {PositionIncreaseParams} from '../../generated/gmxsdk/modules/orders/helpers.js';
|
|
|
|
|
import {PositionIncreaseParams, SwapParams} from '../../generated/gmxsdk/modules/orders/helpers.js';
|
|
|
|
|
import {
|
|
|
|
|
basisPointsToFloat,
|
|
|
|
|
bigintToNumber,
|
|
|
|
|
@@ -42,6 +42,7 @@ import {hashDataMap, hashString} from '../../generated/gmxsdk/utils/hash.js';
|
|
|
|
|
import {CLAIMABLE_FUNDING_AMOUNT} from '../../generated/gmxsdk/configs/dataStore.js';
|
|
|
|
|
import {abis} from '../../generated/gmxsdk/abis/index.js';
|
|
|
|
|
import {DEFAULT_UI_FEE_RECEIVER_ACCOUNT} from '../../generated/gmxsdk/modules/utils/utils.js'
|
|
|
|
|
import {approveContractImpl, getTokenAllowance} from './privy.js';
|
|
|
|
|
|
|
|
|
|
// Add the missing CLAIMABLE_UI_FEE_AMOUNT constant based on the pattern
|
|
|
|
|
export const CLAIMABLE_UI_FEE_AMOUNT = hashString("CLAIMABLE_UI_FEE_AMOUNT");
|
|
|
|
|
@@ -146,6 +147,8 @@ declare module 'fastify' {
|
|
|
|
|
getGmxPriceImpactRebates: typeof getGmxPriceImpactRebates;
|
|
|
|
|
getClaimableUiFees: typeof getClaimableUiFees;
|
|
|
|
|
claimGmxUiFees: typeof claimGmxUiFees;
|
|
|
|
|
swapGmxTokens: typeof swapGmxTokens;
|
|
|
|
|
checkGmxTokenAllowances: typeof checkGmxTokenAllowances;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -191,6 +194,20 @@ const getClaimableUiFeesSchema = z.object({
|
|
|
|
|
account: z.string().nonempty()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Schema for swap-tokens request
|
|
|
|
|
const swapTokensSchema = z.object({
|
|
|
|
|
account: z.string().nonempty(),
|
|
|
|
|
fromTicker: z.string().nonempty(),
|
|
|
|
|
toTicker: z.string().nonempty(),
|
|
|
|
|
amount: z.number().positive(),
|
|
|
|
|
orderType: z.enum(['market', 'limit']).default('market'),
|
|
|
|
|
triggerRatio: z.number().optional(),
|
|
|
|
|
allowedSlippage: z.number().min(0).max(100).default(0.5)
|
|
|
|
|
}).refine((data) => data.fromTicker !== data.toTicker, {
|
|
|
|
|
message: "From and to tickers must be different",
|
|
|
|
|
path: ["toTicker"]
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets a GMX SDK client initialized for the given address
|
|
|
|
|
* If a walletId is provided, it will be used with Privy for signing
|
|
|
|
|
@@ -938,6 +955,8 @@ export default fp(async (fastify) => {
|
|
|
|
|
fastify.decorateRequest('getGmxPriceImpactRebates', getGmxPriceImpactRebates)
|
|
|
|
|
fastify.decorateRequest('getClaimableUiFees', getClaimableUiFees)
|
|
|
|
|
fastify.decorateRequest('claimGmxUiFees', claimGmxUiFees)
|
|
|
|
|
fastify.decorateRequest('swapGmxTokens', swapGmxTokens)
|
|
|
|
|
fastify.decorateRequest('checkGmxTokenAllowances', checkGmxTokenAllowances)
|
|
|
|
|
|
|
|
|
|
// Pre-populate and refresh the markets cache on startup
|
|
|
|
|
fastify.addHook('onReady', async () => {
|
|
|
|
|
@@ -1771,3 +1790,228 @@ export async function claimGmxUiFees(
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Implementation function to swap tokens on GMX using the SDK's built-in swap method
|
|
|
|
|
* @param sdk The GMX SDK client
|
|
|
|
|
* @param fromTicker The ticker symbol of the token to swap from
|
|
|
|
|
* @param toTicker The ticker symbol of the token to swap to
|
|
|
|
|
* @param amount The amount to swap
|
|
|
|
|
* @param orderType The order type (market or limit)
|
|
|
|
|
* @param triggerRatio The trigger ratio for limit orders (optional)
|
|
|
|
|
* @param allowedSlippage The allowed slippage percentage (default 0.5%)
|
|
|
|
|
* @returns The transaction hash
|
|
|
|
|
*/
|
|
|
|
|
export const swapGmxTokensImpl = async (
|
|
|
|
|
sdk: GmxSdk,
|
|
|
|
|
fromTicker: string,
|
|
|
|
|
toTicker: string,
|
|
|
|
|
amount: number,
|
|
|
|
|
orderType: 'market' | 'limit' = 'market',
|
|
|
|
|
triggerRatio?: number,
|
|
|
|
|
allowedSlippage: number = 0.5
|
|
|
|
|
): Promise<string> => {
|
|
|
|
|
try {
|
|
|
|
|
// Get markets and tokens data from GMX SDK with cache
|
|
|
|
|
const {marketsInfoData, tokensData} = await getMarketsInfoWithCache(sdk);
|
|
|
|
|
|
|
|
|
|
if (!marketsInfoData || !tokensData) {
|
|
|
|
|
throw new Error("No markets or tokens info data");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get token data for from and to tokens
|
|
|
|
|
const fromTokenData = getTokenDataFromTicker(fromTicker, tokensData);
|
|
|
|
|
const toTokenData = getTokenDataFromTicker(toTicker, tokensData);
|
|
|
|
|
|
|
|
|
|
console.log("fromTokenData", fromTokenData);
|
|
|
|
|
if (!fromTokenData || !toTokenData) {
|
|
|
|
|
throw new Error(`Token data not found for ${fromTicker} or ${toTicker}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calculate the from token amount with proper decimals
|
|
|
|
|
const fromTokenAmount = BigInt(Math.floor(amount * Math.pow(10, fromTokenData.decimals)));
|
|
|
|
|
|
|
|
|
|
// Check and handle token allowance for ExchangeRouter contract
|
|
|
|
|
const syntheticsRouterRouterAddress = getContract(sdk.chainId, "SyntheticsRouter");
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const currentAllowance = await getTokenAllowance(
|
|
|
|
|
sdk.account,
|
|
|
|
|
fromTokenData.address,
|
|
|
|
|
syntheticsRouterRouterAddress
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
console.log(`Current allowance for ${fromTicker}:`, currentAllowance.toString());
|
|
|
|
|
console.log(`Required amount:`, fromTokenAmount.toString());
|
|
|
|
|
|
|
|
|
|
if (currentAllowance < fromTokenAmount) {
|
|
|
|
|
console.log(`🔧 Insufficient allowance for ${fromTicker}. Auto-approving ExchangeRouter...`);
|
|
|
|
|
|
|
|
|
|
// Calculate a reasonable approval amount (use the larger of required amount or 1000 tokens)
|
|
|
|
|
const tokenUnit = BigInt(Math.pow(10, fromTokenData.decimals));
|
|
|
|
|
const minApprovalAmount = tokenUnit * 1000n; // 1000 tokens
|
|
|
|
|
const approvalAmount = fromTokenAmount > minApprovalAmount ? fromTokenAmount : minApprovalAmount;
|
|
|
|
|
|
|
|
|
|
const approvalHash = await approveContractImpl(
|
|
|
|
|
sdk.account,
|
|
|
|
|
fromTokenData.address,
|
|
|
|
|
syntheticsRouterRouterAddress,
|
|
|
|
|
sdk.chainId,
|
|
|
|
|
approvalAmount
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
console.log(`✅ Token approval successful! Hash: ${approvalHash}`);
|
|
|
|
|
console.log(`📝 Approved ${approvalAmount.toString()} ${fromTicker} for ExchangeRouter`);
|
|
|
|
|
} else {
|
|
|
|
|
console.log(`✅ Sufficient allowance already exists for ${fromTicker}`);
|
|
|
|
|
}
|
|
|
|
|
} catch (allowanceError) {
|
|
|
|
|
console.warn('Could not check or approve token allowance:', allowanceError);
|
|
|
|
|
throw new Error(`Failed to handle token allowance: ${allowanceError instanceof Error ? allowanceError.message : 'Unknown error'}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calculate trigger price for limit orders
|
|
|
|
|
let triggerPrice: bigint | undefined;
|
|
|
|
|
if (orderType === 'limit' && triggerRatio) {
|
|
|
|
|
// Convert trigger ratio to price format (30 decimals)
|
|
|
|
|
triggerPrice = BigInt(Math.floor(triggerRatio * Math.pow(10, 30)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convert slippage percentage to basis points
|
|
|
|
|
const allowedSlippageBps = Math.floor(allowedSlippage * 100); // Convert percentage to basis points
|
|
|
|
|
|
|
|
|
|
// Create SwapParams for the SDK
|
|
|
|
|
const swapParams = {
|
|
|
|
|
fromAmount: fromTokenAmount,
|
|
|
|
|
fromTokenAddress: fromTokenData.address,
|
|
|
|
|
toTokenAddress: toTokenData.address,
|
|
|
|
|
allowedSlippageBps: allowedSlippageBps,
|
|
|
|
|
referralCodeForTxn: encodeReferralCode("kaigen_ai"),
|
|
|
|
|
triggerPrice: triggerPrice, // Only defined for limit orders
|
|
|
|
|
skipSimulation: true,
|
|
|
|
|
} as SwapParams;
|
|
|
|
|
|
|
|
|
|
console.log("swapParams", swapParams);
|
|
|
|
|
|
|
|
|
|
swapParams.marketsInfoData = marketsInfoData;
|
|
|
|
|
swapParams.tokensData = tokensData;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Use the SDK's built-in swap method
|
|
|
|
|
await sdk.orders.swap(swapParams);
|
|
|
|
|
|
|
|
|
|
return "swap_order_created";
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error swapping GMX tokens:', error);
|
|
|
|
|
throw new Error(`Failed to swap tokens: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Swaps tokens on GMX
|
|
|
|
|
* @param this The FastifyRequest instance
|
|
|
|
|
* @param reply The FastifyReply instance
|
|
|
|
|
* @param account The wallet address of the user
|
|
|
|
|
* @param fromTicker The ticker symbol of the token to swap from
|
|
|
|
|
* @param toTicker The ticker symbol of the token to swap to
|
|
|
|
|
* @param amount The amount to swap
|
|
|
|
|
* @param orderType The order type (market or limit)
|
|
|
|
|
* @param triggerRatio The trigger ratio for limit orders (optional)
|
|
|
|
|
* @param allowedSlippage The allowed slippage percentage (default 0.5%)
|
|
|
|
|
* @returns The response object with success status and transaction hash
|
|
|
|
|
*/
|
|
|
|
|
export async function swapGmxTokens(
|
|
|
|
|
this: FastifyRequest,
|
|
|
|
|
reply: FastifyReply,
|
|
|
|
|
account: string,
|
|
|
|
|
fromTicker: string,
|
|
|
|
|
toTicker: string,
|
|
|
|
|
amount: number,
|
|
|
|
|
orderType: 'market' | 'limit' = 'market',
|
|
|
|
|
triggerRatio?: number,
|
|
|
|
|
allowedSlippage: number = 0.5
|
|
|
|
|
) {
|
|
|
|
|
try {
|
|
|
|
|
// Validate the request parameters
|
|
|
|
|
swapTokensSchema.parse({
|
|
|
|
|
account,
|
|
|
|
|
fromTicker,
|
|
|
|
|
toTicker,
|
|
|
|
|
amount,
|
|
|
|
|
orderType,
|
|
|
|
|
triggerRatio,
|
|
|
|
|
allowedSlippage
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Get client for the address
|
|
|
|
|
const sdk = await this.getClientForAddress(account);
|
|
|
|
|
|
|
|
|
|
// Call the implementation function
|
|
|
|
|
const hash = await swapGmxTokensImpl(
|
|
|
|
|
sdk,
|
|
|
|
|
fromTicker,
|
|
|
|
|
toTicker,
|
|
|
|
|
amount,
|
|
|
|
|
orderType,
|
|
|
|
|
triggerRatio,
|
|
|
|
|
allowedSlippage
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
success: true,
|
|
|
|
|
hash,
|
|
|
|
|
message: `Successfully processed ${fromTicker} to ${toTicker} swap. Any required token approvals were handled automatically.`
|
|
|
|
|
};
|
|
|
|
|
} catch (error) {
|
|
|
|
|
// Handle approval-specific errors with better messaging
|
|
|
|
|
if (error instanceof Error && error.message.includes('Failed to handle token allowance')) {
|
|
|
|
|
reply.status(400);
|
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
error: error.message,
|
|
|
|
|
errorType: 'APPROVAL_FAILED',
|
|
|
|
|
suggestion: `Token approval failed. Please ensure you have sufficient funds and try again, or manually approve the ExchangeRouter contract to spend your ${fromTicker} tokens.`
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return handleError(this, reply, error, 'gmx/swap-tokens');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks token allowances for GMX contracts
|
|
|
|
|
* @param account The wallet address
|
|
|
|
|
* @param tokenAddress The token contract address
|
|
|
|
|
* @param chainId The chain ID (defaults to Arbitrum)
|
|
|
|
|
* @returns Object with allowances for different GMX contracts
|
|
|
|
|
*/
|
|
|
|
|
export const checkGmxTokenAllowances = async (
|
|
|
|
|
account: string,
|
|
|
|
|
tokenAddress: string,
|
|
|
|
|
chainId: number = ARBITRUM
|
|
|
|
|
): Promise<{
|
|
|
|
|
exchangeRouter: bigint;
|
|
|
|
|
orderVault: bigint;
|
|
|
|
|
syntheticsRouter: bigint;
|
|
|
|
|
}> => {
|
|
|
|
|
try {
|
|
|
|
|
const exchangeRouterAddress = getContract(chainId, "ExchangeRouter");
|
|
|
|
|
const orderVaultAddress = getContract(chainId, "OrderVault");
|
|
|
|
|
const syntheticsRouterAddress = getContract(chainId, "SyntheticsRouter");
|
|
|
|
|
|
|
|
|
|
const [exchangeRouterAllowance, orderVaultAllowance, syntheticsRouterAllowance] = await Promise.all([
|
|
|
|
|
getTokenAllowance(account, tokenAddress, exchangeRouterAddress),
|
|
|
|
|
getTokenAllowance(account, tokenAddress, orderVaultAddress),
|
|
|
|
|
getTokenAllowance(account, tokenAddress, syntheticsRouterAddress)
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
exchangeRouter: exchangeRouterAllowance,
|
|
|
|
|
orderVault: orderVaultAllowance,
|
|
|
|
|
syntheticsRouter: syntheticsRouterAllowance
|
|
|
|
|
};
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error checking GMX token allowances:', error);
|
|
|
|
|
throw new Error(`Failed to check token allowances: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|