transfer-tokens.sol 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import "../libraries/spl_token.sol";
  2. import "../libraries/mpl_metadata.sol";
  3. @program_id("F1ipperKF9EfD821ZbbYjS319LXYiBmjhzkkf5a26rC")
  4. contract transfer_tokens {
  5. @payer(payer)
  6. constructor() {}
  7. function createTokenMint(
  8. address payer, // payer account
  9. address mint, // mint account to be created
  10. address mintAuthority, // mint authority for the mint account
  11. address freezeAuthority, // freeze authority for the mint account
  12. address metadata, // metadata account to be created
  13. uint8 decimals, // decimals for the mint account
  14. string name, // name for the metadata account
  15. string symbol, // symbol for the metadata account
  16. string uri // uri for the metadata account
  17. ) public {
  18. // Invoke System Program to create a new account for the mint account and,
  19. // Invoke Token Program to initialize the mint account
  20. // Set mint authority, freeze authority, and decimals for the mint account
  21. SplToken.create_mint(
  22. payer, // payer account
  23. mint, // mint account
  24. mintAuthority, // mint authority
  25. freezeAuthority, // freeze authority
  26. decimals // decimals
  27. );
  28. // Invoke Metadata Program to create a new account for the metadata account
  29. MplMetadata.create_metadata_account(
  30. metadata, // metadata account
  31. mint, // mint account
  32. mintAuthority, // mint authority
  33. payer, // payer
  34. payer, // update authority (of the metadata account)
  35. name, // name
  36. symbol, // symbol
  37. uri // uri (off-chain metadata json)
  38. );
  39. }
  40. function mintTo(
  41. address payer, // payer account
  42. address tokenAccount, // token account to create and receive the minted token
  43. address mint, // mint account
  44. address owner, // token account owner
  45. uint64 amount // amount to mint
  46. ) public {
  47. // Mint token to the token account
  48. SplToken.mint_to(
  49. mint, // mint account
  50. tokenAccount, // token account
  51. payer, // mint authority
  52. amount // amount
  53. );
  54. }
  55. // Transfer tokens from one token account to another via Cross Program Invocation to Token Program
  56. function transferTokens(
  57. address from, // token account to transfer from
  58. address to, // token account to transfer to
  59. uint64 amount // amount to transfer
  60. ) public {
  61. SplToken.TokenAccountData from_data = SplToken.get_token_account_data(from);
  62. SplToken.transfer(from, to, from_data.owner, amount);
  63. }
  64. }