Skip to content

Commit

Permalink
Feat/search assets for position keys (#518)
Browse files Browse the repository at this point in the history
* Remove legacy

* remove unused

* Use rpc searchAssets for positionKeys
  • Loading branch information
bryzettler authored Dec 21, 2023
1 parent 50ea66c commit 2762e7b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 27 deletions.
34 changes: 27 additions & 7 deletions packages/spl-utils/src/mplAssetAPI.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Creator, Uses } from "@metaplex-foundation/mpl-bubblegum";
import { collectInstructionDiscriminator } from "@metaplex-foundation/mpl-token-metadata";
import { PublicKey } from "@solana/web3.js";
import axios from "axios";
// @ts-ignore
Expand Down Expand Up @@ -300,10 +301,17 @@ export async function getAssetsByOwner(
export type SearchAssetsOpts = {
sortBy?: { sortBy: "created"; sortDirection: "asc" | "desc" };
page?: number;
limit?: number;
collection?: string;
ownerAddress: string;
creatorAddress: string;
creatorAddress?: string;
creatorVerified?: boolean;
tokenType?:
| "all"
| "compressedNft"
| "regularNft"
| "nonFungible"
| "fungible";
} & { [key: string]: unknown };

export async function searchAssets(
Expand All @@ -312,20 +320,32 @@ export async function searchAssets(
creatorVerified = true,
sortBy = { sortBy: "created", sortDirection: "asc" },
page = 1,
limit = 1000,
collection,
tokenType,
...rest
}: SearchAssetsOpts
): Promise<Asset[]> {
const params = {
page,
limit,
sortBy:
tokenType && ["all", "fungible"].includes(tokenType) ? null : sortBy,
creatorVerified:
tokenType && ["all", "fungible"].includes(tokenType)
? null
: creatorVerified,
tokenType,
...(collection ? { grouping: ["collection", collection] } : {}),
...rest,
};

try {
const response = await axios.post(url, {
jsonrpc: "2.0",
method: "searchAssets",
id: "get-assets-op-1",
params: {
page,
creatorVerified,
sortBy,
...rest,
},
params,
headers: {
"Cache-Control": "no-cache",
Pragma: "no-cache",
Expand Down
58 changes: 38 additions & 20 deletions packages/voter-stake-registry-hooks/src/utils/getPositionKeys.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { AnchorProvider } from "@coral-xyz/anchor";
import { HNT_MINT, IOT_MINT, MOBILE_MINT } from "@helium/spl-utils";
import {
Asset,
HNT_MINT,
IOT_MINT,
MOBILE_MINT,
searchAssets,
} from "@helium/spl-utils";
import {
init as initVsr,
positionKey,
registrarKey,
} from "@helium/voter-stake-registry-sdk";
import { Metadata, Metaplex, Nft, Sft } from "@metaplex-foundation/js";
import { getMint, Mint } from "@solana/spl-token";
import { PublicKey } from "@solana/web3.js";
import { Registrar } from "../sdk/types";

Expand Down Expand Up @@ -36,30 +40,44 @@ const realmNames: Record<string, string> = {
};
export const getPositionKeys = async (
args: GetPositionsArgs
): Promise<{ positionKeys: PublicKey[]; nfts: (Metadata | Nft | Sft)[] }> => {
): Promise<{ positionKeys: PublicKey[] }> => {
const { mint, wallet, provider } = args;
const connection = provider.connection;

const metaplex = new Metaplex(connection);
const registrarPk = getRegistrarKey(mint);
const program = await initVsr(provider as any);
const registrar = (await program.account.registrar.fetch(
registrarPk
)) as Registrar;
const mintCfgs = registrar.votingMints;
const mints: Record<string, Mint> = {};
for (const mcfg of mintCfgs) {
const mint = await getMint(connection, mcfg.mint);
mints[mcfg.mint.toBase58()] = mint;
}

const nfts = (await metaplex.nfts().findAllByOwner({ owner: wallet })).filter(
(nft) => nft.collection?.address.equals(registrar.collection)
);
let page = 1;
const limit = 1000;
let allAssets: Asset[] = [];
while (true) {
const assets =
(await searchAssets(provider.connection.rpcEndpoint, {
page,
limit,
ownerAddress: wallet.toBase58(),
tokenType: "fungible",
})) || [];

allAssets = allAssets.concat(assets);

if (assets.length < limit) {
break;
}

page++;
}

const positionKeys = nfts.map(
(nft) => positionKey((nft as any).mintAddress)[0]
);
const positionKeys = allAssets
.filter((asset) =>
asset.grouping?.find(
(group) =>
group.group_key === "collection" &&
group.group_value.equals(registrar.collection)
)
)
.map((asset) => positionKey(asset.id)[0]);

return { positionKeys, nfts };
return { positionKeys };
};

0 comments on commit 2762e7b

Please sign in to comment.