transfer-tokens.sol 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. @mutableSigner(payer) // payer account
  8. @mutableSigner(mint) // mint account to be created
  9. @mutableAccount(metadata) // metadata account to be created
  10. @signer(mintAuthority) // mint authority for the mint account
  11. @account(rentAddress)
  12. @account(metadataProgramId)
  13. function createTokenMint(
  14. address freezeAuthority, // freeze authority for the mint account
  15. uint8 decimals, // decimals for the mint account
  16. string name, // name for the metadata account
  17. string symbol, // symbol for the metadata account
  18. string uri // uri for the metadata account
  19. ) external {
  20. // Invoke System Program to create a new account for the mint account and,
  21. // Invoke Token Program to initialize the mint account
  22. // Set mint authority, freeze authority, and decimals for the mint account
  23. SplToken.create_mint(
  24. tx.accounts.payer.key, // payer account
  25. tx.accounts.mint.key, // mint account
  26. tx.accounts.mintAuthority.key, // mint authority
  27. freezeAuthority, // freeze authority
  28. decimals // decimals
  29. );
  30. // Invoke Metadata Program to create a new account for the metadata account
  31. MplMetadata.create_metadata_account(
  32. tx.accounts.metadata.key, // metadata account
  33. tx.accounts.mint.key, // mint account
  34. tx.accounts.mintAuthority.key, // mint authority
  35. tx.accounts.payer.key, // payer
  36. tx.accounts.payer.key, // update authority (of the metadata account)
  37. name, // name
  38. symbol, // symbol
  39. uri, // uri (off-chain metadata json)
  40. tx.accounts.rentAddress.key,
  41. tx.accounts.metadataProgramId.key
  42. );
  43. }
  44. @mutableAccount(mint)
  45. @mutableAccount(tokenAccount)
  46. @mutableSigner(mintAuthority)
  47. function mintTo(uint64 amount) external {
  48. // Mint tokens to the token account
  49. SplToken.mint_to(
  50. tx.accounts.mint.key, // mint account
  51. tx.accounts.tokenAccount.key, // token account
  52. tx.accounts.mintAuthority.key, // mint authority
  53. amount // amount
  54. );
  55. }
  56. // Transfer tokens from one token account to another via Cross Program Invocation to Token Program
  57. @mutableAccount(from) // token account to transfer from
  58. @mutableAccount(to) // token account to transfer to
  59. @signer(owner)
  60. function transferTokens(
  61. uint64 amount // amount to transfer
  62. ) external {
  63. SplToken.transfer(
  64. tx.accounts.from.key,
  65. tx.accounts.to.key,
  66. tx.accounts.owner.key,
  67. amount
  68. );
  69. }
  70. }