Enhance migration script to support environment-specific appsettings for Managing.Workers; improve connection string extraction logic with fallback to Managing.Api for SandboxRemote and ProductionRemote environments. Update createSwapOrderTxn to correct variable naming for clarity and add dynamic execution fee calculation in swapGmxTokensImpl.
This commit is contained in:
@@ -30,13 +30,13 @@ export type SwapOrderParams = {
|
||||
|
||||
export async function createSwapOrderTxn(sdk: GmxSdk, p: SwapOrderParams) {
|
||||
const { encodedPayload, totalWntAmount } = await getParams(sdk, p);
|
||||
const { encodedPayload: simulationEncodedPayload, totalWntAmount: sumaltionTotalWntAmount } = await getParams(sdk, p);
|
||||
const { encodedPayload: simulationEncodedPayload, totalWntAmount: simulationTotalWntAmount } = await getParams(sdk, p);
|
||||
|
||||
if (p.orderType !== OrderType.LimitSwap) {
|
||||
await simulateExecuteOrder(sdk, {
|
||||
primaryPriceOverrides: {},
|
||||
createMulticallPayload: simulationEncodedPayload,
|
||||
value: sumaltionTotalWntAmount,
|
||||
value: simulationTotalWntAmount,
|
||||
tokensData: p.tokensData,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -32,6 +32,9 @@ import {hashDataMap, hashString} from '../../generated/gmxsdk/utils/hash.js';
|
||||
import {ContractName, getContract} from '../../generated/gmxsdk/configs/contracts.js';
|
||||
import {abis} from '../../generated/gmxsdk/abis/index.js';
|
||||
import {approveContractImpl, getTokenAllowance} from './privy.js';
|
||||
import {estimateExecuteSwapOrderGasLimit} from '../../generated/gmxsdk/utils/fees/executionFee.js';
|
||||
import {getExecutionFee} from '../../generated/gmxsdk/utils/fees/executionFee.js';
|
||||
import {estimateOrderOraclePriceCount} from '../../generated/gmxsdk/utils/fees/estimateOraclePriceCount.js';
|
||||
import {
|
||||
Position,
|
||||
PositionStatus,
|
||||
@@ -2922,15 +2925,51 @@ export const swapGmxTokensImpl = async (
|
||||
|
||||
// Fall back to createSwapOrderTxn directly with simpler parameters
|
||||
console.log('🔄 Attempting direct createSwapOrderTxn...');
|
||||
|
||||
// Calculate execution fee dynamically
|
||||
const swapPath = [fromTokenData.address, toTokenData.address];
|
||||
// For a direct swap (token A -> token B), there's 1 swap through 1 market
|
||||
// The swapPath length represents the number of markets/swaps in the path
|
||||
// For a simple direct swap, we estimate 1 swap
|
||||
const swapsCount = 1;
|
||||
const gasLimits = await sdk.utils.getGasLimits();
|
||||
const gasPrice = await sdk.utils.getGasPrice();
|
||||
|
||||
if (!gasLimits || gasPrice === undefined) {
|
||||
throw new Error("Failed to get gas limits or gas price for execution fee calculation");
|
||||
}
|
||||
|
||||
const estimatedGasLimit = estimateExecuteSwapOrderGasLimit(gasLimits, {
|
||||
swapsCount: swapsCount,
|
||||
callbackGasLimit: 0n,
|
||||
});
|
||||
|
||||
const oraclePriceCount = estimateOrderOraclePriceCount(swapsCount);
|
||||
|
||||
const executionFee = getExecutionFee(
|
||||
sdk.chainId,
|
||||
gasLimits,
|
||||
tokensData,
|
||||
estimatedGasLimit,
|
||||
gasPrice,
|
||||
oraclePriceCount
|
||||
);
|
||||
|
||||
if (!executionFee) {
|
||||
throw new Error("Failed to calculate execution fee");
|
||||
}
|
||||
|
||||
console.log(`💰 Calculated execution fee: ${(Number(executionFee.feeTokenAmount) / 1e18).toFixed(6)} ETH`);
|
||||
|
||||
await createSwapOrderTxn(sdk, {
|
||||
fromTokenAddress: fromTokenData.address,
|
||||
fromTokenAmount: fromTokenAmount,
|
||||
toTokenAddress: toTokenData.address,
|
||||
swapPath: [fromTokenData.address, toTokenData.address],
|
||||
swapPath: swapPath,
|
||||
orderType: OrderType.MarketSwap,
|
||||
minOutputAmount: BigInt(Math.floor(amount * Math.pow(10, toTokenData.decimals) * (1 - allowedSlippage / 100))),
|
||||
referralCode: encodeReferralCode("kaigen_ai"),
|
||||
executionFee: 100000000000000000n, // 0.1 ETH as execution fee
|
||||
executionFee: executionFee.feeTokenAmount,
|
||||
allowedSlippage: allowedSlippageBps,
|
||||
tokensData: tokensData,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user