-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontend.jsx
47 lines (40 loc) · 1.44 KB
/
frontend.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { useState } from 'react';
import { Connection, PublicKey, clusterApiUrl } from '@solana/web3.js';
import { Program, Provider, web3 } from '@project-serum/anchor';
import idl from './idl.json'; // Your IDL file
const { SystemProgram, Keypair } = web3;
const programID = new PublicKey(idl.metadata.address);
const network = clusterApiUrl('devnet');
const opts = {
preflightCommitment: "processed"
};
const App = () => {
const [walletAddress, setWalletAddress] = useState(null);
const getProvider = () => {
const connection = new Connection(network, opts.preflightCommitment);
const provider = new Provider(connection, window.solana, opts.preflightCommitment);
return provider;
}
const depositUSDC = async (amount) => {
const provider = getProvider();
const program = new Program(idl, programID, provider);
try {
await program.rpc.depositUsdc(new web3.BN(amount), {
accounts: {
vault: vaultPublicKey,
userAccount: userTokenAccountPublicKey,
user: provider.wallet.publicKey,
tokenProgram: TOKEN_PROGRAM_ID,
},
});
} catch (err) {
console.error("Transaction error: ", err);
}
}
return (
<div>
<button onClick={() => depositUSDC(100)}>Deposit 100 USDC</button>
</div>
);
};
export default App;