Skip to content

Instantly share code, notes, and snippets.

@jojobyte
Last active November 28, 2023 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jojobyte/b973baf220f75ca93d22673a9923a24a to your computer and use it in GitHub Desktop.
Save jojobyte/b973baf220f75ca93d22673a9923a24a to your computer and use it in GitHub Desktop.
Calculate Maya Chain balance from REST API data. Zero-Dependencies.
import { getBalance, getPools, getPoolBalances, getLiquidityProviders, } from './fetch.js'
import {
getStableCacaoValue, convertCacaoToStableValue, convertAssetToStableValue, calculateLiquidityPoolBalances,
formatCacaoValue,
} from './calculate.js'
let TARGET_ASSET = 'DASH.DASH'
let STABLE_COIN_ASSET = 'ETH.USDT-0XDAC17F958D2EE523A2206206994597C13D831EC7'
const pools = await getPools()
const targetProviders = await getLiquidityProviders(TARGET_ASSET)
const targetPool = pools.find(p => p.asset === TARGET_ASSET)
const stablePool = pools.find(p => p.asset === STABLE_COIN_ASSET)
// let TARGET_ADDRESS = 'XyZAbCdefGhIjkL01mn2OPqrST345uVwXy' // dash address (this is a bogus one)
// or
// let TARGET_ADDRESS = 'maya01a2345b6c7de89f0g1h2i3j4klmnopq5r67stu' // maya address (this is a bogus one)
// Find your address in the target pool
//
// const targetProvider = targetProviders.find(p => (
// p.asset_address === TARGET_ADDRESS ||
// p.cacao_address === TARGET_ADDRESS
// ))
const targetProvider = targetProviders[0] // Grabs first provider instead of finding by address
const mayaAddressBalanceData = await getBalance(targetProvider.cacao_address)
console.log(
'Maya Pool Data',
{
pools,
targetPool,
stablePool,
targetProviders,
mayaAddressBalanceData,
}
)
const lpBalances = calculateLiquidityPoolBalances(
targetProvider,
targetPool,
stablePool,
)
console.log(
'Maya Liquidity Pool Balances',
lpBalances,
)
const mayaAddressBalance = formatCacaoValue(
mayaAddressBalanceData?.coins?.find(
c => c.asset === 'CACAO'
)?.amount || 0
)
const mayaAddressStableBalance = convertCacaoToStableValue(mayaAddressBalance, stablePool)
console.log(
'Maya Address Balance',
mayaAddressBalance,
mayaAddressStableBalance,
)
export function getStableCacaoValue(stablePool) {
return Number(stablePool.balance_asset / stablePool.balance_cacao) * 100
}
export function convertCacaoToStableValue(value, stablePool) {
return Number(value) * getStableCacaoValue(stablePool)
}
export function convertAssetToStableValue(pool, stablePool) {
return Number(
(stablePool.balance_asset / stablePool.balance_cacao) *
(pool.balance_cacao / pool.balance_asset)
)
}
export function formatCacaoValue(value) {
return Number(value) / 1e10
}
export function formatAssetValue(value) {
return Number(value) / 1e8
}
export function calculateLiquidityPoolBalances(provider, pool, stable) {
const units = BigInt(provider.units)
const cacaoDeposit = BigInt(provider.cacao_deposit_value)
const assetDeposit = BigInt(provider.asset_deposit_value)
const poolUnits = BigInt(pool.pool_units)
const cacaoDepth = BigInt(pool.balance_cacao)
const assetDepth = BigInt(pool.balance_asset)
const cacaoBalance = formatCacaoValue(units * cacaoDepth / poolUnits)
const assetBalance = formatAssetValue(units * assetDepth / poolUnits)
const cacaoDepositValue = formatCacaoValue(cacaoDeposit)
const assetDepositValue = formatAssetValue(assetBalance)
const stableCacaoValue = getStableCacaoValue(stable)
const stableAssetValue = convertAssetToStableValue(pool, stable)
return {
name: provider.asset,
stableAsset: stable.asset,
cacao: {
deposit: cacaoDepositValue,
balance: cacaoBalance,
depositStable: cacaoDepositValue * stableCacaoValue,
balanceStable: cacaoBalance * stableCacaoValue,
stableValue: stableCacaoValue,
},
asset: {
deposit: assetDepositValue,
balance: assetBalance,
depositStable: assetDepositValue * stableAssetValue,
balanceStable: assetBalance * stableAssetValue,
stableValue: stableAssetValue,
},
}
}
export async function getBalance(addr) {
return (await fetch(
`https://midgard.mayachain.info/v2/balance/${addr}`
))?.json()
}
export async function getPools() {
return (await fetch(
`https://mayanode.mayachain.info/mayachain/pools`
))?.json() || []
}
export async function getPoolBalances(addr) {
return (await fetch(
`https://midgard.mayachain.info/v2/member/${addr}`
))?.json() || { pools: [] }
}
export async function getLiquidityProviders(pool) {
return (await fetch(
`https://mayanode.mayachain.info/mayachain/pool/${pool}/liquidity_providers`
))?.json() || []
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment