- Bu konu boş.
1 yazı görüntüleniyor (toplam 1)
-
YazarYazılar
-
Ocak 6, 2025: 4:54 pm #121
admin
Anahtar yöneticiİki farklı swap seçeneği arasında geçiş yapabilmeniz için bir seçim menüsü ekleyebiliriz. Kullanıcı, token swap yönünü (Token 1 → Token 2 veya Token 2 → Token 1) seçebilecek ve bu seçim doğrultusunda işlem yapılacaktır.
Aşağıda HTML ve JavaScript kısmında, bu işlemi nasıl entegre edeceğinizle ilgili güncellenmiş bir örnek bulacaksınız:
Güncellenmiş HTML Kodu:
Kullanıcı, hangi token’lar arasında işlem yapmak istediğini seçebilecektir. Ayrıca, private key ve miktar girdisi de eklenmiştir.
<!DOCTYPE html> <html lang="tr"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Token Swap - Private Key</title> <script src="https://cdn.jsdelivr.net/npm/web3@1.5.2/dist/web3.min.js"></script> </head> <body> <h2>Token Swap İşlemi (Private Key ile)</h2> <label for="privateKey">Private Key:</label> <input id="privateKey" placeholder="Private Key girin" type="text"><br><br> <label for="amount">Miktar (Token 1):</label> <input id="amount" placeholder="Token 1 miktarını girin" type="number"><br><br> <label for="swapDirection">Swap Yönü:</label> <select id="swapDirection"> <option value="1to2">Token 1 → Token 2</option> <option value="2to1">Token 2 → Token 1</option> </select><br><br> <button onclick="swapTokens()">Swap</button> <script src="swap.js"></script> </body> </html>Güncellenmiş JavaScript Kodu (
swap.js):JavaScript kısmında, swap yönü seçimine göre işlem yapılacak.
// Avalanche RPC URL'si const AVALANCHE_RPC_URL = 'https://api.avax.network/ext/bc/C/rpc'; // Web3 provider oluştur const web31 = new Web3(new Web3.providers.HttpProvider(AVALANCHE_RPC_URL)); // TraderJoe Router kontrat adresi (Avalanche Mainnet) const routerAddress = '0x60aE616a2155Ee3d9A68541Ba4544862310933d4'; // Token1 ve Token2 adresleri const token1Address = '0x019A6C9b0E0E758740BdA4e1CE9e34108951ED81'; // Token 1 örneği const token2Address = '0xF0160ce5F106d8667A0f794BC0BfEd0fE007C335'; // Token 2 örneği // Token desimal bilgileri const token1Decimals = 6; const token2Decimals = 18; // Swap fonksiyonu async function swapTokens() { const privateKey = document.getElementById('privateKey').value; const amount = document.getElementById('amount').value; const swapDirection = document.getElementById('swapDirection').value; if (!privateKey || !amount || amount <= 0) { alert("Lütfen geçerli bir private key ve miktar girin!"); return; } // Private Key ile cüzdan oluştur const account = web31.eth.accounts.privateKeyToAccount(privateKey); web31.eth.accounts.wallet.add(account); const userAddress = account.address; const token1 = new web31.eth.Contract([ { "constant": true, "inputs": [], "name": "decimals", "outputs": [ { "name": "", "type": "uint8" } ], "payable": false, "stateMutability": "view", "type": "function" } ], token1Address); const token2 = new web31.eth.Contract([ { "constant": true, "inputs": [], "name": "decimals", "outputs": [ { "name": "", "type": "uint8" } ], "payable": false, "stateMutability": "view", "type": "function" } ], token2Address); const amountInWei = web31.utils.toWei(amount, 'mwei'); // Token1 için 6 desimal // Swap yönünü seç let path, amountOutMin; if (swapDirection === "1to2") { path = [token1Address, token2Address]; // Token 1 -> Token 2 amountOutMin = 0; // Minimum çıkış değeri } else if (swapDirection === "2to1") { path = [token2Address, token1Address]; // Token 2 -> Token 1 amountOutMin = 0; // Minimum çıkış değeri } // TraderJoe Router kontratına etkileşim const router = new web31.eth.Contract([ { "inputs": [ { "internalType": "address", "name": "tokenIn", "type": "address" }, { "internalType": "address", "name": "tokenOut", "type": "address" }, { "internalType": "uint256", "name": "amountIn", "type": "uint256" }, { "internalType": "uint256", "name": "amountOutMin", "type": "uint256" }, { "internalType": "address", "name": "to", "type": "address" }, { "internalType": "uint256", "name": "deadline", "type": "uint256" } ], "name": "swapExactTokensForTokens", "outputs": [ { "internalType": "uint256[]", "name": "", "type": "uint256[]" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" } ], routerAddress); // Swap işlemi için gerekli parametreler const to = userAddress; // Kullanıcı adresi const deadline = Math.floor(Date.now() / 1000) + 60 * 20; // 20 dakika geçerlilik süresi // İşlem gönderme try { const gasPrice = await web31.eth.getGasPrice(); const gasLimit = await router.methods.swapExactTokensForTokens( amountInWei, amountOutMin, path, to, deadline ).estimateGas({ from: userAddress }); // Gas ile işlem gönder const tx = await router.methods.swapExactTokensForTokens( amountInWei, amountOutMin, path, to, deadline ).send({ from: userAddress, gasPrice: gasPrice, gas: gasLimit }); console.log("Transaction successful:", tx); alert("Token swap işlemi başarılı!"); } catch (error) { console.error("Hata oluştu:", error); alert("Token swap işlemi sırasında bir hata oluştu."); } }Açıklamalar:
- Swap Yönü Seçimi:
swapDirectionseçeneği, kullanıcının hangi yönle swap yapmak istediğini belirler. Seçenekler:- Token 1 → Token 2 (
1to2): Bu, Token 1’in Token 2’ye dönüştürülmesi işlemidir. - Token 2 → Token 1 (
2to1): Bu ise tam tersini yaparak Token 2’nin Token 1’e dönüştürülmesidir.
- Token 1 → Token 2 (
pathDeğeri:- Swap işleminin yönüne göre
pathdeğeri dinamik olarak belirlenir. - Token 1 → Token 2 için
path = [token1Address, token2Address]. - Token 2 → Token 1 için
path = [token2Address, token1Address].
- Swap işleminin yönüne göre
- Minimum Çıkış:
amountOutMindeğişkeni, swap işleminde minimum alınacak token miktarını belirtir. Bu örnekte 0 olarak bırakılmıştır (gerçek kullanımda burada minimum bir miktar belirlemek daha güvenli olur).
- Gas Hesaplama ve İşlem Gönderme:
- Swap işlemi,
estimateGasile gerekli gas miktarı hesaplanır. - Ardından işlem gönderilir ve işlem sonucu ekrana yazdırılır.
- Swap işlemi,
Kullanıcı Arayüzü:
- Private key girildikten sonra, miktar ve swap yönü seçilerek işlem yapılabilir.
- Swap yönünü Token 1 → Token 2 ya da Token 2 → Token 1 olarak seçebilirsiniz.
Bu yapı, kullanıcının istediği yönle token swap yapmasını sağlar.
- Swap Yönü Seçimi:
-
YazarYazılar
1 yazı görüntüleniyor (toplam 1)
- Bu konuyu yanıtlamak için giriş yapmış olmalısınız.