solana-mobile / mobile-wallet-adapter

Other
245 stars 101 forks source link

How to use it with Anchor? #190

Closed dFohlen closed 1 year ago

dFohlen commented 2 years ago

Hi @creativedrewy,

I'm new to Solana and the mobile-wallet-adapter. Is there or can you provide an example of how to use this adapter to interact with an Anchor build contract?

In a react based web apps we normally use the solana/wallet-adapter-react like this:

import { useAnchorWallet } from '@solana/wallet-adapter-react';
import { Connection, PublicKey } from '@solana/web3.js';
import anchor from '@project-serum/anchor'
import { IDL, MyProgram } from './../../target/types/my_program'
import { PROGRAM_ID } from '../constants';

const wallet = useAnchorWallet();
const connection = new Connection(config.rpcUrl, {
    commitment: config.commitment,
    confirmTransactionInitialTimeout: 120_000, // timeout for 2 minutes ~blockhash duration
  });
const provider = new anchor.AnchorProvider(connection, wallet, { preflightCommitment: "processed" });
const program = new anchor.Program<MyProgram>(IDL, PROGRAM_ID, provider);
const account = Keypair.generate();

const tx = await program.methods.create().rpc;
await program.rpc.create({
    accounts: {
       data: account.publicKey,
       payer: wallet.publicKey;
       systemProgram: anchor.web3.SystemProgram.programId,
    },
    signers: [account],
});
console.log("Transaction signature", tx);

const accountData = await program.account.data.fetch(account.publicKey);
console.log("Account data", accountData);

Thanks,

d_fohlen

creativedrewy commented 2 years ago

Hey @dFohlen, are you wondering about how to work with Anchor contracts in Kotlin?

dFohlen commented 2 years ago

Thank you for replying @creativedrewy and sorry for the missing information. I want to create a mobile game in React Native (based on the example) like this React Tic-Tac-Toe 😉

steveluscher commented 1 year ago

Sorry that so much time has gone by without an answer to this question!

We want to make sure to keep signal strong in the GitHub issue tracker – to make sure that it remains the best place to track issues that affect the development of the Mobile Wallet Adapter itself.

Questions like yours deserve a purpose-built Q&A forum. Post your question to the Solana Stack Exchange using this link: https://solana.stackexchange.com/questions/ask