-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsturction1.ts
180 lines (160 loc) · 4.3 KB
/
insturction1.ts
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import {
TOKEN_PROGRAM_ID,
getAssociatedTokenAddress,
createAssociatedTokenAccountInstruction,
} from "@solana/spl-token";
import {
sendAndConfirmTransaction,
Connection,
SYSVAR_RENT_PUBKEY,
SystemProgram,
} from "@solana/web3.js";
// Client
class Assignable {
constructor(properties) {
Object.keys(properties).map((key) => {
return (this[key] = properties[key]);
});
}
}
function intToBool(i: number) {
if (i == 0) {
return false;
} else {
return true;
}
}
function boolToInt(t: boolean) {
if (t) {
return 1;
} else {
return 0;
}
}
const boolMapper = {
encode: boolToInt,
decode: intToBool,
};
class InstructionData extends Assignable {
toBuffer() {
return Buffer.from(borsh.serialize(InstructionDataSchema, this));
}
}
const InstructionDataSchema = new Map([
[
InstructionData,
{
kind: "struct",
fields: [
["methods_id", "u32"],
["id", "u32"],
["description", "string"],
["owner", "string"],
["creator", "string"],
["authorize", "u8", boolMapper],
["url", "string"],
["cid", "string"],
["is_mutable", "u8", boolMapper],
],
},
],
]);
const blockhashInfo = await pg.connection.getLatestBlockhash();
// Create transaction
const tx = new web3.Transaction({
...blockhashInfo,
});
const nftMint = new InstructionData({
methods_id: 0, // 2是test方法 0-1 调用报错
id: 3,
description: "hello world!----",
owner: "Ee9tjcAXwDeHtVxKePgQos3YqxGo9sxExryCT8a1DFqe",
creator: "Ee9tjcAXwDeHtVxKePgQos3YqxGo9sxExryCT8a1DFqe",
authorize: true,
url: "https://green-sad-canidae-844.mypinata.cloud/ipfs/QmcUgQvRjpgg1qhsfVNE497unLzMVkHbERQcPkCWdx5tyU/0.json",
cid: "QmcUgQvRjp", //"gg1qhsfVNE497unLzMVkHbERQcPkCWdx5tyU",
is_mutable: false,
});
const connection = new Connection("https://api.devnet.solana.com", "confirmed");
const PROGRAM_ID = "CyGeFwXuqGHVV5HuzJ1iFMRqeL2YxU5CYTXyy5Cpbkg1";
const pubkey = pg.wallet.keypair.publicKey;
const cid = "QmcUgQvRjp";
const [tokenMint] = web3.PublicKey.findProgramAddressSync(
[pubkey.toBuffer(), Buffer.from(cid)],
new web3.PublicKey(PROGRAM_ID)
);
const [mintAuth] = web3.PublicKey.findProgramAddressSync(
[tokenMint.toBuffer()],
new web3.PublicKey(PROGRAM_ID)
);
const [metadata] = web3.PublicKey.findProgramAddressSync(
[tokenMint.toBuffer(), Buffer.from(cid)],
new web3.PublicKey(PROGRAM_ID)
);
// const userAta = await getAssociatedTokenAddress(tokenMint, pubkey);
// const ataAccount = await connection.getAccountInfo(userAta);
// if (!ataAccount) {
// const ataInstruction = createAssociatedTokenAccountInstruction(
// pubkey,
// userAta,
// pubkey,
// tokenMint
// );
// tx.add(ataInstruction);
// }
// Add our hello world program instruction
tx.add(
new web3.TransactionInstruction({
programId: new web3.PublicKey(
"CyGeFwXuqGHVV5HuzJ1iFMRqeL2YxU5CYTXyy5Cpbkg1" // 部署程序的ID
),
keys: [
{
pubkey: pubkey,
isSigner: true,
isWritable: false,
},
{
pubkey: tokenMint,
isSigner: false,
isWritable: true,
},
{
pubkey: mintAuth,
isSigner: false,
isWritable: false,
},
{
pubkey: metadata,
isSigner: false,
isWritable: true,
},
// {
// pubkey: userAta,
// isSigner: false,
// isWritable: true,
// },
{
pubkey: SystemProgram.programId,
isSigner: false,
isWritable: false,
},
{
pubkey: TOKEN_PROGRAM_ID,
isSigner: false,
isWritable: false,
},
{
pubkey: SYSVAR_RENT_PUBKEY,
isSigner: false,
isWritable: false,
},
],
data: nftMint.toBuffer(),
})
);
// Sign transaction
tx.sign(pg.wallet.keypair);
// Send the transaction to the Solana cluster
const txHash = await pg.connection.sendRawTransaction(tx.serialize());
console.log(txHash);