test.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import {
  2. Connection,
  3. Keypair,
  4. PublicKey,
  5. SystemProgram,
  6. SYSVAR_RENT_PUBKEY,
  7. TransactionInstruction,
  8. Transaction,
  9. sendAndConfirmTransaction,
  10. } from '@solana/web3.js';
  11. import {
  12. ASSOCIATED_TOKEN_PROGRAM_ID,
  13. getAssociatedTokenAddress,
  14. MINT_SIZE,
  15. TOKEN_PROGRAM_ID,
  16. } from '@solana/spl-token';
  17. import * as borsh from "borsh";
  18. import { Buffer } from "buffer";
  19. const TOKEN_METADATA_PROGRAM_ID = new PublicKey(
  20. "metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"
  21. );
  22. function createKeypairFromFile(path: string): Keypair {
  23. return Keypair.fromSecretKey(
  24. Buffer.from(JSON.parse(require('fs').readFileSync(path, "utf-8")))
  25. )
  26. };
  27. class Assignable {
  28. constructor(properties) {
  29. Object.keys(properties).map((key) => {
  30. return (this[key] = properties[key]);
  31. });
  32. };
  33. };
  34. class TokenMetadata extends Assignable {
  35. toBuffer() {
  36. return Buffer.from(borsh.serialize(TokenMetadataSchema, this));
  37. }
  38. };
  39. const TokenMetadataSchema = new Map([
  40. [
  41. TokenMetadata, {
  42. kind: 'struct',
  43. fields: [
  44. ['title', 'string'],
  45. ['symbol', 'string'],
  46. ['uri', 'string'],
  47. ]
  48. }
  49. ]
  50. ]);
  51. class MintTokenTo extends Assignable {
  52. toBuffer() {
  53. return Buffer.from(borsh.serialize(MintTokenToSchema, this));
  54. }
  55. };
  56. const MintTokenToSchema = new Map([
  57. [
  58. MintTokenTo, {
  59. kind: 'struct',
  60. fields: [
  61. ['amount', 'u64'],
  62. ]
  63. }
  64. ]
  65. ]);
  66. describe("mint-token", () => {
  67. const connection = new Connection(`http://api.devnet.solana.com/`, 'confirmed');
  68. const payer = createKeypairFromFile(require('os').homedir() + '/.config/solana/id.json');
  69. const program = createKeypairFromFile('./program/target/so/program-keypair.json');
  70. const mintKeypair: Keypair = Keypair.generate();
  71. console.log(`New token: ${mintKeypair.publicKey}`);
  72. it("Mint!", async () => {
  73. const metadataAddress = (await PublicKey.findProgramAddress(
  74. [
  75. Buffer.from("metadata"),
  76. TOKEN_METADATA_PROGRAM_ID.toBuffer(),
  77. mintKeypair.publicKey.toBuffer(),
  78. ],
  79. TOKEN_METADATA_PROGRAM_ID
  80. ))[0];
  81. const metadataInstructionData = new TokenMetadata({
  82. title: "Solana Gold",
  83. symbol: "GOLDSOL",
  84. uri: "https://raw.githubusercontent.com/solana-developers/program-examples/main/tokens/token_metadata.json",
  85. });
  86. // Transact with the "create_mint" function in our on-chain program
  87. //
  88. let ix = new TransactionInstruction({
  89. keys: [
  90. // Mint account
  91. {
  92. pubkey: mintKeypair.publicKey,
  93. isSigner: true,
  94. isWritable: true,
  95. },
  96. // Metadata account
  97. {
  98. pubkey: metadataAddress,
  99. isSigner: false,
  100. isWritable: true,
  101. },
  102. // Mint Authority
  103. {
  104. pubkey: payer.publicKey,
  105. isSigner: true,
  106. isWritable: false,
  107. },
  108. // Rent account
  109. {
  110. pubkey: SYSVAR_RENT_PUBKEY,
  111. isSigner: false,
  112. isWritable: false,
  113. },
  114. // System program
  115. {
  116. pubkey: SystemProgram.programId,
  117. isSigner: false,
  118. isWritable: false,
  119. },
  120. // Token program
  121. {
  122. pubkey: TOKEN_PROGRAM_ID,
  123. isSigner: false,
  124. isWritable: false,
  125. },
  126. // Token metadata program
  127. {
  128. pubkey: TOKEN_METADATA_PROGRAM_ID,
  129. isSigner: false,
  130. isWritable: false,
  131. },
  132. ],
  133. programId: program.publicKey,
  134. data: metadataInstructionData.toBuffer(),
  135. });
  136. await sendAndConfirmTransaction(
  137. connection,
  138. new Transaction().add(ix),
  139. [payer, mintKeypair]
  140. );
  141. });
  142. it("Mint to a wallet!", async () => {
  143. const tokenAddress = await getAssociatedTokenAddress(
  144. mintKeypair.publicKey,
  145. payer.publicKey
  146. );
  147. const mintToInstructionData = new MintTokenTo({
  148. amount: 1,
  149. });
  150. // Transact with the "mint_to" function in our on-chain program
  151. //
  152. let ix = new TransactionInstruction({
  153. keys: [
  154. // Mint account
  155. {
  156. pubkey: mintKeypair.publicKey,
  157. isSigner: true,
  158. isWritable: true,
  159. },
  160. // Token account
  161. {
  162. pubkey: tokenAddress,
  163. isSigner: false,
  164. isWritable: true,
  165. },
  166. // Mint Authority
  167. {
  168. pubkey: payer.publicKey,
  169. isSigner: true,
  170. isWritable: false,
  171. },
  172. // Rent account
  173. {
  174. pubkey: SYSVAR_RENT_PUBKEY,
  175. isSigner: false,
  176. isWritable: false,
  177. },
  178. // System program
  179. {
  180. pubkey: SystemProgram.programId,
  181. isSigner: false,
  182. isWritable: false,
  183. },
  184. // Token program
  185. {
  186. pubkey: TOKEN_PROGRAM_ID,
  187. isSigner: false,
  188. isWritable: false,
  189. },
  190. // Associated token program
  191. {
  192. pubkey: ASSOCIATED_TOKEN_PROGRAM_ID,
  193. isSigner: false,
  194. isWritable: false,
  195. },
  196. ],
  197. programId: program.publicKey,
  198. data: mintToInstructionData.toBuffer(),
  199. });
  200. await sendAndConfirmTransaction(
  201. connection,
  202. new Transaction().add(ix),
  203. [payer, mintKeypair]
  204. );
  205. });
  206. });