import BigNumber from "npm:bignumber.js";
import { createPublicClient, http, parseAbi } from "npm:viem";
import { mainnet } from "npm:viem/chains";
BigNumber.config({ DECIMAL_PLACES: 50, POW_PRECISION: 50, EXPONENTIAL_AT: 20 });
const client = createPublicClient({
chain: mainnet,
transport: http(),
});
export default async function(req: Request): Promise<Response> {
const firstPath = req.url.split("/")[3];
if (firstPath == "")
return Response.json({
ok: false,
message: "No token ID provided",
});
let tokenId;
try {
tokenId = BigInt(firstPath);
} catch (err) {
return Response.json({
ok: false,
message: err.message,
});
}
const [
nonce,
operator,
token0,
token1,
fee,
tickLower,
tickUpper,
liquidity,
feeGrowthInside0LastX128,
feeGrowthInside1LastX128,
tokensOwed0,
tokensOwed1,
] = await client.readContract({
address: "0xc36442b4a4522e871399cd717abdd847ab11fe88",
abi: parseAbi([
"function positions(uint256 tokenId) external view returns (uint96 nonce, address operator, address token0, address token1, uint24 fee, int24 tickLower, int24 tickUpper, uint128 liquidity, uint256 feeGrowthInside0LastX128, uint256 feeGrowthInside1Las
]),
functionName: "positions",
args: [tokenId],
});
const erc20ABI = parseAbi([
"function name() view returns (string)",
"function symbol() view returns (string)",
"function decimals() view returns (uint8)",
]);
const [token0Name, token0Symbol, token0Decimals, token1Name, token1Symbol, token1Decimals] = await client.multicall({
contracts: [
{
address: token0,
abi: erc20ABI,
functionName: "name",
},
{
address: token0,
abi: erc20ABI,
functionName: "symbol",
},
{
address: token0,
abi: erc20ABI,
functionName: "decimals",
},
{
address: token1,
abi: erc20ABI,
functionName: "name",
},
{
address: token1,
abi: erc20ABI,
functionName: "symbol",
},
{
address: token1,
abi: erc20ABI,
functionName: "decimals",
},
],
allowFailure: false,
});
const poolAddress = await client.readContract({
address: "0x1F98431c8aD98523631AE4a59f267346ea31F984",
abi: parseAbi([
"function getPool(address tokenA, address tokenB, uint24 fee) external view returns (address)",