mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
added calculate pool address using tokens and fee
This commit is contained in:
parent
c465234d30
commit
d96fae5728
|
|
@ -12,13 +12,63 @@ contract Helpers {
|
||||||
ISwapRouter public constant router =
|
ISwapRouter public constant router =
|
||||||
ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564);
|
ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564);
|
||||||
|
|
||||||
UniswapV3Pool public constant state =
|
bytes32 internal constant POOL_INIT_CODE_HASH =
|
||||||
UniswapV3Pool(0x17c14D2c404D167802b16C450d3c99F88F2c4F4d);
|
0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54;
|
||||||
|
|
||||||
function getPriceLimit(uint256 amountIn, bool zeroForOne)
|
struct PoolKey {
|
||||||
public
|
address token0;
|
||||||
returns (uint160)
|
address token1;
|
||||||
|
uint24 fee;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPoolAddress(
|
||||||
|
address tokenA,
|
||||||
|
address tokenB,
|
||||||
|
uint24 fee
|
||||||
|
) internal pure returns (address pool) {
|
||||||
|
if (tokenA > tokenB) (tokenA, tokenB) = (tokenB, tokenA);
|
||||||
|
return
|
||||||
|
computeAddress(
|
||||||
|
0x1F98431c8aD98523631AE4a59f267346ea31F984,
|
||||||
|
PoolKey({token0: tokenA, token1: tokenB, fee: fee})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function computeAddress(address factory, PoolKey memory key)
|
||||||
|
internal
|
||||||
|
pure
|
||||||
|
returns (address pool)
|
||||||
{
|
{
|
||||||
|
require(key.token0 < key.token1);
|
||||||
|
pool = address(
|
||||||
|
uint160(
|
||||||
|
uint256(
|
||||||
|
keccak256(
|
||||||
|
abi.encodePacked(
|
||||||
|
hex"ff",
|
||||||
|
factory,
|
||||||
|
keccak256(
|
||||||
|
abi.encode(key.token0, key.token1, key.fee)
|
||||||
|
),
|
||||||
|
POOL_INIT_CODE_HASH
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPriceLimit(
|
||||||
|
uint256 amountIn,
|
||||||
|
bool zeroForOne,
|
||||||
|
address tokenA,
|
||||||
|
address tokenB,
|
||||||
|
uint24 fee
|
||||||
|
) public returns (uint160) {
|
||||||
|
UniswapV3Pool state = UniswapV3Pool(
|
||||||
|
getPoolAddress(tokenA, tokenB, fee)
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
SqrtPriceMath.getNextSqrtPriceFromInput(
|
SqrtPriceMath.getNextSqrtPriceFromInput(
|
||||||
state.slot0().sqrtPriceX96,
|
state.slot0().sqrtPriceX96,
|
||||||
|
|
@ -46,7 +96,13 @@ contract Helpers {
|
||||||
deadline: block.timestamp + 1,
|
deadline: block.timestamp + 1,
|
||||||
amountIn: amountIn,
|
amountIn: amountIn,
|
||||||
amountOutMinimum: amountOutMinimum,
|
amountOutMinimum: amountOutMinimum,
|
||||||
sqrtPriceLimitX96: getPriceLimit(amountIn, zeroForOne)
|
sqrtPriceLimitX96: getPriceLimit(
|
||||||
|
amountIn,
|
||||||
|
zeroForOne,
|
||||||
|
tokenIn,
|
||||||
|
tokenOut,
|
||||||
|
fee
|
||||||
|
)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,45 @@
|
||||||
|
pragma solidity >=0.5.0;
|
||||||
|
|
||||||
|
library PoolAddress {
|
||||||
|
bytes32 internal constant POOL_INIT_CODE_HASH =
|
||||||
|
0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54;
|
||||||
|
|
||||||
|
struct PoolKey {
|
||||||
|
address token0;
|
||||||
|
address token1;
|
||||||
|
uint24 fee;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPoolKey(
|
||||||
|
address tokenA,
|
||||||
|
address tokenB,
|
||||||
|
uint24 fee
|
||||||
|
) internal pure returns (PoolKey memory) {
|
||||||
|
if (tokenA > tokenB) (tokenA, tokenB) = (tokenB, tokenA);
|
||||||
|
return PoolKey({token0: tokenA, token1: tokenB, fee: fee});
|
||||||
|
}
|
||||||
|
|
||||||
|
function computeAddress(address factory, PoolKey memory key)
|
||||||
|
internal
|
||||||
|
pure
|
||||||
|
returns (address pool)
|
||||||
|
{
|
||||||
|
require(key.token0 < key.token1);
|
||||||
|
pool = address(
|
||||||
|
uint160(
|
||||||
|
uint256(
|
||||||
|
keccak256(
|
||||||
|
abi.encodePacked(
|
||||||
|
hex"ff",
|
||||||
|
factory,
|
||||||
|
keccak256(
|
||||||
|
abi.encode(key.token0, key.token1, key.fee)
|
||||||
|
),
|
||||||
|
POOL_INIT_CODE_HASH
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -29,11 +29,38 @@ const WETH_ADDR = "0x82af49447d8a07e3bd95bd0d56f35241523fbab1";
|
||||||
describe("Uniswap-sell-beta", function() {
|
describe("Uniswap-sell-beta", function() {
|
||||||
let UniswapSellBeta, uniswapSellBeta;
|
let UniswapSellBeta, uniswapSellBeta;
|
||||||
before(async () => {
|
before(async () => {
|
||||||
|
const account = "0xa067668661c84476afcdc6fa5d758c4c01c34352";
|
||||||
|
[owner, add1, add2] = await ethers.getSigners();
|
||||||
|
|
||||||
|
const tokenArtifact = await artifacts.readArtifact(
|
||||||
|
"@openzeppelin/contracts/token/ERC20/IERC20.sol:IERC20"
|
||||||
|
);
|
||||||
|
const token = new ethers.Contract(
|
||||||
|
WETH_ADDR,
|
||||||
|
tokenArtifact.abi,
|
||||||
|
ethers.provider
|
||||||
|
);
|
||||||
|
|
||||||
|
await hre.network.provider.request({
|
||||||
|
method: "hardhat_impersonateAccount",
|
||||||
|
params: [account],
|
||||||
|
});
|
||||||
|
|
||||||
|
const signer = await ethers.getSigner(account);
|
||||||
|
console.log(await token.connect(signer).balanceOf(account));
|
||||||
|
await token
|
||||||
|
.connect(signer)
|
||||||
|
.transfer(owner.address, ethers.utils.parseUnits("0.00000001", 18));
|
||||||
|
|
||||||
|
await hre.network.provider.request({
|
||||||
|
method: "hardhat_stopImpersonatingAccount",
|
||||||
|
params: [account],
|
||||||
|
});
|
||||||
|
|
||||||
UniswapSellBeta = await ethers.getContractFactory(
|
UniswapSellBeta = await ethers.getContractFactory(
|
||||||
"UniswapSellBetaArbitrum"
|
"UniswapSellBetaArbitrum"
|
||||||
);
|
);
|
||||||
uniswapSellBeta = await UniswapSellBeta.deploy();
|
uniswapSellBeta = await UniswapSellBeta.deploy();
|
||||||
[owner, add1, add2] = await ethers.getSigners();
|
|
||||||
await uniswapSellBeta.deployed();
|
await uniswapSellBeta.deployed();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -43,11 +70,11 @@ describe("Uniswap-sell-beta", function() {
|
||||||
|
|
||||||
it("Should Perfrom a swap", async () => {
|
it("Should Perfrom a swap", async () => {
|
||||||
const tx = await uniswapSellBeta.sell(
|
const tx = await uniswapSellBeta.sell(
|
||||||
USDC_ADDR,
|
|
||||||
WETH_ADDR,
|
WETH_ADDR,
|
||||||
ethers.utils.parseUnits("0.000000000001"),
|
USDC_ADDR,
|
||||||
|
3000,
|
||||||
ethers.utils.parseUnits("10.0"),
|
ethers.utils.parseUnits("10.0"),
|
||||||
ethers.utils.parseUnits("1.0"),
|
0,
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
console.log(tx);
|
console.log(tx);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user