Skip to content

Instantly share code, notes, and snippets.

@kendalleasterly
Created February 12, 2022 03:31
Show Gist options
  • Save kendalleasterly/eaacca37c1847c96244fca267c921bb9 to your computer and use it in GitHub Desktop.
Save kendalleasterly/eaacca37c1847c96244fca267c921bb9 to your computer and use it in GitHub Desktop.
Mint position unsiwap v3
const { Token } = require("@uniswap/sdk-core")
const WETH = new Token(
137,
"0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619",
18,
"WETH",
"Wrapped Ether"
)
const MATIC = new Token(
137,
"0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270",
18,
"WMATIC",
"Wrapped Matic"
)
const DAI = new Token(
137,
"0x8f3cf7ad23cd3cadbd9735aff958023239c6a063",
18,
"DAI",
"Dai Stablecoin"
)
const USDT = new Token(
137,
"0xc2132d05d31c914a87c6611c10748aeb04b58e8f",
6,
"USDT",
"Tether USD"
)
const WBTC = new Token(
137,
"0x1bfd67037b42cf73acf2047067bd4f2c47d9bfd6",
8,
"WBTC",
"Wrapped BTC"
)
module.exports = {WETH, MATIC, DAI, USDT, WBTC}
const {
Position,
Pool,
nearestUsableTick,
NonfungiblePositionManager,
} = require("@uniswap/v3-sdk");
const {ethers, BigNumber} = require("ethers");
const Web3 = require("web3");
const {USDT, DAI, WETH, WBTC} = require("./uniswap-tokens");
const dotenv = require("dotenv");
const {Percent} = require("@uniswap/sdk-core");
const Web3httpProvider = require("web3-providers-http");
const {
abi: INonfungiblePositionManagerABI,
} = require("@uniswap/v3-periphery/artifacts/contracts/interfaces/INonfungiblePositionManager.sol/INonfungiblePositionManager.json");
const {
abi: IUniswapV3PoolABI,
} = require("@uniswap/v3-core/artifacts/contracts/interfaces/IUniswapV3Pool.sol/IUniswapV3Pool.json");
dotenv.config();
const apiURL =
"https://api.thegraph.com/subgraphs/name/ianlapham/uniswap-v3-polygon";
const provider = new ethers.providers.JsonRpcProvider(process.env.INFURA_URL);
let wallet = new ethers.Wallet(process.env.PRIVATE_KEY);
wallet = wallet.connect(provider);
async function mintPosition() {
const USDT_DAI_ADDRESS = "0x42f0530351471dab7ec968476d19bd36af9ec52d";
const WBTC_WETH_ADDRESS = "0x50eaedb835021e4a108b7290636d62e9765cc6d7";
const [POOL, immutables, state] = await getPool(
WETH,
WBTC,
WBTC_WETH_ADDRESS
);
const newPosition = Position.fromAmount0({
pool: POOL,
tickLower:
nearestUsableTick(state.tick, immutables.tickSpacing) -
immutables.tickSpacing * 10,
tickUpper:
nearestUsableTick(state.tick, immutables.tickSpacing) +
immutables.tickSpacing * 10,
amount0: ethers.utils.parseUnits("0.01", WBTC.decimals),
useFullPrecision: true,
});
console.log(newPosition);
console.log(newPosition.amount0);
console.log(
ethers.utils.formatUnits(
newPosition.amount0.quotient.toString(),
WBTC.decimals
)
);
console.log(
ethers.utils.formatUnits(
newPosition.amount1.quotient.toString(),
WETH.decimals
)
);
const blockNumber = await provider.getBlockNumber();
const {calldata, value} = NonfungiblePositionManager.addCallParameters(
newPosition,
{
slippageTolerance: new Percent(50, 1000),
recipient: wallet.address,
deadline: blockNumber + 200,
}
);
const NFPManagerAddress = "0xC36442b4a4522E871399CD717aBDD847Ab11FE88";
let txn = {
to: NFPManagerAddress,
data: calldata,
value,
};
const gasPrice = await provider.getGasPrice();
console.log(calldata);
const tx = await wallet.sendTransaction({
...txn,
gasPrice,
gasLimit: 600000
})
console.log(tx)
}
//MARK: Helpers
async function getPool(tokenA, tokenB, poolAddress) {
const [immutables, state] = await Promise.all([
getPoolImmutables(poolAddress),
getPoolState(poolAddress),
]);
const POOL = new Pool(
tokenA,
tokenB,
immutables.fee,
state.sqrtPriceX96.toString(),
state.liquidity.toString(),
state.tick
);
return [POOL, immutables, state];
}
async function getPoolImmutables(poolAddress) {
const poolContract = new ethers.Contract(
poolAddress,
IUniswapV3PoolABI,
provider
);
const [factory, token0, token1, fee, tickSpacing, maxLiquidityPerTick] =
await Promise.all([
poolContract.factory(),
poolContract.token0(),
poolContract.token1(),
poolContract.fee(),
poolContract.tickSpacing(),
poolContract.maxLiquidityPerTick(),
]);
const immutables = {
factory,
token0,
token1,
fee,
tickSpacing,
maxLiquidityPerTick,
};
return immutables;
}
async function getPoolState(poolAddress) {
const poolContract = new ethers.Contract(
poolAddress,
IUniswapV3PoolABI,
provider
);
const [liquidity, slot] = await Promise.all([
poolContract.liquidity(),
poolContract.slot0(),
]);
const PoolState = {
liquidity,
sqrtPriceX96: slot[0],
tick: slot[1],
observationIndex: slot[2],
observationCardinality: slot[3],
observationCardinalityNext: slot[4],
feeProtocol: slot[5],
unlocked: slot[6],
};
return PoolState;
}
module.exports = {getInfo, mintPosition, getPool};
@gonzageraci
Copy link

Hello! Did you develop any function to burn the position you created?
cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment