首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Uniswap V3 AlphaRouter --“未能从任何提供程序获得子图池”

Uniswap V3 AlphaRouter --“未能从任何提供程序获得子图池”
EN

Stack Overflow用户
提问于 2022-01-27 18:10:30
回答 1查看 300关注 0票数 0

我试图将WETH与MyToken交换,这是以前在仲裁Rinkeby上创建的Uniswap V3池。但是,在调用AlphaRouter.route时,我会得到以下错误消息

未能从任何提供程序获得子图池。

还有什么需要交换的?我需要创造什么?

我的目标是将WETH替换为给定的MyToken输出。

我只是想简单地在Uniswap V3上得到一个交换,我的池完成了。有什么想法吗?

代码语言:javascript
复制
const Web3 = require('web3');
const {
  ethers
} = require("ethers");
const HDWalletProvider = require('@truffle/hdwallet-provider');
const {
  Token,
  CurrencyAmount,
  TradeType,
  Percent
} = require("@uniswap/sdk-core");
const {
  AlphaRouter
} = require('@uniswap/smart-order-router');
const ABI_UNISWAP_POOL_V3 = require("@uniswap/v3-core/artifacts/contracts/interfaces/IUniswapV3Pool.sol/IUniswapV3Pool.json");
const fs = require('fs');
const JSBI = require('JSBI');

const API_ALCHEMY_ARBITRUM_RINKEBY = 'https://arb-rinkeby.g.alchemy.com/v2/<API KEY>';
const POOL_ADDRESS_MYTOKEN_WETH = '0xc69e7AE1073DD8184FcF6dBfc27ba97d1524716A';

const mnemonic = fs.readFileSync("./.mnemonics").toString().trim();
const hdprovider = new HDWalletProvider(mnemonic, API_ALCHEMY_ARBITRUM_RINKEBY);
const provider = new ethers.providers.Web3Provider(hdprovider);

const owner = hdprovider.addresses[0];

var web3 = new Web3(hdprovider);
const Contract = web3.eth.Contract;

const router = new AlphaRouter({
  chainId: 421611,
  provider: provider
});

async function main() {
  const MyPool = new Contract(ABI_UNISWAP_POOL_V3.abi, POOL_ADDRESS_MYTOKEN_WETH);

  const [factory, token0, token1, fee, tickSpacing, liquidity, maxLiquidityPerTick] =
  await Promise.all([MyPool.methods.factory().call(),
    MyPool.methods.token0().call(),
    MyPool.methods.token1().call(),
    MyPool.methods.fee().call(),
    MyPool.methods.tickSpacing().call(),
    MyPool.methods.liquidity().call(),
    MyPool.methods.maxLiquidityPerTick().call()
  ]);

  const tokenA = new Token(3, token0, 2, "MTK", "MyToken");
  const tokenB = new Token(3, token1, 18, "WETH", "Wrapped Ether");

  var amountOut = 2000;

  amountOut = CurrencyAmount.fromRawAmount(tokenA, JSBI.BigInt(amountOut.toString()));
  const slippageTolerance = new Percent(5, 100);
  const deadline = Date.now() + 15000;
  const route = await router.route(
    amountOut,
    tokenB,
    TradeType.EXACT_OUTPUT, {
      recipient: owner,
      slippageTolerance: slippageTolerance,
      deadline: deadline
    }
  );
  hdprovider.engine.stop();
}

main();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-06 19:50:16

我看到在这段代码中有两件事是不对的:

首先,您要传递给router.routerouter.route应该转换为wei

代码语言:javascript
复制
const amountOutInWei=ethers.utils.parseUnits(amountOut.toString(),decimals)
// amountOutInWei should be converted to currentAmount
const currencyAmount= CurrencyAmount.fromRawAmount(tokenA,JSBI.BigInt(amountOutInWei))

应该将currencyAmount传递给router.route(currencyAmount,..)

第二个问题的截止日期必须在几秒钟内。

代码语言:javascript
复制
const deadline=Math.floor(Date.now() / 1000)+10*60 // addded 10 minutes
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70883644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档