Skip to content

Instantly share code, notes, and snippets.

@avral
Last active September 15, 2023 20:47
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/43a6dacbad1f3db3fe3b0e56b53ba7e7 to your computer and use it in GitHub Desktop.
Save avral/43a6dacbad1f3db3fe3b0e56b53ba7e7 to your computer and use it in GitHub Desktop.
Get position amount using alcor v2 sdk
import fetch from 'node-fetch'
import { Token, Position, Pool } from '@alcorexchange/alcor-swap-sdk'
import { asset } from 'eos-common'
import { JsonRpc } from 'eosjs'
import { Serialize } 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 });
const types: any = Serialize.createInitialTypes()
export const nameToUint64 = (name) => {
const ser = new Serialize.SerialBuffer()
ser.pushName(name)
return types.get('uint64').deserialize(ser)
}
async function main() {
const account = '3mob2.wam'
const { rows: pools } = await rpc.get_table_rows({
scope: 'swap.alcor',
table: 'pools',
code: 'swap.alcor',
})
// First pool for example (TLM / WAX)
const { tokenA, tokenB, currSlot: { sqrtPriceX64, tick } } = pools[0]
const pool = new Pool({
...pools[0],
tokenA: parseToken(tokenA),
tokenB: parseToken(tokenB),
sqrtPriceX64,
tickCurrent: tick
})
const { rows: positions } = await rpc.get_table_rows({
scope: pool.id,
table: 'positions',
code: 'swap.alcor',
key_type: 'i64',
index_position: 3,
lower_bound: nameToUint64(account),
upper_bound: nameToUint64(account)
})
const position = new Position({
...positions[0], // Only first of account position
pool
})
console.log('amountA:', position.amountA.toAsset())
console.log('amountB:', position.amountB.toAsset())
}
main()
// amountA: 103.4332 TLM
// amountB: 29.16056021 WAX
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment