i am trying to use pancakeswap on the BSC chain to fetch token price.
I set provider like this for bsc chain.
and, I created contact to use the quoteExactInputSingle function and handed it over to the wallet factor.
I set params to run the function and called the function statically, but I found missing revers data error. Where should I change it?
(ex. USDC/UNI)
<code>import hre from "hardhat";
const url = "https://bsc-dataseed.binance.org/";
const provider = new hre.ethers.JsonRpcProvider(url);
const wallet = new hre.ethers.Wallet(process.env.PRIVATE_KEY, provider)
const contract = new hre.ethers.Contract(
"0xB048Bbc1Ee6b733FFfCFb9e9CeF7375518e25997",
[
{
inputs: [
{
components: [
{
internalType: "address",
name: "tokenIn",
type: "address",
},
{
internalType: "address",
name: "tokenOut",
type: "address",
},
{
internalType: "uint256",
name: "amountIn",
type: "uint256",
},
{
internalType: "uint24",
name: "fee",
type: "uint24",
},
{
internalType: "uint160",
name: "sqrtPriceLimitX96",
type: "uint160",
},
],
internalType:
"struct IQuoterV2.QuoteExactInputSingleParams",
name: "params",
type: "tuple",
},
],
name: "quoteExactInputSingle",
outputs: [
{
internalType: "uint256",
name: "amountOut",
type: "uint256",
},
{
internalType: "uint160",
name: "sqrtPriceX96After",
type: "uint160",
},
{
internalType: "uint32",
name: "initializedTicksCrossed",
type: "uint32",
},
{
internalType: "uint256",
name: "gasEstimate",
type: "uint256",
},
],
stateMutability: "nonpayable",
type: "function",
},
],
wallet
);
const params = {
tokenIn: "0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d",
tokenOut: "0xBf5140A22578168FD562DCcF235E5D43A02ce9B1",
amountIn: hre.ethers.parseUnits("10", 6),
fee: 3000,
sqrtPriceLimitX96: 0,
};
try {
const output = await contract.quoteExactInputSingle.staticCall(params);
console.log(hre.ethers.formatUnits(output.amountOut, 18));
} catch (error) {
console.error("Error:", error);
}
</code>
<code>import hre from "hardhat";
const url = "https://bsc-dataseed.binance.org/";
const provider = new hre.ethers.JsonRpcProvider(url);
const wallet = new hre.ethers.Wallet(process.env.PRIVATE_KEY, provider)
const contract = new hre.ethers.Contract(
"0xB048Bbc1Ee6b733FFfCFb9e9CeF7375518e25997",
[
{
inputs: [
{
components: [
{
internalType: "address",
name: "tokenIn",
type: "address",
},
{
internalType: "address",
name: "tokenOut",
type: "address",
},
{
internalType: "uint256",
name: "amountIn",
type: "uint256",
},
{
internalType: "uint24",
name: "fee",
type: "uint24",
},
{
internalType: "uint160",
name: "sqrtPriceLimitX96",
type: "uint160",
},
],
internalType:
"struct IQuoterV2.QuoteExactInputSingleParams",
name: "params",
type: "tuple",
},
],
name: "quoteExactInputSingle",
outputs: [
{
internalType: "uint256",
name: "amountOut",
type: "uint256",
},
{
internalType: "uint160",
name: "sqrtPriceX96After",
type: "uint160",
},
{
internalType: "uint32",
name: "initializedTicksCrossed",
type: "uint32",
},
{
internalType: "uint256",
name: "gasEstimate",
type: "uint256",
},
],
stateMutability: "nonpayable",
type: "function",
},
],
wallet
);
const params = {
tokenIn: "0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d",
tokenOut: "0xBf5140A22578168FD562DCcF235E5D43A02ce9B1",
amountIn: hre.ethers.parseUnits("10", 6),
fee: 3000,
sqrtPriceLimitX96: 0,
};
try {
const output = await contract.quoteExactInputSingle.staticCall(params);
console.log(hre.ethers.formatUnits(output.amountOut, 18));
} catch (error) {
console.error("Error:", error);
}
</code>
import hre from "hardhat";
const url = "https://bsc-dataseed.binance.org/";
const provider = new hre.ethers.JsonRpcProvider(url);
const wallet = new hre.ethers.Wallet(process.env.PRIVATE_KEY, provider)
const contract = new hre.ethers.Contract(
"0xB048Bbc1Ee6b733FFfCFb9e9CeF7375518e25997",
[
{
inputs: [
{
components: [
{
internalType: "address",
name: "tokenIn",
type: "address",
},
{
internalType: "address",
name: "tokenOut",
type: "address",
},
{
internalType: "uint256",
name: "amountIn",
type: "uint256",
},
{
internalType: "uint24",
name: "fee",
type: "uint24",
},
{
internalType: "uint160",
name: "sqrtPriceLimitX96",
type: "uint160",
},
],
internalType:
"struct IQuoterV2.QuoteExactInputSingleParams",
name: "params",
type: "tuple",
},
],
name: "quoteExactInputSingle",
outputs: [
{
internalType: "uint256",
name: "amountOut",
type: "uint256",
},
{
internalType: "uint160",
name: "sqrtPriceX96After",
type: "uint160",
},
{
internalType: "uint32",
name: "initializedTicksCrossed",
type: "uint32",
},
{
internalType: "uint256",
name: "gasEstimate",
type: "uint256",
},
],
stateMutability: "nonpayable",
type: "function",
},
],
wallet
);
const params = {
tokenIn: "0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d",
tokenOut: "0xBf5140A22578168FD562DCcF235E5D43A02ce9B1",
amountIn: hre.ethers.parseUnits("10", 6),
fee: 3000,
sqrtPriceLimitX96: 0,
};
try {
const output = await contract.quoteExactInputSingle.staticCall(params);
console.log(hre.ethers.formatUnits(output.amountOut, 18));
} catch (error) {
console.error("Error:", error);
}
I expected to return the number of UNI tokens for 10 USDCs once the code was executed, but I couldn’t.