conrad
conrad

Reputation: 19

how to get pda data using solnet

I am trying to convert some js code which uses solana/anchor api to microsoft .net and was planning to use the solnet .net library.

But I cannot figure out how to get the data/state for a pda, in js the call for this looks like - program.account.projectParameter.fetch(projectPDA)

Some sample code below to give you a idea of what I am trying to do.

import React, { useState, useEffect } from "react"; import { Program, AnchorProvider, web3 } from "@project-serum/anchor

const getProvider = () => { const provider = new AnchorProvider( connection, window.solana, opts.preflightCommitment ); return provider; };

const getVoters = async (projectId) => { const program = new Program(project, projectProgramID, provider);

        const [projectPDA, projectBump] =
        await anchor.web3.PublicKey.findProgramAddress(
            [
                Buffer.from("project"),
                Buffer.from(projectId.substring(0, 18)),
                Buffer.from(projectId.substring(18, 36)),
            ],
            program.programId
        );

        const state = await program.account.projectParameter.fetch(projectPDA);

        let x = state.signatories;

        let y = x.map((val, index) => {
            console.log(val.key);
            return {
                key: val.key.toBase58(),
                text: val.key.toBase58(),
                value: val.key.toBase58(),
            };
        });

        setSigs(y);

        console.log(state.transferAmount.reciever.toBase58());

Upvotes: 0

Views: 520

Answers (1)

Jon C
Jon C

Reputation: 8462

Since Anchor isn't supported by Solnet, there's more legwork required in order to deserialize the data. You can see an example of how to create a deserializer from the token program: https://github.com/bmresearch/Solnet/blob/master/src/Solnet.Programs/Models/TokenProgram/TokenAccount.cs

Once you have that, the general process would go:

var accountInfo = rpcClient.GetAccountInfo(projectPDA);
var obj = MyProgramAccount.Deserialize(Convert.FromBase64String(accountInfo.Result.Value.Data[0]));

Upvotes: 0

Related Questions