pair.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { config, BIN_STEP } from '../config'
  2. import { getPublicClient, getAccount } from './client'
  3. import { LB_PAIR_ABI } from './abi'
  4. const pair = () => ({
  5. address: config.contracts.lbPair,
  6. abi: LB_PAIR_ABI,
  7. })
  8. export async function getActiveId(): Promise<number> {
  9. const client = getPublicClient()
  10. const result = await client.readContract({
  11. ...pair(),
  12. functionName: 'getActiveId',
  13. })
  14. return Number(result)
  15. }
  16. export async function getBinStep(): Promise<number> {
  17. const client = getPublicClient()
  18. const result = await client.readContract({
  19. ...pair(),
  20. functionName: 'getBinStep',
  21. })
  22. return Number(result)
  23. }
  24. export async function getBinReserves(
  25. binId: number,
  26. ): Promise<{ reserveX: bigint; reserveY: bigint }> {
  27. const client = getPublicClient()
  28. const [reserveX, reserveY] = await client.readContract({
  29. ...pair(),
  30. functionName: 'getBin',
  31. args: [binId],
  32. })
  33. return { reserveX, reserveY }
  34. }
  35. export async function getPoolReserves(): Promise<{ reserveX: bigint; reserveY: bigint }> {
  36. const client = getPublicClient()
  37. const [reserveX, reserveY] = await client.readContract({
  38. ...pair(),
  39. functionName: 'getReserves',
  40. })
  41. return { reserveX, reserveY }
  42. }
  43. export async function getUserBinBalance(binId: number): Promise<bigint> {
  44. const client = getPublicClient()
  45. const account = getAccount()
  46. return await client.readContract({
  47. ...pair(),
  48. functionName: 'balanceOf',
  49. args: [account.address, BigInt(binId)],
  50. })
  51. }
  52. export async function getUserBinBalancesBatch(
  53. binIds: number[],
  54. ): Promise<Map<number, bigint>> {
  55. const client = getPublicClient()
  56. const account = getAccount()
  57. const accounts = binIds.map(() => account.address)
  58. const ids = binIds.map((id) => BigInt(id))
  59. const balances = await client.readContract({
  60. ...pair(),
  61. functionName: 'balanceOfBatch',
  62. args: [accounts, ids],
  63. })
  64. const result = new Map<number, bigint>()
  65. binIds.forEach((id, i) => {
  66. if (balances[i] > 0n) result.set(id, balances[i])
  67. })
  68. return result
  69. }
  70. export async function getBinTotalSupply(binId: number): Promise<bigint> {
  71. const client = getPublicClient()
  72. return await client.readContract({
  73. ...pair(),
  74. functionName: 'totalSupply',
  75. args: [BigInt(binId)],
  76. })
  77. }
  78. export async function isApprovedForAll(spender: `0x${string}`): Promise<boolean> {
  79. const client = getPublicClient()
  80. const account = getAccount()
  81. return await client.readContract({
  82. ...pair(),
  83. functionName: 'isApprovedForAll',
  84. args: [account.address, spender],
  85. })
  86. }
  87. export function getPriceFromBinId(binId: number, binStep = BIN_STEP): number {
  88. return (1 + binStep / 10_000) ** (binId - 8388608)
  89. }
  90. export function getBinIdFromPrice(price: number, binStep = BIN_STEP): number {
  91. return Math.round(Math.log(price) / Math.log(1 + binStep / 10_000) + 8388608)
  92. }
  93. export async function getActivePrice(): Promise<{ activeId: number; price: number }> {
  94. const activeId = await getActiveId()
  95. const price = getPriceFromBinId(activeId)
  96. return { activeId, price }
  97. }
  98. export async function findUserBinIds(searchRange = 50): Promise<number[]> {
  99. const activeId = await getActiveId()
  100. const account = getAccount()
  101. const client = getPublicClient()
  102. const allBinIds: number[] = []
  103. for (let offset = -searchRange; offset <= searchRange; offset++) {
  104. allBinIds.push(activeId + offset)
  105. }
  106. const accounts = allBinIds.map(() => account.address)
  107. const ids = allBinIds.map((id) => BigInt(id))
  108. const balances = await client.readContract({
  109. ...pair(),
  110. functionName: 'balanceOfBatch',
  111. args: [accounts, ids],
  112. })
  113. return allBinIds.filter((_, i) => balances[i] > 0n)
  114. }