Skip to content

Commit

Permalink
Calculate: currentSqrtPriceX96 for swapParams, tick ranges for liquidity
Browse files Browse the repository at this point in the history
  • Loading branch information
nobita851 committed Dec 1, 2024
1 parent 87b51f0 commit 9690816
Showing 1 changed file with 72 additions and 1 deletion.
73 changes: 72 additions & 1 deletion operator/tickOracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const fs = require('fs');
const path = require('path');
dotenv.config();

const { getPrice } = require('./price.js');

// Check if the process.env object is empty
if (!Object.keys(process.env).length) {
throw new Error("process.env object is empty");
Expand All @@ -19,7 +21,6 @@ const avsDeploymentData = JSON.parse(fs.readFileSync(path.resolve(__dirname, `..
// Load core deployment data
const coreDeploymentData = JSON.parse(fs.readFileSync(path.resolve(__dirname, `../contracts/deployments/core/${chainId}.json`), 'utf8'));


const delegationManagerAddress = coreDeploymentData.addresses.delegation; // todo: reminder to fix the naming of this contract in the deployment file, change to delegationManager
const avsDirectoryAddress = coreDeploymentData.addresses.avsDirectory;
const tickOracleServiceManagerAddress = avsDeploymentData.addresses.tickOracleServiceManager;
Expand All @@ -37,6 +38,47 @@ const tickOracleServiceManager = new ethers.Contract(tickOracleServiceManagerAdd
const ecdsaRegistryContract = new ethers.Contract(ecdsaStakeRegistryAddress, ecdsaRegistryABI, wallet);
const avsDirectory = new ethers.Contract(avsDirectoryAddress, avsDirectoryABI, wallet);

const ERC20ABI = [
{
"constant": true,
"inputs": [],
"name": "decimals",
"outputs": [
{
"name": "",
"type": "uint8"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "spender",
"type": "address"
},
{
"name": "amount",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"name": "success",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
];


/// HOOKATHON: Update the function
const signAndRespondToTask = async (taskIndex: number) => {
/// HOOKATHON: Update the message for signature ?
Expand All @@ -50,6 +92,35 @@ const signAndRespondToTask = async (taskIndex: number) => {
const operators = [await wallet.getAddress()];
const signatures = [signature];

/// HOOKATHON: Update the symbols for the tokens
const symbol0 = 'USDCUSDT';
const symbol1 = 'ETHUSDT';

const token0Address = avsDeploymentData.addresses.token0;
const token1Address = avsDeploymentData.addresses.token1;

const price0 = await getPrice(symbol0);
const price1 = await getPrice(symbol1);

const token0 = new ethers.Contract(token0Address, ERC20ABI, wallet);
const token1 = new ethers.Contract(token1Address, ERC20ABI, wallet);

// assuming decimals to even number
const decimals0 = await token0.decimals();
const decimals1 = await token1.decimals();
const multiplier = (decimals0 - decimals1) / 2 - 18;

var sqrtPriceX96;
if(multiplier > 0)
sqrtPriceX96 = ethers.parseEther(`${Math.sqrt(price0 / price1)}`) * (2n ** 96n) * (10n ** BigInt(multiplier));
else
sqrtPriceX96 = ethers.parseEther(`${Math.sqrt(price0 / price1)}`) * (2n ** 96n) / (10n ** BigInt(-multiplier));

const currentTick = Math.floor((Math.log(price0) - Math.log(price1) + Math.log(10) * (decimals1 - decimals0)) / Math.log(1.0001));
// +- 5% price range
const tickUpper = currentTick + 488;
const tickLower = currentTick - 488;

/// HOOKATHON: Update the ABI encoding ?
const signedTask = ethers.AbiCoder.defaultAbiCoder().encode(
["address[]", "bytes[]", "uint32"],
Expand Down

0 comments on commit 9690816

Please sign in to comment.