Skip to content

Commit

Permalink
fix: ensure 'basic' anchor template works with Anchor 0.30.x
Browse files Browse the repository at this point in the history
  • Loading branch information
beeman committed Jul 1, 2024
1 parent daf676c commit f8eccd6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Here we export some useful types and functions for interacting with the Anchor program.
import { AnchorProvider, Program } from '@coral-xyz/anchor';
import { PublicKey } from '@solana/web3.js';
import { Cluster, PublicKey } from '@solana/web3.js';
import <%= className %>IDL from '../target/idl/<%= fileNameUnderscore %>.json';
import type { <%= className %> } from '../target/types/<%= fileNameUnderscore %>';
import { IDL as <%= className %>IDL } from '../target/types/<%= fileNameUnderscore %>';

// Re-export the generated IDL and type
export { <%= className %>, <%= className %>IDL };
Expand All @@ -14,3 +14,14 @@ export const <%= upperCaseName %>_PROGRAM_ID = new PublicKey(<%= className %>IDL
export function get<%= className %>Program(provider: AnchorProvider) {
return new Program(<%= className %>IDL as <%= className %>, provider);
}

// This is a helper function to get the program ID for the <%= className %> program depending on the cluster.
export function get<%= className %>ProgramId(cluster: Cluster) {
switch (cluster) {
case 'devnet':
case 'testnet':
case 'mainnet-beta':
default:
return <%= upperCaseName %>_PROGRAM_ID
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
'use client';

<% } %>
import { programId, get<%= className %>Program } from '@<%= npmScope %>/anchor';
import { get<%= className %>Program, get<%= className %>ProgramId } from '@<%= npmScope %>/anchor'
import { Program } from '@coral-xyz/anchor';
import { useConnection } from '@solana/wallet-adapter-react';
import { Keypair } from '@solana/web3.js';
import { Cluster, Keypair } from '@solana/web3.js';
import { useMutation, useQuery } from '@tanstack/react-query';
import { useMemo } from 'react';
import toast from 'react-hot-toast';
import { useCluster } from '../cluster/cluster-data-access';
import { useAnchorProvider } from '../solana/solana-provider';
Expand All @@ -17,7 +18,11 @@ export function use<%= className %>Program() {
const { cluster } = useCluster();
const transactionToast = useTransactionToast();
const provider = useAnchorProvider();
const program = get<%= className %>Program(provider);
const programId = useMemo(
() => get<%= className %>ProgramId(cluster.network as Cluster),
[cluster]
);
const program = get<%= className %>Program(provider)

const getProgramAccount = useQuery({
queryKey: ['get-program-account', { cluster }],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = composePlugins(withNx(), withReact(), (config) => {
config.resolve.fallback = {
crypto: require.resolve('crypto-browserify'),
stream: require.resolve('stream-browserify'),
vm: false,
};

return config;
Expand Down
9 changes: 9 additions & 0 deletions packages/preset-react/src/utils/generate-react-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,14 @@ export async function generateReactApplication(tree: Tree, options: NormalizedRe
return json
})

updateJson(tree, join(project.root, 'tsconfig.app.json'), (json) => {
json.compilerOptions = {
...json.compilerOptions,
resolveJsonModule: true,
allowSyntheticDefaultImports: true,
}
return json
})

return getProjects(tree).get(options.webName)
}

0 comments on commit f8eccd6

Please sign in to comment.