Skip to content

Instantly share code, notes, and snippets.

@avral
Last active September 15, 2023 20:46
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 avral/239e31232eb9a173b77c56dc537ddb6d to your computer and use it in GitHub Desktop.
Save avral/239e31232eb9a173b77c56dc537ddb6d to your computer and use it in GitHub Desktop.
Alcor SDK v2 getting pool price
import fetch from 'node-fetch'
import { Token, Pool } from '@alcorexchange/alcor-swap-sdk'
import { asset } from 'eos-common'
import { JsonRpc } from 'eosjs'
export function parseToken(token) {
return new Token(
token.contract,
asset(token.quantity).symbol.precision(),
asset(token.quantity).symbol.code().to_string(),
(asset(token.quantity).symbol.code().to_string() + '-' + token.contract).toLowerCase()
)
}
const rpc = new JsonRpc('https://waxnode02.alcor.exchange', { fetch });
async function main() {
const { rows } = await rpc.get_table_rows({
scope: 'swap.alcor',
table: 'pools',
code: 'swap.alcor',
})
// First pool for example
const { tokenA, tokenB, currSlot: { sqrtPriceX64, tick } } = rows[0]
const pool = new Pool({
...rows[0],
tokenA: parseToken(tokenA),
tokenB: parseToken(tokenB),
sqrtPriceX64,
tickCurrent: tick
})
console.log('priceA', pool.tokenAPrice.toFixed())
console.log('priceB', pool.tokenBPrice.toFixed())
}
main()
//priceA 0.2820
//priceB 3.5466
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment