seahorse.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import asyncio
  2. from anchorpy import create_workspace, close_workspace, Context
  3. from solders.system_program import ID as SYS_PROGRAM_ID
  4. from solders.pubkey import Pubkey
  5. from solders.keypair import Keypair
  6. from solders.sysvar import RENT
  7. async def main():
  8. # Read the deployed program from the workspace.
  9. workspace = create_workspace()
  10. program = workspace["seahorse"]
  11. TOKEN_PROGRAM_ID = Pubkey.from_string("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")
  12. ASSOCITAED_TOKEN_PROGRAM_ID = Pubkey.from_string("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL")
  13. # Create a Mint keypair. This will be our token mint.
  14. mint = Keypair()
  15. # Execute the instructions
  16. # Create a token
  17. create_token = await program.rpc["create_token"](ctx=Context(accounts={
  18. "mint": mint.pubkey(),
  19. "signer": program.provider.wallet.payer.pubkey(),
  20. "system_program": SYS_PROGRAM_ID,
  21. "rent": RENT,
  22. "token_program": TOKEN_PROGRAM_ID
  23. }, signers=[program.provider.wallet.payer, mint]))
  24. print("Create token signature: ", create_token)
  25. # Create a token account
  26. associated_token_account_pubkey, nonce = Pubkey.find_program_address([bytes(program.provider.wallet.payer.pubkey()), bytes(TOKEN_PROGRAM_ID), bytes(mint.pubkey())], ASSOCITAED_TOKEN_PROGRAM_ID)
  27. create_associated_token_account = await program.rpc["create_associated_token_account"](ctx=Context(accounts={
  28. "mint": mint.pubkey(),
  29. "token_account" : associated_token_account_pubkey,
  30. "signer": program.provider.wallet.payer.pubkey(),
  31. "system_program": SYS_PROGRAM_ID,
  32. "rent": RENT,
  33. "token_program": TOKEN_PROGRAM_ID,
  34. "associated_token_program" : ASSOCITAED_TOKEN_PROGRAM_ID
  35. }, signers=[program.provider.wallet.payer]))
  36. print("Create associated token account signature: ", create_associated_token_account)
  37. # Mint tokens
  38. mint_token = await program.rpc["mint_token"](1000, ctx=Context(accounts={
  39. "mint": mint.pubkey(),
  40. "recipient" : associated_token_account_pubkey,
  41. "signer": program.provider.wallet.payer.pubkey(),
  42. "system_program": SYS_PROGRAM_ID,
  43. "rent": RENT,
  44. "token_program": TOKEN_PROGRAM_ID,
  45. }, signers=[program.provider.wallet.payer]))
  46. print("Mint token signature: ", mint_token)
  47. # Transfer tokens (the tokens are transfered to the sender here, but you can of course create a new associated token account for the recipient)
  48. transfer = await program.rpc["transfer"](1000, ctx=Context(accounts={
  49. "signer_token_account": associated_token_account_pubkey,
  50. "recipient" : associated_token_account_pubkey,
  51. "signer": program.provider.wallet.payer.pubkey(),
  52. "system_program": SYS_PROGRAM_ID,
  53. "rent": RENT,
  54. "token_program": TOKEN_PROGRAM_ID,
  55. "mint": mint.pubkey()
  56. }, signers=[program.provider.wallet.payer]))
  57. print("Transfer signature: ", transfer)
  58. # Close all HTTP clients in the workspace.
  59. await close_workspace(workspace)
  60. asyncio.run(main())