-
Notifications
You must be signed in to change notification settings - Fork 156
/
UpdateMetadata.tsx
102 lines (96 loc) · 3.79 KB
/
UpdateMetadata.tsx
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { FC, useCallback, useState } from 'react';
import { useConnection, useWallet } from '@solana/wallet-adapter-react';
import { Transaction, PublicKey } from '@solana/web3.js';
import {
DataV2,
createUpdateMetadataAccountV2Instruction,
PROGRAM_ID
} from "@metaplex-foundation/mpl-token-metadata";
export const UpdateMetadata: FC = () => {
const { connection } = useConnection();
const { publicKey, sendTransaction } = useWallet();
const [tokenMint, setTokenMint] = useState('')
const [tokenName, setTokenName] = useState('')
const [symbol, setSymbol] = useState('')
const [metadata, setMetadata] = useState('')
const onClick = useCallback(async (form) => {
const mint = new PublicKey(form.tokenMint)
console.log(mint.toString())
console.log(form.tokenMint)
const metadataPDA = PublicKey.findProgramAddressSync(
[
Buffer.from("metadata"),
PROGRAM_ID.toBuffer(),
mint.toBuffer(),
],
PROGRAM_ID,
)[0]
const tokenMetadata = {
name: form.tokenName,
symbol: form.symbol,
uri: form.metadata,
sellerFeeBasisPoints: 0,
creators: null,
collection: null,
uses: null
} as DataV2;
const updateMetadataTransaction = new Transaction().add(
createUpdateMetadataAccountV2Instruction(
{
metadata: metadataPDA,
updateAuthority: publicKey,
},
{
updateMetadataAccountArgsV2: {
data: tokenMetadata,
updateAuthority: publicKey,
primarySaleHappened: true,
isMutable: true,
},
}
)
);
await sendTransaction(updateMetadataTransaction, connection);
}, [publicKey, connection, sendTransaction]);
return (
<div className="my-6">
<input
type="text"
className="form-control block mb-2 w-full px-4 py-2 text-xl font-normal text-gray-700 bg-white bg-clip-padding border border-solid border-gray-300 rounded transition ease-in-out m-0 focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none"
placeholder="Token Mint Address"
onChange={(e) => setTokenMint(e.target.value)}
/>
<input
type="text"
className="form-control block mb-2 w-full px-4 py-2 text-xl font-normal text-gray-700 bg-white bg-clip-padding border border-solid border-gray-300 rounded transition ease-in-out m-0 focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none"
placeholder="Token Name"
onChange={(e) => setTokenName(e.target.value)}
/>
<input
type="text"
className="form-control block mb-2 w-full px-4 py-2 text-xl font-normal text-gray-700 bg-white bg-clip-padding border border-solid border-gray-300 rounded transition ease-in-out m-0 focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none"
placeholder="Symbol"
onChange={(e) => setSymbol(e.target.value)}
/>
<input
type="text"
className="form-control block mb-2 w-full px-4 py-2 text-xl font-normal text-gray-700 bg-white bg-clip-padding border border-solid border-gray-300 rounded transition ease-in-out m-0 focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none"
placeholder="Metadata Url"
onChange={(e) => setMetadata(e.target.value)}
/>
<button
className="px-8 m-2 btn animate-pulse bg-gradient-to-r from-[#9945FF] to-[#14F195] hover:from-pink-500 hover:to-yellow-500 ..."
onClick={() =>
onClick({
metadata: metadata,
symbol: symbol,
tokenName: tokenName,
tokenMint: tokenMint
})
}
>
<span>Update Metadata</span>
</button>
</div>
);
}