From b66d5616ecbcbd6168362a9dbcdfebf3dc075333 Mon Sep 17 00:00:00 2001 From: g1nt0ki <99907941+g1nt0ki@users.noreply.github.com> Date: Tue, 10 Dec 2024 15:19:10 +0100 Subject: [PATCH] code refactor --- projects/access-protocol/index.js | 21 ++++++++------ projects/helper/utils/solana/layout.js | 3 +- .../utils/solana/layouts/mixed-layout.js | 28 ++++++++++++++++++- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/projects/access-protocol/index.js b/projects/access-protocol/index.js index 8dd2b1c143dd..de1f60c51060 100644 --- a/projects/access-protocol/index.js +++ b/projects/access-protocol/index.js @@ -1,13 +1,16 @@ -const { CentralStateV2, ACCESS_PROGRAM_ID } = require("@accessprotocol/js"); -const { getConnection } = require("../helper/solana"); +const { getMultipleAccounts, decodeAccount } = require("../helper/solana"); +const { PublicKey } = require("@solana/web3.js"); -async function staking() { - const connection = getConnection(); - const [centralKey] = CentralStateV2.getKey(ACCESS_PROGRAM_ID); - const centralState = await CentralStateV2.retrieve(connection, centralKey); - return { - [`solana:${centralState.tokenMint.toString()}`]: Number(centralState.totalStaked.toString()) - } +async function staking(api) { + const programId = new PublicKey('6HW8dXjtiTGkD4jzXs7igdFmZExPpmwUrRN5195xGup') + const accountKey = getKey(programId); + const [account] = await getMultipleAccounts([accountKey]); + const decoded = decodeAccount('access', account); + api.add(decoded.tokenMint.toBase58(), decoded.totalStaked); +} + +const getKey = (address) => { + return PublicKey.findProgramAddressSync([address.toBuffer()], address)[0].toBase58(); } module.exports = { diff --git a/projects/helper/utils/solana/layout.js b/projects/helper/utils/solana/layout.js index 42e27acd27d9..e409ec8d92c6 100644 --- a/projects/helper/utils/solana/layout.js +++ b/projects/helper/utils/solana/layout.js @@ -5,7 +5,7 @@ const { parsePhoenix } = require('./layouts/phoenix-dex') const { RAYDIUM_LIQUIDITY_STATE_LAYOUT_CLMM, RAYDIUM_STABLE_STATE_LAYOUT_V1, } = require('./layouts/raydium-layout') const { INVESTIN_FUND_DATA, } = require('./layouts/investin-layout') const { MARKET_STATE_LAYOUT_V3, OPEN_ORDERS_LAYOUT_V2, MARKET_STATE_LAYOUT_V3_MINIMAL } = require('./layouts/openbook-layout') -const { ReserveLayout, ReserveLayoutLarix, MintLayout, AccountLayout, TokenSwapLayout, ESOLStakePoolLayout, PARLAY_LAYOUT_PARTIAL, HH_PARI_LAYOUT_PARTIAL, } = require('./layouts/mixed-layout'); +const { ReserveLayout, ReserveLayoutLarix, MintLayout, AccountLayout, TokenSwapLayout, ESOLStakePoolLayout, PARLAY_LAYOUT_PARTIAL, HH_PARI_LAYOUT_PARTIAL, ACCESS_LAYOUT, } = require('./layouts/mixed-layout'); const { SCN_STAKE_POOL, TOKEN_LAYOUT, } = require("./layouts/scnSOL"); const { SANCTUM_INFINITY } = require("./layouts/sanctum-infinity-layout"); const { parseSanctumLstStateList } = require("./layouts/sanctum-validators-lsts-layout"); @@ -65,6 +65,7 @@ const customDecoders = { stakePool: defaultParseLayout(STAKE_POOL_LAYOUT), hhParlay: defaultParseLayout(PARLAY_LAYOUT_PARTIAL), hhPari: defaultParseLayout(HH_PARI_LAYOUT_PARTIAL), + access: defaultParseLayout(ACCESS_LAYOUT), } function decodeAccount(layout, accountInfo) { diff --git a/projects/helper/utils/solana/layouts/mixed-layout.js b/projects/helper/utils/solana/layouts/mixed-layout.js index 53962af37c5f..3fe49025b417 100644 --- a/projects/helper/utils/solana/layouts/mixed-layout.js +++ b/projects/helper/utils/solana/layouts/mixed-layout.js @@ -269,8 +269,34 @@ const HH_PARI_LAYOUT_PARTIAL = BufferLayout.struct([ BufferLayout.seq(u64(), u8().span, 'amounts'), ]); +const ACCESS_LAYOUT = BufferLayout.struct([ + BufferLayout.u8('tag'), + BufferLayout.u8('bumpSeed'), + uint64('dailyInflation'), + publicKey('tokenMint'), + publicKey('authority'), + uint64('creationTime'), + uint64('totalStaked'), + uint64('totalStakedSnapshot'), + uint64('lastSnapshotOffset'), + uint128('ixGate'), + publicKey('freezeAuthority'), + uint128('adminIxGate'), + BufferLayout.u16('feeBasisPoints'), + uint64('lastFeeDistributionTime'), + BufferLayout.u32('feeRecipientsCount'), + BufferLayout.seq( + BufferLayout.struct([ + publicKey('owner'), + uint64('percentage'), + ]), + 10, + 'recipients' + ), +]); + module.exports = { ReserveLayout, ReserveLayoutLarix, MintLayout, AccountLayout, TokenSwapLayout, ESOLStakePoolLayout, - PARLAY_LAYOUT_PARTIAL, HH_PARI_LAYOUT_PARTIAL, + PARLAY_LAYOUT_PARTIAL, HH_PARI_LAYOUT_PARTIAL, ACCESS_LAYOUT }