-
-
Save avral/239e31232eb9a173b77c56dc537ddb6d to your computer and use it in GitHub Desktop.
Alcor SDK v2 getting pool price
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 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