@okx-dex/okx-dex-sdk
Version:
OKX DEX SDK
70 lines (69 loc) • 3.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
// example.ts or test.ts
const index_1 = require("../../index");
require("dotenv/config");
const wallet_1 = require("../../core/wallet");
const web3_js_1 = require("@solana/web3.js");
const connection = new web3_js_1.Connection(process.env.SOLANA_RPC_URL);
const wallet = (0, wallet_1.createWallet)(process.env.SOLANA_PRIVATE_KEY, connection);
const client = new index_1.OKXDexClient({
apiKey: process.env.OKX_API_KEY,
secretKey: process.env.OKX_SECRET_KEY,
apiPassphrase: process.env.OKX_API_PASSPHRASE,
projectId: process.env.OKX_PROJECT_ID,
solana: {
wallet: wallet
}
});
const walletAddress = wallet.publicKey.toString();
async function main() {
var _a;
try {
// Example 2: Get swap data and use it for gas estimation
console.log('\n=== EVM Swap Transaction Gas Limit ===');
const swapData = await client.dex.getSwapData({
chainIndex: '501', // Solana mainnet
fromTokenAddress: '11111111111111111111111111111111', // SOL
toTokenAddress: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', // USDC
amount: '10000', // 1 SOL in lamports
slippagePercent: '0.05',
userWalletAddress: walletAddress,
fromTokenReferrerWalletAddress: walletAddress,
feePercent: '0.0001'
});
console.log('Swap Data Retrieved Successfully');
const txData = (_a = swapData.data[0]) === null || _a === void 0 ? void 0 : _a.tx;
console.log('Transaction gas from swap data:', txData === null || txData === void 0 ? void 0 : txData.gas);
// Add another pause before the next API call
console.log('Waiting 2 more seconds before gas limit estimation...');
await new Promise(resolve => setTimeout(resolve, 2000));
// Use the actual transaction data from the swap for gas limit estimation
if (txData && txData.data) {
try {
const swapGasLimit = await client.dex.getGasLimit({
chainIndex: '501', // Solana mainnet
fromAddress: txData.from,
toAddress: txData.to,
txAmount: txData.value, // Use actual transaction value
extJson: {
inputData: txData.data // Use actual transaction calldata
}
});
console.log('Swap Gas Limit:', JSON.stringify(swapGasLimit, null, 2));
}
catch (error) {
console.error('Swap gas limit error:', error);
console.log('You can also use the gas from the swap data:', txData === null || txData === void 0 ? void 0 : txData.gas);
}
}
else {
console.log('No transaction data available for gas estimation');
console.log('You can also use the gas from the swap data:', txData === null || txData === void 0 ? void 0 : txData.gas);
}
}
catch (error) {
console.error('Error:', error);
}
}
main();