Created
November 9, 2024 04:07
-
-
Save iboss-ptk/d6dbe9bbab579ee739b5fd220ef479bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate"; | |
const rpcURL = "https://rpc.osmosis.zone:443"; | |
const contractAddress = | |
"osmo1em6xs47hd82806f5cxgyufguxrrc7l0aqx7nzzptjuqgswczk8csavdxek"; | |
const client = await SigningCosmWasmClient.connect(rpcURL); | |
type Limiter = [[string, string], { static_limiter: { upper_limit: string } }]; | |
type Limiters = { | |
limiters: Limiter[]; | |
}; | |
type Liquidity = { | |
total_pool_liquidity: { | |
denom: string; | |
amount: string; | |
}[]; | |
}; | |
async function queryUsdtTransmuter<T>(queryMsg: object): Promise<T> { | |
return await client.queryContractSmart(contractAddress, queryMsg); | |
} | |
async function queryLimiters() { | |
const { limiters } = await queryUsdtTransmuter<Limiters>({ | |
list_limiters: {}, | |
}); | |
return limiters.reduce( | |
( | |
acc: Record<string, { label: string; upper_limit: number }>, | |
[ | |
[denom, label], | |
{ | |
static_limiter: { upper_limit }, | |
}, | |
] | |
) => { | |
acc[denom] = { label, upper_limit: Number(upper_limit) }; | |
return acc; | |
}, | |
{} | |
); | |
} | |
async function queryLiquidity() { | |
const { total_pool_liquidity } = await queryUsdtTransmuter<Liquidity>({ | |
get_total_pool_liquidity: {}, | |
}); | |
return total_pool_liquidity.reduce( | |
(acc: Record<string, number>, { denom, amount }) => { | |
acc[denom] = Number(amount); | |
return acc; | |
}, | |
{} | |
); | |
} | |
// ------------------------------------- | |
const limitersMap = await queryLimiters(); | |
const liquidityMap = await queryLiquidity(); | |
const totalLiquidity = Object.values(liquidityMap).reduce( | |
(acc, liquidity) => acc + liquidity, | |
0 | |
); | |
const weights = Object.entries(liquidityMap) | |
.map(([denom, liquidity]) => [denom, liquidity / totalLiquidity]) | |
.reduce((acc: Record<string, number>, [denom, weight]) => { | |
acc[denom] = Number(weight); | |
return acc; | |
}, {}); | |
// diff = upper limit - weight | |
const diffs = Object.entries(limitersMap) | |
.map(([denom, { label, upper_limit }]) => ({ | |
denom, | |
label, | |
upper_limit, | |
weight: weights[denom], | |
gap: upper_limit - weights[denom], | |
amount_gap_human: | |
Math.ceil((upper_limit - weights[denom]) * totalLiquidity) / 10 ** 6, // assume all 6 decimals | |
})) | |
.sort((a, b) => a.gap - b.gap); | |
console.table(diffs); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment