Created
November 26, 2024 02:37
-
-
Save jeftarmascarenhas/f86473d1a410c366608683819e6d6157 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ethers } from 'ethers'; | |
import { SwapRouter, Pool } from '@uniswap/v3-sdk'; | |
import { Token, TradeType, Route, Trade, CurrencyAmount } from '@uniswap/sdk-core'; | |
// async function getQuote( | |
// tokenIn, | |
// tokenOut, | |
// amountIn, | |
// slippageTolerance | |
// ) { | |
// // Configuração do provider e contratos | |
// const provider = new ethers.providers.JsonRpcProvider(process.env.RPC_URL); | |
// const swapRouterAddress = "0xE592427A0AEce92De3Edee1F18E0157C05861564"; | |
// // Configurar os tokens como objetos Uniswap | |
// const tokenInObj = new Token(1, tokenIn, 18, 'TOKEN_IN', 'Token In'); | |
// const tokenOutObj = new Token(1, tokenOut, 18, 'TOKEN_OUT', 'Token Out'); | |
// // Obter cotação do pool | |
// const pool = await this.getPool(provider, tokenInObj, tokenOutObj); | |
// const route = new Route([pool], tokenInObj, tokenOutObj); | |
// // Quantidade de entrada como CurrencyAmount | |
// const inputAmount = CurrencyAmount.fromRawAmount(tokenInObj, ethers.utils.parseUnits(amountIn, 18).toString()); | |
// // Criar uma simulação de troca | |
// const trade = Trade.exactIn(route, inputAmount); | |
// // Calcula a quantidade mínima considerando o slippage | |
// const slippage = ethers.BigNumber.from(slippageTolerance); // Exemplo: 500 para 0.5% | |
// const amountOutMin = ethers.BigNumber.from(trade.outputAmount.toFixed(0)) | |
// .sub(trade.outputAmount.toFixed(0) * slippage.div(10000)); | |
// // Tempo de validade (ajustável conforme necessário) | |
// const deadline = Math.floor(Date.now() / 1000) + 60 * 5; // 5 minutos | |
// return { | |
// amountOut: trade.outputAmount.toFixed(18), | |
// amountOutMin: ethers.utils.formatUnits(amountOutMin, 18), | |
// deadline, | |
// route: route.midPrice.toSignificant(6) // Preço da rota | |
// }; | |
// } | |
async function createSwapCalldata( | |
tokenIn, | |
tokenOut, | |
amountIn, | |
amountOutMinimum, | |
recipient, | |
deadline | |
) { | |
// Configuração do contrato Uniswap (swapRouter) | |
const provider = new ethers.providers.JsonRpcProvider(process.env.RPC_URL); | |
const swapRouterAddress = "0xE592427A0AEce92De3Edee1F18E0157C05861564"; | |
// Criação do calldata usando o SDK da Uniswap | |
const swapParams = { | |
tokenIn: tokenIn, | |
tokenOut: tokenOut, | |
fee: 3000, | |
recipient: recipient, | |
deadline: deadline, // Passar o prazo de validade calculado | |
amountIn: ethers.utils.parseUnits(amountIn, 18), | |
amountOutMinimum: ethers.utils.parseUnits(amountOutMinimum, 18), | |
sqrtPriceLimitX96: 0 | |
}; | |
const swapRouter = new SwapRouter(provider, swapRouterAddress); | |
const calldata = await swapRouter.populateTransaction.exactInputSingle(swapParams); | |
// Retorno do `calldata` para a transação | |
return { | |
target: swapRouterAddress, | |
value: "0x0", | |
data: calldata.data | |
}; | |
} | |
async function importAssets() { | |
try { | |
// from: '0xd7efD88D5D307E9436450b8a33B5CE2EDA382b59', | |
const params = { | |
to: '0xE592427A0AEce92De3Edee1F18E0157C05861564', | |
from: 'tua carteira', | |
data: ``, | |
chainId: '0x89', | |
}; | |
console.log('account => ', account); | |
console.log('tokenRebalacing => ', params); | |
const sendRequest = await window.ethereum.request({ | |
method: 'eth_sendTransaction', | |
params: [params], | |
}); | |
console.log(sendRequest); | |
} catch (error) { | |
console.log(error); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment