Advertisement
Guest User

solana_metadata

a guest
Sep 11th, 2023
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.04 KB | Cryptocurrency | 0 0
  1. from solana.rpc.api import Client, Transaction
  2. from solders.pubkey import Pubkey
  3. from solana.transaction import AccountMeta
  4. from solders.instruction import Instruction
  5. from borsh_construct import CStruct, String, U8, U16, Vec, Option, Bool
  6.  
  7. # some important public keys
  8. system_program = Pubkey.from_string('11111111111111111111111111111111')
  9. system_rent = Pubkey.from_string('SysvarRent111111111111111111111111111111111')
  10. token_program = Pubkey.from_string("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")
  11. token_metadata_program = Pubkey.from_string("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s")
  12.  
  13. # my own keypair
  14. my_keypair = MY_KEYPAIR
  15. # public key of my token
  16. new_token = TOKEN_PUBLIC_KEY
  17.  
  18. # structure of the instruction
  19. instruction_structure = CStruct(
  20.     "instructionDiscriminator" / U8,
  21.     "createMetadataAccountArgsV3" / CStruct(
  22.         "data" / CStruct(
  23.             "name" / String,
  24.             "symbol" / String,
  25.             "uri" / String,
  26.             "sellerFeeBasisPoints" / U16,
  27.             "creators" / Option(Vec(CStruct(
  28.                 "address" / String,
  29.                 "verified" / Bool,
  30.                 "share" / U8
  31.             )))
  32.         ),
  33.         "is_mutable" / Bool
  34.     )
  35. )
  36.  
  37. # data for the instruction
  38. instruction_data = {
  39.     "instructionDiscriminator": 33,
  40.     "createMetadataAccountArgsV3": {
  41.         "data": {
  42.             "name": "My Token Name",
  43.             "symbol": "MTN",
  44.             "uri": "https://arweave.net/somewhere",
  45.             "sellerFeeBasisPoints": 500,
  46.             "creators": [
  47.                 {
  48.                     "address": str(my_keypair.pubkey()),
  49.                     "verified": 1,
  50.                     "share": 100
  51.                 }
  52.             ],
  53.         },
  54.         "is_mutable": 1
  55.     }
  56. }
  57.  
  58. # get pda of mint account
  59. metadata_pda = Pubkey.find_program_address([b"metadata", bytes(token_metadata_program), bytes(new_token)], token_metadata_program)[0]
  60.  
  61. # account list for instruction
  62. accounts = [
  63.     AccountMeta(pubkey=metadata_pda, is_signer=False, is_writable=True), # metadata
  64.     AccountMeta(pubkey=new_token, is_signer=False, is_writable=True), # mint
  65.     AccountMeta(pubkey=my_keypair.pubkey(), is_signer=True, is_writable=False), # mint authority
  66.     AccountMeta(pubkey=my_keypair.pubkey(), is_signer=True, is_writable=True), # payer
  67.     AccountMeta(pubkey=my_keypair.pubkey(), is_signer=False, is_writable=False), # update authority
  68.     AccountMeta(pubkey=system_program, is_signer=False, is_writable=False), # system program
  69.     AccountMeta(pubkey=system_rent, is_signer=False, is_writable=False) # rent
  70. ]
  71.  
  72. # create instruction
  73. instruction = Instruction(token_metadata_program, instruction_structure.build(instruction_data), accounts)
  74.  
  75. # send a transaction with the instruction to the network
  76. client = Client("https://api.devnet.solana.com")
  77. transaction = Transaction()
  78. transaction.add(instruction)
  79. transaction.recent_blockhash = client.get_latest_blockhash().value.blockhash
  80. transaction.sign(my_keypair)
  81. print(client.send_raw_transaction(transaction.serialize()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement